workpattern 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_week.rb ADDED
@@ -0,0 +1,236 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestWeek < Test::Unit::TestCase #:nodoc:
4
+
5
+ def setup
6
+
7
+ end
8
+
9
+ must "create a working week" do
10
+ start=DateTime.new(2000,1,1,11,3)
11
+ finish=DateTime.new(2005,12,31,16,41)
12
+ working_week=week(start,finish,1)
13
+ assert_equal DateTime.new(start.year,start.month,start.day), working_week.start
14
+ assert_equal DateTime.new(finish.year,finish.month,finish.day), working_week.finish
15
+ assert_equal 3156480, working_week.total#2192
16
+ end
17
+
18
+ must "create a resting week" do
19
+ start=DateTime.new(2000,1,1,11,3)
20
+ finish=DateTime.new(2005,12,31,16,41)
21
+ resting_week=week(start,finish,0)
22
+ assert_equal DateTime.new(start.year,start.month,start.day), resting_week.start
23
+ assert_equal DateTime.new(finish.year,finish.month,finish.day), resting_week.finish
24
+ assert_equal 0, resting_week.total#2192
25
+ assert_equal 0,resting_week.week_total
26
+ end
27
+
28
+ must 'duplicate all of a week' do
29
+ start=DateTime.new(2000,1,1,11,3)
30
+ finish=DateTime.new(2005,12,31,16,41)
31
+ week=week(start,finish,1)
32
+ new_week=week.duplicate
33
+ assert_equal DateTime.new(start.year,start.month,start.day), new_week.start
34
+ assert_equal DateTime.new(finish.year,finish.month,finish.day), new_week.finish
35
+ assert_equal 3156480, new_week.total#2192
36
+
37
+ end
38
+
39
+ must "set patterns correctly" do
40
+ start=DateTime.new(2000,1,1,0,0)
41
+ finish=DateTime.new(2005,12,31,8,59)
42
+ working_week=week(start,finish,1)
43
+ assert_equal 10080, working_week.week_total
44
+ working_week.workpattern(:all,start,finish,0)
45
+ assert_equal 6300, working_week.week_total
46
+ working_week.workpattern(:sun,start,finish,1)
47
+ assert_equal 6840, working_week.week_total
48
+ working_week.workpattern(:mon,start,finish,1)
49
+ assert_equal 7380, working_week.week_total
50
+ working_week.workpattern(:all,clock(18,0),clock(18,19),0)
51
+ assert_equal 7240, working_week.week_total
52
+ working_week.workpattern(:all,clock(0,0),clock(23,59),0)
53
+ assert_equal 0, working_week.week_total
54
+ working_week.workpattern(:all,clock(0,0),clock(0,0),1)
55
+ assert_equal 7, working_week.week_total
56
+ working_week.workpattern(:all,clock(23,59),clock(23,59),1)
57
+ assert_equal 14, working_week.week_total
58
+ working_week.workpattern(:all,clock(0,0),clock(23,59),1)
59
+ assert_equal 10080, working_week.week_total
60
+ working_week.workpattern(:weekend,clock(0,0),clock(23,59),0)
61
+ assert_equal 7200, working_week.week_total
62
+
63
+ end
64
+
65
+ must 'add minutes in a working week' do
66
+ start=DateTime.new(2000,1,1,0,0)
67
+ finish=DateTime.new(2005,12,31,8,59)
68
+ working_week=week(start,finish,1)
69
+ result_date, result_duration= working_week.calc(start,0)
70
+ assert_equal start,result_date, "#{start} + #{0}"
71
+ result_date,result_duration=working_week.calc(finish,0)
72
+ assert_equal finish,result_date, "#{finish} + #{0}"
73
+ result_date,result_duration=working_week.calc(finish,10)
74
+ assert_equal DateTime.new(2005,12,31,9,9),result_date, "#{finish} + #{10}"
75
+ end
76
+
77
+ must 'add minutes in a resting week' do
78
+ assert true
79
+ end
80
+
81
+ must 'add minutes in a patterned week' do
82
+ assert true
83
+ end
84
+
85
+ must 'subtract minutes in a working week' do
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
+ result_date, result_duration= working_week.calc(start,0)
90
+ assert_equal start,result_date, "#{start} + #{0}"
91
+ result_date,result_duration=working_week.calc(finish,0)
92
+ assert_equal finish,result_date, "#{finish} + #{0}"
93
+ result_date,result_duration=working_week.calc(finish,-10)
94
+ assert_equal DateTime.new(2005,12,31,8,49),result_date, "#{finish} - #{10}"
95
+ result_date,result_duration=working_week.calc(DateTime.new(2005,12,31,0,0),-10)
96
+ assert_equal DateTime.new(2005,12,30,23,50),result_date, "#{DateTime.new(2005,12,31,0,0)} - #{10}"
97
+ end
98
+
99
+ must 'subtract minutes in a resting week' do
100
+ assert true
101
+ end
102
+
103
+ must 'subtract minutes in a patterned week' do
104
+ assert true
105
+ end
106
+
107
+
108
+ must 'create complex patterns' do
109
+ assert true
110
+ end
111
+
112
+ must "calculate difference between dates in working week" do
113
+ start=DateTime.new(2012,10,1)
114
+ finish=DateTime.new(2012,10,7)
115
+ week=week(start,finish,1)
116
+
117
+ [
118
+ [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 0, 0,2012,10, 1, 0, 0],
119
+ [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 1, 1,2012,10, 1, 0, 1],
120
+ [ 2012,10, 1, 0,50, 2012,10, 1, 0,59, 9,2012,10, 1, 0,59],
121
+ [ 2012,10, 1, 8,50, 2012,10, 1, 9, 0, 10,2012,10, 1, 9, 0],
122
+ [ 2012,10, 1, 0, 0, 2012,10, 1,23,59, 1439,2012,10, 1,23,59],
123
+ [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 0, 1440,2012,10, 2, 0, 0],
124
+ [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 1, 1441,2012,10, 2, 0, 1],
125
+ [ 2012,10, 1, 0, 0, 2013, 3,22, 6,11,10080,2012,10, 8, 0, 0],
126
+ [ 2012,10, 1, 0, 1, 2012,10, 1, 0, 0, 1,2012,10, 1, 0, 1],
127
+ [ 2012,10, 1, 0,59, 2012,10, 1, 0,50, 9,2012,10, 1, 0,59],
128
+ [ 2012,10, 1, 9, 0, 2012,10, 1, 8,50, 10,2012,10, 1, 9, 0],
129
+ [ 2012,10, 1,23,59, 2012,10, 1, 0, 0, 1439,2012,10, 1,23,59],
130
+ [ 2012,10, 2, 0, 0, 2012,10, 1, 0, 0, 1440,2012,10, 2, 0, 0],
131
+ [ 2012,10, 2, 0, 1, 2012,10, 1, 0, 0, 1441,2012,10, 2, 0, 1],
132
+ [ 2013, 3,22, 6,11, 2012,10, 1, 0, 0,10080,2012,10, 8, 0, 0],
133
+ [ 2012,10, 2, 6,11, 2012,10, 4, 8, 9, 2998,2012,10, 4, 8, 9]
134
+ ].each {|start_year, start_month, start_day, start_hour,start_min,
135
+ finish_year, finish_month, finish_day, finish_hour,finish_min,result,
136
+ y,m,d,h,n|
137
+ start=DateTime.new(start_year, start_month, start_day, start_hour,start_min)
138
+ finish=DateTime.new(finish_year, finish_month, finish_day, finish_hour,finish_min)
139
+ expected_date=DateTime.new(y,m,d,h,n)
140
+ duration, result_date=week.diff(start,finish)
141
+ assert_equal result, duration,"duration diff(#{start}, #{finish})"
142
+ assert_equal expected_date, result_date,"date diff(#{start}, #{finish})"
143
+ }
144
+ end
145
+
146
+ must "calculate difference between dates in resting week" do
147
+
148
+ start=DateTime.new(2012,10,1)
149
+ finish=DateTime.new(2012,10,7)
150
+ week=week(start,finish,0)
151
+
152
+ [
153
+ [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 0, 0,2012,10, 1, 0, 0],
154
+ [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 1, 0,2012,10, 1, 0, 1],
155
+ [ 2012,10, 1, 0,50, 2012,10, 1, 0,59, 0,2012,10, 1, 0,59],
156
+ [ 2012,10, 1, 8,50, 2012,10, 1, 9, 0, 0,2012,10, 1, 9, 0],
157
+ [ 2012,10, 1, 0, 0, 2012,10, 1,23,59, 0,2012,10, 1,23,59],
158
+ [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 0, 0,2012,10, 2, 0, 0],
159
+ [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 1, 0,2012,10, 2, 0, 1],
160
+ [ 2012,10, 1, 0, 0, 2013, 3,22, 6,11, 0,2012,10, 8, 0, 0],
161
+ [ 2012,10, 1, 0, 1, 2012,10, 1, 0, 0, 0,2012,10, 1, 0, 1],
162
+ [ 2012,10, 1, 0,59, 2012,10, 1, 0,50, 0,2012,10, 1, 0,59],
163
+ [ 2012,10, 1, 9, 0, 2012,10, 1, 8,50, 0,2012,10, 1, 9, 0],
164
+ [ 2012,10, 1,23,59, 2012,10, 1, 0, 0, 0,2012,10, 1,23,59],
165
+ [ 2012,10, 2, 0, 0, 2012,10, 1, 0, 0, 0,2012,10, 2, 0, 0],
166
+ [ 2012,10, 2, 0, 1, 2012,10, 1, 0, 0, 0,2012,10, 2, 0, 1],
167
+ [ 2013, 3,22, 6,11, 2012,10, 1, 0, 0, 0,2012,10, 8, 0, 0]
168
+ ].each {|start_year, start_month, start_day, start_hour,start_min,
169
+ finish_year, finish_month, finish_day, finish_hour,finish_min,result,
170
+ y,m,d,h,n|
171
+ start=DateTime.new(start_year, start_month, start_day, start_hour,start_min)
172
+ finish=DateTime.new(finish_year, finish_month, finish_day, finish_hour,finish_min)
173
+ expected_date=DateTime.new(y,m,d,h,n)
174
+ duration, result_date=week.diff(start,finish)
175
+ assert_equal result, duration,"duration diff(#{start}, #{finish})"
176
+ assert_equal expected_date, result_date,"date diff(#{start}, #{finish})"
177
+ }
178
+ end
179
+
180
+ must "calculate difference between dates in pattern week" do
181
+
182
+ day = Workpattern::Day.new(1)
183
+ [[0,0,8,59],
184
+ [12,0,12,59],
185
+ [17,0,22,59]
186
+ ].each {|start_hour,start_min,finish_hour,finish_min|
187
+ day.workpattern(clock(start_hour, start_min),
188
+ clock(finish_hour, finish_min),
189
+ 0)
190
+ }
191
+ assert_equal 480, day.total, "minutes in patterned day should be 480"
192
+ assert_equal 1, day.minutes(16,59,16,59),"16:59 should be 1"
193
+ assert_equal 0, day.minutes(17,0,17,0),"17:00 should be 0"
194
+ assert_equal 0, day.minutes(22,59,22,59),"22:59 should be 0"
195
+ assert_equal 1, day.minutes(23,0,23,0),"23:00 should be 1"
196
+
197
+ [
198
+ [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 0, 0,2012,10, 1, 0, 0],
199
+ [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 1, 0,2012,10, 1, 0, 1],
200
+ [ 2012,10, 1, 0,50, 2012,10, 1, 9,59, 59,2012,10, 1, 9,59],
201
+ [ 2012,10, 1, 8,50, 2012,10, 1, 9,10, 10,2012,10, 1, 9,10],
202
+ [ 2012,10, 1, 0, 0, 2012,10, 1,23,59, 479,2012,10, 1,23,59],
203
+ [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 0, 480,2012,10, 2, 0, 0],
204
+ [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 1, 480,2012,10, 2, 0, 0],
205
+ [ 2012,10, 1, 0, 0, 2013, 3,22, 6,11, 480,2012,10, 2, 0, 0],
206
+ [ 2012,10, 1, 0, 1, 2012,10, 1, 0, 0, 0,2012,10, 1, 0, 1],
207
+ [ 2012,10, 1, 9,59, 2012,10, 1, 0,50, 59,2012,10, 1, 9,59],
208
+ [ 2012,10, 1, 9, 0, 2012,10, 1, 8,50, 0,2012,10, 1, 9, 0],
209
+ [ 2012,10, 1,23,59, 2012,10, 1, 0, 0, 479,2012,10, 1,23,59],
210
+ [ 2012,10, 2, 0, 0, 2012,10, 1, 0, 0, 480,2012,10, 2, 0, 0],
211
+ [ 2012,10, 2, 0, 1, 2012,10, 1, 0, 0, 480,2012,10, 2, 0, 0],
212
+ [ 2013, 3,22, 6,11, 2012,10, 1, 0, 0, 480,2012,10, 2, 0, 0]
213
+ ].each {|start_year, start_month, start_day, start_hour,start_min,
214
+ finish_year, finish_month, finish_day, finish_hour,finish_min,result,
215
+ y,m,d,h,n|
216
+ start=DateTime.new(start_year, start_month, start_day, start_hour,start_min)
217
+ finish=DateTime.new(finish_year, finish_month, finish_day, finish_hour,finish_min)
218
+ expected_date=DateTime.new(y,m,d,h,n)
219
+ duration, result_date=day.diff(start,finish)
220
+ assert_equal result, duration,"duration diff(#{start}, #{finish})"
221
+ assert_equal expected_date, result_date,"date diff(#{start}, #{finish})"
222
+ }
223
+
224
+ end
225
+
226
+ private
227
+
228
+ def week(start,finish,type)
229
+ return Workpattern::Week.new(start,finish,type)
230
+ end
231
+
232
+ def clock(hour,min)
233
+ return Workpattern.clock(hour,min)
234
+ end
235
+ end
236
+
@@ -0,0 +1,260 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestWorkpattern < Test::Unit::TestCase #:nodoc:
4
+
5
+ def setup
6
+ Workpattern.clear()
7
+ end
8
+
9
+ must "create a working workpattern" do
10
+ name='mywp'
11
+ base=2001
12
+ span=11
13
+ wp=Workpattern.new(name,base,span)
14
+ assert_equal name, wp.name
15
+ assert_equal base, wp.base
16
+ assert_equal span, wp.span
17
+ assert_equal DateTime.new(base), wp.from
18
+ assert_equal DateTime.new(base+span-1,12,31,23,59), wp.to
19
+ end
20
+
21
+ must "set patterns correctly" do
22
+ name='mypattern'
23
+ base=2000
24
+ span=11
25
+ wp=Workpattern.new(name,base,span)
26
+
27
+ start=clock(0,0)
28
+ finish=clock(8,59)
29
+ assert_equal 10080, get_week(wp.weeks).week_total
30
+ wp.workpattern(:days=>:all,:from_time=>start,:to_time=>finish,:work_type=>0)
31
+ assert_equal 6300, get_week(wp.weeks).week_total
32
+ wp.workpattern(:days=>:sun,:from_time=>start,:to_time=>finish,:work_type=>1)
33
+ assert_equal 6840, get_week(wp.weeks).week_total
34
+ wp.workpattern(:days=>:mon,:from_time=>start,:to_time=>finish,:work_type=>1)
35
+ assert_equal 7380, get_week(wp.weeks).week_total
36
+ wp.workpattern(:days=>:all,:from_time=>clock(18,0),:to_time=>clock(18,19),:work_type=>0)
37
+ assert_equal 7240, get_week(wp.weeks).week_total
38
+ wp.workpattern(:days=>:all,:from_time=>clock(0,0),:to_time=>clock(23,59),:work_type=>0)
39
+ assert_equal 0, get_week(wp.weeks).week_total
40
+ wp.workpattern(:days=>:all,:from_time=>clock(0,0),:to_time=>clock(0,0),:work_type=>1)
41
+ assert_equal 7, get_week(wp.weeks).week_total
42
+ wp.workpattern(:days=>:all,:from_time=>clock(23,59),:to_time=>clock(23,59),:work_type=>1)
43
+ assert_equal 14, get_week(wp.weeks).week_total
44
+ wp.workpattern(:days=>:all,:from_time=>clock(0,0),:to_time=>clock(23,59),:work_type=>1)
45
+ assert_equal 10080, get_week(wp.weeks).week_total
46
+ wp.workpattern(:days=>:weekend,:from_time=>clock(0,0),:to_time=>clock(23,59),:work_type=>0)
47
+ assert_equal 7200, get_week(wp.weeks).week_total
48
+
49
+ end
50
+
51
+ must 'add minutes in a working workpattern' do
52
+ name='mypattern'
53
+ base=1999
54
+ span=11
55
+ wp=Workpattern.new(name,base,span)
56
+ tests=[[2000,1,1,0,0,3,2000,1,1,0,3],
57
+ [2000,1,1,23,59,0,2000,1,1,23,59],
58
+ [2000,1,1,23,59,1,2000,1,2,0,0],
59
+ [2000,1,1,23,59,2,2000,1,2,0,1],
60
+ [2000,1,1,9,10,33,2000,1,1,9,43],
61
+ [2000,1,1,9,10,60,2000,1,1,10,10],
62
+ [2000,1,1,9,0,931,2000,1,2,0,31],
63
+ [2000,1,1,0,0,3,2000,1,1,0,3]
64
+ ]
65
+ clue="add minutes in a working workpattern"
66
+ calc_test(wp,tests,clue)
67
+ end
68
+
69
+ must 'add minutes in a resting workpattern' do
70
+ name='mypattern'
71
+ base=1999
72
+ span=11
73
+ wp=Workpattern.new(name,base,span)
74
+ start=DateTime.new(1999,6,11,0,0)
75
+ finish=DateTime.new(2003,6,8,0,0)
76
+ wp.workpattern(:days=>:all,:start=> start, :finish=>finish, :work_type=>0 )
77
+ tests=[[2000,1,1,0,0,3,2003,6,9,0,3],
78
+ [2000,1,1,23,59,0,2000,1,1,23,59],
79
+ [2000,1,1,23,59,1,2003,6,9,0,1],
80
+ [2000,1,1,23,59,2,2003,6,9,0,2],
81
+ [2000,1,1,9,10,33,2003,6,9,0,33],
82
+ [2000,1,1,9,10,60,2003,6,9,1,0],
83
+ [2000,1,1,9,0,931,2003,6,9,15,31],
84
+ [2000,1,1,0,0,3,2003,6,9,0,3]
85
+ ]
86
+ clue="add minutes in a resting workpattern"
87
+ calc_test(wp,tests,clue)
88
+ end
89
+
90
+ must 'add minutes in a patterned workpattern' do
91
+ assert true
92
+ end
93
+
94
+ must 'subtract minutes in a working workpattern' do
95
+ name='mypattern'
96
+ base=1999
97
+ span=11
98
+ wp=Workpattern.new(name,base,span)
99
+ tests=[[2000,1,1,0,0,-3,1999,12,31,23,57],
100
+ [2000,1,1,23,59,0,2000,1,1,23,59],
101
+ [2000,1,1,23,59,-1,2000,1,1,23,58],
102
+ [2000,1,1,23,59,-2,2000,1,1,23,57],
103
+ [2000,1,1,9,10,-33,2000,1,1,8,37],
104
+ [2000,1,1,9,10,-60,2000,1,1,8,10],
105
+ [2000,1,1,9,0,-931,1999,12,31,17,29],
106
+ [2000,1,1,0,0,-3,1999,12,31,23,57]
107
+ ]
108
+ clue="subtract minutes in a working workpattern"
109
+ calc_test(wp,tests,clue)
110
+ end
111
+
112
+ must 'subtract minutes in a resting workpattern' do
113
+ name='mypattern'
114
+ base=1999
115
+ span=11
116
+ wp=Workpattern.new(name,base,span)
117
+ start=DateTime.new(1999,6,11,0,0)
118
+ finish=DateTime.new(2003,6,8,0,0)
119
+ wp.workpattern(:days=>:all,:start=> start, :finish=>finish, :work_type=>0 )
120
+ tests=[[2000,1,1,0,0,-3,1999,6,10,23,57],
121
+ [2000,1,1,23,59,0,2000,1,1,23,59],
122
+ [2000,1,1,23,59,-1,1999,6,10,23,59],
123
+ [2000,1,1,23,59,-2,1999,6,10,23,58],
124
+ [2000,1,1,9,10,-33,1999,6,10,23,27],
125
+ [2000,1,1,9,10,-60,1999,6,10,23,0],
126
+ [2000,1,1,9,0,-931,1999,6,10,8,29],
127
+ [2000,1,1,0,0,-3,1999,6,10,23,57]
128
+ ]
129
+ clue="subtract minutes in a resting workpattern"
130
+ calc_test(wp,tests,clue)
131
+ end
132
+
133
+ must 'subtract minutes in a patterned workpattern' do
134
+ assert true
135
+ end
136
+
137
+
138
+ must "calculate difference between dates in working calender" do
139
+ name='mypattern'
140
+ base=1999
141
+ span=40
142
+ wp=Workpattern.new(name,base,span)
143
+
144
+ [
145
+ [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 0, 0],
146
+ [ 2012,10, 1, 0, 0, 2012,10, 1, 0, 1, 1],
147
+ [ 2012,10, 1, 0,50, 2012,10, 1, 0,59, 9],
148
+ [ 2012,10, 1, 8,50, 2012,10, 1, 9, 0, 10],
149
+ [ 2012,10, 1, 0, 0, 2012,10, 1,23,59, 1439],
150
+ [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 0, 1440],
151
+ [ 2012,10, 1, 0, 0, 2012,10, 2, 0, 1, 1441],
152
+ [ 2012,10, 1, 0, 0, 2013, 3,22, 6,11,248051],
153
+ [ 2012,10, 1, 0, 1, 2012,10, 1, 0, 0, 1],
154
+ [ 2012,10, 1, 0,59, 2012,10, 1, 0,50, 9],
155
+ [ 2012,10, 1, 9, 0, 2012,10, 1, 8,50, 10],
156
+ [ 2012,10, 1,23,59, 2012,10, 1, 0, 0, 1439],
157
+ [ 2012,10, 2, 0, 0, 2012,10, 1, 0, 0, 1440],
158
+ [ 2012,10, 2, 0, 1, 2012,10, 1, 0, 0, 1441],
159
+ [ 2013, 3,22, 6,11, 2012,10, 1, 0, 0,248051],
160
+ [ 2012,10, 2, 6,11, 2012,10, 4, 8, 9, 2998]
161
+ ].each {|start_year, start_month, start_day, start_hour,start_min,
162
+ finish_year, finish_month, finish_day, finish_hour,finish_min,result|
163
+ start=DateTime.new(start_year, start_month, start_day, start_hour,start_min)
164
+ finish=DateTime.new(finish_year, finish_month, finish_day, finish_hour,finish_min)
165
+ duration, result_date=wp.diff(start,finish)
166
+ assert_equal result, duration,"duration diff(#{start}, #{finish})"
167
+ }
168
+ end
169
+
170
+ must "calculate difference between minutes in resting workpattern" do
171
+ assert true
172
+ end
173
+
174
+ must "calculate difference between minutes in pattern workpattern" do
175
+ assert true
176
+ end
177
+
178
+ must "follow the example in workpattern" do
179
+ mywp=Workpattern.new('My Workpattern',2011,10)
180
+ mywp.resting(:days => :weekend)
181
+ mywp.resting(:days =>:weekday, :from_time=>Workpattern.clock(0,0),:to_time=>Workpattern.clock(8,59))
182
+ mywp.resting(:days =>:weekday, :from_time=>Workpattern.clock(12,0),:to_time=>Workpattern.clock(12,59))
183
+ mywp.resting(:days =>:weekday, :from_time=>Workpattern.clock(18,0),:to_time=>Workpattern.clock(23,59))
184
+ mydate=DateTime.civil(2011,9,1,9,0)
185
+ result_date = mywp.calc(mydate,1920) # => 6/9/11@18:00
186
+ assert_equal DateTime.civil(2011,9,6,18,0), result_date, 'example in workpattern'
187
+ end
188
+
189
+ must "calculate across week patterns" do
190
+ name='mypattern'
191
+ base=2011
192
+ span=11
193
+ wp=Workpattern.new(name,base,span)
194
+ start=DateTime.new(2012,9,24,0,0)
195
+ finish=DateTime.new(2012,10,14,0,0)
196
+ wp.resting(:days =>:all,:start=> start, :finish=>finish)
197
+ wp.working(:days =>:mon,:start=> start, :finish=>finish, :from_time=>Workpattern.clock(1,0),:to_time=>Workpattern.clock(1,59))
198
+ wp.working(:days =>:tue,:start=> start, :finish=>finish, :from_time=>Workpattern.clock(2,0),:to_time=>Workpattern.clock(2,59))
199
+ wp.working(:days =>:wed,:start=> start, :finish=>finish, :from_time=>Workpattern.clock(3,0),:to_time=>Workpattern.clock(3,59))
200
+ wp.working(:days =>:thu,:start=> start, :finish=>finish, :from_time=>Workpattern.clock(4,0),:to_time=>Workpattern.clock(4,59))
201
+ wp.working(:days =>:fri,:start=> start, :finish=>finish, :from_time=>Workpattern.clock(5,0),:to_time=>Workpattern.clock(5,59))
202
+ wp.working(:days =>:sat,:start=> start, :finish=>finish, :from_time=>Workpattern.clock(6,0),:to_time=>Workpattern.clock(6,59))
203
+ wp.working(:days =>:sun,:start=> start, :finish=>finish, :from_time=>Workpattern.clock(0,0),:to_time=>Workpattern.clock(23,59))
204
+
205
+ # Mon Tue Wed Thu Fri Sat Sun
206
+ # 24 25 26 27 28 29 30
207
+ # 1 2 3 4 5 6 7
208
+ # 8 9 10 11 12 13 14
209
+ # Mon 01:00 - 01:59
210
+ # Tue 02:00 - 02:59
211
+ # Wed 03:00 - 03:59
212
+ # Thu 04:00 - 04:59
213
+ # Fri 05:00 - 05:59
214
+ # Sat 06:00 - 06:59
215
+ # Sun 00:00 - 23:59
216
+ #
217
+ tests=[[2012,10,1,1,0,1,2012,10,1,1,1],
218
+ [2012,10,14,23,59,1,2012,10,15,0,0],
219
+ [2012,10,1,1,0,60*60+1,2012,10,15,0,1],
220
+ [2012,10,1,2,0,-1,2012,10,1,1,59],
221
+ [2012,10,2,3,0,-61,2012,10,1,1,59],
222
+ [2012,9,24,1,1,-2,2012,9,23,23,59],
223
+ [2012,10,1,1,59,61,2012,10,2,3,0],
224
+ [2012,10,1,1,1,-1,2012,10,1,1,0],
225
+ [2012,10,1,1,0,-1,2012,9,30,23,59]
226
+ ]
227
+ clue="calculate across week patterns"
228
+ calc_test(wp,tests,clue)
229
+ end
230
+
231
+ must "know whether a time is working or resting" do
232
+ name='working?'
233
+ base=2011
234
+ span=11
235
+ wp=Workpattern.new(name,base,span)
236
+ wp.resting(:to_time=>Workpattern.clock(8,59))
237
+ assert wp.working?(DateTime.new(2012,1,1,9,0))
238
+ assert !wp.working?(DateTime.new(2012,1,1,8,59))
239
+ end
240
+
241
+
242
+ private
243
+
244
+ def get_week(ss)
245
+ ss.each {|obj| return obj}
246
+ end
247
+
248
+
249
+ def calc_test(wp,tests,clue)
250
+
251
+ tests.each{|y,m,d,h,n,add,yr,mr,dr,hr,nr|
252
+ start_date=DateTime.new(y,m,d,h,n)
253
+ result_date = wp.calc(start_date,add)
254
+ assert_equal DateTime.new(yr,mr,dr,hr,nr), result_date, "result date calc(#{start_date},#{add}) for #{clue}"
255
+ }
256
+ end
257
+ def clock(hour,min)
258
+ return Workpattern.clock(hour,min)
259
+ end
260
+ end
@@ -0,0 +1,93 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestWorkpatternModule < Test::Unit::TestCase #:nodoc:
4
+
5
+ def setup
6
+ Workpattern.clear()
7
+ end
8
+
9
+ must "create workpattern with given name" do
10
+
11
+ wp = Workpattern.new()
12
+ assert_equal Workpattern::DEFAULT_WORKPATTERN_NAME, wp.name, 'not returned the default workpattern name'
13
+ assert_equal Workpattern::DEFAULT_BASE_YEAR, wp.from.year, 'not returned the default workpattern base year'
14
+ assert_equal Workpattern::DEFAULT_SPAN, wp.span, 'not returned the default workpattern span'
15
+
16
+ mywp_name='barrie callender'
17
+ mywp_base=1963
18
+ mywp_span=48
19
+ mywp = Workpattern.new(mywp_name, mywp_base,mywp_span)
20
+ assert_equal mywp_name, mywp.name, 'not returned the supplied workpattern name'
21
+ assert_equal mywp_base, mywp.from.year, 'not returned the supplied workpattern base year'
22
+ assert_equal mywp_span, mywp.span, 'not returned the supplied workpattern span'
23
+
24
+ end
25
+
26
+ must "raise error when creating workpattern with existing name" do
27
+
28
+ assert_raise NameError do
29
+ mywp_name='duplicate'
30
+ wp=Workpattern.new(mywp_name)
31
+ wp=Workpattern.new(mywp_name)
32
+ end
33
+
34
+ end
35
+
36
+ must "return an array of all known workpattern objects" do
37
+
38
+ names =%w{fred harry sally}
39
+ names.each {|name| wp=Workpattern.new(name)}
40
+ wp_names = Workpattern.to_a
41
+
42
+ assert_equal names.size, wp_names.size, "lists are not the same size"
43
+
44
+ wp_names.each {|name, wp| assert names.include?(name)}
45
+ end
46
+
47
+ must "return empty array when no workpatterns exist" do
48
+
49
+ assert Workpattern.to_a.empty?
50
+ end
51
+
52
+ must "return existing workpattern" do
53
+
54
+ names =%w{fred harry sally}
55
+ names.each {|name| wp=Workpattern.new(name)}
56
+
57
+ names.each {|name|
58
+ wp=Workpattern.get(name)
59
+ }
60
+ end
61
+
62
+ must "raise error when workpattern does not exist" do
63
+
64
+ names =%w{fred harry sally}
65
+ names.each {|name| wp=Workpattern.new(name)}
66
+ assert_raise NameError do
67
+ wp=Workpattern.get('missing')
68
+ end
69
+ end
70
+
71
+ must "delete existing workpattern returning true" do
72
+
73
+ names =%w{fred harry sally}
74
+ names.each {|name| wp=Workpattern.new(name)}
75
+ names.each {|name| assert Workpattern.delete(name)}
76
+ end
77
+
78
+ must "return false deleting workpattern that does not exist" do
79
+ names =%w{fred harry sally}
80
+ names.each {|name| wp=Workpattern.new(name)}
81
+ assert !Workpattern.delete('missing')
82
+ end
83
+
84
+ must "delete all workpatterns" do
85
+
86
+ names =%w{fred harry sally}
87
+ names.each {|name| wp=Workpattern.new(name)}
88
+ Workpattern.clear
89
+ assert Workpattern.to_a.empty?
90
+ end
91
+
92
+
93
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "workpattern/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "workpattern"
7
+ s.version = Workpattern::VERSION
8
+ s.authors = ["Barrie Callender"]
9
+ s.email = ["barrie@callenb.org"]
10
+ s.homepage = ""
11
+ s.summary = %q{temporal calculations}
12
+ s.description = %q{Workpattern performs date calculations that take into account working and resting periods.}
13
+
14
+ s.rubyforge_project = "workpattern"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: workpattern
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Barrie Callender
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-31 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Workpattern performs date calculations that take into account working
15
+ and resting periods.
16
+ email:
17
+ - barrie@callenb.org
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - CHANGELOG
24
+ - Gemfile
25
+ - README.md
26
+ - Rakefile
27
+ - config/website.yml
28
+ - lib/workpattern.rb
29
+ - lib/workpattern/clock.rb
30
+ - lib/workpattern/day.rb
31
+ - lib/workpattern/hour.rb
32
+ - lib/workpattern/utility/base.rb
33
+ - lib/workpattern/version.rb
34
+ - lib/workpattern/week.rb
35
+ - lib/workpattern/workpattern.rb
36
+ - script/console
37
+ - script/destroy
38
+ - script/generate
39
+ - script/txt2html
40
+ - test/test_clock.rb
41
+ - test/test_day.rb
42
+ - test/test_helper.rb
43
+ - test/test_hour.rb
44
+ - test/test_week.rb
45
+ - test/test_workpattern.rb
46
+ - test/test_workpattern_module.rb
47
+ - workpattern.gemspec
48
+ homepage: ''
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project: workpattern
68
+ rubygems_version: 1.8.15
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: temporal calculations
72
+ test_files: []