diff options
Diffstat (limited to 'spec/ruby/core/range/shared/cover.rb')
| -rw-r--r-- | spec/ruby/core/range/shared/cover.rb | 150 |
1 files changed, 125 insertions, 25 deletions
diff --git a/spec/ruby/core/range/shared/cover.rb b/spec/ruby/core/range/shared/cover.rb index 1d9c008a87..189f3da4bf 100644 --- a/spec/ruby/core/range/shared/cover.rb +++ b/spec/ruby/core/range/shared/cover.rb @@ -1,31 +1,31 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) +# 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 be_true + (a..'z').send(@method, 'b').should == true end it "uses a continuous inclusion test" do - ('a'..'f').send(@method, 'aa').should be_true - ('a'..'f').send(@method, 'babe').should be_true - ('a'..'f').send(@method, 'baby').should be_true - ('a'..'f').send(@method, 'ga').should be_false - (-10..-2).send(@method, -2.5).should be_true + ('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 be_true - ('a'...'c').send(@method, 'b').should be_true + ('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 be_true - ('a'...'c').send(@method, 'bc').should be_true + ('a'..'c').send(@method, 'bc').should == true + ('a'...'c').send(@method, 'bc').should == true end end @@ -36,27 +36,27 @@ describe :range_cover, shared: true do end it "returns false if other is less than first element" do - @range.send(@method, RangeSpecs::TenfoldSucc.new(0)).should be_false + @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 be_true + @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 be_true + @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 be_true + @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 be_true + @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 be_false + @range.send(@method, RangeSpecs::TenfoldSucc.new(100)).should == false end end @@ -66,28 +66,128 @@ describe :range_cover, shared: true do end it "returns false if other is less than first element" do - @range.send(@method, RangeSpecs::TenfoldSucc.new(0)).should be_false + @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 be_true + @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 be_true + @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 be_true + @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 be_false + @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 be_false + @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 |
