Skip to content

Commit

Permalink
Fix occurring_between? to capture start of range
Browse files Browse the repository at this point in the history
  • Loading branch information
avit authored and rlivsey committed Jun 18, 2013
1 parent 0259ba2 commit 0a15739
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
16 changes: 10 additions & 6 deletions lib/ice_cube/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,20 @@ def occurrences_between(begin_time, closing_time)
find_occurrences(begin_time, closing_time)
end

# Return a boolean indicating if an occurrence falls between
# two times
# Return a boolean indicating if an occurrence falls between two times
def occurs_between?(begin_time, closing_time)
!find_occurrences(begin_time, closing_time, 1).empty?
end

# Return a boolean indicating if an occurrence is occurring between
# two times, inclusive
def occurring_between?(begin_time, closing_time)
occurs_between?(begin_time - duration + 1, closing_time + duration - 1)
# Return a boolean indicating if an occurrence is occurring between two
# times, inclusive of its duration. This counts zero-length occurrences
# that intersect the start of the range and within the range, but not
# occurrences at the end of the range since none of their duration
# intersects the range.
def occurring_between?(opening_time, closing_time)
opening_time = opening_time - duration
closing_time = closing_time - 1 if duration > 0
occurs_between?(opening_time, closing_time)
end

# Return a boolean indicating if an occurrence falls on a certain date
Expand Down
40 changes: 28 additions & 12 deletions spec/examples/recur_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,50 @@
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 occur when the range is entirely contained' do
schedule.occurring_between?(start_time + 1, start_time + 20).should be_true
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 occur when the range is offset left' do
schedule.occurring_between?(start_time - 60, start_time - 29).should be_true
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 occur when the range is offset right' do
schedule.occurring_between?(start_time + 29, start_time + 40).should be_true
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 occur when the range is overflowing' do
schedule.occurring_between?(start_time - 29, start_time + 40).should be_true
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 be false when the range starts after the duration expires' do
schedule.occurring_between?(start_time + 30, start_time + 40).should be_false
it 'should deny an occurrence before the range' do
schedule.occurring_between?(end_time + 1, end_time + 2).should be_false
end

it 'should be false when the range ends before the start' do
schedule.occurring_between?(start_time - 40, start_time - 30).should be_false
it 'should deny an occurrence after the range' do
schedule.occurring_between?(start_time - 2, start_time - 1).should be_false
end

end
Expand Down

0 comments on commit 0a15739

Please sign in to comment.