runt 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,612 +1,76 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'test/unit'
4
- require 'runt'
5
- require 'date'
6
- require 'time'
7
-
8
- $DEBUG=false
3
+ require 'baseexpressiontest'
9
4
 
10
5
  # Unit tests for TExpr classes
11
6
  # Author:: Matthew Lipper
12
7
 
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
8
+ class TExprTest < BaseExpressionTest
330
9
 
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)))
10
+ include TExpr
338
11
 
12
+ def test_include
13
+ assert !self.include?(true), "Default include? method should always return false"
339
14
  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
15
 
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) ) }
16
+ def test_to_s
17
+ assert_equal self.to_s, 'TExpr', "Default to_s method should always return 'TExpr'"
464
18
  end
465
19
 
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
20
+ def test_or_from_union
21
+ union = Union.new
22
+ same_union = union.or(@stub1)
23
+ assert_same union, same_union, "Expected same Union instance that received the or method"
24
+ assert_same @stub1, union.expressions.first, "Union instance should have added the stub expression"
471
25
  end
472
26
 
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
27
+ def test_or_from_nonunion
28
+ result = @stub1.or(@stub2) {|e| e}
29
+ assert_equal Runt::Union, result.class, "Expected an Union instance. Instead got #{result.class}"
30
+ assert_same @stub1, result.expressions.first, "Result should be new Union instance containing both stub expressions"
31
+ assert_same @stub2, result.expressions.last, "Result should be new Union instance containing both stub expressions"
478
32
  end
479
33
 
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
34
+ def test_and_from_intersect
35
+ intersect = Intersect.new
36
+ result = intersect.and(@stub1)
37
+ assert_same intersect, result, "Expected same Intersect instance that received the and method"
38
+ assert_same @stub1, intersect.expressions.first, "Intersect instance should have added the stub expression"
492
39
  end
493
40
 
494
- def test_week_in_month_to_s
495
- assert_equal 'last week of any month', WIMonth.new(Last).to_s
41
+ def test_or_from_nonintersect
42
+ result = @stub1.and(@stub2) {|e| e}
43
+ assert_equal Runt::Intersect, result.class, "Expected an Intersect instance. Instead got #{result.class}"
44
+ assert_same @stub1, result.expressions.first, "Result should be new Intersect instance containing both stub expressions"
45
+ assert_same @stub2, result.expressions.last, "Result should be new Intersect instance containing both stub expressions"
496
46
  end
497
47
 
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
48
+ def test_minus
49
+ result = @stub1.minus(@stub2) {|e| e}
50
+ assert_equal Runt::Diff, result.class, "Expected an Diff instance. Instead got #{result.class}"
51
+ assert_same @stub1, result.expr1, "Expected first stub instance used to create Diff expression"
52
+ assert_same @stub2, result.expr2, "Expected second stub instance used to create Diff expression"
515
53
  end
516
54
 
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))
55
+ def test_dates_no_limit
56
+ # Normally, your range is made up of Date-like Objects
57
+ range = 1..3
58
+ assert @stub1.dates(range).empty?, "Expected empty Array of Objects returned from stub expression"
59
+ @stub1.match = true
60
+ result = @stub1.dates(range)
61
+ assert_equal 1, result[0], "Expected Array of Objects given by range to be returned from stub expression"
62
+ assert_equal 2, result[1], "Expected Array of Objects given by range to be returned from stub expression"
63
+ assert_equal 3, result[2], "Expected Array of Objects given by range to be returned from stub expression"
549
64
  end
550
65
 
551
- def test_year_te
552
- assert_equal 'during the year 1934', YearTE.new(1934).to_s
66
+ def test_dates_with_limit
67
+ range = 1..3
68
+ assert @stub1.dates(range).empty?, "Expected empty Array of Objects returned from stub expression"
69
+ @stub1.match = true
70
+ result = @stub1.dates(range,2)
71
+ assert_equal 2, result.size, "Expected Array of only 2 Objects. Got #{result.size}"
72
+ assert_equal 1, result[0], "Expected Array of Objects given by range to be returned from stub expression"
73
+ assert_equal 2, result[1], "Expected Array of Objects given by range to be returned from stub expression"
553
74
  end
554
75
 
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
76
  end
data/test/uniontest.rb ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'baseexpressiontest'
4
+
5
+ # Unit tests for Union class
6
+ # Author:: Matthew Lipper
7
+
8
+ class UnionTest < BaseExpressionTest
9
+
10
+ def setup
11
+ super
12
+ @union = Union.new
13
+ @date = @pdate_20071028
14
+ end
15
+
16
+ def test_include
17
+ assert !@union.include?(@date), "Empty Union instance should not include any dates"
18
+ @union.add(@stub1).add(@stub2) # both expressions will return false
19
+ assert !@union.include?(@date), "Union instance should not include any dates"
20
+ @stub2.match = true
21
+ assert @union.include?(@date), "Union instance should include any dates"
22
+ @stub2.match = false
23
+ @stub1.match = true
24
+ assert @union.include?(@date), "Union instance should include any dates"
25
+
26
+ end
27
+
28
+ def test_to_s
29
+ assert_equal 'empty', @union.to_s
30
+ @union.add(@stub1)
31
+ assert_equal 'every ' + @stub1.to_s, @union.to_s
32
+ @union.add(@stub2)
33
+ assert_equal 'every ' + @stub1.to_s + ' or ' + @stub2.to_s, @union.to_s
34
+ end
35
+
36
+ end