workpattern 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_day.rb ADDED
@@ -0,0 +1,402 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestDay < Test::Unit::TestCase #:nodoc:
4
+
5
+ def setup
6
+ end
7
+
8
+ must "create a working day" do
9
+
10
+ working_day = Workpattern::Day.new(1)
11
+ assert_equal 1440, working_day.total,"24 hour working total minutes"
12
+ end
13
+
14
+ must "ceate a resting day" do
15
+
16
+ resting_day = Workpattern::Day.new(0)
17
+ assert_equal 0, resting_day.total,"24 hour resting total minutes"
18
+ end
19
+
20
+ must "set patterns correctly" do
21
+
22
+ times=Array.new()
23
+ [[0,0,8,59],
24
+ [12,0,12,59],
25
+ [17,0,22,59]
26
+ ].each {|start_hour,start_min,finish_hour,finish_min|
27
+ times<<[clock(start_hour,start_min),clock(finish_hour,finish_min)]
28
+ }
29
+
30
+ [[24,480,clock(9,0),clock(23,59)]
31
+ ].each{|hours_in_day,total,first_time,last_time|
32
+ working_day=Workpattern::Day.new(1)
33
+ times.each{|start_time,finish_time|
34
+ working_day.workpattern(start_time,finish_time,0)
35
+ }
36
+ assert_equal total,working_day.total, "#{hours_in_day} hour total working minutes"
37
+ assert_equal first_time.hour, working_day.first_hour, "#{hours_in_day} hour first hour of the day"
38
+ assert_equal first_time.min, working_day.first_min, "#{hours_in_day} hour first minute of the day"
39
+ assert_equal last_time.hour, working_day.last_hour, "#{hours_in_day} hour last hour of the day"
40
+ assert_equal last_time.min, working_day.last_min, "#{hours_in_day} hour last minute of the day"
41
+ }
42
+ end
43
+
44
+ must "duplicate all of day" do
45
+ day=Workpattern::Day.new(1)
46
+ new_day = day.duplicate
47
+ assert_equal 1440, new_day.total,"24 hour duplicate working total minutes"
48
+ # y m ,d ,h ,n ,dur ,yr ,mr,dr,hr,nr,rem ,midnight, midnightr
49
+ tests=[
50
+ [2000,1 ,1 ,0 , 0,3 ,2000,1 ,1 ,0 ,3 ,0 ,false ,false],
51
+ [2000,1 ,1 ,23,59,0 ,2000,1 ,1 ,23,59,0 ,false ,false],
52
+ [2000,1 ,1 ,23,59,1 ,2000,1 ,2 ,0 ,0 ,0 ,false ,false],
53
+ [2000,1 ,1 ,23,59,2 ,2000,1 ,2 ,0 ,0 ,1 ,false ,false],
54
+ [2000,1 ,1 ,9 ,10,33 ,2000,1 ,1 ,9 ,43,0 ,false ,false],
55
+ [2000,1 ,1 ,9 ,10,60 ,2000,1 ,1 ,10,10,0 ,false ,false],
56
+ [2000,1 ,1 ,9 , 0,931 ,2000,1 ,2 ,0 ,0 ,31 ,false ,false]
57
+ ]
58
+ clue="duplicate working pattern"
59
+ calc_test(new_day,tests,clue)
60
+
61
+ day = Workpattern::Day.new(0)
62
+ new_day=day.duplicate
63
+ assert_equal 0, new_day.total,"24 hour resting total minutes"
64
+ # y m ,d ,h ,n ,dur ,yr ,mr,dr,hr,nr,rem ,midnight, midnightr
65
+ tests=[
66
+ [2000,1,1,0,0,3,2000,1,2,0,0,3,false,false],
67
+ [2000,1,1,23,59,0,2000,1,1,23,59,0,false,false],
68
+ [2000,1,1,23,59,1,2000,1,2,0,0,1,false,false],
69
+ [2000,1,1,23,59,2,2000,1,2,0,0,2,false,false],
70
+ [2000,1,1,9,10,33,2000,1,2,0,0,33,false,false],
71
+ [2000,1,1,9,10,60,2000,1,2,0,0,60,false,false],
72
+ [2000,1,1,9,0,931,2000,1,2,0,0,931,false,false]
73
+ ]
74
+ clue="duplicate resting pattern"
75
+ calc_test(new_day,tests,clue)
76
+
77
+
78
+ times=Array.new()
79
+ [[0,0,8,59],
80
+ [12,0,12,59],
81
+ [17,0,22,59]
82
+ ].each {|start_hour,start_min,finish_hour,finish_min|
83
+ times<<[Workpattern::Clock.new(start_hour,start_min),Workpattern::Clock.new(finish_hour,finish_min)]
84
+ }
85
+
86
+ [[24,480,clock(9,0),clock(23,59)]
87
+ ].each{|hours_in_day,total,first_time,last_time|
88
+ day=Workpattern::Day.new(1)
89
+ times.each{|start_time,finish_time|
90
+ day.workpattern(start_time,finish_time,0)
91
+ }
92
+ new_day=day.duplicate
93
+
94
+ assert_equal total,new_day.total, "#{hours_in_day} hour total working minutes"
95
+ assert_equal first_time.hour, new_day.first_hour, "#{hours_in_day} hour first hour of the day"
96
+ assert_equal first_time.min, new_day.first_min, "#{hours_in_day} hour first minute of the day"
97
+ assert_equal last_time.hour, new_day.last_hour, "#{hours_in_day} hour last hour of the day"
98
+ assert_equal last_time.min, new_day.last_min, "#{hours_in_day} hour last minute of the day"
99
+
100
+ new_day.workpattern(clock(13,0),clock(13,0),0)
101
+
102
+ assert_equal total,day.total, "#{hours_in_day} hour total original working minutes"
103
+
104
+ assert_equal total-1,new_day.total, "#{hours_in_day} hour total new working minutes"
105
+
106
+ }
107
+
108
+ end
109
+
110
+ must 'add minutes in a working day' do
111
+
112
+ day = Workpattern::Day.new(1)
113
+ # y m ,d ,h ,n ,dur ,yr ,mr,dr,hr,nr,rem ,midnight, midnightr
114
+ tests=[
115
+ [2000,1,1,0,0,3,2000,1,1,0,3,0,false,false],
116
+ [2000,1,1,0,0,0,2000,1,1,0,0,0,false,false],
117
+ [2000,1,1,0,59,0,2000,1,1,0,59,0,false,false],
118
+ [2000,1,1,0,11,3,2000,1,1,0,14,0,false,false],
119
+ [2000,1,1,0,0,60,2000,1,1,1,0,0,false,false],
120
+ [2000,1,1,0,0,61,2000,1,1,1,1,0,false,false],
121
+ [2000,1,1,0,30,60,2000,1,1,1,30,0,false,false],
122
+ [2000,12,31,23,59,1,2001,1,1,0,0,0,false,false],
123
+ [2000,1,1,9,10,33,2000,1,1,9,43,0,false,false],
124
+ [2000,1,1,9,10,60,2000,1,1,10,10,0,false,false],
125
+ [2000,1,1,9,0,931,2000,1,2,0,0,31,false,false]
126
+ ]
127
+ clue = "add minutes in a working day"
128
+ calc_test(day,tests,clue)
129
+
130
+ end
131
+
132
+ must 'add minutes in a resting day' do
133
+
134
+ day = Workpattern::Day.new(0)
135
+ # y m ,d ,h ,n ,dur ,yr ,mr,dr,hr,nr,rem ,midnight, midnightr
136
+ tests=[
137
+ [2000,1,1,0,0,3,2000,1,2,0,0,3,false,false],
138
+ [2000,1,1,23,59,0,2000,1,1,23,59,0,false,false],
139
+ [2000,1,1,23,59,1,2000,1,2,0,0,1,false,false],
140
+ [2000,1,1,23,59,2,2000,1,2,0,0,2,false,false],
141
+ [2000,1,1,9,10,33,2000,1,2,0,0,33,false,false],
142
+ [2000,1,1,9,10,60,2000,1,2,0,0,60,false,false],
143
+ [2000,1,1,9,0,931,2000,1,2,0,0,931,false,false]
144
+ ]
145
+ clue="add minutes in a resting day"
146
+ calc_test(day,tests,clue)
147
+ end
148
+
149
+ must 'add minutes in a patterned day' do
150
+
151
+ day = Workpattern::Day.new(1)
152
+ [[0,0,8,59],
153
+ [12,0,12,59],
154
+ [17,0,22,59]
155
+ ].each {|start_hour,start_min,finish_hour,finish_min|
156
+ day.workpattern(clock(start_hour, start_min),
157
+ clock(finish_hour, finish_min),
158
+ 0)
159
+ }
160
+ assert_equal 480, day.total, "minutes in patterned day should be 480"
161
+ # y m ,d ,h ,n ,dur ,yr ,mr,dr,hr,nr,rem ,midnight, midnightr
162
+ tests=[
163
+ [2000,1,1,0,0,3,2000,1,1,9,3,0,false,false],
164
+ [2000,1,1,0,0,0,2000,1,1,0,0,0,false,false],
165
+ [2000,1,1,0,59,0,2000,1,1,0,59,0,false,false],
166
+ [2000,1,1,0,11,3,2000,1,1,9,3,0,false,false],
167
+ [2000,1,1,0,0,60,2000,1,1,10,0,0,false,false],
168
+ [2000,1,1,0,0,61,2000,1,1,10,1,0,false,false],
169
+ [2000,1,1,9,30,60,2000,1,1,10,30,0,false,false],
170
+ [2000,12,31,22,59,1,2000,12,31,23,1,0,false,false],
171
+ [2000,1,1,9,10,33,2000,1,1,9,43,0,false,false],
172
+ [2000,1,1,9,10,60,2000,1,1,10,10,0,false,false],
173
+ [2000,1,1,9,0,931,2000,1,2,0,0,451,false,false],
174
+ [2000,1,1,12,0,1,2000,1,1,13,1,0,false,false],
175
+ [2000,1,1,12,59,1,2000,1,1,13,1,0,false,false]
176
+ ]
177
+ clue = "add minutes in a patterned day"
178
+ calc_test(day,tests,clue)
179
+ end
180
+
181
+ must 'subtract minutes in a working day' do
182
+
183
+ day = Workpattern::Day.new(1)
184
+ # y ,m ,d ,h ,n ,dur ,yr ,mr,dr,hr,nr,rem ,midnight,midnightr
185
+ tests=[
186
+ [2000,1 ,1 ,0 ,0 ,-3 ,1999,12,31,0 ,0 ,-3 ,false ,true],
187
+ [2000,1 ,1 ,23,59,0 ,2000,1 ,1 ,23,59,0 ,false ,false],
188
+ [2000,1 ,1 ,23,59,-1 ,2000,1 ,1 ,23,58,0 ,false ,false],
189
+ [2000,1 ,1 ,23,59,-2 ,2000,1 ,1 ,23,57,0 ,false ,false],
190
+ [2000,1 ,1 ,9 ,10,-33 ,2000,1 ,1 ,8 ,37,0 ,false ,false],
191
+ [2000,1 ,1 ,9 ,10,-60 ,2000,1 ,1 ,8 ,10,0 ,false ,false],
192
+ [2000,1 ,1 ,9 ,4 ,-3 ,2000,1 ,1 ,9 ,1 ,0 ,false ,false],
193
+ [2000,1 ,1 ,9 ,0 ,-931,1999,12,31,0 ,0 ,-391,false ,true],
194
+ [2000,1 ,1 ,0 ,0 ,-3 ,2000,1 ,1 ,23,57,0 ,true ,false],
195
+ [2000,1 ,1 ,23,59,0 ,2000,1 ,1 ,23,59,0 ,true ,false],
196
+ [2000,1 ,1 ,23,59,-1 ,2000,1 ,1 ,23,58,0 ,true ,false],
197
+ [2000,1 ,1 ,0 ,0 ,-2 ,2000,1 ,1 ,23,58,0 ,true ,false],
198
+ [2000,1 ,1 ,0 ,0 ,-33 ,2000,1 ,1 ,23,27,0 ,true ,false],
199
+ [2000,1 ,1 ,0 ,0 ,-60 ,2000,1 ,1 ,23,0 ,0 ,true ,false],
200
+ [2000,1 ,1 ,0 ,0 ,-931,2000,1 ,1 ,8 ,29,0 ,true ,false]
201
+ ]
202
+ clue="subtract minutes in a working day"
203
+ calc_test(day,tests,clue)
204
+ end
205
+
206
+ must 'subtract minutes in a resting day' do
207
+
208
+ day = Workpattern::Day.new(0)
209
+ # y ,m ,d ,h ,n ,dur ,yr ,mr,dr,hr,nr,rem ,midnight,midnightr
210
+ tests=[
211
+ [2000,1 ,1 ,0 ,0 ,-3 ,1999,12,31,0 ,0 ,-3 ,false ,true],
212
+ [2000,1 ,1 ,23,59,0 ,2000,1 ,1 ,23,59,0 ,false ,false],
213
+ [2000,1 ,1 ,23,59,-1 ,1999,12,31,0 ,0 ,-1 ,false ,true],
214
+ [2000,1 ,1 ,23,59,-2 ,1999,12,31,0 ,0 ,-2 ,false ,true],
215
+ [2000,1 ,1 ,9 ,10,-33 ,1999,12,31,0 ,0 ,-33 ,false ,true],
216
+ [2000,1 ,1 ,9 ,10,-60 ,1999,12,31,0 ,0 ,-60 ,false ,true],
217
+ [2000,1 ,1 ,9 ,0 ,-931,1999,12,31,0 ,0 ,-931,false ,true],
218
+ [2000,1 ,1 ,0 ,0 ,-3 ,1999,12,31,0 ,0 ,-3 ,true ,true],
219
+ [2000,1 ,1 ,23,59,0 ,2000,1 ,1 ,23,59,0 ,true ,false],
220
+ [2000,1 ,1 ,23,59,-1 ,1999,12,31,0 ,0 ,-1 ,true ,true],
221
+ [2000,1 ,1 ,23,59,-2 ,1999,12,31,0 ,0 ,-2 ,true ,true],
222
+ [2000,1 ,1 ,9 ,10,-33 ,1999,12,31,0 ,0 ,-33 ,true ,true],
223
+ [2000,1 ,1 ,9 ,10,-60 ,1999,12,31,0 ,0 ,-60 ,true ,true],
224
+ [2000,1 ,1 ,9 ,0 ,-931,1999,12,31,0 ,0 ,-931,true ,true]
225
+ ]
226
+ clue="subtract minutes in a resting day"
227
+ calc_test(day,tests,clue)
228
+ end
229
+
230
+ must 'subtract minutes in a patterned day' do
231
+
232
+ day = Workpattern::Day.new(1)
233
+ [[0,0,8,59],
234
+ [12,0,12,59],
235
+ [17,0,22,59]
236
+ ].each {|start_hour,start_min,finish_hour,finish_min|
237
+ day.workpattern(clock(start_hour, start_min),
238
+ clock(finish_hour, finish_min),
239
+ 0)
240
+ }
241
+ assert_equal 480, day.total, "minutes in patterned day should be 480"
242
+ assert_equal 1, day.minutes(16,59,16,59),"16:59 should be 1"
243
+ assert_equal 0, day.minutes(17,0,17,0),"17:00 should be 0"
244
+ assert_equal 0, day.minutes(22,59,22,59),"22:59 should be 0"
245
+ assert_equal 1, day.minutes(23,0,23,0),"23:00 should be 1"
246
+ # y ,m ,d ,h ,n ,dur ,yr ,mr,dr,hr,nr,rem ,midnight,midnightr
247
+ tests=[
248
+ [2000,1 ,1 ,0 ,0 ,-3 ,1999,12,31,0 ,0 ,-3 ,false ,true],
249
+ [2000,1 ,1 ,0 ,0 ,0 ,2000,1 ,1 ,0 ,0 ,0 ,false ,false],
250
+ [2000,1 ,1 ,0 ,59,0 ,2000,1 ,1 ,0 ,59,0 ,false ,false],
251
+ [2000,1 ,1 ,9 ,4 ,-3 ,2000,1 ,1 ,9 ,1 ,0 ,false ,false],
252
+ [2000,1 ,1 ,0 ,0 ,-60 ,1999,12,31,0 ,0 ,-60 ,false ,true],
253
+ [2000,1 ,1 ,0 ,0 ,-61 ,1999,12,31,0 ,0 ,-61 ,false ,true],
254
+ [2000,1 ,1 ,9 ,30,-60 ,1999,12,31,0 ,0 ,-30 ,false ,true],
255
+ [2000,12,31,22,59,-1 ,2000,12,31,16,59,0 ,false ,false],
256
+ [2000,1 ,1 ,9 ,10,-33 ,1999,12,31,0 ,0 ,-23 ,false ,true],
257
+ [2000,1 ,1 ,9 ,10,-60 ,1999,12,31,0 ,0 ,-50 ,false ,true],
258
+ [2000,1 ,1 ,9 ,1 ,-931,1999,12,31,0 ,0 ,-930,false ,true],
259
+ [2000,1 ,1 ,12,0 ,-1 ,2000,1 ,1 ,11,59,0 ,false ,false],
260
+ [2000,1 ,1 ,12,59,-1 ,2000,1 ,1 ,11,59,0 ,false ,false],
261
+ [2000,1 ,1 ,0 ,0 ,-3 ,2000,1 ,1 ,23,57,0 ,true ,false],
262
+ [2000,1 ,1 ,0 ,0 ,0 ,2000,1 ,1 ,0 ,0 ,0 ,true ,false],
263
+ [2000,1 ,1 ,0 ,59,0 ,2000,1 ,1 ,0 ,59,0 ,true ,false],
264
+ [2000,1 ,1 ,9 ,4 ,-3 ,2000,1 ,1 ,9 ,1 ,0 ,true ,false],
265
+ [2000,1 ,1 ,0 ,0 ,-60 ,2000,1 ,1 ,23,0 ,0 ,true ,false],
266
+ [2000,1 ,1 ,0 ,0 ,-61 ,2000,1 ,1 ,16,59,0 ,true ,false],
267
+ [2000,1 ,1 ,0 ,0 ,-931,1999,12,31,0 ,0 ,-451,true ,true],
268
+ [2000,1 ,1 ,12,0 ,-1 ,2000,1 ,1 ,11,59,0 ,true ,false]
269
+ ]
270
+ clue = "subtract minutes in a patterned day"
271
+ calc_test(day,tests,clue)
272
+
273
+ end
274
+
275
+
276
+ must "calculate difference between times in working day" do
277
+ day = Workpattern::Day.new(1)
278
+
279
+ [
280
+ [ 2000, 1, 1, 0, 0, 2000, 1, 1, 0, 0, 0,2000, 1, 1, 0, 0],
281
+ [ 2000, 1, 1, 0, 0, 2000, 1, 1, 0, 1, 1,2000, 1, 1, 0, 1],
282
+ [ 2000, 1, 1, 0,50, 2000, 1, 1, 0,59, 9,2000, 1, 1, 0,59],
283
+ [ 2000, 1, 1, 8,50, 2000, 1, 1, 9, 0, 10,2000, 1, 1, 9, 0],
284
+ [ 2000, 1, 1, 0, 0, 2000, 1, 1,23,59,1439,2000, 1, 1,23,59],
285
+ [ 2000, 1, 1, 0, 0, 2000, 1, 2, 0, 0,1440,2000, 1, 2, 0, 0],
286
+ [ 2000, 1, 1, 0, 0, 2000, 1, 2, 0, 1,1440,2000, 1, 2, 0, 0],
287
+ [ 2000, 1, 1, 0, 0, 2010, 3,22, 6,11,1440,2000, 1, 2, 0, 0],
288
+ [ 2000, 1, 1, 0, 1, 2000, 1, 1, 0, 0, 1,2000, 1, 1, 0, 1],
289
+ [ 2000, 1, 1, 0,59, 2000, 1, 1, 0,50, 9,2000, 1, 1, 0,59],
290
+ [ 2000, 1, 1, 9, 0, 2000, 1, 1, 8,50, 10,2000, 1, 1, 9, 0],
291
+ [ 2000, 1, 1,23,59, 2000, 1, 1, 0, 0,1439,2000, 1, 1,23,59],
292
+ [ 2000, 1, 2, 0, 0, 2000, 1, 1, 0, 0,1440,2000, 1, 2, 0, 0],
293
+ [ 2000, 1, 2, 0, 1, 2000, 1, 1, 0, 0,1440,2000, 1, 2, 0, 0],
294
+ [ 2010, 3,22, 6,11, 2000, 1, 1, 0, 0,1440,2000, 1, 2, 0, 0]
295
+ ].each {|start_year, start_month, start_day, start_hour,start_min,
296
+ finish_year, finish_month, finish_day, finish_hour,finish_min,result,
297
+ y,m,d,h,n|
298
+ start=DateTime.new(start_year, start_month, start_day, start_hour,start_min)
299
+ finish=DateTime.new(finish_year, finish_month, finish_day, finish_hour,finish_min)
300
+ expected_date=DateTime.new(y,m,d,h,n)
301
+ duration, result_date=day.diff(start,finish)
302
+ assert_equal result, duration,"duration diff(#{start}, #{finish})"
303
+ assert_equal expected_date, result_date,"date diff(#{start}, #{finish})"
304
+ }
305
+ end
306
+
307
+ must "calculate difference between times in resting day" do
308
+ day = Workpattern::Day.new(0)
309
+
310
+ [
311
+ [ 2000, 1, 1, 0, 0, 2000, 1, 1, 0, 0, 0,2000, 1, 1, 0, 0],
312
+ [ 2000, 1, 1, 0, 0, 2000, 1, 1, 0, 1, 0,2000, 1, 1, 0, 1],
313
+ [ 2000, 1, 1, 0,50, 2000, 1, 1, 0,59, 0,2000, 1, 1, 0,59],
314
+ [ 2000, 1, 1, 8,50, 2000, 1, 1, 9, 0, 0,2000, 1, 1, 9, 0],
315
+ [ 2000, 1, 1, 0, 0, 2000, 1, 1,23,59, 0,2000, 1, 1,23,59],
316
+ [ 2000, 1, 1, 0, 0, 2000, 1, 2, 0, 0, 0,2000, 1, 2, 0, 0],
317
+ [ 2000, 1, 1, 0, 0, 2000, 1, 2, 0, 1, 0,2000, 1, 2, 0, 0],
318
+ [ 2000, 1, 1, 0, 0, 2010, 3,22, 6,11, 0,2000, 1, 2, 0, 0],
319
+ [ 2000, 1, 1, 0, 1, 2000, 1, 1, 0, 0, 0,2000, 1, 1, 0, 1],
320
+ [ 2000, 1, 1, 0,59, 2000, 1, 1, 0,50, 0,2000, 1, 1, 0,59],
321
+ [ 2000, 1, 1, 9, 0, 2000, 1, 1, 8,50, 0,2000, 1, 1, 9, 0],
322
+ [ 2000, 1, 1,23,59, 2000, 1, 1, 0, 0, 0,2000, 1, 1,23,59],
323
+ [ 2000, 1, 2, 0, 0, 2000, 1, 1, 0, 0, 0,2000, 1, 2, 0, 0],
324
+ [ 2000, 1, 2, 0, 1, 2000, 1, 1, 0, 0, 0,2000, 1, 2, 0, 0],
325
+ [ 2010, 3,22, 6,11, 2000, 1, 1, 0, 0, 0,2000, 1, 2, 0, 0]
326
+ ].each {|start_year, start_month, start_day, start_hour,start_min,
327
+ finish_year, finish_month, finish_day, finish_hour,finish_min,result,
328
+ y,m,d,h,n|
329
+ start=DateTime.new(start_year, start_month, start_day, start_hour,start_min)
330
+ finish=DateTime.new(finish_year, finish_month, finish_day, finish_hour,finish_min)
331
+ expected_date=DateTime.new(y,m,d,h,n)
332
+ duration, result_date=day.diff(start,finish)
333
+ assert_equal result, duration,"duration diff(#{start}, #{finish})"
334
+ assert_equal expected_date, result_date,"date diff(#{start}, #{finish})"
335
+ }
336
+ end
337
+
338
+ must "calculate difference between times in pattern day" do
339
+
340
+ day = Workpattern::Day.new(1)
341
+ [[0,0,8,59],
342
+ [12,0,12,59],
343
+ [17,0,22,59]
344
+ ].each {|start_hour,start_min,finish_hour,finish_min|
345
+ day.workpattern(clock(start_hour, start_min),
346
+ clock(finish_hour, finish_min),
347
+ 0)
348
+ }
349
+ assert_equal 480, day.total, "minutes in patterned day should be 480"
350
+ assert_equal 1, day.minutes(16,59,16,59),"16:59 should be 1"
351
+ assert_equal 0, day.minutes(17,0,17,0),"17:00 should be 0"
352
+ assert_equal 0, day.minutes(22,59,22,59),"22:59 should be 0"
353
+ assert_equal 1, day.minutes(23,0,23,0),"23:00 should be 1"
354
+
355
+ [
356
+ [ 2000, 1, 1, 0, 0, 2000, 1, 1, 0, 0, 0,2000, 1, 1, 0, 0],
357
+ [ 2000, 1, 1, 0, 0, 2000, 1, 1, 0, 1, 0,2000, 1, 1, 0, 1],
358
+ [ 2000, 1, 1, 0,50, 2000, 1, 1, 9,59, 59,2000, 1, 1, 9,59],
359
+ [ 2000, 1, 1, 8,50, 2000, 1, 1, 9,10, 10,2000, 1, 1, 9,10],
360
+ [ 2000, 1, 1, 0, 0, 2000, 1, 1,23,59, 479,2000, 1, 1,23,59],
361
+ [ 2000, 1, 1, 0, 0, 2000, 1, 2, 0, 0, 480,2000, 1, 2, 0, 0],
362
+ [ 2000, 1, 1, 0, 0, 2000, 1, 2, 0, 1, 480,2000, 1, 2, 0, 0],
363
+ [ 2000, 1, 1, 0, 0, 2010, 3,22, 6,11, 480,2000, 1, 2, 0, 0],
364
+ [ 2000, 1, 1, 0, 1, 2000, 1, 1, 0, 0, 0,2000, 1, 1, 0, 1],
365
+ [ 2000, 1, 1, 9,59, 2000, 1, 1, 0,50, 59,2000, 1, 1, 9,59],
366
+ [ 2000, 1, 1, 9, 0, 2000, 1, 1, 8,50, 0,2000, 1, 1, 9, 0],
367
+ [ 2000, 1, 1,23,59, 2000, 1, 1, 0, 0, 479,2000, 1, 1,23,59],
368
+ [ 2000, 1, 2, 0, 0, 2000, 1, 1, 0, 0, 480,2000, 1, 2, 0, 0],
369
+ [ 2000, 1, 2, 0, 1, 2000, 1, 1, 0, 0, 480,2000, 1, 2, 0, 0],
370
+ [ 2010, 3,22, 6,11, 2000, 1, 1, 0, 0, 480,2000, 1, 2, 0, 0]
371
+ ].each {|start_year, start_month, start_day, start_hour,start_min,
372
+ finish_year, finish_month, finish_day, finish_hour,finish_min,result,
373
+ y,m,d,h,n|
374
+ start=DateTime.new(start_year, start_month, start_day, start_hour,start_min)
375
+ finish=DateTime.new(finish_year, finish_month, finish_day, finish_hour,finish_min)
376
+ expected_date=DateTime.new(y,m,d,h,n)
377
+ duration, result_date=day.diff(start,finish)
378
+ assert_equal result, duration,"duration diff(#{start}, #{finish})"
379
+ assert_equal expected_date, result_date,"date diff(#{start}, #{finish})"
380
+ }
381
+
382
+ end
383
+
384
+ private
385
+
386
+ def calc_test(day,tests,clue)
387
+ tests.each{|y,m,d,h,n,dur,yr,mr,dr,hr,nr,rem, midnight, midnightr|
388
+ start_date=DateTime.new(y,m,d,h,n)
389
+ result_date,remainder, result_midnight = day.calc(start_date,dur, midnight)
390
+ assert_equal DateTime.new(yr,mr,dr,hr,nr), result_date, "result date calc(#{start_date},#{dur},#{midnight}) for #{clue}"
391
+ assert_equal rem, remainder, "result remainder calc(#{start_date},#{dur},#{midnight}) for #{clue}"
392
+ assert_equal midnightr,result_midnight, "result midnight calc(#{start_date},#{dur},#{midnight}) for #{clue}"
393
+ }
394
+
395
+
396
+ end
397
+
398
+ def clock(hour,min)
399
+ return Workpattern.clock(hour,min)
400
+ end
401
+ end
402
+
@@ -0,0 +1,25 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/workpattern.rb'
4
+
5
+ module Test::Unit
6
+ # Used to fix a minor minitest/unit incompatibility in flexmock
7
+ #AssertionFailedError = Class.new(StandardError)
8
+
9
+ class TestCase
10
+
11
+ def self.must(name, &block)
12
+ test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
13
+ defined = instance_method(test_name) rescue false
14
+ raise "#{test_name} is already defined in #{self}" if defined
15
+ if block_given?
16
+ define_method(test_name, &block)
17
+ else
18
+ define_method(test_name) do
19
+ flunk "No implementation provided for #{name}"
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
data/test/test_hour.rb ADDED
@@ -0,0 +1,252 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestHour < Test::Unit::TestCase #:nodoc:
4
+
5
+ def setup
6
+ @working_hour = Workpattern::WORKING_HOUR
7
+ @resting_hour = Workpattern::RESTING_HOUR
8
+ end
9
+
10
+ must "ceate a working hour" do
11
+ working_hour = Workpattern::WORKING_HOUR
12
+ assert_equal 60, working_hour.total,"working total minutes"
13
+ end
14
+
15
+ must "ceate a resting hour" do
16
+ resting_hour = Workpattern::RESTING_HOUR
17
+ assert_equal 0, resting_hour.total,"resting total minutes"
18
+ end
19
+
20
+ must "set patterns correctly" do
21
+ working_hour = Workpattern::WORKING_HOUR
22
+ working_hour = working_hour.workpattern(0,0,0)
23
+ working_hour = working_hour.workpattern(59,59,0)
24
+ working_hour = working_hour.workpattern(11,30,0)
25
+ assert_equal 38,working_hour.total, "total working minutes"
26
+ assert_equal 1, working_hour.first, "first minute of the day"
27
+ assert_equal 58, working_hour.last, "last minute of the day"
28
+ assert !working_hour.working?(0)
29
+ assert working_hour.working?(1)
30
+ end
31
+
32
+ must 'add minutes in a working hour' do
33
+
34
+ working_hour = Workpattern::WORKING_HOUR
35
+ [
36
+ [2000,1,1,0,0,3,2000,1,1,0,3,0],
37
+ [2000,1,1,0,0,0,2000,1,1,0,0,0],
38
+ [2000,1,1,0,59,0,2000,1,1,0,59,0],
39
+ [2000,1,1,0,11,3,2000,1,1,0,14,0],
40
+ [2000,1,1,0,0,60,2000,1,1,1,0,0],
41
+ [2000,1,1,0,0,61,2000,1,1,1,0,1],
42
+ [2000,1,1,0,30,60,2000,1,1,1,0,30],
43
+ [2000,12,31,23,59,1,2001,1,1,0,0,0]
44
+ ].each{|y,m,d,h,n,add,yr,mr,dr,hr,nr,rem|
45
+ start=DateTime.new(y,m,d,h,n)
46
+ result,remainder = working_hour.calc(start,add)
47
+ assert_equal DateTime.new(yr,mr,dr,hr,nr), result, "result calc(#{start},#{add})"
48
+ assert_equal rem, remainder, "remainder calc(#{start},#{add})"
49
+ }
50
+
51
+ end
52
+
53
+ must 'add minutes in a resting hour' do
54
+ resting_hour = Workpattern::RESTING_HOUR
55
+ [
56
+ [2000,1,1,0,0,3,2000,1,1,1,0,3],
57
+ [2000,1,1,0,0,0,2000,1,1,0,0,0],
58
+ [2000,1,1,0,59,0,2000,1,1,0,59,0],
59
+ [2000,1,1,0,11,3,2000,1,1,1,0,3],
60
+ [2000,1,1,0,0,60,2000,1,1,1,0,60],
61
+ [2000,1,1,0,0,61,2000,1,1,1,0,61],
62
+ [2000,1,1,0,30,60,2000,1,1,1,0,60],
63
+ [2000,12,31,23,59,1,2001,1,1,0,0,1]
64
+ ].each{|y,m,d,h,n,add,yr,mr,dr,hr,nr,rem|
65
+ start=DateTime.new(y,m,d,h,n)
66
+ result,remainder = resting_hour.calc(start,add)
67
+ assert_equal DateTime.new(yr,mr,dr,hr,nr), result, "result calc(#{start},#{add})"
68
+ assert_equal rem, remainder, "remainder calc(#{start},#{add})"
69
+ }
70
+
71
+ end
72
+
73
+ must 'add minutes in a patterned hour' do
74
+
75
+ pattern_hour = Workpattern::WORKING_HOUR
76
+ pattern_hour = pattern_hour.workpattern(1,10,0)
77
+ pattern_hour = pattern_hour.workpattern(55,59,0)
78
+ [
79
+ [2000,1,1,0,0,3,2000,1,1,0,13,0],
80
+ [2000,1,1,0,0,0,2000,1,1,0,0,0],
81
+ [2000,1,1,0,59,0,2000,1,1,0,59,0],
82
+ [2000,1,1,0,11,3,2000,1,1,0,14,0],
83
+ [2000,1,1,0,0,60,2000,1,1,1,0,15],
84
+ [2000,1,1,0,0,61,2000,1,1,1,0,16],
85
+ [2000,1,1,0,30,60,2000,1,1,1,0,35],
86
+ [2000,12,31,23,59,1,2001,1,1,0,0,1]
87
+ ].each{|y,m,d,h,n,add,yr,mr,dr,hr,nr,rem|
88
+ start=DateTime.new(y,m,d,h,n)
89
+ result,remainder = pattern_hour.calc(start,add)
90
+ assert_equal DateTime.new(yr,mr,dr,hr,nr), result, "result calc(#{start},#{add})"
91
+ assert_equal rem, remainder, "remainder calc(#{start},#{add})"
92
+ }
93
+
94
+ end
95
+
96
+ must 'subtract minutes in a working hour' do
97
+ working_hour = Workpattern::WORKING_HOUR
98
+ [
99
+ [2000,1,1,0,10,-3,2000,1,1,0,7,0],
100
+ [2000,1,1,0,10,0,2000,1,1,0,10,0],
101
+ [2000,1,1,0,59,0,2000,1,1,0,59,0],
102
+ [2000,1,1,0,11,-3,2000,1,1,0,8,0],
103
+ [2000,1,1,0,10,-60,2000,1,1,0,0,-50],
104
+ [2000,1,1,0,10,-61,2000,1,1,0,0,-51],
105
+ [2000,1,1,0,30,-60,2000,1,1,0,0,-30],
106
+ [2001,1,1,0,0,-1,2001,1,1,0,0,-1]
107
+ ].each{|y,m,d,h,n,add,yr,mr,dr,hr,nr,rem|
108
+ start=DateTime.new(y,m,d,h,n)
109
+ result,remainder = working_hour.calc(start,add)
110
+ assert_equal DateTime.new(yr,mr,dr,hr,nr), result, "result calc(#{start},#{add})"
111
+ assert_equal rem, remainder, "remainder calc(#{start},#{add})"
112
+ }
113
+
114
+ end
115
+
116
+ must 'subtract minutes in a resting hour' do
117
+ resting_hour = Workpattern::RESTING_HOUR
118
+ [
119
+ [2000,1,1,0,10,-3,2000,1,1,0,0,-3],
120
+ [2000,1,1,0,10,0,2000,1,1,0,10,0],
121
+ [2000,1,1,0,59,0,2000,1,1,0,59,0],
122
+ [2000,1,1,0,11,-3,2000,1,1,0,0,-3],
123
+ [2000,1,1,0,10,-60,2000,1,1,0,0,-60],
124
+ [2000,1,1,0,10,-61,2000,1,1,0,0,-61],
125
+ [2000,1,1,0,30,-60,2000,1,1,0,0,-60],
126
+ [2001,1,1,0,0,-1,2001,1,1,0,0,-1]
127
+ ].each{|y,m,d,h,n,add,yr,mr,dr,hr,nr,rem|
128
+ start=DateTime.new(y,m,d,h,n)
129
+ result,remainder = resting_hour.calc(start,add)
130
+ assert_equal DateTime.new(yr,mr,dr,hr,nr), result, "result calc(#{start},#{add})"
131
+ assert_equal rem, remainder, "remainder calc(#{start},#{add})"
132
+ }
133
+
134
+ end
135
+
136
+ must 'subtract minutes in a patterned hour' do
137
+
138
+ pattern_hour = Workpattern::WORKING_HOUR
139
+ pattern_hour = pattern_hour.workpattern(1,10,0)
140
+ pattern_hour = pattern_hour.workpattern(55,59,0)
141
+ [
142
+ [2000,1,1,0,0,-3,2000,1,1,0,0,-3],
143
+ [2000,1,1,0,0,0,2000,1,1,0,0,0],
144
+ [2000,1,1,0,59,0,2000,1,1,0,59,0],
145
+ [2000,1,1,0,11,-2,2000,1,1,0,0,-1],
146
+ [2000,1,1,0,0,-60,2000,1,1,0,0,-60],
147
+ [2000,1,1,0,0,-61,2000,1,1,0,0,-61],
148
+ [2000,1,1,0,30,-60,2000,1,1,0,0,-40],
149
+ [2001,1,1,23,59,-1,2001,1,1,23,54,0]
150
+ ].each{|y,m,d,h,n,add,yr,mr,dr,hr,nr,rem|
151
+ start=DateTime.new(y,m,d,h,n)
152
+ result,remainder = pattern_hour.calc(start,add)
153
+ assert_equal DateTime.new(yr,mr,dr,hr,nr), result, "result calc(#{start},#{add})"
154
+ assert_equal rem, remainder, "remainder calc(#{start},#{add})"
155
+ }
156
+ end
157
+
158
+
159
+ must 'create complex patterns' do
160
+ working_hour = Workpattern::WORKING_HOUR
161
+ control=Array.new(60) {|i| 1}
162
+ j=0
163
+ [[0,0,0,59,1,59],
164
+ [59,59,0,58,1,58],
165
+ [11,30,0,38,1,58],
166
+ [1,15,0,28,31,58],
167
+ [2,5,1,32,2,58],
168
+ [0,59,1,60,0,59],
169
+ [0,59,0,0,nil,nil],
170
+ [0,0,1,1,0,0],
171
+ [59,59,1,2,0,59]
172
+ ].each{|start,finish,type,total,first,last|
173
+ working_hour = working_hour.workpattern(start,finish,type)
174
+ assert_equal total,working_hour.total, "total working minutes #{j}"
175
+ assert_equal first, working_hour.first, "first minute of the day #{j}"
176
+ assert_equal last, working_hour.last, "last minute of the day #{j}"
177
+ start.upto(finish) {|i| control[i]=type}
178
+ 0.upto(59) {|i|
179
+ if (control[i]==0)
180
+ assert !working_hour.working?(i)
181
+ else
182
+ assert working_hour.working?(i)
183
+ end
184
+ }
185
+ j+=1
186
+ }
187
+ end
188
+
189
+ must "calculate difference between minutes in working hour" do
190
+
191
+ working_hour = Workpattern::WORKING_HOUR
192
+ [[0,0,0],
193
+ [0,1,1],
194
+ [50,59,9],
195
+ [50,60,10],
196
+ [0,59,59],
197
+ [0,60,60],
198
+ [1,0,1],
199
+ [59,50,9],
200
+ [60,50,10],
201
+ [59,0,59],
202
+ [60,0,60]
203
+ ].each {|start,finish,result|
204
+ assert_equal result, working_hour.diff(start,finish),"diff(#{start},#{finish})"
205
+ }
206
+
207
+ end
208
+
209
+ must "calculate difference between minutes in resting hour" do
210
+ resting_hour = Workpattern::RESTING_HOUR
211
+ [[0,0,0],
212
+ [0,1,0],
213
+ [50,59,0],
214
+ [50,60,0],
215
+ [0,59,0],
216
+ [0,60,0],
217
+ [1,0,0],
218
+ [59,50,0],
219
+ [60,50,0],
220
+ [59,0,0],
221
+ [60,0,0]
222
+ ].each {|start,finish,result|
223
+ assert_equal result, resting_hour.diff(start,finish),"diff(#{start},#{finish})"
224
+ }
225
+
226
+ end
227
+
228
+ must "calculate difference between minutes in pattern hour" do
229
+
230
+ pattern_hour = Workpattern::WORKING_HOUR
231
+ pattern_hour = pattern_hour.workpattern(1,10,0)
232
+ pattern_hour = pattern_hour.workpattern(55,59,0)
233
+ pattern_hour = pattern_hour.workpattern(59,59,1)
234
+
235
+ [[0,0,0],
236
+ [0,1,1],
237
+ [50,59,5],
238
+ [50,60,6],
239
+ [0,59,45],
240
+ [0,60,46],
241
+ [1,0,1],
242
+ [59,50,5],
243
+ [60,50,6],
244
+ [59,0,45],
245
+ [60,0,46]
246
+ ].each {|start,finish,result|
247
+ assert_equal result, pattern_hour.diff(start,finish),"diff(#{start},#{finish})"
248
+ }
249
+ end
250
+
251
+ end
252
+