diff options
Diffstat (limited to 'spec/ruby/core/range/shared/cover.rb')
| -rw-r--r-- | spec/ruby/core/range/shared/cover.rb | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/spec/ruby/core/range/shared/cover.rb b/spec/ruby/core/range/shared/cover.rb new file mode 100644 index 0000000000..189f3da4bf --- /dev/null +++ b/spec/ruby/core/range/shared/cover.rb @@ -0,0 +1,193 @@ +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' + +describe :range_cover, shared: true do + it "uses the range element's <=> to make the comparison" do + a = mock('a') + a.should_receive(:<=>).twice.and_return(-1,-1) + (a..'z').send(@method, 'b').should == true + end + + it "uses a continuous inclusion test" do + ('a'..'f').send(@method, 'aa').should == true + ('a'..'f').send(@method, 'babe').should == true + ('a'..'f').send(@method, 'baby').should == true + ('a'..'f').send(@method, 'ga').should == false + (-10..-2).send(@method, -2.5).should == true + end + + describe "on string elements" do + it "returns true if other is matched by element.succ" do + ('a'..'c').send(@method, 'b').should == true + ('a'...'c').send(@method, 'b').should == true + end + + it "returns true if other is not matched by element.succ" do + ('a'..'c').send(@method, 'bc').should == true + ('a'...'c').send(@method, 'bc').should == true + end + end + + describe "with weird succ" do + describe "when included end value" do + before :each do + @range = RangeSpecs::TenfoldSucc.new(1)..RangeSpecs::TenfoldSucc.new(99) + end + + it "returns false if other is less than first element" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(0)).should == false + end + + it "returns true if other is equal as first element" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should == true + end + + it "returns true if other is matched by element.succ" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should == true + end + + it "returns true if other is not matched by element.succ" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should == true + end + + it "returns true if other is equal as last element but not matched by element.succ" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(99)).should == true + end + + it "returns false if other is greater than last element but matched by element.succ" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(100)).should == false + end + end + + describe "when excluded end value" do + before :each do + @range = RangeSpecs::TenfoldSucc.new(1)...RangeSpecs::TenfoldSucc.new(99) + end + + it "returns false if other is less than first element" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(0)).should == false + end + + it "returns true if other is equal as first element" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should == true + end + + it "returns true if other is matched by element.succ" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should == true + end + + it "returns true if other is not matched by element.succ" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should == true + end + + it "returns false if other is equal as last element but not matched by element.succ" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(99)).should == false + end + + it "returns false if other is greater than last element but matched by element.succ" do + @range.send(@method, RangeSpecs::TenfoldSucc.new(100)).should == false + end + end + end +end + +describe :range_cover_subrange, shared: true do + context "range argument" do + it "accepts range argument" do + (0..10).send(@method, (3..7)).should == true + (0..10).send(@method, (3..15)).should == false + (0..10).send(@method, (-2..7)).should == false + + (1.1..7.9).send(@method, (2.5..6.5)).should == true + (1.1..7.9).send(@method, (2.5..8.5)).should == false + (1.1..7.9).send(@method, (0.5..6.5)).should == false + + ('c'..'i').send(@method, ('d'..'f')).should == true + ('c'..'i').send(@method, ('d'..'z')).should == false + ('c'..'i').send(@method, ('a'..'f')).should == false + + range_10_100 = RangeSpecs::TenfoldSucc.new(10)..RangeSpecs::TenfoldSucc.new(100) + range_20_90 = RangeSpecs::TenfoldSucc.new(20)..RangeSpecs::TenfoldSucc.new(90) + range_20_110 = RangeSpecs::TenfoldSucc.new(20)..RangeSpecs::TenfoldSucc.new(110) + range_0_90 = RangeSpecs::TenfoldSucc.new(0)..RangeSpecs::TenfoldSucc.new(90) + + range_10_100.send(@method, range_20_90).should == true + range_10_100.send(@method, range_20_110).should == false + range_10_100.send(@method, range_0_90).should == false + end + + it "supports boundaries of different comparable types" do + (0..10).send(@method, (3.1..7.9)).should == true + (0..10).send(@method, (3.1..15.9)).should == false + (0..10).send(@method, (-2.1..7.9)).should == false + end + + it "returns false if types are not comparable" do + (0..10).send(@method, ('a'..'z')).should == false + (0..10).send(@method, (RangeSpecs::TenfoldSucc.new(0)..RangeSpecs::TenfoldSucc.new(100))).should == false + end + + it "honors exclusion of right boundary (:exclude_end option)" do + # Integer + (0..10).send(@method, (0..10)).should == true + (0...10).send(@method, (0...10)).should == true + + (0..10).send(@method, (0...10)).should == true + (0...10).send(@method, (0..10)).should == false + + (0...11).send(@method, (0..10)).should == true + (0..10).send(@method, (0...11)).should == true + + # Float + (0..10.1).send(@method, (0..10.1)).should == true + (0...10.1).send(@method, (0...10.1)).should == true + + (0..10.1).send(@method, (0...10.1)).should == true + (0...10.1).send(@method, (0..10.1)).should == false + + (0...11.1).send(@method, (0..10.1)).should == true + (0..10.1).send(@method, (0...11.1)).should == false + end + end + + it "allows self to be a beginless range" do + (...10).send(@method, (3..7)).should == true + (...10).send(@method, (3..15)).should == false + + (..7.9).send(@method, (2.5..6.5)).should == true + (..7.9).send(@method, (2.5..8.5)).should == false + + (..'i').send(@method, ('d'..'f')).should == true + (..'i').send(@method, ('d'..'z')).should == false + end + + it "allows self to be a endless range" do + eval("(0...)").send(@method, (3..7)).should == true + eval("(5...)").send(@method, (3..15)).should == false + + eval("(1.1..)").send(@method, (2.5..6.5)).should == true + eval("(3.3..)").send(@method, (2.5..8.5)).should == false + + eval("('a'..)").send(@method, ('d'..'f')).should == true + eval("('p'..)").send(@method, ('d'..'z')).should == false + end + + it "accepts beginless range argument" do + (..10).send(@method, (...10)).should == true + (0..10).send(@method, (...10)).should == false + + (1.1..7.9).send(@method, (...10.5)).should == false + + ('c'..'i').send(@method, (..'i')).should == false + end + + it "accepts endless range argument" do + eval("(0..)").send(@method, eval("(0...)")).should == true + (0..10).send(@method, eval("(0...)")).should == false + + (1.1..7.9).send(@method, eval("(0.8...)")).should == false + + ('c'..'i').send(@method, eval("('a'..)")).should == false + end +end |
