Browse files

add range_intersection, range_within_other tests

  • Loading branch information...
1 parent e5a9d07 commit 48b5ad301c0614b7789c0519dfb0b1edd50174c3 @ericgj committed Feb 19, 2012
Showing with 277 additions and 4 deletions.
  1. +5 −1 lib/tempr/date_time_range.rb
  2. +125 −0 test/range_intersection.rb
  3. +141 −0 test/range_within_other.rb
  4. +6 −3 test/suite.rb
View
6 lib/tempr/date_time_range.rb
@@ -478,11 +478,13 @@ def time_range(rng=self,utc_offset=nil)
# Set-like operations on two ranges
# true iff self.begin <= other.begin and self.end >= other.end
+ # (with accomodations for exclusive-end ranges)
def within?(other)
range_within_other?(self,other)
end
# true iff other.begin <= self.begin and other.end >= self.end
+ # (with accomodations for exclusive-end ranges)
def subsume?(other)
range_within_other?(other,self)
end
@@ -499,7 +501,7 @@ def intersection_with(other)
other.respond_to?(:exclude_end?) && other.exclude_end?
)
unless max_begin > min_end
- Range.new(max_begin, min_end, excl).extend(self.class)
+ Range.new(max_begin, min_end, excl).extend(DateTimeRange)
end
end
@@ -508,6 +510,8 @@ def intersects?(other)
!!intersection_with(other)
end
+ # ---
+
# convenience wrapper for SubRangeIterator.new(self) { ... }
def build_subrange(&builder)
SubRangeIterator.new(self, &builder)
View
125 test/range_intersection.rb
@@ -0,0 +1,125 @@
+require File.expand_path('test_helper', File.dirname(__FILE__))
+
+module RangeIntersectionTests
+
+ Fixtures = [
+ {
+ :case => "inclusive ranges, other equals self",
+ :subject => (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
+ :other => Date.civil(2012,2,13)..Date.civil(2012,2,17),
+ :expected => Date.civil(2012,2,13)..Date.civil(2012,2,17)
+ },
+ {
+ :case => "inclusive ranges, other within self",
+ :subject => (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
+ :other => Date.civil(2012,2,13)..Date.civil(2012,2,16),
+ :expected => Date.civil(2012,2,13)..Date.civil(2012,2,16)
+ },
+ {
+ :case => "inclusive ranges, self within other",
+ :subject => (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
+ :other => Date.civil(2012,2,13)..Date.civil(2012,2,18),
+ :expected => Date.civil(2012,2,13)..Date.civil(2012,2,17)
+ },
+ {
+ :case => "inclusive ranges, self intersects but not within other",
+ :subject => (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
+ :other => Date.civil(2012,2,14)..Date.civil(2012,2,18),
+ :expected => Date.civil(2012,2,14)..Date.civil(2012,2,17)
+ },
+ {
+ :case => "inclusive ranges, self does not intersect other",
+ :subject => (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
+ :other => Date.civil(2012,2,18)..Date.civil(2012,2,19),
+ :expected => nil
+ },
+ {
+ :case => "exclusive ranges, same endpoint, both exclusive",
+ :subject => (Date.civil(2012,2,13)...
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
+ :other => Date.civil(2012,2,13)...Date.civil(2012,2,17),
+ :expected => Date.civil(2012,2,13)...Date.civil(2012,2,17),
+ :exclusive => true
+ },
+ {
+ :case => "exclusive ranges, same endpoint, other is non-exclusive, self is exclusive",
+ :subject => (Date.civil(2012,2,13)...
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
+ :other => Date.civil(2012,2,13)..Date.civil(2012,2,17),
+ :expected => Date.civil(2012,2,13)...Date.civil(2012,2,17),
+ :exclusive => true
+ },
+ {
+ :case => "exclusive ranges, same endpoint, other is exclusive, self is non-exclusive",
+ :subject => (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
+ :other => Date.civil(2012,2,13)...Date.civil(2012,2,17),
+ :expected => Date.civil(2012,2,13)...Date.civil(2012,2,17),
+ :exclusive => true
+ },
+ {
+ :case => "exclusive ranges, different endpoint, both exclusive",
+ :subject => (Date.civil(2012,2,13)...
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
+ :other => Date.civil(2012,2,14)...Date.civil(2012,2,18),
+ :expected => Date.civil(2012,2,14)...Date.civil(2012,2,17),
+ :exclusive => true
+ },
+ {
+ :case => "exclusive ranges, different endpoint, other is non-exclusive, self is exclusive",
+ :subject => (Date.civil(2012,2,13)...
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
+ :other => Date.civil(2012,2,12)..Date.civil(2012,2,16),
+ :expected => Date.civil(2012,2,13)..Date.civil(2012,2,16),
+ :exclusive => true
+ },
+ {
+ :case => "exclusive ranges, different endpoint, other is exclusive, self is non-exclusive",
+ :subject => (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
+ :other => Date.civil(2012,2,11)...Date.civil(2012,2,15),
+ :expected => Date.civil(2012,2,13)...Date.civil(2012,2,15),
+ :exclusive => true
+ }
+ ]
+
+
+
+ describe "DateTimeRange#intersection_with, date ranges" do
+
+ Fixtures.each do |fixture|
+
+ describe fixture[:case] do
+ let(:subject) { fixture[:subject] }
+ let(:other) { fixture[:other] }
+ let(:expected) { fixture[:expected] }
+
+ it 'should return expected range' do
+ actual = subject.intersection_with(other)
+ if expected.nil?
+ assert_nil actual
+ else
+ assert_equal expected.begin, actual.begin
+ assert_equal expected.end, actual.end
+ end
+ end
+
+ if fixture[:exclusive]
+ it 'should return expected exclusivity' do
+ actual = subject.intersection_with(other)
+ assert_equal expected.exclude_end?, actual.exclude_end?
+ end
+ end
+
+ end
+
+ end
+
+ end
+
+end
View
141 test/range_within_other.rb
@@ -0,0 +1,141 @@
+require File.expand_path('test_helper', File.dirname(__FILE__))
+
+module RangeWithinOtherTests
+
+ describe "DateTimeRange#within, non-exclusive date ranges" do
+
+ describe "other equals self" do
+ let(:subject) { (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
+ }
+ let(:other) { Date.civil(2012,2,13)..Date.civil(2012,2,17) }
+
+ it "within should return true" do
+ assert_equal true, subject.within?(other)
+ end
+
+ it "subsume should return true" do
+ assert_equal true, subject.subsume?(other)
+ end
+
+ end
+
+ describe "other within self" do
+ let(:subject) { (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
+ }
+ let(:other) { Date.civil(2012,2,13)..Date.civil(2012,2,16) }
+
+ it "within should return false" do
+ assert_equal false, subject.within?(other)
+ end
+
+ it "subsume should return true" do
+ assert_equal true, subject.subsume?(other)
+ end
+
+ end
+
+ describe "self within other" do
+ let(:subject) { (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
+ }
+ let(:other) { Date.civil(2012,2,13)..Date.civil(2012,2,18) }
+
+ it "within should return true" do
+ assert_equal true, subject.within?(other)
+ end
+
+ it "subsume should return false" do
+ assert_equal false, subject.subsume?(other)
+ end
+
+ end
+
+ describe "self intersects but not within other" do
+ let(:subject) { (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
+ }
+ let(:other) { Date.civil(2012,2,14)..Date.civil(2012,2,18) }
+
+ it "within should return false" do
+ assert_equal false, subject.within?(other)
+ end
+
+ it "subsume should return false" do
+ assert_equal false, subject.subsume?(other)
+ end
+
+ end
+
+ describe "self does not intersect other" do
+ let(:subject) { (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
+ }
+ let(:other) { Date.civil(2012,2,18)..Date.civil(2012,2,19) }
+
+ it "within should return false" do
+ assert_equal false, subject.within?(other)
+ end
+
+ it "subsume should return false" do
+ assert_equal false, subject.subsume?(other)
+ end
+
+ end
+
+ end
+
+ describe "DateTimeRange#within, exclusive date ranges" do
+
+ describe "same endpoint, both exclusive" do
+ let(:subject) { (Date.civil(2012,2,13)...
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
+ }
+ let(:other) { Date.civil(2012,2,13)...Date.civil(2012,2,17) }
+
+ it "within should return true" do
+ assert_equal true, subject.within?(other)
+ end
+
+ it "subsume should return true" do
+ assert_equal true, subject.subsume?(other)
+ end
+
+ end
+
+ describe "same endpoint, other is non-exclusive, self is exclusive" do
+ let(:subject) { (Date.civil(2012,2,13)...
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
+ }
+ let(:other) { Date.civil(2012,2,13)..Date.civil(2012,2,17) }
+
+ it "within should return true" do
+ assert_equal true, subject.within?(other)
+ end
+
+ it "subsume should return false" do
+ assert_equal false, subject.subsume?(other)
+ end
+
+ end
+
+ describe "same endpoint, other is exclusive, self is non-exclusive" do
+ let(:subject) { (Date.civil(2012,2,13)..
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
+ }
+ let(:other) { Date.civil(2012,2,13)...Date.civil(2012,2,17) }
+
+ it "within should return false" do
+ assert_equal false, subject.within?(other)
+ end
+
+ it "subsume should return true" do
+ assert_equal true, subject.subsume?(other)
+ end
+
+ end
+
+ end
+
+end
View
9 test/suite.rb
@@ -1,3 +1,6 @@
-%w[ time_subrange each_time_of_day ].each do |test|
- require File.expand_path(test,File.dirname(__FILE__))
-end
+%w[ time_subrange
+ each_time_of_day
+ range_within_other
+ ].each do |test|
+ require File.expand_path(test,File.dirname(__FILE__))
+ end

0 comments on commit 48b5ad3

Please sign in to comment.