spec/cronline_spec.rb in rufus-scheduler-2.0.19 vs spec/cronline_spec.rb in rufus-scheduler-2.0.20

- old
+ new

@@ -22,17 +22,10 @@ end def to_a(line, array) cl(line).to_array.should == array end - def local(*args) - Time.local(*args) - end - def utc(*args) - Time.utc(*args) - end - describe '.new' do it 'interprets cron strings correctly' do to_a '* * * * *', [ [0], nil, nil, nil, nil, nil, nil, nil ] @@ -52,10 +45,17 @@ to_a '1 * * * * *', [ [1], nil, nil, nil, nil, nil, nil, nil ] to_a '7 10-12 * * * *', [ [7], [10, 11, 12], nil, nil, nil, nil, nil, nil ] to_a '1-5 * * * * *', [ [1,2,3,4,5], nil, nil, nil, nil, nil, nil, nil ] to_a '0 0 1 1 *', [ [0], [0], [0], [1], [1], nil, nil, nil ] + + to_a '0 23-24 * * *', [ [0], [0], [23, 0], nil, nil, nil, nil, nil ] + # + # as reported by Aimee Rose in + # https://github.com/jmettraux/rufus-scheduler/issues/56 + + to_a '0 23-2 * * *', [ [0], [0], [23, 0, 1, 2], nil, nil, nil, nil, nil ] end it 'rejects invalid weekday expressions' do lambda { cl '0 17 * * MON_FRI' }.should raise_error @@ -82,11 +82,11 @@ it 'interprets cron strings with / (slashes) correctly' do to_a( '0 */2 * * *', - [ [0], [0], (0..12).collect { |e| e * 2 }, nil, nil, nil, nil, nil ]) + [ [0], [0], (0..11).collect { |e| e * 2 }, nil, nil, nil, nil, nil ]) to_a( '0 7-23/2 * * *', [ [0], [0], (7..23).select { |e| e.odd? }, nil, nil, nil, nil, nil ]) to_a( '*/10 * * * *', @@ -103,11 +103,12 @@ it 'accepts items with initial 0' do to_a '09 * * * *', [ [0], [9], nil, nil, nil, nil, nil, nil ] to_a '09-12 * * * *', [ [0], [9, 10, 11, 12], nil, nil, nil, nil, nil, nil ] to_a '07-08 * * * *', [ [0], [7, 8], nil, nil, nil, nil, nil, nil ] - to_a '* */08 * * *', [ [0], nil, [0, 8, 16, 24], nil, nil, nil, nil, nil ] + to_a '* */08 * * *', [ [0], nil, [0, 8, 16], nil, nil, nil, nil, nil ] + to_a '* */07 * * *', [ [0], nil, [0, 7, 14, 21], nil, nil, nil, nil, nil ] to_a '* 01-09/04 * * *', [ [0], nil, [1, 5, 9], nil, nil, nil, nil, nil ] to_a '* * * * 06', [ [0], nil, nil, nil, nil, [6], nil, nil ] end it 'interprets cron strings with L correctly' do @@ -130,10 +131,22 @@ it 'raises if L is used for something else than days' do lambda { cl '* L * * *'}.should raise_error(ArgumentError) end + + it 'raises for out of range input' do + + lambda { cl '60-62 * * * *'}.should raise_error(ArgumentError) + lambda { cl '62 * * * *'}.should raise_error(ArgumentError) + lambda { cl '60 * * * *'}.should raise_error(ArgumentError) + lambda { cl '* 25-26 * * *'}.should raise_error(ArgumentError) + lambda { cl '* 25 * * *'}.should raise_error(ArgumentError) + # + # as reported by Aimee Rose in + # https://github.com/jmettraux/rufus-scheduler/pull/58 + end end describe '#next_time' do def nt(cronline, now) @@ -149,13 +162,20 @@ nt('* * * * * *', now).should == now + 1 nt('* * 13 * fri', now).should == now + 3715200 nt('10 12 13 12 *', now).should == now + 29938200 # this one is slow (1 year == 3 seconds) + # + # historical note: + # (comment made in 2006 or 2007, the underlying libs got better and + # that slowness is gone) nt('0 0 * * thu', now).should == now + 604800 + nt('0 0 * * *', now).should == now + 24 * 3600 + nt('0 24 * * *', now).should == now + 24 * 3600 + now = local(2008, 12, 31, 23, 59, 59, 0) nt('* * * * *', now).should == now + 1 end @@ -223,40 +243,62 @@ # nt("0 0 * * thu #{zone}", now).should == local(1970, 1, 7, 18) #end it 'computes the next time correctly when there is a sun#2 involved' do - now = local(1970, 1, 1) + nt('* * * * sun#1', local(1970, 1, 1)).should == local(1970, 1, 4) + nt('* * * * sun#2', local(1970, 1, 1)).should == local(1970, 1, 11) - nt('* * * * sun#1', now).should == local(1970, 1, 4) - nt('* * * * sun#2', now).should == local(1970, 1, 11) + nt('* * * * sun#2', local(1970, 1, 12)).should == local(1970, 2, 8) + end - now = local(1970, 1, 12) + it 'computes the next time correctly when there is a sun#2,sun#3 involved' do - nt('* * * * sun#2', now).should == local(1970, 2, 8) + nt('* * * * sun#2,sun#3', local(1970, 1, 1)).should == local(1970, 1, 11) + nt('* * * * sun#2,sun#3', local(1970, 1, 12)).should == local(1970, 1, 18) end - it 'computes the next time correctly when there is a sun#2,sun#3 involved' do + it 'understands sun#L' do - now = local(1970, 1, 1) + nt('* * * * sun#L', local(1970, 1, 1)).should == local(1970, 1, 25) + end - nt('* * * * sun#2,sun#3', now).should == local(1970, 1, 11) + it 'understands sun#-1' do - now = local(1970, 1, 12) + nt('* * * * sun#-1', local(1970, 1, 1)).should == local(1970, 1, 25) + end - nt('* * * * sun#2,sun#3', now).should == local(1970, 1, 18) + it 'understands sun#-2' do + + nt('* * * * sun#-2', local(1970, 1, 1)).should == local(1970, 1, 18) end - it 'computes the next time correctly when there is a L (last day of month)' do + it 'computes the next time correctly when "L" (last day of month)' do - nt('* * L * *', local(1970,1,1)).should == local(1970, 1, 31) - nt('* * L * *', local(1970,2,1)).should == local(1970, 2, 28) - nt('* * L * *', local(1972,2,1)).should == local(1972, 2, 29) - nt('* * L * *', local(1970,4,1)).should == local(1970, 4, 30) - end + nt('* * L * *', lo(1970, 1, 1)).should == lo(1970, 1, 31) + nt('* * L * *', lo(1970, 2, 1)).should == lo(1970, 2, 28) + nt('* * L * *', lo(1972, 2, 1)).should == lo(1972, 2, 29) + nt('* * L * *', lo(1970, 4, 1)).should == lo(1970, 4, 30) + end end + describe '#previous_time' do + + def pt(cronline, now) + Rufus::CronLine.new(cronline).previous_time(now) + end + + it 'returns the previous time the cron should have triggered' do + + pt('* * * * sun', lo(1970, 1, 1)).should == lo(1969, 12, 28, 23, 59, 00) + pt('* * 13 * *', lo(1970, 1, 1)).should == lo(1969, 12, 13, 23, 59, 00) + pt('0 12 13 * *', lo(1970, 1, 1)).should == lo(1969, 12, 13, 12, 00) + + pt('* * * * * sun', lo(1970, 1, 1)).should == lo(1969, 12, 28, 23, 59, 59) + end + end + describe '#matches?' do it 'matches correctly in UTC (TZ not specified)' do match '* * * * *', utc(1970, 1, 1, 0, 1) @@ -323,19 +365,24 @@ match '* * * * sun#2,sun#3', local(1970, 1, 18) no_match '* * * * sun#2,sun#3', local(1970, 1, 25) end end - describe '.monthday' do + describe '#monthdays' do it 'returns the appropriate "sun#2"-like string' do - d = local(1970, 1, 1) - Rufus::CronLine.monthday(d).should == 'thu#1' - Rufus::CronLine.monthday(d + 6 * 24 * 3600).should == 'wed#1' - Rufus::CronLine.monthday(d + 13 * 24 * 3600).should == 'wed#2' + class Rufus::CronLine + public :monthdays + end - Rufus::CronLine.monthday(local(2011, 3, 11)).should == 'fri#2' + cl = Rufus::CronLine.new('* * * * *') + + cl.monthdays(local(1970, 1, 1)).should == %w[ thu#1 thu#-5 ] + cl.monthdays(local(1970, 1, 7)).should == %w[ wed#1 wed#-4 ] + cl.monthdays(local(1970, 1, 14)).should == %w[ wed#2 wed#-3 ] + + cl.monthdays(local(2011, 3, 11)).should == %w[ fri#2 fri#-3 ] end end end