Skip to content

Commit feae0d6

Browse files
committed
Fix tests deprecations and style
1 parent 5ff2cd5 commit feae0d6

File tree

2 files changed

+107
-108
lines changed

2 files changed

+107
-108
lines changed

lib/cron_parser.rb

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class InternalTime
99
attr_accessor :year, :month, :day, :hour, :min
1010
attr_accessor :time_source
1111

12-
def initialize(time,time_source = Time)
12+
def initialize(time, time_source = Time)
1313
@year = time.year
1414
@month = time.month
1515
@day = time.day
@@ -29,29 +29,29 @@ def inspect
2929
end
3030

3131
SYMBOLS = {
32-
"jan" => "1",
33-
"feb" => "2",
34-
"mar" => "3",
35-
"apr" => "4",
36-
"may" => "5",
37-
"jun" => "6",
38-
"jul" => "7",
39-
"aug" => "8",
40-
"sep" => "9",
41-
"oct" => "10",
42-
"nov" => "11",
43-
"dec" => "12",
44-
45-
"sun" => "0",
46-
"mon" => "1",
47-
"tue" => "2",
48-
"wed" => "3",
49-
"thu" => "4",
50-
"fri" => "5",
51-
"sat" => "6"
32+
"jan" => "1",
33+
"feb" => "2",
34+
"mar" => "3",
35+
"apr" => "4",
36+
"may" => "5",
37+
"jun" => "6",
38+
"jul" => "7",
39+
"aug" => "8",
40+
"sep" => "9",
41+
"oct" => "10",
42+
"nov" => "11",
43+
"dec" => "12",
44+
45+
"sun" => "0",
46+
"mon" => "1",
47+
"tue" => "2",
48+
"wed" => "3",
49+
"thu" => "4",
50+
"fri" => "5",
51+
"sat" => "6"
5252
}
5353

54-
def initialize(source,time_source = Time)
54+
def initialize(source, time_source = Time)
5555
@source = interpret_vixieisms(source)
5656
@time_source = time_source
5757
validate_source
@@ -76,7 +76,6 @@ def interpret_vixieisms(spec)
7676
end
7777
end
7878

79-
8079
# returns the next occurence after the given date
8180
def next(now = @time_source.now, num = 1)
8281
t = InternalTime.new(now, @time_source)
@@ -100,15 +99,15 @@ def next(now = @time_source.now, num = 1)
10099
nudge_minute(t)
101100
t = t.to_time
102101
if num > 1
103-
recursive_calculate(:next,t,num)
102+
recursive_calculate(:next, t, num)
104103
else
105104
t
106105
end
107106
end
108107

109108
# returns the last occurence before the given date
110-
def last(now = @time_source.now, num=1)
111-
t = InternalTime.new(now,@time_source)
109+
def last(now = @time_source.now, num = 1)
110+
t = InternalTime.new(now, @time_source)
112111

113112
unless time_specs[:month][0].include?(t.month)
114113
nudge_month(t, :last)
@@ -129,13 +128,12 @@ def last(now = @time_source.now, num=1)
129128
nudge_minute(t, :last)
130129
t = t.to_time
131130
if num > 1
132-
recursive_calculate(:last,t,num)
131+
recursive_calculate(:last, t, num)
133132
else
134133
t
135134
end
136135
end
137136

138-
139137
SUBELEMENT_REGEX = %r{^(\d+)(-(\d+)(/(\d+))?)?$}
140138
def parse_element(elem, allowed_range)
141139
values = elem.split(',').map do |subel|
@@ -160,10 +158,9 @@ def parse_element(elem, allowed_range)
160158
[Set.new(values), values, elem]
161159
end
162160

163-
164161
protected
165162

166-
def recursive_calculate(meth,time,num)
163+
def recursive_calculate(meth, time, num)
167164
array = [time]
168165
num.-(1).times do |num|
169166
array << self.send(meth, array.last)
@@ -249,11 +246,11 @@ def time_specs
249246
# tokens now contains the 5 fields
250247
tokens = substitute_parse_symbols(@source).split(/\s+/)
251248
{
252-
:minute => parse_element(tokens[0], 0..59), #minute
253-
:hour => parse_element(tokens[1], 0..23), #hour
254-
:dom => parse_element(tokens[2], 1..31), #DOM
255-
:month => parse_element(tokens[3], 1..12), #mon
256-
:dow => parse_element(tokens[4], 0..6) #DOW
249+
:minute => parse_element(tokens[0], 0..59), # minute
250+
:hour => parse_element(tokens[1], 0..23), # hour
251+
:dom => parse_element(tokens[2], 1..31), # DOM
252+
:month => parse_element(tokens[3], 1..12), # mon
253+
:dow => parse_element(tokens[4], 0..6) # DOW
257254
}
258255
end
259256
end
@@ -264,7 +261,6 @@ def substitute_parse_symbols(str)
264261
end
265262
end
266263

267-
268264
def stepped_range(rng, step = 1)
269265
len = rng.last - rng.first
270266

@@ -275,7 +271,6 @@ def stepped_range(rng, step = 1)
275271
result
276272
end
277273

278-
279274
# returns the smallest element from allowed which is greater than current
280275
# returns nil if no matching value was found
281276
def find_best_next(current, allowed, dir)

spec/cron_parser_spec.rb

Lines changed: 75 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,92 +15,96 @@ def parse_date(str)
1515
["10", 0..59, [10]],
1616
["10,30", 0..59, [10, 30]],
1717
["10-15", 0..59, [10, 11, 12, 13, 14, 15]],
18-
["10-40/10", 0..59, [10, 20, 30, 40]],
18+
["10-40/10", 0..59, [10, 20, 30, 40]]
1919
].each do |element, range, expected|
2020
it "should return #{expected} for '#{element}' when range is #{range}" do
2121
parser = CronParser.new('* * * * *')
22-
parser.parse_element(element, range).first.to_a.sort.should == expected.sort
22+
expect(parser.parse_element(element, range).first.to_a.sort).to eq(expected.sort)
2323
end
2424
end
2525
end
2626

2727
describe "CronParser#next" do
2828
[
29-
["* * * * *", "2011-08-15 12:00", "2011-08-15 12:01",1],
30-
["* * * * *", "2011-08-15 02:25", "2011-08-15 02:26",1],
31-
["* * * * *", "2011-08-15 02:59", "2011-08-15 03:00",1],
32-
["*/15 * * * *", "2011-08-15 02:02", "2011-08-15 02:15",1],
33-
["*/15,25 * * * *", "2011-08-15 02:15", "2011-08-15 02:25",1],
34-
["30 3,6,9 * * *", "2011-08-15 02:15", "2011-08-15 03:30",1],
35-
["30 9 * * *", "2011-08-15 10:15", "2011-08-16 09:30",1],
36-
["30 9 * * *", "2011-08-31 10:15", "2011-09-01 09:30",1],
37-
["30 9 * * *", "2011-09-30 10:15", "2011-10-01 09:30",1],
38-
["0 9 * * *", "2011-12-31 10:15", "2012-01-01 09:00",1],
39-
["* * 12 * *", "2010-04-15 10:15", "2010-05-12 00:00",1],
40-
["* * * * 1,3", "2010-04-15 10:15", "2010-04-19 00:00",1],
41-
["* * * * MON,WED", "2010-04-15 10:15", "2010-04-19 00:00",1],
42-
["0 0 1 1 *", "2010-04-15 10:15", "2011-01-01 00:00",1],
43-
["0 0 * * 1", "2011-08-01 00:00", "2011-08-08 00:00",1],
44-
["0 0 * * 1", "2011-07-25 00:00", "2011-08-01 00:00",1],
45-
["45 23 7 3 *", "2011-01-01 00:00", "2011-03-07 23:45",1],
46-
["0 0 1 jun *", "2013-05-14 11:20", "2013-06-01 00:00",1],
47-
["0 0 1 may,jul *", "2013-05-14 15:00", "2013-07-01 00:00",1],
48-
["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-07-01 00:00",1],
49-
["40 5 * * *", "2014-02-01 15:56", "2014-02-02 05:40",1],
50-
["0 5 * * 1", "2014-02-01 15:56", "2014-02-03 05:00",1],
51-
["10 8 15 * *", "2014-02-01 15:56", "2014-02-15 08:10",1],
52-
["50 6 * * 1", "2014-02-01 15:56", "2014-02-03 06:50",1],
53-
["1 2 * apr mOn", "2014-02-01 15:56", "2014-04-07 02:01",1],
54-
["1 2 3 4 7", "2014-02-01 15:56", "2014-04-03 02:01",1],
55-
["1 2 3 4 7", "2014-04-04 15:56", "2014-04-06 02:01",1],
56-
["1-20/3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01",1],
57-
["1,2,3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01",1],
58-
["1-9,15-30 * * * *", "2014-02-01 15:56", "2014-02-01 16:01",1],
59-
["1-9/3,15-30/4 * * * *", "2014-02-01 15:56", "2014-02-01 16:01",1],
60-
["1 2 3 jan mon", "2014-02-01 15:56", "2015-01-03 02:01",1],
61-
["1 2 3 4 mON", "2014-02-01 15:56", "2014-04-03 02:01",1],
62-
["1 2 3 jan 5", "2014-02-01 15:56", "2015-01-02 02:01",1],
63-
["@yearly", "2014-02-01 15:56", "2015-01-01 00:00",1],
64-
["@annually", "2014-02-01 15:56", "2015-01-01 00:00",1],
65-
["@monthly", "2014-02-01 15:56", "2014-03-01 00:00",1],
66-
["@weekly", "2014-02-01 15:56", "2014-02-02 00:00",1],
67-
["@daily", "2014-02-01 15:56", "2014-02-02 00:00",1],
68-
["@midnight", "2014-02-01 15:56", "2014-02-02 00:00",1],
69-
["@hourly", "2014-02-01 15:56", "2014-02-01 16:00",1],
70-
["*/3 * * * *", "2014-02-01 15:56", "2014-02-01 15:57",1],
71-
["0 5 * 2,3 *", "2014-02-01 15:56", "2014-02-02 05:00",1],
72-
["15-59/15 * * * *", "2014-02-01 15:56", "2014-02-01 16:15",1],
73-
["15-59/15 * * * *", "2014-02-01 15:00", "2014-02-01 15:15",1],
74-
["15-59/15 * * * *", "2014-02-01 15:01", "2014-02-01 15:15",1],
75-
["15-59/15 * * * *", "2014-02-01 15:16", "2014-02-01 15:30",1],
76-
["15-59/15 * * * *", "2014-02-01 15:26", "2014-02-01 15:30",1],
77-
["15-59/15 * * * *", "2014-02-01 15:36", "2014-02-01 15:45",1],
78-
["15-59/15 * * * *", "2014-02-01 15:45", "2014-02-01 16:15",4],
79-
["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 16:15",3],
80-
["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 16:15",2],
81-
].each do |line, now, expected_next,num|
29+
["* * * * *", "2011-08-15 12:00", "2011-08-15 12:01", 1],
30+
["* * * * *", "2011-08-15 02:25", "2011-08-15 02:26", 1],
31+
["* * * * *", "2011-08-15 02:59", "2011-08-15 03:00", 1],
32+
["*/15 * * * *", "2011-08-15 02:02", "2011-08-15 02:15", 1],
33+
["*/15,25 * * * *", "2011-08-15 02:15", "2011-08-15 02:25", 1],
34+
["30 3,6,9 * * *", "2011-08-15 02:15", "2011-08-15 03:30", 1],
35+
["30 9 * * *", "2011-08-15 10:15", "2011-08-16 09:30", 1],
36+
["30 9 * * *", "2011-08-31 10:15", "2011-09-01 09:30", 1],
37+
["30 9 * * *", "2011-09-30 10:15", "2011-10-01 09:30", 1],
38+
["0 9 * * *", "2011-12-31 10:15", "2012-01-01 09:00", 1],
39+
["* * 12 * *", "2010-04-15 10:15", "2010-05-12 00:00", 1],
40+
["* * * * 1,3", "2010-04-15 10:15", "2010-04-19 00:00", 1],
41+
["* * * * MON,WED", "2010-04-15 10:15", "2010-04-19 00:00", 1],
42+
["0 0 1 1 *", "2010-04-15 10:15", "2011-01-01 00:00", 1],
43+
["0 0 * * 1", "2011-08-01 00:00", "2011-08-08 00:00", 1],
44+
["0 0 * * 1", "2011-07-25 00:00", "2011-08-01 00:00", 1],
45+
["45 23 7 3 *", "2011-01-01 00:00", "2011-03-07 23:45", 1],
46+
["0 0 1 jun *", "2013-05-14 11:20", "2013-06-01 00:00", 1],
47+
["0 0 1 may,jul *", "2013-05-14 15:00", "2013-07-01 00:00", 1],
48+
["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-07-01 00:00", 1],
49+
["40 5 * * *", "2014-02-01 15:56", "2014-02-02 05:40", 1],
50+
["0 5 * * 1", "2014-02-01 15:56", "2014-02-03 05:00", 1],
51+
["10 8 15 * *", "2014-02-01 15:56", "2014-02-15 08:10", 1],
52+
["50 6 * * 1", "2014-02-01 15:56", "2014-02-03 06:50", 1],
53+
["1 2 * apr mOn", "2014-02-01 15:56", "2014-04-07 02:01", 1],
54+
["1 2 3 4 7", "2014-02-01 15:56", "2014-04-03 02:01", 1],
55+
["1 2 3 4 7", "2014-04-04 15:56", "2014-04-06 02:01", 1],
56+
["1-20/3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1],
57+
["1,2,3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1],
58+
["1-9,15-30 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1],
59+
["1-9/3,15-30/4 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1],
60+
["1 2 3 jan mon", "2014-02-01 15:56", "2015-01-03 02:01", 1],
61+
["1 2 3 4 mON", "2014-02-01 15:56", "2014-04-03 02:01", 1],
62+
["1 2 3 jan 5", "2014-02-01 15:56", "2015-01-02 02:01", 1],
63+
["@yearly", "2014-02-01 15:56", "2015-01-01 00:00", 1],
64+
["@annually", "2014-02-01 15:56", "2015-01-01 00:00", 1],
65+
["@monthly", "2014-02-01 15:56", "2014-03-01 00:00", 1],
66+
["@weekly", "2014-02-01 15:56", "2014-02-02 00:00", 1],
67+
["@daily", "2014-02-01 15:56", "2014-02-02 00:00", 1],
68+
["@midnight", "2014-02-01 15:56", "2014-02-02 00:00", 1],
69+
["@hourly", "2014-02-01 15:56", "2014-02-01 16:00", 1],
70+
["*/3 * * * *", "2014-02-01 15:56", "2014-02-01 15:57", 1],
71+
["0 5 * 2,3 *", "2014-02-01 15:56", "2014-02-02 05:00", 1],
72+
["15-59/15 * * * *", "2014-02-01 15:56", "2014-02-01 16:15", 1],
73+
["15-59/15 * * * *", "2014-02-01 15:00", "2014-02-01 15:15", 1],
74+
["15-59/15 * * * *", "2014-02-01 15:01", "2014-02-01 15:15", 1],
75+
["15-59/15 * * * *", "2014-02-01 15:16", "2014-02-01 15:30", 1],
76+
["15-59/15 * * * *", "2014-02-01 15:26", "2014-02-01 15:30", 1],
77+
["15-59/15 * * * *", "2014-02-01 15:36", "2014-02-01 15:45", 1],
78+
["15-59/15 * * * *", "2014-02-01 15:45", "2014-02-01 16:15", 4],
79+
["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 16:15", 3],
80+
["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 16:15", 2]
81+
].each do |line, now, expected_next, num|
8282
it "returns #{expected_next} for '#{line}' when now is #{now}" do
8383
parsed_now = parse_date(now)
8484
expected = parse_date(expected_next)
85+
8586
parser = CronParser.new(line)
86-
parser.next(parsed_now).xmlschema.should == expected.xmlschema
87+
88+
expect(parser.next(parsed_now).xmlschema).to eq(expected.xmlschema)
8789
end
8890
it "returns the expected class" do
8991
parsed_now = parse_date(now)
90-
expected = parse_date(expected_next)
92+
9193
parser = CronParser.new(line)
92-
result = parser.next(parsed_now,num)
93-
result.class.to_s.should == (num > 1 ? 'Array' : 'Time')
94+
95+
result = parser.next(parsed_now, num)
96+
expect(result.class.to_s).to eq((num > 1 ? 'Array' : 'Time'))
9497
end
9598
it "returns the expected count" do
9699
parsed_now = parse_date(now)
97-
expected = parse_date(expected_next)
100+
98101
parser = CronParser.new(line)
99-
result = parser.next(parsed_now,num)
102+
103+
result = parser.next(parsed_now, num)
100104
if result.class.to_s == 'Array'
101-
result.size.should == num
105+
expect(result.size).to eq(num)
102106
else
103-
result.class.to_s.should == 'Time'
107+
expect(result.class.to_s).to eq('Time')
104108
end
105109
end
106110
end
@@ -156,15 +160,15 @@ def parse_date(str)
156160
["15-59/15 * * * *", "2014-02-01 15:26", "2014-02-01 15:15"],
157161
["15-59/15 * * * *", "2014-02-01 15:36", "2014-02-01 15:30"],
158162
["15-59/15 * * * *", "2014-02-01 15:45", "2014-02-01 15:30"],
159-
["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 15:45"],
160-
].each do |line, now, expected_next|
161-
it "should return #{expected_next} for '#{line}' when now is #{now}" do
162-
now = parse_date(now)
163-
expected_next = parse_date(expected_next)
163+
["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 15:45"]
164+
].each do |line, now, expected_last|
165+
it "should return #{expected_last} for '#{line}' when now is #{now}" do
166+
parsed_now = parse_date(now)
167+
expected = parse_date(expected_last)
164168

165169
parser = CronParser.new(line)
166170

167-
parser.last(now).should == expected_next
171+
expect(parser.last(parsed_now)).to eq(expected)
168172
end
169173
end
170174
end
@@ -182,7 +186,7 @@ def parse_date(str)
182186
describe "time source" do
183187
it "should use an alternate specified time source" do
184188
ExtendedTime = Class.new(Time)
185-
ExtendedTime.should_receive(:local).once
186-
CronParser.new("* * * * *",ExtendedTime).next
189+
allow(ExtendedTime).to receive(:local).once
190+
CronParser.new("* * * * *", ExtendedTime).next
187191
end
188192
end

0 commit comments

Comments
 (0)