Skip to content

Commit 087748e

Browse files
committed
Clock: Canonical test improvements
Generated from x-common clock data Now generates tests from data in x-common. The template file is skinny, the generator a little fatter. fixes #282 references exercism/problem-specifications#166
1 parent 1e34cd4 commit 087748e

File tree

6 files changed

+332
-83
lines changed

6 files changed

+332
-83
lines changed

bin/generate-clock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env ruby
2+
3+
require_relative '../lib/generator'
4+
require_relative '../lib/clock_cases'
5+
6+
Generator.new('clock', ClockCases).generate

exercises/clock/.version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

exercises/clock/clock_test.rb

Lines changed: 223 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,146 +3,300 @@
33
require 'minitest/autorun'
44
require_relative 'clock'
55

6-
module HelperUnits
7-
MINUTE = 60
8-
HOUR = MINUTE * 60
9-
DAY = HOUR * 24
6+
# Test data version:
7+
# eb8d142 Merge pull request #220 from IanWhitney/sieve_ordering
108

11-
def minutes(minutes)
12-
minutes
9+
class ClockTest < Minitest::Test
10+
def test_on_the_hour
11+
# skip
12+
assert '08:00', Clock.at(8, 0)
1313
end
1414

15-
def hours(hours)
16-
hours * MINUTE
15+
def test_past_the_hour
16+
skip
17+
assert '11:09', Clock.at(11, 9)
1718
end
1819

19-
def days(days)
20-
days * DAY
20+
def test_midnight_is_zero_hours
21+
skip
22+
assert '00:00', Clock.at(24, 0)
2123
end
22-
end
2324

24-
class ClockTest < Minitest::Test
25-
include HelperUnits
25+
def test_hour_rolls_over
26+
skip
27+
assert '01:00', Clock.at(25, 0)
28+
end
2629

27-
def test_on_the_hour
28-
assert_equal '08:00', Clock.at(8).to_s
29-
assert_equal '09:00', Clock.at(9).to_s
30+
def test_hour_rolls_over_continuously
31+
skip
32+
assert '04:00', Clock.at(100, 0)
3033
end
3134

32-
def test_past_the_hour
35+
def test_sixty_minutes_is_next_hour
3336
skip
34-
assert_equal '11:09', Clock.at(11, 9).to_s
37+
assert '02:00', Clock.at(1, 60)
3538
end
3639

37-
def test_add_a_few_minutes
40+
def test_minutes_roll_over
3841
skip
39-
clock = Clock.at(10) + 3
40-
assert_equal '10:03', clock.to_s
42+
assert '02:40', Clock.at(0, 160)
4143
end
4244

43-
def test_add_over_an_hour
45+
def test_minutes_roll_over_continuously
4446
skip
45-
clock = Clock.at(10) + 61
46-
assert_equal '11:01', clock.to_s
47+
assert '04:43', Clock.at(0, 1723)
4748
end
4849

49-
def test_wrap_around_at_midnight
50+
def test_hour_and_minutes_roll_over
5051
skip
51-
clock = Clock.at(23, 30) + 60
52-
assert_equal '00:30', clock.to_s
52+
assert '03:40', Clock.at(25, 160)
5353
end
5454

55-
def test_subtract_minutes
55+
def test_hour_and_minutes_roll_over_continuously
5656
skip
57-
clock = Clock.at(10) - 90
58-
assert_equal '08:30', clock.to_s
57+
assert '11:01', Clock.at(201, 3001)
5958
end
6059

61-
def test_hour_rolls_over_continuously
60+
def test_hour_and_minutes_roll_over_to_exactly_midnight
6261
skip
63-
clock = Clock.at(0) + hours(100)
64-
assert_equal '04:00', clock.to_s
62+
assert '00:00', Clock.at(72, 8640)
6563
end
6664

67-
def test_minutes_roll_over_continuously
65+
def test_negative_hour
6866
skip
69-
clock = Clock.at(0) + minutes(1723)
70-
assert_equal '04:43', clock.to_s
67+
assert '23:15', Clock.at(-1, 15)
7168
end
7269

73-
def test_hour_and_minutes_roll_over_continuously
70+
def test_negative_hour_rolls_over
7471
skip
75-
clock = Clock.at(0) + hours(201) + minutes(3001)
76-
assert_equal '11:01', clock.to_s
72+
assert '23:00', Clock.at(-25, 0)
7773
end
7874

79-
def test_negative_hours_roll_over_continuously
75+
def test_negative_hour_rolls_over_continuously
8076
skip
81-
clock = Clock.at(0) - hours(91)
82-
assert_equal '05:00', clock.to_s
77+
assert '05:00', Clock.at(-91, 0)
78+
end
79+
80+
def test_negative_minutes
81+
skip
82+
assert '00:20', Clock.at(1, -40)
83+
end
84+
85+
def test_negative_minutes_roll_over
86+
skip
87+
assert '22:20', Clock.at(1, -160)
8388
end
8489

8590
def test_negative_minutes_roll_over_continuously
8691
skip
87-
clock = Clock.at(0) - minutes(4820)
88-
assert_equal '15:40', clock.to_s
92+
assert '16:40', Clock.at(1, -4820)
93+
end
94+
95+
def test_negative_hour_and_minutes_both_roll_over
96+
skip
97+
assert '20:20', Clock.at(-25, -160)
8998
end
9099

91-
def test_negative_hours_and_minutes_both_roll_over_continuously
100+
def test_negative_hour_and_minutes_both_roll_over_continuously
92101
skip
93-
clock = Clock.at(0) - hours(121) - minutes(5810)
94-
assert_equal '22:10', clock.to_s
102+
assert '22:10', Clock.at(-121, -5810)
103+
end
104+
105+
def test_add_minutes
106+
skip
107+
assert '10:03', Clock.at(10, 0) + 3
95108
end
96109

97110
def test_add_no_minutes
98111
skip
99-
clock = Clock.at(0) + minutes(0)
100-
assert_equal '00:00', clock.to_s
112+
assert '06:41', Clock.at(6, 41) + 0
113+
end
114+
115+
def test_add_to_next_hour
116+
skip
117+
assert '01:25', Clock.at(0, 45) + 40
118+
end
119+
120+
def test_add_more_than_one_hour
121+
skip
122+
assert '11:01', Clock.at(10, 0) + 61
101123
end
102124

103125
def test_add_more_than_two_hours_with_carry
104126
skip
105-
clock = Clock.at(0, 45) + minutes(160)
106-
assert_equal '03:25', clock.to_s
127+
assert '03:25', Clock.at(0, 45) + 160
128+
end
129+
130+
def test_add_across_midnight
131+
skip
132+
assert '00:01', Clock.at(23, 59) + 2
133+
end
134+
135+
def test_add_more_than_one_day__1500_min_is_equal_to_25_hrs_
136+
skip
137+
assert '06:32', Clock.at(5, 32) + 1500
107138
end
108139

109140
def test_add_more_than_two_days
110141
skip
111-
clock = Clock.at(1, 1) + days(2) + hours(10) + minutes(20)
112-
assert_equal '11:21', clock.to_s
142+
assert '11:21', Clock.at(1, 1) + 3500
143+
end
144+
145+
def test_subtract_minutes
146+
skip
147+
assert '10:00', Clock.at(10, 3) + -3
148+
end
149+
150+
def test_subtract_to_previous_hour
151+
skip
152+
assert '09:33', Clock.at(10, 3) + -30
153+
end
154+
155+
def test_subtract_more_than_an_hour
156+
skip
157+
assert '08:53', Clock.at(10, 3) + -70
158+
end
159+
160+
def test_subtract_across_midnight
161+
skip
162+
assert '23:59', Clock.at(0, 3) + -4
163+
end
164+
165+
def test_subtract_more_than_two_hours
166+
skip
167+
assert '21:20', Clock.at(0, 0) + -160
113168
end
114169

115-
def test_subtrackt_more_than_two_hours_with_borrow
170+
def test_subtract_more_than_two_hours_with_borrow
116171
skip
117-
clock = Clock.at(6, 15) - hours(2) - minutes(40)
118-
assert_equal '03:35', clock.to_s
172+
assert '03:35', Clock.at(6, 15) + -160
173+
end
174+
175+
def test_subtract_more_than_one_day__1500_min_is_equal_to_25_hrs_
176+
skip
177+
assert '04:32', Clock.at(5, 32) + -1500
119178
end
120179

121180
def test_subtract_more_than_two_days
122181
skip
123-
clock = Clock.at(2, 20) - days(2) - hours(2)
124-
assert_equal '00:20', clock.to_s
182+
assert '00:20', Clock.at(2, 20) + -3000
125183
end
126184

127-
def test_equivalent_clocks
185+
def test_clocks_with_same_time
128186
skip
129187
clock1 = Clock.at(15, 37)
130188
clock2 = Clock.at(15, 37)
131-
assert_equal clock1, clock2
189+
assert clock1 == clock2
132190
end
133191

134-
def test_inequivalent_clocks
192+
def test_clocks_a_minute_apart
135193
skip
136-
clock1 = Clock.at(15, 37)
137-
clock2 = Clock.at(15, 36)
138-
clock3 = Clock.at(14, 37)
139-
refute_equal clock1, clock2
140-
refute_equal clock1, clock3
194+
clock1 = Clock.at(15, 36)
195+
clock2 = Clock.at(15, 37)
196+
refute clock1 == clock2
197+
end
198+
199+
def test_clocks_an_hour_apart
200+
skip
201+
clock1 = Clock.at(14, 37)
202+
clock2 = Clock.at(15, 37)
203+
refute clock1 == clock2
204+
end
205+
206+
def test_clocks_with_hour_overflow
207+
skip
208+
clock1 = Clock.at(10, 37)
209+
clock2 = Clock.at(34, 37)
210+
assert clock1 == clock2
211+
end
212+
213+
def test_clocks_with_hour_overflow_by_several_days
214+
skip
215+
clock1 = Clock.at(3, 11)
216+
clock2 = Clock.at(99, 11)
217+
assert clock1 == clock2
218+
end
219+
220+
def test_clocks_with_negative_hour
221+
skip
222+
clock1 = Clock.at(22, 40)
223+
clock2 = Clock.at(-2, 40)
224+
assert clock1 == clock2
225+
end
226+
227+
def test_clocks_with_negative_hour_that_wraps
228+
skip
229+
clock1 = Clock.at(17, 3)
230+
clock2 = Clock.at(-31, 3)
231+
assert clock1 == clock2
232+
end
233+
234+
def test_clocks_with_negative_hour_that_wraps_multiple_times
235+
skip
236+
clock1 = Clock.at(13, 49)
237+
clock2 = Clock.at(-83, 49)
238+
assert clock1 == clock2
239+
end
240+
241+
def test_clocks_with_minute_overflow
242+
skip
243+
clock1 = Clock.at(0, 1)
244+
clock2 = Clock.at(0, 1441)
245+
assert clock1 == clock2
246+
end
247+
248+
def test_clocks_with_minute_overflow_by_several_days
249+
skip
250+
clock1 = Clock.at(2, 2)
251+
clock2 = Clock.at(2, 4322)
252+
assert clock1 == clock2
253+
end
254+
255+
def test_clocks_with_negative_minute
256+
skip
257+
clock1 = Clock.at(2, 40)
258+
clock2 = Clock.at(3, -20)
259+
assert clock1 == clock2
260+
end
261+
262+
def test_clocks_with_negative_minute_that_wraps
263+
skip
264+
clock1 = Clock.at(4, 10)
265+
clock2 = Clock.at(5, -1490)
266+
assert clock1 == clock2
267+
end
268+
269+
def test_clocks_with_negative_minute_that_wraps_multiple_times
270+
skip
271+
clock1 = Clock.at(6, 15)
272+
clock2 = Clock.at(6, -4305)
273+
assert clock1 == clock2
274+
end
275+
276+
def test_clocks_with_negative_hours_and_minutes
277+
skip
278+
clock1 = Clock.at(7, 32)
279+
clock2 = Clock.at(-12, -268)
280+
assert clock1 == clock2
281+
end
282+
283+
def test_clocks_with_negative_hours_and_minutes_that_wrap
284+
skip
285+
clock1 = Clock.at(18, 7)
286+
clock2 = Clock.at(-54, -11_513)
287+
assert clock1 == clock2
141288
end
142289

143-
def test_wrap_around_backwards
290+
# Problems in exercism evolve over time,
291+
# as we find better ways to ask questions.
292+
# The version number refers to the version of the problem you solved,
293+
# not your solution.
294+
#
295+
# Define a constant named VERSION inside of Clock.
296+
# If you are curious, read more about constants on RubyDoc:
297+
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
298+
def test_bookkeeping
144299
skip
145-
clock = Clock.at(0, 30) - 60
146-
assert_equal '23:30', clock.to_s
300+
assert_equal 1, Clock::VERSION
147301
end
148302
end

0 commit comments

Comments
 (0)