runt 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,402 +1,612 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'test/unit'
4
- require 'runt'
5
- require 'date'
6
-
7
- $DEBUG=false
8
-
9
- # Unit tests for TExpr classes
10
- # Author:: Matthew Lipper
11
-
12
- class TExprTest < Test::Unit::TestCase
13
-
14
- include Runt
15
- include DPrecision
16
-
17
-
18
- def test_collection_te
19
- #base class that should always return false
20
- expr = Collection.new
21
- assert(!expr.include?(Date.today))
22
- end
23
-
24
- def test_union_te
25
- #midnight to 6:30am AND/OR first Tuesday of the month
26
- expr = REDay.new(0,0,6,30) | DIMonth.new(First,Tuesday)
27
- assert(expr.include?(PDate.day(2004,1,6))) #January 6th, 2004 (First Tuesday)
28
- assert(expr.include?(PDate.hour(1966,2,8,4))) #4am (February, 8th, 1966 - ignored)
29
- assert(!expr.include?(PDate.min(2030,7,4,6,31))) #6:31am, July, 4th, 2030
30
- end
31
-
32
- def test_arbitrary_te
33
- expr1 = Spec.new(PDate.day(2003,12,30))
34
- expr2 = Spec.new(PDate.day(2004,1,1))
35
- assert(expr1.include?(Date.new(2003,12,30)))
36
- assert(!expr1.include?(Date.new(2003,12,31)))
37
- assert(expr2.include?(Date.new(2004,1,1)))
38
- assert(!expr2.include?(Date.new(2003,1,1)))
39
- end
40
-
41
- def test_arbitrary_range_te
42
- #NOTE:
43
- #Using standard range functionality like the following:
44
- #... expr1 = RSpec.new(r_start..r_end)
45
- #... assert(expr1.include?((r_start+10)..(r_end-10)))
46
- #will work. However, it takes a LONG time to evaluate if range is large
47
- #and/or precision is small. Use DateRange instead
48
-
49
- r_start = PDate.sec(2004,2,29,16,24,12)
50
- r_end = PDate.sec(2004,3,2,4,22,58)
51
- #inclusive range equivalent to r_start..r_end
52
- expr1 = RSpec.new(DateRange.new(r_start,r_end))
53
- assert(expr1.include?(PDate.sec(2004,2,29,16,24,12)))
54
- assert(expr1.include?(PDate.sec(2004,3,2,4,22,58)))
55
- assert(expr1.include?(DateTime.new(2004,3,1,23,00)))
56
- assert(!expr1.include?(DateTime.new(2004,3,2,4,22,59)))
57
- assert(!expr1.include?(Date.new(2003,3,1)))
58
- #exclusive range equivalent to r_start...r_end
59
- expr2 = RSpec.new(DateRange.new(r_start,r_end,true))
60
- assert(expr2.include?(PDate.sec(2004,2,29,16,24,12)))
61
- assert(!expr2.include?(PDate.sec(2004,3,2,4,22,58)))
62
- r_sub = DateRange.new( (r_start+10), (r_end-10) )
63
- assert(expr1.include?(r_sub))
64
- end
65
-
66
- def test_intersection_te
67
- #Should match the first Sunday of March and April
68
- expr1 = REYear.new(3,4) & DIMonth.new(First,Sunday)
69
- assert(expr1.include?(PDate.new(2004,3,7))) #Sunday, March 7th, 2004
70
- assert(!expr1.include?(PDate.new(2004,4,1))) #First Sunday in February, 2004
71
- expr2 = REWeek.new(Mon,Fri) & REDay.new(8,00,8,30)
72
- assert(expr2.include?( PDate.new(2004,5,4,8,06)))
73
- assert(!expr2.include?(PDate.new(2004,5,1,8,06)))
74
- assert(!expr2.include?(PDate.new(2004,5,3,9,06)))
75
- end
76
-
77
- def test_difference_te
78
- #Should match for 8:30 pm to 11:04 pm
79
- diff_expr = REDay.new(20,30,00,00) - REDay.new(23,04,6,20)
80
- #8:45 pm (May 1st, 2003 - ignored)
81
- assert(diff_expr.include?(PDate.new(2003,5,1,20,45)))
82
- #11:05 pm (February 1st, 2004 - ignored)
83
- assert(!diff_expr.include?(PDate.new(2004,2,1,23,05)))
84
- #8:00 pm (May 1st, 2003 - ignored)
85
- assert(!diff_expr.include?(PDate.new(2003,5,1,20,00)))
86
- end
87
-
88
- def test_day_in_month_te
89
- #Friday, January 16th 2004
90
- dt1 = Date.civil(2004,1,16)
91
- #Friday, January 9th 2004
92
- dt2 = Date.civil(2004,1,9)
93
- #third Friday of the month
94
- expr1 = DIMonth.new(Third,Friday)
95
- #second Friday of the month
96
- expr2 = DIMonth.new(Second,Friday)
97
- assert(expr1.include?(dt1))
98
- assert(!expr1.include?(dt2))
99
- assert(expr2.include?(dt2))
100
- assert(!expr2.include?(dt1))
101
- #Sunday, January 25th 2004
102
- dt3 = Date.civil(2004,1,25)
103
- #last Sunday of the month
104
- expr3 = DIMonth.new(Last_of,Sunday)
105
- assert(expr3.include?(dt3))
106
- end
107
-
108
- def test_day_in_week_te
109
- #Friday (woo-hoo!)
110
- expr = DIWeek.new(Friday)
111
- #Friday, January 9th 2004
112
- assert(expr.include?(PDate.new(2004,1,9)))
113
- #Friday, January 16th 2004
114
- assert(expr.include?(PDate.new(2004,1,16)))
115
- #Monday, January 12th 2004
116
- assert(!expr.include?(PDate.new(2004,1,12)))
117
- end
118
- def test_week_in_month_te
119
- expr = WIMonth.new(Third)
120
- assert(expr.include?(PDate.day(2004,2,19)))
121
- assert(!expr.include?(PDate.day(2004,2,29)))
122
- expr2 = WIMonth.new(Last_of)
123
- assert(expr2.include?(PDate.day(2004,2,29)))
124
- expr3 = WIMonth.new(Second_to_last)
125
- assert(expr3.include?(PDate.day(2004,2,22)))
126
- end
127
-
128
- def test_range_each_year_te
129
- # November 1st, 1961
130
- dt1 = Date.civil(1961,11,1)
131
- #June, 1986
132
- dt2 = PDate::month(1986,6)
133
- #November and December
134
- expr1 = REYear.new(11,12)
135
- #May 31st through and September 6th
136
- expr2 = REYear.new(5,31,9,6)
137
- assert(expr1.include?(dt1))
138
- assert(!expr1.include?(dt2))
139
- assert(expr2.include?(dt2))
140
- #August
141
- expr3 = REYear.new(8)
142
- assert(!expr3.include?(dt1))
143
- assert(!expr3.include?(dt2))
144
- #August 6th, 2004
145
- dt3 = Date::new(2004,8,6)
146
- assert(expr3.include?(dt3))
147
- end
148
-
149
- def test_range_each_day_te
150
- #noon to 4:30pm
151
- expr1 = REDay.new(12,0,16,30)
152
- #3:15 pm (May 8th, 2012 - ignored)
153
- assert(expr1.include?(PDate.hour(2012,5,8,15,15)))
154
- #4:30 pm (April 18th, 1922 - ignored)
155
- assert(expr1.include?(PDate.hour(1922,4,18,16,30)))
156
- #noon (June 5th, 1975 - ignored)
157
- assert(expr1.include?(PDate.hour(1975,6,5,12,0)))
158
- #3:15 am (May 8th, 2012 - ignored)
159
- assert(!expr1.include?(PDate.hour(2012,5,8,3,15)))
160
- #8:30pm to 12:00 midnite
161
- expr2 = REDay.new(20,30,00,00)
162
- #9:00 pm (January 28th, 2004 - ignored)
163
- assert(expr2.include?(PDate.min(2004,1,28,21,00)))
164
- #12:00 am (January 28th, 2004 - ignored)
165
- assert(expr2.include?(PDate.min(2004,1,28,0,0)))
166
- #12:01 am (January 28th, 2004 - ignored)
167
- assert(!expr2.include?(PDate.min(2004,1,28,0,01)))
168
- end
169
-
170
- def test_range_each_day_te_again
171
- dr = DateRange.new(PDate.day(2005,9,19),PDate.day(2005,9,20))
172
- red = REDay.new(8,0,10,0)
173
- result = false
174
- dr.each do |interval|
175
- result = red.include?(interval)
176
- break if result
177
- end
178
- assert(result)
179
- end
180
-
181
- def test_range_each_week_te
182
- assert_raises(ArgumentError){ expr = REWeek.new(10,4) }
183
- expr1 = REWeek.new(Mon,Fri) & REDay.new(8,00,8,30)
184
- assert(!expr1.include?(PDate.new(2004,5,1,8,06)))
185
- #Sunday through Thursday
186
- expr2 = REWeek.new(0,4)
187
- assert(expr2.include?(PDate.min(2004,2,19,23,59,59)))
188
- assert(!expr2.include?(PDate.min(2004,2,20,0,0,0)))
189
- end
190
-
191
- def test_combined_te
192
- #This is a hack.....
193
- #In the U.S., Memorial Day begins the last Monday of May
194
- #
195
- #The month of May
196
- may=REYear.new(5)
197
- #Monday through Saturday
198
- monday_to_saturday = REWeek.new(1,6)
199
- #Last week of (any) month
200
- last_week_in = WIMonth.new(Last_of)
201
- #So, to say 'starting from the last Monday in May',
202
- #we need to select just that last week of May begining with
203
- #the Monday of that week
204
- last_week_of_may = may & monday_to_saturday & last_week_in
205
-
206
- #This is another hack similar to the above, except instead of selecting a range
207
- #starting at the begining of the month, we need to select only the time period in
208
- #September up until Labor Day.
209
- #
210
- #In the U.S., Labor Day is the first Monday in September
211
- #
212
- #The month of September
213
- september=REYear.new(9)
214
- #First week of (any) month
215
- first_week_in = WIMonth.new(First)
216
- entire_first_week_of_september = september & first_week_in
217
- #To exclude everything in the first week which occurs on or after Monday.
218
- first_week_of_september=entire_first_week_of_september - monday_to_saturday
219
- #June through August
220
- june_through_august=REYear.new(6,First,8)
221
- assert(june_through_august.include?(PDate.day(2004,7,4)))
222
- #Finally!
223
- summer_time = last_week_of_may | first_week_of_september | june_through_august
224
-
225
- #Will work, but will be incredibly slow:
226
- # assert(summer_time.include?(PDate.min(2004,5,31,0,0)))
227
- assert(summer_time.include?(PDate.day(2004,5,31,0,0)))
228
- assert(summer_time.include?(PDate.day(2004,7,4)))
229
- #also works...also slow:
230
- # assert(!summer_time.include?(PDate.min(2004,9,6,0,0)))
231
- assert(!summer_time.include?(PDate.hour(2004,9,6,0,0)))
232
-
233
- end
234
- def test_nyc_parking_te
235
-
236
- #Monday, Wednesday, Friday
237
- mon_wed_fri = DIWeek.new(Mon) | \
238
- DIWeek.new(Wed) | \
239
- DIWeek.new(Fri)
240
-
241
-
242
- #Wednesday (at 7:15pm - ignored)
243
- assert(mon_wed_fri.include?(DateTime.new(2004,3,10,19,15)))
244
-
245
- #Sunday (at 9:00am - ignored)
246
- assert(!mon_wed_fri.include?(DateTime.new(2004,3,14,9,00)))
247
-
248
- #8am to 11am
249
- eight_to_eleven = REDay.new(8,00,11,00)
250
-
251
- #=> Mon,Wed,Fri - 8am to 11am
252
- expr1 = mon_wed_fri & eight_to_eleven
253
-
254
- #Tuesdays, Thursdays
255
- tues_thurs = DIWeek.new(Tue) | DIWeek.new(Thu)
256
-
257
- #11:30am to 2pm
258
- eleven_thirty_to_two = REDay.new(11,30,14,00)
259
-
260
- #Noon (on Monday - ignored)
261
- assert(eleven_thirty_to_two.include?(DateTime.new(2004,3,8,12,00)))
262
-
263
- #Midnite (on Thursday - ignored)
264
- assert(!eleven_thirty_to_two.include?(DateTime.new(2004,3,11,00,00)))
265
-
266
-
267
- #=> Tues,Thurs - 11:30am to 2pm
268
- expr2 = tues_thurs & eleven_thirty_to_two
269
-
270
- #
271
- #Sigh...now if I can only get my dad to remember this...
272
- #
273
- parking_ticket = expr1 | expr2
274
-
275
- assert(parking_ticket.include?(DateTime.new(2004,3,11,12,15)))
276
- assert(parking_ticket.include?(DateTime.new(2004,3,10,9,15)))
277
- assert(parking_ticket.include?(DateTime.new(2004,3,10,8,00)))
278
-
279
- assert(!parking_ticket.include?(DateTime.new(2004,3,11,1,15)))
280
-
281
- # Simplified
282
- e1 = (DIWeek.new(Mon) | DIWeek.new(Wed) | DIWeek.new(Fri)) & REDay.new(8,00,11,00)
283
- e2 = (DIWeek.new(Tue) | DIWeek.new(Thu)) & REDay.new(11,30,14,00)
284
- ticket = expr1 | expr2
285
- assert(ticket.include?(DateTime.new(2004,3,11,12,15)))
286
- assert(ticket.include?(DateTime.new(2004,3,10,9,15)))
287
- assert(ticket.include?(DateTime.new(2004,3,10,8,00)))
288
- assert(!ticket.include?(DateTime.new(2004,3,11,1,15)))
289
- end
290
-
291
- def test_re_month_te
292
- # October 22nd, 2005
293
- dt1 = Date.civil(2005,10,22)
294
- # The 20th through the 29th of any month
295
- expr1 = REMonth.new(20,29)
296
- assert(expr1.include?(dt1))
297
- assert(!expr1.include?(PDate.new(2010,12,12)))
298
- # August 17th, 1975
299
- dt2 = Date.civil(1975,8,17)
300
- # The 17th of any month
301
- expr2 = REMonth.new(17)
302
- assert(expr2.include?(dt2))
303
- assert(!expr2.include?(dt1))
304
- end
305
-
306
- ###
307
- # Dates functionality & tests contributed by Emmett Shear
308
- ###
309
- def test_day_in_month_dates
310
- date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 12, 31)
311
- month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
312
- expr = DIMonth.new(First, Tuesday)
313
- dates = expr.dates(date_range)
314
- assert dates.size == 12
315
- dates.each do |d|
316
- assert d.wday == 2 # tuesday
317
- assert d.day < 8 # in the first week
318
- end
319
- expr2 = DIMonth.new(Last, Friday)
320
- dates2 = expr2.dates(date_range)
321
- assert dates2.size == 12
322
- dates2.each do |d|
323
- assert d.wday == 5 # friday
324
- assert d.day > month_days[d.month-1] - 8 # last week
325
- end
326
- end
327
-
328
- def test_day_in_week_dates
329
- date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 1, 31)
330
- expr = DIWeek.new(Sunday)
331
- dates = expr.dates(date_range)
332
- assert( dates.size == 5 )
333
- assert( dates.include?( Date.civil(2005, 1, 16) ) )
334
- assert( dates.include?( Date.civil(2005, 1, 30) ) )
335
- end
336
-
337
- def test_union_dates
338
- date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 12, 31)
339
- month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
340
- expr = DIMonth.new(Last, Friday) | DIMonth.new(1, Tuesday)
341
- dates = expr.dates(date_range)
342
- assert dates.size == 24
343
- dates.each do |d|
344
- unless (d.wday == 2 and d.day < 8) or \
345
- (d.wday == 5 and d.day > month_days[d.month-1] - 8)
346
- assert false, d.to_s
347
- end
348
- end
349
- end
350
-
351
- def test_intersection_dates
352
- date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 12, 31)
353
- expr = DIWeek.new(Sunday) & DIMonth.new(Second, Sunday)
354
- dates = expr.dates(date_range)
355
- assert( dates.size == 12 )
356
- other_dates = DIMonth.new(Second, Sunday).dates(date_range)
357
- dates.each { |d| assert( other_dates.include?(d) ) }
358
- end
359
-
360
- def test_range_each_week_dates
361
- date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 1, 31)
362
- expr = REWeek.new(3, 5)
363
- dates = expr.dates(date_range)
364
- assert dates.size == 12
365
- end
366
-
367
- def test_range_each_year_dates
368
- date_range = Date.civil(2004, 5, 1)..Date.civil(2006, 5, 4)
369
- expr = REYear.new(4, 28, 5, 6)
370
- dates = expr.dates(date_range)
371
- assert dates.size == 22, dates.size
372
- end
373
-
374
- def test_week_in_month_dates
375
- date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 2, 28)
376
- expr = WIMonth.new(2)
377
- dates = expr.dates(date_range)
378
- assert dates.size == 14, dates.size
379
- assert dates.first.mday == 8
380
- assert dates.last.mday == 14
381
- expr_2 = WIMonth.new(Last)
382
- dates_2 = expr_2.dates(date_range)
383
- assert dates_2.size == 14, dates_2.size
384
- assert dates_2.first.mday == 25
385
- assert dates_2.last.mday == 28
386
- end
387
-
388
- def test_range_each_month_dates
389
- date_range = Date.civil(2005, 1, 7)..Date.civil(2005, 1, 15)
390
- expr = REMonth.new(5, 9)
391
- dates = expr.dates(date_range)
392
- assert dates.size == 3, dates.size
393
- assert false if dates.include? Date.civil(2005, 1, 6)
394
- end
395
-
396
- def test_diff_dates
397
- date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 1, 31)
398
- expr = REYear.new(1, 1, 1, 31) - REMonth.new(7, 15)
399
- dates = expr.dates(date_range)
400
- assert dates.size == 22, dates.size
401
- end
402
- end
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'runt'
5
+ require 'date'
6
+ require 'time'
7
+
8
+ $DEBUG=false
9
+
10
+ # Unit tests for TExpr classes
11
+ # Author:: Matthew Lipper
12
+
13
+ class TExprTest < Test::Unit::TestCase
14
+
15
+ include Runt
16
+ include DPrecision
17
+
18
+ def test_collection_te
19
+ #base class that should always return false
20
+ expr = Collection.new
21
+ assert(!expr.include?(Date.today))
22
+ end
23
+
24
+
25
+ def test_collection_te_to_s
26
+ assert_equal 'empty', Collection.new.to_s
27
+ assert_equal 'empty', Collection.new.to_s{['b','oo']}
28
+ expr = Collection.new
29
+ dim = DIMonth.new(First,Tuesday)
30
+ expr.expressions << dim
31
+ assert_equal 'ff' + dim.to_s, expr.to_s{['ff','nn']}
32
+ red = REDay.new(0,0,6,30)
33
+ expr.expressions << red
34
+ assert_equal 'ff' + dim.to_s + 'nn' + red.to_s, expr.to_s{['ff','nn']}
35
+ wim = WIMonth.new(Second_to_last)
36
+ expr.expressions << wim
37
+ assert_equal 'ff' + dim.to_s + 'nn' + red.to_s + 'nn' + wim.to_s, expr.to_s{['ff','nn']}
38
+ end
39
+
40
+ def test_union_te_to_s
41
+ dim = DIMonth.new(First,Tuesday)
42
+ red = REDay.new(0,0,6,30)
43
+ expr = dim | red
44
+ assert_equal 'every ' + dim.to_s + ' or ' + red.to_s, expr.to_s
45
+ end
46
+
47
+ def test_union_te
48
+ #midnight to 6:30am AND/OR first Tuesday of the month
49
+ expr = REDay.new(0,0,6,30) | DIMonth.new(First,Tuesday)
50
+ assert(expr.include?(PDate.day(2004,1,6))) #January 6th, 2004 (First Tuesday)
51
+ assert(expr.include?(PDate.hour(1966,2,8,4))) #4am (February, 8th, 1966 - ignored)
52
+ assert(!expr.include?(PDate.min(2030,7,4,6,31))) #6:31am, July, 4th, 2030
53
+ end
54
+
55
+ def test_spec_te_include
56
+ expr1 = Spec.new(PDate.day(2003,12,30))
57
+ expr2 = Spec.new(PDate.day(2004,1,1))
58
+ assert expr1.include?(Date.new(2003,12,30))
59
+ assert !expr1.include?(Date.new(2003,12,31))
60
+ assert expr2.include?(Date.new(2004,1,1))
61
+ assert !expr2.include?(Date.new(2003,1,1))
62
+ expr3 = Spec.new(DateTime.civil(2006,3,11,8,30))
63
+ assert expr3.include?(DateTime.civil(2006,3,11,8,30))
64
+ end
65
+
66
+ def test_spec_te_to_s
67
+ pdate = PDate.day(2003,12,30)
68
+ expr1 = Spec.new(pdate)
69
+ assert_equal expr1.to_s, pdate.to_s
70
+ end
71
+
72
+ def test_rspec_te
73
+ #NOTE:
74
+ #Using standard range functionality like the following:
75
+ #... expr1 = RSpec.new(r_start..r_end)
76
+ #... assert(expr1.include?((r_start+10)..(r_end-10)))
77
+ #will work. However, it takes a LONG time to evaluate if range is large
78
+ #and/or precision is small. Use DateRange instead
79
+
80
+ r_start = PDate.sec(2004,2,29,16,24,12)
81
+ r_end = PDate.sec(2004,3,2,4,22,58)
82
+ #inclusive range equivalent to r_start..r_end
83
+ expr1 = RSpec.new(DateRange.new(r_start,r_end))
84
+ assert(expr1.include?(PDate.sec(2004,2,29,16,24,12)))
85
+ assert(expr1.include?(PDate.sec(2004,3,2,4,22,58)))
86
+ assert(expr1.include?(DateTime.new(2004,3,1,23,00)))
87
+ assert(!expr1.include?(DateTime.new(2004,3,2,4,22,59)))
88
+ assert(!expr1.include?(Date.new(2003,3,1)))
89
+ #exclusive range equivalent to r_start...r_end
90
+ expr2 = RSpec.new(DateRange.new(r_start,r_end,true))
91
+ assert(expr2.include?(PDate.sec(2004,2,29,16,24,12)))
92
+ assert(!expr2.include?(PDate.sec(2004,3,2,4,22,58)))
93
+ r_sub = DateRange.new( (r_start+10), (r_end-10) )
94
+ assert(expr1.include?(r_sub))
95
+ end
96
+
97
+ def test_rspec_te_to_s
98
+ range = DateRange.new(PDate.new(2006,2,25),PDate.new(2006,4,8))
99
+ expr = RSpec.new(range)
100
+ assert_equal expr.to_s, range.to_s
101
+ end
102
+
103
+ def test_intersection_te
104
+ #Should match the first Sunday of March and April
105
+ expr1 = REYear.new(3,4) & DIMonth.new(First,Sunday)
106
+ assert(expr1.include?(PDate.new(2004,3,7))) #Sunday, March 7th, 2004
107
+ assert(!expr1.include?(PDate.new(2004,4,1))) #First Sunday in February, 2004
108
+ expr2 = REWeek.new(Mon,Fri) & REDay.new(8,00,8,30)
109
+ assert(expr2.include?( PDate.new(2004,5,4,8,06)))
110
+ assert(!expr2.include?(PDate.new(2004,5,1,8,06)))
111
+ assert(!expr2.include?(PDate.new(2004,5,3,9,06)))
112
+ end
113
+
114
+ def test_intersection_te_to_s
115
+ dim = DIMonth.new(First,Tuesday)
116
+ red = REDay.new(0,0,6,30)
117
+ expr = dim & red
118
+ assert_equal 'every ' + dim.to_s + ' and ' + red.to_s, expr.to_s
119
+ end
120
+
121
+ def test_difference_te
122
+ #Should match for 8:30 pm to 11:04 pm
123
+ diff_expr = REDay.new(20,30,00,00) - REDay.new(23,04,6,20)
124
+ #8:45 pm (May 1st, 2003 - ignored)
125
+ assert(diff_expr.include?(PDate.new(2003,5,1,20,45)))
126
+ #11:05 pm (February 1st, 2004 - ignored)
127
+ assert(!diff_expr.include?(PDate.new(2004,2,1,23,05)))
128
+ #8:00 pm (May 1st, 2003 - ignored)
129
+ assert(!diff_expr.include?(PDate.new(2003,5,1,20,00)))
130
+ end
131
+
132
+ def test_diff_to_s
133
+ rey1 = REYear.new 3,1,6,2
134
+ rey2 = REYear.new 4,15,5,20
135
+ expr = rey1 - rey2
136
+ assert_equal rey1.to_s + ' except for ' + rey2.to_s, expr.to_s
137
+ end
138
+
139
+
140
+ def test_memorial_day
141
+ # Monday through Friday, from 9am to 5pm
142
+ job = REWeek.new(Mon,Fri) & REDay.new(9,00,17,00)
143
+ # Memorial Day (U.S.)
144
+ memorial_day = REYear.new(5) & DIMonth.new(Last,Monday)
145
+ # May 29th, 2006
146
+ last_monday_in_may = PDate.min(2006,5,29,10,12)
147
+ # Before
148
+ assert job.include?(last_monday_in_may)
149
+ assert job.include?(PDate.min(20006,5,30,14,00))
150
+ # Add Diff expression
151
+ job_with_holiday = job - last_monday_in_may
152
+ assert !job_with_holiday.include?(last_monday_in_may)
153
+ # Still have to work on Tuesday
154
+ assert job.include?(PDate.min(20006,5,30,14,00))
155
+ end
156
+
157
+ def test_day_in_month_te
158
+ #Friday, January 16th 2004
159
+ dt1 = Date.civil(2004,1,16)
160
+ #Friday, January 9th 2004
161
+ dt2 = Date.civil(2004,1,9)
162
+ #third Friday of the month
163
+ expr1 = DIMonth.new(Third,Friday)
164
+ #second Friday of the month
165
+ expr2 = DIMonth.new(Second,Friday)
166
+ assert(expr1.include?(dt1))
167
+ assert(!expr1.include?(dt2))
168
+ assert(expr2.include?(dt2))
169
+ assert(!expr2.include?(dt1))
170
+ #Sunday, January 25th 2004
171
+ dt3 = Date.civil(2004,1,25)
172
+ #last Sunday of the month
173
+ expr3 = DIMonth.new(Last_of,Sunday)
174
+ assert(expr3.include?(dt3))
175
+ end
176
+
177
+ def test_day_in_month_te_to_s
178
+ assert_equal 'last Sunday of the month', DIMonth.new(Last_of,Sunday).to_s
179
+ end
180
+
181
+ def test_day_in_week_te
182
+ #Friday (woo-hoo!)
183
+ expr = DIWeek.new(Friday)
184
+ #Friday, January 9th 2004
185
+ assert(expr.include?(PDate.new(2004,1,9)))
186
+ #Friday, January 16th 2004
187
+ assert(expr.include?(PDate.new(2004,1,16)))
188
+ #Monday, January 12th 2004
189
+ assert(!expr.include?(PDate.new(2004,1,12)))
190
+ end
191
+
192
+ def test_day_in_week_te
193
+ assert_equal 'Friday', DIWeek.new(Friday).to_s
194
+ end
195
+
196
+ def test_week_in_month_te
197
+ expr = WIMonth.new(Third)
198
+ assert(expr.include?(PDate.day(2004,2,19)))
199
+ assert(!expr.include?(PDate.day(2004,2,29)))
200
+ expr2 = WIMonth.new(Last_of)
201
+ assert(expr2.include?(PDate.day(2004,2,29)))
202
+ expr3 = WIMonth.new(Second_to_last)
203
+ assert(expr3.include?(PDate.day(2004,2,22)))
204
+ end
205
+
206
+ def test_range_each_year_te
207
+ # November 1st, 1961
208
+ dt1 = Date.civil(1961,11,1)
209
+ #June, 1986
210
+ dt2 = PDate::month(1986,6)
211
+ #November and December
212
+ expr1 = REYear.new(11,12)
213
+ #May 31st through September 6th
214
+ expr2 = REYear.new(5,31,9,6)
215
+ assert(expr1.include?(dt1))
216
+ assert(!expr1.include?(dt2))
217
+ assert(expr2.include?(dt2))
218
+ #August
219
+ expr3 = REYear.new(8)
220
+ assert(!expr3.include?(dt1))
221
+ assert(!expr3.include?(dt2))
222
+ #August 6th, 2004
223
+ dt3 = Date::new(2004,8,6)
224
+ assert(expr3.include?(dt3))
225
+ end
226
+
227
+ def test_range_each_year_te_to_s
228
+ assert_equal 'June 1st through July 2nd', REYear.new(6, 1, 7, 2).to_s
229
+ end
230
+
231
+ def test_range_each_day_te
232
+ #noon to 4:30pm
233
+ expr1 = REDay.new(12,0,16,30)
234
+ #3:15 pm (May 8th, 2012 - ignored)
235
+ assert(expr1.include?(PDate.hour(2012,5,8,15,15)))
236
+ #4:30 pm (April 18th, 1922 - ignored)
237
+ assert(expr1.include?(PDate.hour(1922,4,18,16,30)))
238
+ #noon (June 5th, 1975 - ignored)
239
+ assert(expr1.include?(PDate.hour(1975,6,5,12,0)))
240
+ #3:15 am (May 8th, 2012 - ignored)
241
+ assert(!expr1.include?(PDate.hour(2012,5,8,3,15)))
242
+ #8:30pm to 12:00 midnite
243
+ expr2 = REDay.new(20,30,00,00)
244
+ #9:00 pm (January 28th, 2004 - ignored)
245
+ assert(expr2.include?(PDate.min(2004,1,28,21,00)))
246
+ #12:00 am (January 28th, 2004 - ignored)
247
+ assert(expr2.include?(PDate.min(2004,1,28,0,0)))
248
+ #12:01 am (January 28th, 2004 - ignored)
249
+ assert(!expr2.include?(PDate.min(2004,1,28,0,01)))
250
+ end
251
+
252
+ def test_range_each_day_te_again
253
+ dr = DateRange.new(PDate.day(2005,9,19),PDate.day(2005,9,20))
254
+ red = REDay.new(8,0,10,0)
255
+ result = false
256
+ dr.each do |interval|
257
+ result = red.include?(interval)
258
+ break if result
259
+ end
260
+ assert(result)
261
+ end
262
+
263
+ def test_range_each_day_te_to_s
264
+ assert_equal 'from 11:10PM to 01:20AM daily', REDay.new(23,10,1,20).to_s
265
+ end
266
+
267
+ def test_range_each_week_te
268
+ assert_raises(ArgumentError){ expr = REWeek.new(10,4) }
269
+ expr1 = REWeek.new(Mon,Fri) & REDay.new(8,00,8,30)
270
+ assert(!expr1.include?(PDate.new(2004,5,1,8,06)))
271
+ #Sunday through Thursday
272
+ expr2 = REWeek.new(0,4)
273
+ assert(expr2.include?(PDate.min(2004,2,19,23,59,59)))
274
+ assert(!expr2.include?(PDate.min(2004,2,20,0,0,0)))
275
+ end
276
+
277
+ def test_range_each_week_te
278
+ #Friday through Tuesday
279
+ expr = Runt::REWeek.new(5,2)
280
+
281
+ assert expr.include?(Time.mktime(2007,9,28,0,0,0)), "#{expr.inspect} should include Fri 12am"
282
+ assert expr.include?(Time.mktime(2007,9,25,11,59,59)),"#{expr.inspect} should include Tue 11:59pm"
283
+ assert ! expr.include?(Time.mktime(2007,9,26,0,0,0)), "#{expr.inspect} should not include Wed 12am"
284
+ assert ! expr.include?(Time.mktime(2007,9,27,6,59,59)), "#{expr.inspect} should not include Thurs 6:59am"
285
+ assert ! expr.include?(Time.mktime(2007,9,27,11,59,0)), "#{expr.inspect} should not include Thurs 1159am"
286
+ assert expr.include?(Time.mktime(2007,9,29,11,0,0)), "#{expr.inspect} should include Sat 11am"
287
+ assert expr.include?(Time.mktime(2007,9,29,0,0,0)), "#{expr.inspect} should include Sat midnight"
288
+ assert expr.include?(Time.mktime(2007,9,29,23,59,59)), "#{expr.inspect} should include Saturday one minute before midnight"
289
+ assert expr.include?(Time.mktime(2007,9,30,23,59,59)), "#{expr.inspect} should include Sunday one minute before midnight"
290
+ end
291
+
292
+ def test_range_each_week_te_to_s
293
+ assert_equal 'all week', REWeek.new(Tuesday,Tuesday).to_s
294
+ assert_equal 'Thursday through Saturday', REWeek.new(Thursday,Saturday).to_s
295
+ end
296
+
297
+ def test_combined_te
298
+ #This is a hack.....
299
+ #In the U.S., Memorial Day begins the last Monday of May
300
+ #
301
+ #The month of May
302
+ may=REYear.new(5)
303
+ #Monday through Saturday
304
+ monday_to_saturday = REWeek.new(1,6)
305
+ #Last week of (any) month
306
+ last_week_in = WIMonth.new(Last_of)
307
+ #So, to say 'starting from the last Monday in May',
308
+ #we need to select just that last week of May begining with
309
+ #the Monday of that week
310
+ last_week_of_may = may & monday_to_saturday & last_week_in
311
+
312
+ #This is another hack similar to the above, except instead of selecting a range
313
+ #starting at the begining of the month, we need to select only the time period in
314
+ #September up until Labor Day.
315
+ #
316
+ #In the U.S., Labor Day is the first Monday in September
317
+ #
318
+ #The month of September
319
+ september=REYear.new(9)
320
+ #First week of (any) month
321
+ first_week_in = WIMonth.new(First)
322
+ entire_first_week_of_september = september & first_week_in
323
+ #To exclude everything in the first week which occurs on or after Monday.
324
+ first_week_of_september=entire_first_week_of_september - monday_to_saturday
325
+ #June through August
326
+ june_through_august=REYear.new(6,First,8)
327
+ assert(june_through_august.include?(PDate.day(2004,7,4)))
328
+ #Finally!
329
+ summer_time = last_week_of_may | first_week_of_september | june_through_august
330
+
331
+ #Will work, but will be incredibly slow:
332
+ # assert(summer_time.include?(PDate.min(2004,5,31,0,0)))
333
+ assert(summer_time.include?(PDate.day(2004,5,31,0,0)))
334
+ assert(summer_time.include?(PDate.day(2004,7,4)))
335
+ #also works...also slow:
336
+ # assert(!summer_time.include?(PDate.min(2004,9,6,0,0)))
337
+ assert(!summer_time.include?(PDate.hour(2004,9,6,0,0)))
338
+
339
+ end
340
+ def test_nyc_parking_te
341
+
342
+ #Monday, Wednesday, Friday
343
+ mon_wed_fri = DIWeek.new(Mon) | \
344
+ DIWeek.new(Wed) | \
345
+ DIWeek.new(Fri)
346
+
347
+
348
+ #Wednesday (at 7:15pm - ignored)
349
+ assert(mon_wed_fri.include?(DateTime.new(2004,3,10,19,15)))
350
+
351
+ #Sunday (at 9:00am - ignored)
352
+ assert(!mon_wed_fri.include?(DateTime.new(2004,3,14,9,00)))
353
+
354
+ #8am to 11am
355
+ eight_to_eleven = REDay.new(8,00,11,00)
356
+
357
+ #=> Mon,Wed,Fri - 8am to 11am
358
+ expr1 = mon_wed_fri & eight_to_eleven
359
+
360
+ #Tuesdays, Thursdays
361
+ tues_thurs = DIWeek.new(Tue) | DIWeek.new(Thu)
362
+
363
+ #11:30am to 2pm
364
+ eleven_thirty_to_two = REDay.new(11,30,14,00)
365
+
366
+ #Noon (on Monday - ignored)
367
+ assert(eleven_thirty_to_two.include?(DateTime.new(2004,3,8,12,00)))
368
+
369
+ #Midnite (on Thursday - ignored)
370
+ assert(!eleven_thirty_to_two.include?(DateTime.new(2004,3,11,00,00)))
371
+
372
+
373
+ #=> Tues,Thurs - 11:30am to 2pm
374
+ expr2 = tues_thurs & eleven_thirty_to_two
375
+
376
+ #
377
+ #Sigh...now if I can only get my dad to remember this...
378
+ #
379
+ parking_ticket = expr1 | expr2
380
+
381
+ assert(parking_ticket.include?(DateTime.new(2004,3,11,12,15)))
382
+ assert(parking_ticket.include?(DateTime.new(2004,3,10,9,15)))
383
+ assert(parking_ticket.include?(DateTime.new(2004,3,10,8,00)))
384
+
385
+ assert(!parking_ticket.include?(DateTime.new(2004,3,11,1,15)))
386
+
387
+ # Simplified
388
+ e1 = (DIWeek.new(Mon) | DIWeek.new(Wed) | DIWeek.new(Fri)) & REDay.new(8,00,11,00)
389
+ e2 = (DIWeek.new(Tue) | DIWeek.new(Thu)) & REDay.new(11,30,14,00)
390
+ ticket = expr1 | expr2
391
+ assert(ticket.include?(DateTime.new(2004,3,11,12,15)))
392
+ assert(ticket.include?(DateTime.new(2004,3,10,9,15)))
393
+ assert(ticket.include?(DateTime.new(2004,3,10,8,00)))
394
+ assert(!ticket.include?(DateTime.new(2004,3,11,1,15)))
395
+ end
396
+
397
+ def test_re_month_te
398
+ # October 22nd, 2005
399
+ dt1 = Date.civil(2005,10,22)
400
+ # The 20th through the 29th of any month
401
+ expr1 = REMonth.new(20,29)
402
+ assert(expr1.include?(dt1))
403
+ assert(!expr1.include?(PDate.new(2010,12,12)))
404
+ # August 17th, 1975
405
+ dt2 = Date.civil(1975,8,17)
406
+ # The 17th of any month
407
+ expr2 = REMonth.new(17)
408
+ assert(expr2.include?(dt2))
409
+ assert(!expr2.include?(dt1))
410
+ end
411
+
412
+ ###
413
+ # Dates functionality & tests contributed by Emmett Shear
414
+ ###
415
+ def test_day_in_month_dates
416
+ date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 12, 31)
417
+ month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
418
+ expr = DIMonth.new(First, Tuesday)
419
+ dates = expr.dates(date_range)
420
+ assert dates.size == 12
421
+ dates.each do |d|
422
+ assert d.wday == 2 # tuesday
423
+ assert d.day < 8 # in the first week
424
+ end
425
+ expr2 = DIMonth.new(Last, Friday)
426
+ dates2 = expr2.dates(date_range)
427
+ assert dates2.size == 12
428
+ dates2.each do |d|
429
+ assert d.wday == 5 # friday
430
+ assert d.day > month_days[d.month-1] - 8 # last week
431
+ end
432
+ end
433
+
434
+ def test_day_in_week_dates
435
+ date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 1, 31)
436
+ expr = DIWeek.new(Sunday)
437
+ dates = expr.dates(date_range)
438
+ assert( dates.size == 5 )
439
+ assert( dates.include?( Date.civil(2005, 1, 16) ) )
440
+ assert( dates.include?( Date.civil(2005, 1, 30) ) )
441
+ end
442
+
443
+ def test_union_dates
444
+ date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 12, 31)
445
+ month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
446
+ expr = DIMonth.new(Last, Friday) | DIMonth.new(1, Tuesday)
447
+ dates = expr.dates(date_range)
448
+ assert dates.size == 24
449
+ dates.each do |d|
450
+ unless (d.wday == 2 and d.day < 8) or \
451
+ (d.wday == 5 and d.day > month_days[d.month-1] - 8)
452
+ assert false, d.to_s
453
+ end
454
+ end
455
+ end
456
+
457
+ def test_intersection_dates
458
+ date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 12, 31)
459
+ expr = DIWeek.new(Sunday) & DIMonth.new(Second, Sunday)
460
+ dates = expr.dates(date_range)
461
+ assert( dates.size == 12 )
462
+ other_dates = DIMonth.new(Second, Sunday).dates(date_range)
463
+ dates.each { |d| assert( other_dates.include?(d) ) }
464
+ end
465
+
466
+ def test_range_each_week_dates
467
+ date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 1, 31)
468
+ expr = REWeek.new(3, 5)
469
+ dates = expr.dates(date_range)
470
+ assert dates.size == 12
471
+ end
472
+
473
+ def test_range_each_year_dates
474
+ date_range = Date.civil(2004, 5, 1)..Date.civil(2006, 5, 4)
475
+ expr = REYear.new(4, 28, 5, 6)
476
+ dates = expr.dates(date_range)
477
+ assert dates.size == 22, dates.size
478
+ end
479
+
480
+ def test_week_in_month_dates
481
+ date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 2, 28)
482
+ expr = WIMonth.new(2)
483
+ dates = expr.dates(date_range)
484
+ assert dates.size == 14, dates.size
485
+ assert dates.first.mday == 8
486
+ assert dates.last.mday == 14
487
+ expr_2 = WIMonth.new(Last)
488
+ dates_2 = expr_2.dates(date_range)
489
+ assert dates_2.size == 14, dates_2.size
490
+ assert dates_2.first.mday == 25
491
+ assert dates_2.last.mday == 28
492
+ end
493
+
494
+ def test_week_in_month_to_s
495
+ assert_equal 'last week of any month', WIMonth.new(Last).to_s
496
+ end
497
+
498
+ def test_range_each_month_dates
499
+ date_range = Date.civil(2005, 1, 7)..Date.civil(2005, 1, 15)
500
+ expr = REMonth.new(5, 9)
501
+ dates = expr.dates(date_range)
502
+ assert dates.size == 3, dates.size
503
+ assert false if dates.include? Date.civil(2005, 1, 6)
504
+ end
505
+
506
+ def test_range_each_month_to_s
507
+ assert_equal 'from the 2nd to the 5th monthly',REMonth.new(2,5).to_s
508
+ end
509
+
510
+ def test_diff_dates
511
+ date_range = Date.civil(2005, 1, 1)..Date.civil(2005, 1, 31)
512
+ expr = REYear.new(1, 1, 1, 31) - REMonth.new(7, 15)
513
+ dates = expr.dates(date_range)
514
+ assert dates.size == 22, dates.size
515
+ end
516
+
517
+ def test_day_interval_te
518
+ date1 = Date.civil(2005,10,28)
519
+ # Match every 8 days
520
+ expr = DayIntervalTE.new(date1, 8)
521
+ assert expr.include?((date1 + 8))
522
+ assert expr.include?((date1 + 16))
523
+ assert expr.include?((date1 + 64))
524
+ # Now use DateTime
525
+ date2 = DateTime.now
526
+ # Match every 6 days
527
+ expr2 = DayIntervalTE.new(date2, 6)
528
+ assert expr2.include?((date2 + 12))
529
+ assert expr2.include?((date2 + 24))
530
+ end
531
+
532
+ def test_day_interval_te_to_s
533
+ every_four_days = DayIntervalTE.new(Date.new(2006,2,26), 4)
534
+ assert_equal "every 4th day after #{Runt.format_date(Date.new(2006,2,26))}", every_four_days.to_s
535
+ end
536
+
537
+ def test_year_te
538
+ # second sun of any month
539
+ second_sun = DIMonth.new(Second, Sunday)
540
+ # simple year matching expression which will return true for
541
+ # any date in 2005
542
+ year_te = YearTE.new(2005)
543
+ # Second Sunday of a month in 2002
544
+ dt_in_2002 = Date.civil(2002,9,8)
545
+ # Second Sunday of a month in 2005
546
+ dt_in_2005 = Date.civil(2005,12,11)
547
+ assert(year_te.include?(dt_in_2005))
548
+ assert(!year_te.include?(dt_in_2002))
549
+ end
550
+
551
+ def test_year_te
552
+ assert_equal 'during the year 1934', YearTE.new(1934).to_s
553
+ end
554
+
555
+ def test_use_time_class
556
+ monday=DIWeek.new(Mon) & REDay.new(9,30,17,30)
557
+ tues_to_fri=REWeek.new(Tue, Fri) & REDay.new(9,00,17,30)
558
+ exp=monday | tues_to_fri
559
+ assert(!exp.include?(Time.parse('Monday 06 November 2006 07:38')))
560
+ assert(exp.include?(Time.parse('Monday 06 November 2006 13:37')))
561
+ assert(exp.include?(Time.parse('Friday 10 November 2006 16:59')))
562
+ assert(!exp.include?(Time.parse('Friday 10 November 2006 17:31')))
563
+ end
564
+
565
+ def test_every_te_minutes
566
+ # Match every 2 minutes
567
+ xpr=EveryTE.new(PDate.min(2006,12,5,5,54), 2)
568
+ assert !xpr.include?(PDate.min(2006,12,4,5,54))
569
+ assert xpr.include?(PDate.min(2006,12,5,5,54))
570
+ assert xpr.include?(PDate.min(2006,12,5,5,56))
571
+ assert xpr.include?(PDate.sec(2006,12,5,5,58,03))
572
+ assert xpr.include?(PDate.min(2006,12,5,6,00))
573
+ assert !xpr.include?(PDate.min(2006,12,5,5,59))
574
+ assert xpr.include?(Time.parse('Tuesday 05 December 2006 07:08'))
575
+ # Match every 3 days
576
+ xpr2=EveryTE.new(PDate.day(2006,5,4), 3)
577
+ assert !xpr2.include?(Date.new(2006,5,5))
578
+ assert !xpr2.include?(PDate.new(2006,5,6))
579
+ assert xpr2.include?(PDate.new(2006,5,7))
580
+ assert xpr2.include?(PDate.min(2006,5,10,6,45))
581
+ end
582
+
583
+ def test_every_te_days
584
+ dstart = DateTime.parse("US-Eastern:19970902T090000")
585
+ dstart.date_precision = DPrecision::DAY
586
+
587
+ xpr=EveryTE.new(dstart, 10) & REWeek.new(Sun,Sat)
588
+
589
+ assert !xpr.include?(DateTime.parse("US-Eastern:19970901T090000")) #Sep 1
590
+ assert xpr.include?(DateTime.parse("US-Eastern:19970902T090000")) #Sep 2
591
+ assert !xpr.include?(DateTime.parse("US-Eastern:19970904T090000")) #Sep 3
592
+ assert !xpr.include?(DateTime.parse("US-Eastern:19970904T090000")) #Sep 4
593
+ assert !xpr.include?(DateTime.parse("US-Eastern:19970905T090000")) #Sep 5
594
+ assert !xpr.include?(DateTime.parse("US-Eastern:19970906T090000")) #Sep 6
595
+ assert !xpr.include?(DateTime.parse("US-Eastern:19970907T090000")) #Sep 7
596
+ assert !xpr.include?(DateTime.parse("US-Eastern:19970908T090000")) #Sep 8
597
+ assert !xpr.include?(DateTime.parse("US-Eastern:19970909T090000")) #Sep 9
598
+ assert !xpr.include?(DateTime.parse("US-Eastern:19970910T090000")) #Sep 10
599
+ assert !xpr.include?(DateTime.parse("US-Eastern:19970911T090000")) #Sep 11
600
+ assert xpr.include?(DateTime.parse("US-Eastern:19970912T090000")) #Sep 12
601
+ assert xpr.include?(DateTime.parse("US-Eastern:19970922T090000")) #Sep 22
602
+ assert xpr.include?(DateTime.parse("US-Eastern:19971002T090000")) #Oct 2
603
+ assert xpr.include?(DateTime.parse("US-Eastern:19971012T090000")) #Oct 12
604
+ end
605
+
606
+ def test_every_te_to_s
607
+ date=PDate.new(2006,12,5,6,0,0)
608
+ every_thirty_seconds=EveryTE.new(date, 30)
609
+ assert_equal "every 30 seconds starting #{Runt.format_date(date)}", every_thirty_seconds.to_s
610
+ end
611
+
612
+ end