workpattern 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_helper.rb CHANGED
@@ -1,3 +1,2 @@
1
- require 'stringio'
2
1
  require 'minitest/autorun'
3
2
  require File.dirname(__FILE__) + '/../lib/workpattern.rb'
data/test/test_hour.rb CHANGED
@@ -274,6 +274,13 @@ class TestHour < MiniTest::Unit::TestCase #:nodoc:
274
274
  assert_equal DateTime.new(2013,1,1,1,2), result
275
275
  assert_equal 0,remainder
276
276
  end
277
+ ######
278
+ def test_can_subtract_using_next_hour_and_non_working_59_in_patterned_hour
279
+ start_date=DateTime.new(2013,1,1,0,0)
280
+ result, remainder=@pattern_hour.wp_calc(start_date,-9,true)
281
+ assert_equal DateTime.new(2013,1,1,0,50), result
282
+ assert_equal 0,remainder
283
+ end
277
284
 
278
285
  def test_can_change_working_to_resting
279
286
  new_hour=@working_hour.wp_workpattern(0,59,0)
data/test/test_week.rb CHANGED
@@ -3,6 +3,17 @@ require File.dirname(__FILE__) + '/test_helper.rb'
3
3
  class TestWeek < MiniTest::Unit::TestCase #:nodoc:
4
4
 
5
5
  def setup
6
+ start=DateTime.new(2000,1,3)
7
+ finish=DateTime.new(2000,1,9)
8
+ @working_week=Workpattern::Week.new(start,finish,1)
9
+
10
+ @resting_week=Workpattern::Week.new(start,finish,0)
11
+
12
+ @pattern_week=Workpattern::Week.new(start,finish,1)
13
+ @pattern_week.workpattern(:weekend,Workpattern.clock(0,0),Workpattern.clock(23,59),0)
14
+ @pattern_week.workpattern(:weekday,Workpattern.clock(0,0),Workpattern.clock(8,59),0)
15
+ @pattern_week.workpattern(:weekday,Workpattern.clock(12,30),Workpattern.clock(13,0),0)
16
+ @pattern_week.workpattern(:weekday,Workpattern.clock(17,0),Workpattern.clock(23,59),0)
6
17
 
7
18
  end
8
19
 
@@ -20,11 +31,6 @@ class TestWeek < MiniTest::Unit::TestCase #:nodoc:
20
31
  assert_equal 60, duration
21
32
  assert_equal DateTime.civil(2013,9,27,0,0), start
22
33
  end
23
-
24
-
25
- ######################################################
26
- ######################################################
27
- ######################################################
28
34
 
29
35
  def test_must_create_a_working_week
30
36
  start=DateTime.new(2000,1,1,11,3)
@@ -55,6 +61,24 @@ class TestWeek < MiniTest::Unit::TestCase #:nodoc:
55
61
  assert_equal 3156480, new_week.total#2192
56
62
 
57
63
  end
64
+
65
+ def test_must_set_week_pattern_correctly
66
+ start=DateTime.new(2000,1,3)
67
+ finish=DateTime.new(2000,1,9)
68
+
69
+ pattern_week=Workpattern::Week.new(start,finish,1)
70
+ assert_equal start, pattern_week.start
71
+ assert_equal finish, pattern_week.finish
72
+ assert_equal 10080, pattern_week.week_total
73
+ pattern_week.workpattern(:weekend,Workpattern.clock(0,0),Workpattern.clock(23,59),0)
74
+ assert_equal 7200, pattern_week.week_total
75
+ pattern_week.workpattern(:weekday,Workpattern.clock(0,0),Workpattern.clock(8,59),0)
76
+ assert_equal 4500, pattern_week.week_total
77
+ pattern_week.workpattern(:weekday,Workpattern.clock(12,30),Workpattern.clock(13,0),0)
78
+ assert_equal 4345, pattern_week.week_total
79
+ pattern_week.workpattern(:weekday,Workpattern.clock(17,0),Workpattern.clock(23,59),0)
80
+ assert_equal 2245, pattern_week.week_total
81
+ end
58
82
 
59
83
  def test_must_set_patterns_correctly
60
84
  start=DateTime.new(2000,1,1,0,0)
@@ -78,275 +102,324 @@ class TestWeek < MiniTest::Unit::TestCase #:nodoc:
78
102
  working_week.workpattern(:all,clock(0,0),clock(23,59),1)
79
103
  assert_equal 10080, working_week.week_total
80
104
  working_week.workpattern(:weekend,clock(0,0),clock(23,59),0)
81
- assert_equal 7200, working_week.week_total
82
-
105
+ assert_equal 7200, working_week.week_total
83
106
  end
84
-
85
- def test_must_add_minutes_in_a_working_week
86
- start=DateTime.new(2000,1,1,0,0)
87
- finish=DateTime.new(2005,12,31,8,59)
88
- working_week=week(start,finish,1)
89
-
90
- [# yyyy,mm,dd,hh,mn,durtn,ryyyy,rmm,rdd,rhh,rmn,rdurtn
91
- [ 2000, 1, 1, 0, 0, 0, 2000, 1, 1, 0, 0, 0],
92
- [ 2005,12,31, 8,59, 10, 2005, 12, 31, 9, 9, 0],
93
- [ 2005,12,31,23,59, 1, 2006, 1, 1, 0, 0, 0],
94
- [ 2005,12,31,23,59, 2, 2006, 1, 1, 0, 0, 1],
95
- [ 2005,11,30,23,59, 2, 2005, 12, 1, 0, 1, 0]
96
- ].each {|yyyy,mm,dd,hh,mn,durtn,ryyyy,rmm,rdd,rhh,rmn,rdurtn|
97
- start=DateTime.new(yyyy,mm,dd,hh,mn)
98
- result_date, result_duration= working_week.calc(start,durtn)
99
- assert_equal DateTime.new(ryyyy,rmm,rdd,rhh,rmn), result_date, "result_date for working_week.calc(#{start},#{durtn})"
100
- assert_equal rdurtn, result_duration,"result_duration for working_week.calc(#{start},#{durtn})"
101
- }
107
+
108
+ def test_must_add_minutes_in_a_working_week_result_in_same_day
109
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,3,7,31),29)
110
+ assert_equal DateTime.new(2000,1,3,8,0), result_date
111
+ refute midnight_flag
112
+ assert_equal 0, result_duration
102
113
  end
103
-
114
+
115
+ def test_must_add_minutes_in_a_working_week_result_in_next_day
116
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,3,7,31),990)
117
+ assert_equal DateTime.new(2000,1,4,0,1), result_date
118
+ refute midnight_flag
119
+ assert_equal 0, result_duration
120
+ end
121
+
122
+ def test_must_add_minutes_in_a_working_week_result_in_later_day
123
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,3,7,31),2430)
124
+ assert_equal DateTime.new(2000,1,5,0,1), result_date
125
+ refute midnight_flag
126
+ assert_equal 0, result_duration
127
+ end
128
+
129
+ def test_must_add_minutes_in_a_working_week_result_in_start_next_day
130
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,3,7,31),989)
131
+ assert_equal DateTime.new(2000,1,4,0,0), result_date
132
+ refute midnight_flag
133
+ assert_equal 0, result_duration
134
+ end
135
+
136
+ def test_must_add_0_minutes_in_a_working_week
137
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,3,7,31),0)
138
+ assert_equal DateTime.new(2000,1,3,7,31), result_date
139
+ refute midnight_flag
140
+ assert_equal 0, result_duration
141
+ end
142
+
143
+ def test_must_add_too_many_minutes_in_a_working_week
144
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,3,7,31),9630)
145
+ assert_equal DateTime.new(2000,1,10,0,0), result_date
146
+ refute midnight_flag
147
+ assert_equal 1, result_duration
148
+ end
149
+
104
150
  def test_must_add_minutes_in_a_resting_week
105
- start=DateTime.new(2000,1,1,0,0)
106
- finish=DateTime.new(2005,12,31,8,59)
107
- resting_week=week(start,finish,0)
108
-
109
- [# yyyy,mm,dd,hh,mn,durtn,midnight,ryyyy,rmm,rdd,rhh,rmn,rdurtn,rmidnight
110
- [ 2000, 1, 1, 0, 0, 0, false, 2000, 1, 1, 0, 0, 0, false],
111
- [ 2005,12,31, 8,59, 10, false, 2006, 1, 1, 0, 0, 10, false],
112
- [ 2005,12,31,23,59, 1, false, 2006, 1, 1, 0, 0, 1, false],
113
- [ 2005,12,31,23,59, 2, false, 2006, 1, 1, 0, 0, 2, false],
114
- [ 2005,11,30,23,59, 2, false, 2006, 1, 1, 0, 0, 2, false],
115
- [ 2000, 1, 1, 0, 0, 0, true, 2000, 1, 1, 0, 0, 0, false],
116
- [ 2005,12,31, 8,59, 10, true, 2006, 1, 1, 0, 0, 10, false],
117
- [ 2005,12,31,23,59, 1, true, 2006, 1, 1, 0, 0, 1, false],
118
- [ 2005,12,31,23,59, 2, true, 2006, 1, 1, 0, 0, 2, false],
119
- [ 2005,11,30,23,59, 2, true, 2006, 1, 1, 0, 0, 2, false]
120
- ].each {|yyyy,mm,dd,hh,mn,durtn,midnight,ryyyy,rmm,rdd,rhh,rmn,rdurtn,rmidnight|
121
- start=DateTime.new(yyyy,mm,dd,hh,mn)
122
- result_date, result_duration,result_midnight= resting_week.calc(start,durtn,midnight)
123
- assert_equal DateTime.new(ryyyy,rmm,rdd,rhh,rmn), result_date, "result_date for resting_week.calc(#{start},#{durtn},#{midnight})"
124
- assert_equal rdurtn, result_duration,"result_duration for resting_week.calc(#{start},#{durtn},#{midnight})"
125
- assert_equal rmidnight, result_midnight,"result_midnight for resting_week.calc(#{start},#{durtn},#{midnight})"
126
- }
151
+ result_date, result_duration, midnight_flag = @resting_week.calc(DateTime.new(2000,1,3,7,31),29)
152
+ assert_equal DateTime.new(2000,1,10,0,0), result_date
153
+ refute midnight_flag
154
+ assert_equal 29, result_duration
127
155
  end
128
-
129
- def test_must_add_minutes_in_a_patterned_week
130
- start=DateTime.new(2000,1,1,0,0) #saturday
131
- finish=DateTime.new(2005,12,31,8,59) #saturday
132
- working_week=week(start,finish,0)
133
- working_week.workpattern(:sun,clock(9,0),clock(9,13),1)
134
-
135
- working_week.workpattern(:weekday,clock(9,0),clock(11,59),1)
136
- working_week.workpattern(:weekday,clock(13,0),clock(17,59),1)
137
-
138
- working_week.workpattern(:mon,clock(0,0),clock(23,59),0)
139
- working_week.workpattern(:mon,clock(9,0),clock(9,13),1)
140
- working_week.workpattern(:mon,clock(10,1),clock(10,1),1)
141
-
142
-
143
- [# yyyy,mm,dd,hh,mn,durtn,midnight,ryyyy,rmm,rdd,rhh,rmn,rdurtn,rmidnight
144
- [ 2000, 1, 1, 0, 0, 0, false, 2000, 1, 1, 0, 0, 0, false],
145
- [ 2000, 1, 1, 0, 0, 1, false, 2000, 1, 2, 9, 1, 0, false],
146
- [ 2000, 1, 2, 9, 0, 14, false, 2000, 1, 2, 9, 14, 0, false], #Issue #9 - getting wrong time when hour had exactly the right number of minutes
147
- [ 2000, 1, 2, 9, 0, 15, false, 2000, 1, 3, 9, 1, 0, false],
148
- [ 2000, 1, 2, 9, 0, 29, false, 2000, 1, 3, 10, 2, 0, false],
149
- [ 2000, 1, 2, 9, 0, 1950, false, 2000, 1, 9, 9, 1, 0, false],
150
- [ 2005,12,25, 9, 0, 1950, false, 2006, 1, 1, 0, 0, 1, false],
151
- [ 2005,12,25, 9, 0, 1949, false, 2005, 12, 30, 18, 0, 0, false]#,
152
- ].each {|yyyy,mm,dd,hh,mn,durtn,midnight,ryyyy,rmm,rdd,rhh,rmn,rdurtn,rmidnight|
153
- start=DateTime.new(yyyy,mm,dd,hh,mn)
154
- result_date, result_duration,result_midnight= working_week.calc(start,durtn,midnight)
155
- assert_equal DateTime.new(ryyyy,rmm,rdd,rhh,rmn), result_date, "result_date for working_week.calc(#{start},#{durtn},#{midnight})"
156
- assert_equal rdurtn, result_duration,"result_duration for working_week.calc(#{start},#{durtn},#{midnight})"
157
- assert_equal rmidnight, result_midnight,"result_midnight for working_week.calc(#{start},#{durtn},#{midnight})"
158
- }
156
+
157
+ def test_must_add_minutes_from_start_of_resting_week
158
+ result_date, result_duration, midnight_flag = @resting_week.calc(DateTime.new(2000,1,3,0,0),990)
159
+ assert_equal DateTime.new(2000,1,10,0,0), result_date
160
+ refute midnight_flag
161
+ assert_equal 990, result_duration
159
162
  end
160
-
161
- def test_must_subtract_minutes_in_a_working_week
162
- start=DateTime.new(2000,1,1,0,0)
163
- finish=DateTime.new(2005,12,31,8,59)
164
- working_week=week(start,finish,1)
165
- [# yyyy,mm,dd,hh,mn,durtn,midnight,ryyyy,rmm,rdd,rhh,rmn,rdurtn,rmidnight
166
- [ 2000, 1, 1, 0, 0, 0, false, 2000, 1, 1, 0, 0, 0, false],
167
- [ 2005,12,31, 0, 0, -10, false, 2005, 12, 30, 23, 50, 0, false],
168
- [ 2005,12,31, 0, 0, -1, false, 2005, 12, 30, 23, 59, 0, false],
169
- [ 2005,12,31, 0, 1, -2, false, 2005, 12, 30, 23, 59, 0, false], #Issue 6 - available minutes not calculating correctly for a time of 00:01
170
- [ 2000, 1, 1, 0, 1, -2, false, 1999, 12, 31, 0, 0, -1, true], #Issue 6 - available minutes not calculating correctly for a time of 00:01
171
- [ 2000, 1, 1, 0, 0, 0, true, 2000, 1, 1, 0, 0, 0, false],
172
- [ 2005,12,31, 0, 0, -10, true, 2005, 12, 31, 23, 50, 0, false],
173
- [ 2005,12,31, 0, 0, -1, true, 2005, 12, 31, 23, 59, 0, false],
174
- [ 2005,12,31, 0, 1, -2, true, 2005, 12, 31, 23, 58, 0, false],#Issue 7 - midnight flag should override hour and minutes
175
- [ 2000, 1, 1, 0, 1, -2, true, 2000, 1, 1, 23, 58, 0, false] #Issue 7 - midnight flag should override hour and minutes
176
- ].each {|yyyy,mm,dd,hh,mn,durtn,midnight,ryyyy,rmm,rdd,rhh,rmn,rdurtn,rmidnight|
177
- start=DateTime.new(yyyy,mm,dd,hh,mn)
178
- result_date, result_duration,result_midnight= working_week.calc(start,durtn,midnight)
179
- assert_equal DateTime.new(ryyyy,rmm,rdd,rhh,rmn), result_date, "result_date for working_week.calc(#{start},#{durtn},#{midnight})"
180
- assert_equal rdurtn, result_duration,"result_duration for working_week.calc(#{start},#{durtn},#{midnight})"
181
- assert_equal rmidnight, result_midnight,"result_midnight for working_week.calc(#{start},#{durtn},#{midnight})"
182
- }
163
+
164
+ def test_must_add_minutes_to_last_minute_of_a_resting_week
165
+ result_date, result_duration, midnight_flag = @resting_week.calc(DateTime.new(2000,1,9,23,59),2430)
166
+ assert_equal DateTime.new(2000,1,10,0,0), result_date
167
+ refute midnight_flag
168
+ assert_equal 2430, result_duration
183
169
  end
184
-
185
- def test_must_subtract_minutes_in_a_resting_week
186
- start=DateTime.new(2000,1,1,0,0)
187
- finish=DateTime.new(2005,12,31,8,59)
188
- resting_week=week(start,finish,0)
189
- [# yyyy,mm,dd,hh,mn,durtn,midnight,ryyyy,rmm,rdd,rhh,rmn,rdurtn,rmidnight
190
- [ 2000, 1, 1, 0, 0, 0, false, 2000, 1, 1, 0, 0, 0, false],
191
- [ 2005,12,31, 0, 0, -10, false, 1999, 12, 31, 0, 0, -10, true],
192
- [ 2005,12,31, 0, 0, -1, false, 1999, 12, 31, 0, 0, -1, true],
193
- [ 2005,12,31, 0, 1, -2, false, 1999, 12, 31, 0, 0, -2, true],
194
- [ 2000, 1, 1, 0, 1, -2, false, 1999, 12, 31, 0, 0, -2, true]
195
- ].each {|yyyy,mm,dd,hh,mn,durtn,midnight,ryyyy,rmm,rdd,rhh,rmn,rdurtn,rmidnight|
196
- start=DateTime.new(yyyy,mm,dd,hh,mn)
197
- result_date, result_duration, result_midnight= resting_week.calc(start,durtn,midnight)
198
- assert_equal DateTime.new(ryyyy,rmm,rdd,rhh,rmn), result_date, "result_date for resting_week.calc(#{start},#{durtn},#{midnight})"
199
- assert_equal rdurtn, result_duration,"result_duration for resting_week.calc(#{start},#{durtn},#{midnight})"
200
- assert_equal rmidnight, result_midnight,"result_midnight for resting_week.calc(#{start},#{durtn},#{midnight})"
201
- }
170
+
171
+ def test_must_add_zero_minutes_in_a_resting_week
172
+ result_date, result_duration, midnight_flag = @resting_week.calc(DateTime.new(2000,1,3,7,31),0)
173
+ assert_equal DateTime.new(2000,1,3,7,31), result_date
174
+ refute midnight_flag
175
+ assert_equal 0, result_duration
176
+ end
177
+
178
+ def test_must_add_minutes_from_working_in_a_pattern_week_result_in_same_day
179
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,3,10,11),110)
180
+ assert_equal DateTime.new(2000,1,3,12,1), result_date
181
+ refute midnight_flag
182
+ assert_equal 0, result_duration
183
+ end
184
+
185
+ def test_must_add_minutes_from_resting_in_a_pattern_week_result_in_same_day
186
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,3,12,45),126)
187
+ assert_equal DateTime.new(2000,1,3,15,7), result_date
188
+ refute midnight_flag
189
+ assert_equal 0, result_duration
190
+ end
191
+
192
+ def test_must_add_minutes_from_working_in_a_pattern_week_result_in_next_day
193
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,3,10,11),379)
194
+ assert_equal DateTime.new(2000,1,4,9,1), result_date
195
+ refute midnight_flag
196
+ assert_equal 0, result_duration
197
+ end
198
+
199
+ def test_must_add_minutes_from_resting_in_a_pattern_week_result_in_next_day
200
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,3,12,45),240)
201
+ assert_equal DateTime.new(2000,1,4,9,1), result_date
202
+ refute midnight_flag
203
+ assert_equal 0, result_duration
204
+ end
205
+
206
+ def test_must_add_minutes_from_working_in_a_working_week_result_in_later_day
207
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,3,10,11),828)
208
+ assert_equal DateTime.new(2000,1,5,9,1), result_date
209
+ refute midnight_flag
210
+ assert_equal 0, result_duration
211
+ end
212
+
213
+ def test_must_add_minutes_from_resting_in_a_working_week_result_in_later_day
214
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,3,12,45),689)
215
+ assert_equal DateTime.new(2000,1,5,9,1), result_date
216
+ refute midnight_flag
217
+ assert_equal 0, result_duration
218
+ end
219
+
220
+ def test_must_add_0_minutes_from_working_in_a_resting_week
221
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,3,10,11),0)
222
+ assert_equal DateTime.new(2000,1,3,10,11), result_date
223
+ refute midnight_flag
224
+ assert_equal 0, result_duration
225
+ end
226
+
227
+ def test_must_add_0_minutes_from_resting_in_a_resting_week
228
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,3,12,45),0)
229
+ assert_equal DateTime.new(2000,1,3,12,45), result_date
230
+ refute midnight_flag
231
+ assert_equal 0, result_duration
232
+ end
233
+
234
+ def test_must_add_too_many_minutes_in_a_pattern__week
235
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,3,10,11),2175)
236
+ assert_equal DateTime.new(2000,1,10,0,0), result_date
237
+ refute midnight_flag
238
+ assert_equal 1, result_duration
239
+ end
240
+
241
+ def test_must_subtract_minutes_in_a_working_week_result_in_same_day
242
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,8,7,31),-29)
243
+ assert_equal DateTime.new(2000,1,8,7,2), result_date
244
+ refute midnight_flag
245
+ assert_equal 0, result_duration
246
+ end
247
+
248
+ def test_must_subtract_minutes_in_a_working_week_result_in_previous_day
249
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,8,7,31),-452)
250
+ assert_equal DateTime.new(2000,1,7,23,59), result_date
251
+ refute midnight_flag
252
+ assert_equal 0, result_duration
253
+ end
254
+
255
+ def test_must_subtract_minutes_in_a_working_week_result_in_earlier_day
256
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,8,7,31),-1892)
257
+ assert_equal DateTime.new(2000,1,6,23,59), result_date
258
+ refute midnight_flag
259
+ assert_equal 0, result_duration
260
+ end
261
+
262
+ def test_must_subtract_minutes_in_a_working_week_result_at_start_of_day
263
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,8,7,31),-451)
264
+ assert_equal DateTime.new(2000,1,8,0,0), result_date
265
+ refute midnight_flag
266
+ assert_equal 0, result_duration
267
+ end
268
+
269
+ def test_must_subtract_minutes_in_a_working_week_result_at_start_of_previous_day
270
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,8,7,31),-1891)
271
+ assert_equal DateTime.new(2000,1,7,0,0), result_date
272
+ refute midnight_flag
273
+ assert_equal 0, result_duration
274
+ end
275
+
276
+ def test_must_subtract_too_many_minutes_from_a_working_week
277
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,8,7,31),-7652)
278
+ assert_equal DateTime.new(2000,1,2,0,0), result_date
279
+ assert midnight_flag
280
+ assert_equal -1, result_duration
281
+ end
282
+
283
+ def test_must_subtract_1_minute_from_start_of_next_day_after_working_week
284
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,9,0,0),-1,true)
285
+ assert_equal DateTime.new(2000,1,9,23,59), result_date
286
+ refute midnight_flag
287
+ assert_equal 0, result_duration
288
+ end
289
+
290
+ def test_must_subtract_2_minutes_from_start_of_next_day_after_working_week
291
+ result_date, result_duration, midnight_flag = @working_week.calc(DateTime.new(2000,1,9,0,0),-2,true)
292
+ assert_equal DateTime.new(2000,1,9,23,58), result_date
293
+ refute midnight_flag
294
+ assert_equal 0, result_duration
295
+ end
296
+
297
+ def test_must_subtract_minutes_from_last_day_in_a_resting_week
298
+ result_date, result_duration, midnight_flag = @resting_week.calc(DateTime.new(2000,1,10,7,31),-29)
299
+ assert_equal DateTime.new(2000,1,2,0,0), result_date
300
+ assert midnight_flag
301
+ assert_equal -29, result_duration
302
+ end
303
+
304
+ def test_must_subtract_minutes_from_middle_day_in_a_resting_week
305
+ result_date, result_duration, midnight_flag = @resting_week.calc(DateTime.new(2000,1,8,7,31),-452)
306
+ assert_equal DateTime.new(2000,1,2,0,0), result_date
307
+ assert midnight_flag
308
+ assert_equal -452, result_duration
309
+ end
310
+
311
+ def test_must_subtract_minutes_from_start_of_resting_week
312
+ result_date, result_duration, midnight_flag = @resting_week.calc(DateTime.new(2000,1,3,0,0),-1892)
313
+ assert_equal DateTime.new(2000,1,2,0,0), result_date
314
+ assert midnight_flag
315
+ assert_equal -1892, result_duration
316
+ end
317
+
318
+ def test_must_subtract_minutes_from_start_of_next_day_after_resting_week
319
+ result_date, result_duration, midnight_flag = @resting_week.calc(DateTime.new(2000,1,9,0,0),-1,true)
320
+ assert_equal DateTime.new(2000,1,2,0,0), result_date
321
+ assert midnight_flag
322
+ assert_equal -1, result_duration
202
323
  end
203
324
 
204
- def test_must_subtract_minutes_in_a_patterned_week
205
- start=DateTime.new(2000,1,1,0,0) #saturday
206
- finish=DateTime.new(2005,12,31,8,59) #saturday
207
- working_week=week(start,finish,0)
208
- working_week.workpattern(:sun,clock(9,0),clock(9,13),1)
209
-
210
- working_week.workpattern(:weekday,clock(9,0),clock(11,59),1)
211
- working_week.workpattern(:weekday,clock(13,0),clock(17,59),1)
212
-
213
- working_week.workpattern(:mon,clock(0,0),clock(23,59),0)
214
- working_week.workpattern(:mon,clock(9,0),clock(9,13),1)
215
- working_week.workpattern(:mon,clock(10,1),clock(10,1),1)
216
-
217
-
218
- [# yyyy,mm,dd,hh,mn,durtn,midnight,ryyyy,rmm,rdd,rhh,rmn,rdurtn,rmidnight
219
- [ 2000, 1, 1, 0, 0, 0, false, 2000, 1, 1, 0, 0, 0, false],
220
- [ 2000, 1, 1, 0, 0, -1, false, 1999, 12, 31, 0, 0, -1, true],
221
- [ 2000, 1, 2, 9, 0, 14, false, 2000, 1, 2, 9, 14, 0, false],
222
- [ 2000, 1, 2, 9, 0, 15, false, 2000, 1, 3, 9, 1, 0, false],
223
- [ 2000, 1, 2, 9, 0, 29, false, 2000, 1, 3, 10, 2, 0, false],
224
- [ 2000, 1, 2, 9, 0, 1950, false, 2000, 1, 9, 9, 1, 0, false],
225
- [ 2005,12,25, 9, 0, 1950, false, 2006, 1, 1, 0, 0, 1, false],
226
- [ 2005,12,25, 9, 0, 1949, false, 2005, 12, 30, 18, 0, 0, false]#,
227
- ].each {|yyyy,mm,dd,hh,mn,durtn,midnight,ryyyy,rmm,rdd,rhh,rmn,rdurtn,rmidnight|
228
- start=DateTime.new(yyyy,mm,dd,hh,mn)
229
- result_date, result_duration,result_midnight= working_week.calc(start,durtn,midnight)
230
- assert_equal DateTime.new(ryyyy,rmm,rdd,rhh,rmn), result_date, "result_date for working_week.calc(#{start},#{durtn},#{midnight})"
231
- assert_equal rdurtn, result_duration,"result_duration for working_week.calc(#{start},#{durtn},#{midnight})"
232
- assert_equal rmidnight, result_midnight,"result_midnight for working_week.calc(#{start},#{durtn},#{midnight})"
233
- }
325
+ def test_must_subtract_minutes_from_resting_day_in_a_pattern_week
326
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,8,13,29),-29)
327
+ assert_equal DateTime.new(2000,1,7,16,31), result_date
328
+ refute midnight_flag
329
+ assert_equal 0, result_duration
330
+ end
331
+
332
+ def test_must_subtract_minutes_from_working_day_in_a_pattern_week
333
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,7,13,29),-29)
334
+ assert_equal DateTime.new(2000,1,7,12,29), result_date
335
+ refute midnight_flag
336
+ assert_equal 0, result_duration
337
+ end
338
+
339
+ def test_must_subtract_minutes_in_a_pattern_week_result_in_previous_day
340
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,7,9,1),-2)
341
+ assert_equal DateTime.new(2000,1,6,16,59), result_date
342
+ refute midnight_flag
343
+ assert_equal 0, result_duration
344
+ end
345
+
346
+ def test_must_subtract_minutes_in_a_pattern_week_result_in_earlier_day
347
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,7,13,29),-240)
348
+ assert_equal DateTime.new(2000,1,6,16,58), result_date
349
+ refute midnight_flag
350
+ assert_equal 0, result_duration
351
+ end
352
+
353
+ def test_must_subtract_minutes_in_a_pattern_week_result_at_start_of_day
354
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,7,13,29),-238)
355
+ assert_equal DateTime.new(2000,1,7,9,0), result_date
356
+ refute midnight_flag
357
+ assert_equal 0, result_duration
358
+ end
359
+
360
+ def test_must_subtract_minutes_in_a_pattern_week_result_at_start_of_previous_day
361
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,7,13,29),-687)
362
+ assert_equal DateTime.new(2000,1,6,9,0), result_date
363
+ refute midnight_flag
364
+ assert_equal 0, result_duration
365
+ end
366
+
367
+ def test_must_subtract_too_many_minutes_from_a_pattern_week
368
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,7,9,0),-1797)
369
+ assert_equal DateTime.new(2000,1,2,0,0), result_date
370
+ assert midnight_flag
371
+ assert_equal -1, result_duration
234
372
  end
373
+
374
+ def test_must_subtract_1_minute_from_start_of_next_day_after_pattern_week
375
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,9,0,0),-1,true)
376
+ assert_equal DateTime.new(2000,1,7,16,59), result_date
377
+ refute midnight_flag
378
+ assert_equal 0, result_duration
379
+ end
380
+
381
+ def test_must_subtract_2_minutes_from_start_of_next_day_after_pattern_week
382
+ result_date, result_duration, midnight_flag = @pattern_week.calc(DateTime.new(2000,1,9,0,0),-2,true)
383
+ assert_equal DateTime.new(2000,1,7,16,58), result_date
384
+ refute midnight_flag
385
+ assert_equal 0, result_duration
386
+ end
387
+
388
+ ######################################################
389
+ # start=DateTime.new(2000,1,3)
390
+ # finish=DateTime.new(2000,1,9)
391
+ #
392
+ # @pattern_week=Workpattern::Week.new(start,finish,1)
393
+ # @pattern_week.workpattern(:weekend,Workpattern.clock(0,0),Workpattern.clock(23,59),0)
394
+ # @pattern_week.workpattern(:weekday,Workpattern.clock(0,0),Workpattern.clock(8,59),0)
395
+ # @pattern_week.workpattern(:weekday,Workpattern.clock(12,30),Workpattern.clock(13,0),0)
396
+ # @pattern_week.workpattern(:weekday,Workpattern.clock(17,0),Workpattern.clock(23,59),0)
397
+
398
+ ### @pattern_week centric
399
+
235
400
 
236
401
  def test_must_calculate_difference_between_dates_in_working_week
237
- start=DateTime.new(2012,10,1)
238
- finish=DateTime.new(2012,10,7)
239
- week=week(start,finish,1)
240
-
241
- [
242
- [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 0, 0,2012,10, 1, 0, 0],
243
- [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 1, 1,2012,10, 1, 0, 1],
244
- [ 2012,10, 1, 0,50, 2012,10, 1, 0,59, 9,2012,10, 1, 0,59],
245
- [ 2012,10, 1, 8,50, 2012,10, 1, 9, 0, 10,2012,10, 1, 9, 0],
246
- [ 2012,10, 1, 0, 0, 2012,10, 1,23,59, 1439,2012,10, 1,23,59],
247
- [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 0, 1440,2012,10, 2, 0, 0],
248
- [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 1, 1441,2012,10, 2, 0, 1],
249
- [ 2012,10, 1, 0, 0, 2013, 3,22, 6,11,10080,2012,10, 8, 0, 0],
250
- [ 2012,10, 1, 0, 1, 2012,10, 1, 0, 0, 1,2012,10, 1, 0, 1],
251
- [ 2012,10, 1, 0,59, 2012,10, 1, 0,50, 9,2012,10, 1, 0,59],
252
- [ 2012,10, 1, 9, 0, 2012,10, 1, 8,50, 10,2012,10, 1, 9, 0],
253
- [ 2012,10, 1,23,59, 2012,10, 1, 0, 0, 1439,2012,10, 1,23,59],
254
- [ 2012,10, 2, 0, 0, 2012,10, 1, 0, 0, 1440,2012,10, 2, 0, 0],
255
- [ 2012,10, 2, 0, 1, 2012,10, 1, 0, 0, 1441,2012,10, 2, 0, 1],
256
- [ 2013, 3,22, 6,11, 2012,10, 1, 0, 0,10080,2012,10, 8, 0, 0],
257
- [ 2012,10, 2, 6,11, 2012,10, 4, 8, 9, 2998,2012,10, 4, 8, 9]
258
- ].each {|start_year, start_month, start_day, start_hour,start_min,
259
- finish_year, finish_month, finish_day, finish_hour,finish_min,result,
260
- y,m,d,h,n|
261
- start=DateTime.new(start_year, start_month, start_day, start_hour,start_min)
262
- finish=DateTime.new(finish_year, finish_month, finish_day, finish_hour,finish_min)
263
- expected_date=DateTime.new(y,m,d,h,n)
264
- duration, result_date=week.diff(start,finish)
265
- assert_equal result, duration,"duration diff(#{start}, #{finish})"
266
- assert_equal expected_date, result_date,"date diff(#{start}, #{finish})"
267
- }
402
+ late_date=DateTime.new(2000,1,6,9,32)
403
+ early_date=DateTime.new(2000,1,6,8,20)
404
+ result_dur, result_date = @working_week.diff(early_date,late_date)
405
+ assert_equal 72, result_dur
406
+ assert_equal late_date, result_date
268
407
  end
269
408
 
270
409
  def test_must_calculate_difference_between_dates_in_resting_week
271
-
272
- start=DateTime.new(2012,10,1)
273
- finish=DateTime.new(2012,10,7)
274
- week=week(start,finish,0)
275
-
276
- [
277
- [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 0, 0,2012,10, 1, 0, 0],
278
- [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 1, 0,2012,10, 1, 0, 1],
279
- [ 2012,10, 1, 0,50, 2012,10, 1, 0,59, 0,2012,10, 1, 0,59],
280
- [ 2012,10, 1, 8,50, 2012,10, 1, 9, 0, 0,2012,10, 1, 9, 0],
281
- [ 2012,10, 1, 0, 0, 2012,10, 1,23,59, 0,2012,10, 1,23,59],
282
- [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 0, 0,2012,10, 2, 0, 0],
283
- [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 1, 0,2012,10, 2, 0, 1],
284
- [ 2012,10, 1, 0, 0, 2013, 3,22, 6,11, 0,2012,10, 8, 0, 0],
285
- [ 2012,10, 1, 0, 1, 2012,10, 1, 0, 0, 0,2012,10, 1, 0, 1],
286
- [ 2012,10, 1, 0,59, 2012,10, 1, 0,50, 0,2012,10, 1, 0,59],
287
- [ 2012,10, 1, 9, 0, 2012,10, 1, 8,50, 0,2012,10, 1, 9, 0],
288
- [ 2012,10, 1,23,59, 2012,10, 1, 0, 0, 0,2012,10, 1,23,59],
289
- [ 2012,10, 2, 0, 0, 2012,10, 1, 0, 0, 0,2012,10, 2, 0, 0],
290
- [ 2012,10, 2, 0, 1, 2012,10, 1, 0, 0, 0,2012,10, 2, 0, 1],
291
- [ 2013, 3,22, 6,11, 2012,10, 1, 0, 0, 0,2012,10, 8, 0, 0]
292
- ].each {|start_year, start_month, start_day, start_hour,start_min,
293
- finish_year, finish_month, finish_day, finish_hour,finish_min,result,
294
- y,m,d,h,n|
295
- start=DateTime.new(start_year, start_month, start_day, start_hour,start_min)
296
- finish=DateTime.new(finish_year, finish_month, finish_day, finish_hour,finish_min)
297
- expected_date=DateTime.new(y,m,d,h,n)
298
- duration, result_date=week.diff(start,finish)
299
- assert_equal result, duration,"duration diff(#{start}, #{finish})"
300
- assert_equal expected_date, result_date,"date diff(#{start}, #{finish})"
301
- }
410
+ late_date=DateTime.new(2000,1,6,9,32)
411
+ early_date=DateTime.new(2000,1,6,8,20)
412
+ result_dur, result_date = @resting_week.diff(early_date,late_date)
413
+ assert_equal 0, result_dur
414
+ assert_equal late_date, result_date
302
415
  end
303
416
 
304
417
  def test_must_calculate_difference_between_dates_in_pattern_week
305
- start=DateTime.new(2000,1,1)
306
- finish=DateTime.new(2012,12,31)
307
- week=week(start,finish,1)
308
- return
309
- [[0,0,8,59],
310
- [12,0,12,59],
311
- [17,0,22,59]
312
- ].each {|start_hour,start_min,finish_hour,finish_min|
313
- week.workpattern(clock(start_hour, start_min),
314
- clock(finish_hour, finish_min),
315
- 0)
316
- }
317
- assert_equal 480, day.total, "minutes in patterned day should be 480"
318
- assert_equal 1, day.minutes(16,59,16,59),"16:59 should be 1"
319
- assert_equal 0, day.minutes(17,0,17,0),"17:00 should be 0"
320
- assert_equal 0, day.minutes(22,59,22,59),"22:59 should be 0"
321
- assert_equal 1, day.minutes(23,0,23,0),"23:00 should be 1"
322
-
323
- [
324
- [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 0, 0,2012,10, 1, 0, 0],
325
- [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 1, 0,2012,10, 1, 0, 1],
326
- [ 2012,10, 1, 0,50, 2012,10, 1, 9,59, 59,2012,10, 1, 9,59],
327
- [ 2012,10, 1, 8,50, 2012,10, 1, 9,10, 10,2012,10, 1, 9,10],
328
- [ 2012,10, 1, 0, 0, 2012,10, 1,23,59, 479,2012,10, 1,23,59],
329
- [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 0, 480,2012,10, 2, 0, 0],
330
- [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 1, 480,2012,10, 2, 0, 0],
331
- [ 2012,10, 1, 0, 0, 2013, 3,22, 6,11, 480,2012,10, 2, 0, 0],
332
- [ 2012,10, 1, 0, 1, 2012,10, 1, 0, 0, 0,2012,10, 1, 0, 1],
333
- [ 2012,10, 1, 9,59, 2012,10, 1, 0,50, 59,2012,10, 1, 9,59],
334
- [ 2012,10, 1, 9, 0, 2012,10, 1, 8,50, 0,2012,10, 1, 9, 0],
335
- [ 2012,10, 1,23,59, 2012,10, 1, 0, 0, 479,2012,10, 1,23,59],
336
- [ 2012,10, 2, 0, 0, 2012,10, 1, 0, 0, 480,2012,10, 2, 0, 0],
337
- [ 2012,10, 2, 0, 1, 2012,10, 1, 0, 0, 480,2012,10, 2, 0, 0],
338
- [ 2013, 3,22, 6,11, 2012,10, 1, 0, 0, 480,2012,10, 2, 0, 0]
339
- ].each {|start_year, start_month, start_day, start_hour,start_min,
340
- finish_year, finish_month, finish_day, finish_hour,finish_min,result,
341
- y,m,d,h,n|
342
- start=DateTime.new(start_year, start_month, start_day, start_hour,start_min)
343
- finish=DateTime.new(finish_year, finish_month, finish_day, finish_hour,finish_min)
344
- expected_date=DateTime.new(y,m,d,h,n)
345
- duration, result_date=day.diff(start,finish)
346
- assert_equal result, duration,"duration diff(#{start}, #{finish})"
347
- assert_equal expected_date, result_date,"date diff(#{start}, #{finish})"
348
- }
349
-
418
+ late_date=DateTime.new(2000,1,6,13,1)
419
+ early_date=DateTime.new(2000,1,6,12,29)
420
+ result_dur, result_date = @pattern_week.diff(early_date,late_date)
421
+ assert_equal 1, result_dur
422
+ assert_equal late_date, result_date
350
423
  end
351
424
 
352
425
  private