-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathrecur_spec.rb
More file actions
187 lines (152 loc) · 7.15 KB
/
Copy pathrecur_spec.rb
File metadata and controls
187 lines (152 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
require File.dirname(__FILE__) + '/../spec_helper'
require 'timecop'
include IceCube
describe :remaining_occurrences do
it 'should get the proper remaining occurrences from now' do
start_time = Time.now
end_time = Time.local(start_time.year, start_time.month, start_time.day, 23, 59, 59)
schedule = Schedule.new(start_time)
schedule.add_recurrence_rule(Rule.hourly.until(end_time))
schedule.remaining_occurrences(start_time).size.should == 24 - schedule.start_time.hour
end
it 'should get the proper ramining occurrences past the end of the year' do
start_time = Time.now
schedule = Schedule.new(start_time)
schedule.add_recurrence_rule(Rule.hourly.until(start_time + ONE_DAY))
schedule.remaining_occurrences(start_time + 366 * ONE_DAY).size.should == 0
end
it 'should raise an error if there is nothing to stop it' do
schedule = IceCube::Schedule.new
schedule.add_recurrence_rule IceCube::Rule.daily
expect { schedule.remaining_occurrences }.to raise_error ArgumentError
end
end
describe :occurring_between? do
let(:start_time) { Time.local(2012, 7, 7, 7) }
let(:end_time) { start_time + 30 }
let(:schedule) do
IceCube::Schedule.new(start_time, :duration => 30).tap do |schedule|
schedule.rrule IceCube::Rule.daily
end
end
it 'should affirm an occurrence that spans the range exactly' do
schedule.occurring_between?(start_time, end_time).should be_true
end
it 'should affirm a zero-length occurrence at the start of the range' do
schedule.duration = 0
schedule.occurring_between?(start_time, start_time).should be_true
end
it 'should deny a zero-length occurrence at the end of the range' do
schedule.duration = 0
schedule.occurring_between?(end_time, end_time).should be_false
end
it 'should affirm an occurrence entirely contained within the range' do
schedule.occurring_between?(start_time + 1, end_time - 1).should be_true
end
it 'should affirm an occurrence spanning across the start of the range' do
schedule.occurring_between?(start_time - 1, start_time + 1).should be_true
end
it 'should affirm an occurrence spanning across the end of the range' do
schedule.occurring_between?(end_time - 1, end_time + 1).should be_true
end
it 'should affirm an occurrence spanning across the range entirely' do
schedule.occurring_between?(start_time - 1, end_time + 1).should be_true
end
it 'should deny an occurrence before the range' do
schedule.occurring_between?(end_time + 1, end_time + 2).should be_false
end
it 'should deny an occurrence after the range' do
schedule.occurring_between?(start_time - 2, start_time - 1).should be_false
end
end
describe :next_occurrence do
it 'should get the next occurrence from now' do
start_time = Time.local(2010, 10, 10, 10, 0, 0)
schedule = Schedule.new(start_time, :end_time => start_time + 24 * ONE_HOUR)
schedule.add_recurrence_rule(Rule.hourly)
schedule.next_occurrence(schedule.start_time).should == schedule.start_time + 1 * ONE_HOUR
end
it 'should get the next occurrence past the end of the year' do
start_time = Time.now
schedule = Schedule.new(start_time, :end_time => start_time + 24 * ONE_HOUR)
schedule.add_recurrence_rule(Rule.hourly)
schedule.next_occurrence(schedule.end_time + 366 * ONE_DAY).should == schedule.end_time + 366 * ONE_DAY + 1 * ONE_HOUR
end
it 'should be able to use next_occurrence on a never-ending schedule' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_rule Rule.hourly
schedule.next_occurrence(schedule.start_time).should == schedule.start_time + ONE_HOUR
end
it 'should get the next occurrence when a recurrence date is also added' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_time(schedule.start_time + 30 * ONE_MINUTE)
schedule.add_recurrence_rule Rule.hourly
schedule.next_occurrence(schedule.start_time).should == schedule.start_time + 30 * ONE_MINUTE
end
it 'should get the next occurrence and ignore recurrence dates that are before the desired time' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_time(schedule.start_time + 30 * ONE_MINUTE)
schedule.add_recurrence_time(schedule.start_time - 30 * ONE_MINUTE)
schedule.add_recurrence_rule Rule.hourly
schedule.next_occurrence(schedule.start_time).should == schedule.start_time + 30 * ONE_MINUTE
end
it 'should get the next occurrence across the daylight savings time boundary' do
# 2016 daylight savings time cutoff is Sunday March 13
# Time.zone = 'America/New_York'
start_time = Time.zone.local(2016, 3, 13, 0, 0, 0)
expected_next_time = Time.zone.local(2016, 3, 13, 5, 0, 0)
schedule = Schedule.new(start_time)
schedule.add_recurrence_rule(Rule.hourly(interval=4))
Timecop.freeze(start_time) do
schedule.next_occurrence(schedule.start_time).should == expected_next_time
end
end
end
describe :next_occurrences do
it 'should get the next 3 occurrence from now' do
start_time = Time.local(2010, 1, 1, 10, 0, 0)
schedule = Schedule.new(start_time, :end_time => start_time + ONE_HOUR * 24)
schedule.add_recurrence_rule(Rule.hourly)
schedule.next_occurrences(3, start_time).should == [
schedule.start_time + 1 * ONE_HOUR,
schedule.start_time + 2 * ONE_HOUR,
schedule.start_time + 3 * ONE_HOUR]
end
it 'should get the next 3 occurrence past the end of the year' do
schedule = Schedule.new(Time.now, :end_time => Time.now + ONE_HOUR * 24)
schedule.add_recurrence_rule(Rule.hourly.until(Time.now + 365 * ONE_DAY))
schedule.next_occurrences(3, schedule.end_time + 366 * ONE_DAY).should == []
end
it 'should be able to use next_occurrences on a never-ending schedule' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_rule Rule.hourly
schedule.next_occurrences(3, schedule.start_time).should == [
schedule.start_time + 1 * ONE_HOUR,
schedule.start_time + 2 * ONE_HOUR,
schedule.start_time + 3 * ONE_HOUR]
end
it 'should get the next 3 occurrences when a recurrence date is also added' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_rule Rule.hourly
schedule.add_recurrence_time(schedule.start_time + 30 * ONE_MINUTE)
schedule.next_occurrences(3, schedule.start_time).should == [
schedule.start_time + 30 * ONE_MINUTE,
schedule.start_time + 1 * ONE_HOUR,
schedule.start_time + 2 * ONE_HOUR]
end
it 'should get the next 3 occurrences and ignore recurrence dates that are before the desired time' do
schedule = Schedule.new(Time.now)
schedule.add_recurrence_time(schedule.start_time + 30 * ONE_MINUTE)
schedule.add_recurrence_time(schedule.start_time - 30 * ONE_MINUTE)
schedule.add_recurrence_rule Rule.hourly
schedule.next_occurrences(3, schedule.start_time).should == [
schedule.start_time + 30 * ONE_MINUTE,
schedule.start_time + ONE_HOUR,
schedule.start_time + ONE_HOUR * 2]
end
it 'should generate the same comparable time objects (down to millisecond) on two runs' do
schedule = Schedule.new Time.now
schedule.rrule Rule.daily
schedule.next_occurrences(5).should == schedule.next_occurrences(5)
end
end