summaryrefslogtreecommitdiff
path: root/spec/ruby/core/range
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-01-20 20:38:57 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-01-20 20:38:57 +0000
commit6204e0804b24f1675b49d5880da014411bcfb831 (patch)
treece6c00bf078fc416936ca3cdc972b9b3c1c78dae /spec/ruby/core/range
parent58573c33e4720315ed27491e31dcc22892e1ce95 (diff)
Update to ruby/spec@35a9fba
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/range')
-rw-r--r--spec/ruby/core/range/case_compare_spec.rb12
-rw-r--r--spec/ruby/core/range/fixtures/classes.rb22
-rw-r--r--spec/ruby/core/range/new_spec.rb26
-rw-r--r--spec/ruby/core/range/percent_spec.rb18
-rw-r--r--spec/ruby/core/range/shared/cover.rb60
-rw-r--r--spec/ruby/core/range/step_spec.rb35
6 files changed, 159 insertions, 14 deletions
diff --git a/spec/ruby/core/range/case_compare_spec.rb b/spec/ruby/core/range/case_compare_spec.rb
index 9a33c5b73b..37d8cc4677 100644
--- a/spec/ruby/core/range/case_compare_spec.rb
+++ b/spec/ruby/core/range/case_compare_spec.rb
@@ -9,12 +9,20 @@ describe "Range#===" do
range.should_receive(:include?).with(2).and_return(:true)
(range === 2).should == :true
end
+
+ it "requires #succ method to be implemented" do
+ range = RangeSpecs::WithoutSucc.new(0)..RangeSpecs::WithoutSucc.new(10)
+
+ lambda do
+ range === RangeSpecs::WithoutSucc.new(2)
+ end.should raise_error(TypeError, /can't iterate from/)
+ end
end
ruby_version_is "2.6" do
it "returns the result of calling #cover? on self" do
- range = RangeSpecs::Custom.new(0)..RangeSpecs::Custom.new(10)
- (range === RangeSpecs::Custom.new(2)).should == true
+ range = RangeSpecs::WithoutSucc.new(0)..RangeSpecs::WithoutSucc.new(10)
+ (range === RangeSpecs::WithoutSucc.new(2)).should == true
end
end
end
diff --git a/spec/ruby/core/range/fixtures/classes.rb b/spec/ruby/core/range/fixtures/classes.rb
index b62704ca39..3a1df010b2 100644
--- a/spec/ruby/core/range/fixtures/classes.rb
+++ b/spec/ruby/core/range/fixtures/classes.rb
@@ -40,6 +40,28 @@ module RangeSpecs
end
end
+ class WithoutSucc
+ include Comparable
+ attr_reader :n
+
+ def initialize(n)
+ @n = n
+ end
+
+ def eql?(other)
+ inspect.eql? other.inspect
+ end
+ alias :== :eql?
+
+ def inspect
+ "WithoutSucc(#{@n})"
+ end
+
+ def <=>(other)
+ @n <=> other.n
+ end
+ end
+
class Xs < Custom # represent a string of 'x's
def succ
Xs.new(@length + 1)
diff --git a/spec/ruby/core/range/new_spec.rb b/spec/ruby/core/range/new_spec.rb
index 26ea12867d..c110687163 100644
--- a/spec/ruby/core/range/new_spec.rb
+++ b/spec/ruby/core/range/new_spec.rb
@@ -42,4 +42,30 @@ describe "Range.new" do
-> { Range.new(a, b) }.should raise_error(RangeSpecs::ComparisonError)
end
end
+
+ describe "endless range" do
+ it "does not allow range without left boundary" do
+ -> { Range.new(nil, 1) }.should raise_error(ArgumentError, /bad value for range/)
+ end
+
+ ruby_version_is ""..."2.6" do
+ it "does not allow range without right boundary" do
+ -> { Range.new(1, nil) }.should raise_error(ArgumentError, /bad value for range/)
+ end
+ end
+
+ ruby_version_is "2.6" do
+ it "allows endless right boundary" do
+ range = Range.new(1, nil)
+ range.end.should == nil
+ end
+
+ it "distinguishes ranges with included and excluded right boundary" do
+ range_exclude = Range.new(1, nil, true)
+ range_include = Range.new(1, nil, false)
+
+ range_exclude.should_not == range_include
+ end
+ end
+ end
end
diff --git a/spec/ruby/core/range/percent_spec.rb b/spec/ruby/core/range/percent_spec.rb
new file mode 100644
index 0000000000..41badd4f72
--- /dev/null
+++ b/spec/ruby/core/range/percent_spec.rb
@@ -0,0 +1,18 @@
+require_relative '../../spec_helper'
+
+ruby_version_is "2.6" do
+ describe "Range#%" do
+ it "works as a Range#step" do
+ aseq = (1..10) % 2
+ aseq.class.should == Enumerator::ArithmeticSequence
+ aseq.begin.should == 1
+ aseq.end.should == 10
+ aseq.step.should == 2
+ aseq.to_a.should == [1, 3, 5, 7, 9]
+ end
+
+ it "produces an arithmetic sequence with a percent sign in #inspect" do
+ ((1..10) % 2).inspect.should == "((1..10).%(2))"
+ end
+ end
+end
diff --git a/spec/ruby/core/range/shared/cover.rb b/spec/ruby/core/range/shared/cover.rb
index b2de86531d..7d2367d712 100644
--- a/spec/ruby/core/range/shared/cover.rb
+++ b/spec/ruby/core/range/shared/cover.rb
@@ -90,4 +90,64 @@ describe :range_cover, shared: true do
end
end
end
+
+ ruby_version_is "2.6" do
+ context "range argument" do
+ it "accepts range argument" do
+ (0..10).send(@method, (3..7)).should be_true
+ (0..10).send(@method, (3..15)).should be_false
+ (0..10).send(@method, (-2..7)).should be_false
+
+ (1.1..7.9).send(@method, (2.5..6.5)).should be_true
+ (1.1..7.9).send(@method, (2.5..8.5)).should be_false
+ (1.1..7.9).send(@method, (0.5..6.5)).should be_false
+
+ ('c'..'i').send(@method, ('d'..'f')).should be_true
+ ('c'..'i').send(@method, ('d'..'z')).should be_false
+ ('c'..'i').send(@method, ('a'..'f')).should be_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 be_true
+ range_10_100.send(@method, range_20_110).should be_false
+ range_10_100.send(@method, range_0_90).should be_false
+ end
+
+ it "supports boundaries of different comparable types" do
+ (0..10).send(@method, (3.1..7.9)).should be_true
+ (0..10).send(@method, (3.1..15.9)).should be_false
+ (0..10).send(@method, (-2.1..7.9)).should be_false
+ end
+
+ it "returns false if types are not comparable" do
+ (0..10).send(@method, ('a'..'z')).should be_false
+ (0..10).send(@method, (RangeSpecs::TenfoldSucc.new(0)..RangeSpecs::TenfoldSucc.new(100))).should be_false
+ end
+
+ it "honors exclusion of right boundary (:exclude_end option)" do
+ # Integer
+ (0..10).send(@method, (0..10)).should be_true
+ (0...10).send(@method, (0...10)).should be_true
+
+ (0..10).send(@method, (0...10)).should be_true
+ (0...10).send(@method, (0..10)).should be_false
+
+ (0...11).send(@method, (0..10)).should be_true
+ (0..10).send(@method, (0...11)).should be_true
+
+ # Float
+ (0..10.1).send(@method, (0..10.1)).should be_true
+ (0...10.1).send(@method, (0...10.1)).should be_true
+
+ (0..10.1).send(@method, (0...10.1)).should be_true
+ (0...10.1).send(@method, (0..10.1)).should be_false
+
+ (0...11.1).send(@method, (0..10.1)).should be_true
+ (0..10.1).send(@method, (0...11.1)).should be_false
+ end
+ end
+ end
end
diff --git a/spec/ruby/core/range/step_spec.rb b/spec/ruby/core/range/step_spec.rb
index cdf66e4565..818207974a 100644
--- a/spec/ruby/core/range/step_spec.rb
+++ b/spec/ruby/core/range/step_spec.rb
@@ -1,21 +1,10 @@
require_relative '../../spec_helper'
describe "Range#step" do
- step_enum_class = Enumerator
- ruby_version_is "2.6" do
- step_enum_class = Enumerator::ArithmeticSequence
- end
-
before :each do
ScratchPad.record []
end
- it "returns an #{step_enum_class} when no block is given" do
- enum = (1..10).step(4)
- enum.should be_an_instance_of(step_enum_class)
- enum.to_a.should eql([1, 5, 9])
- end
-
it "returns self" do
r = 1..2
r.step { }.should equal(r)
@@ -268,7 +257,7 @@ describe "Range#step" do
end
describe "when no block is given" do
- describe "returned #{step_enum_class}" do
+ describe "returned Enumerator" do
describe "size" do
it "raises a TypeError if step does not respond to #to_int" do
obj = mock("Range#step non-integer")
@@ -363,6 +352,28 @@ describe "Range#step" do
enum.size.should == nil
end
end
+
+ describe "type" do
+ ruby_version_is ""..."2.6" do
+ it "returns an instance of Enumerator" do
+ (1..10).step.class.should == Enumerator
+ end
+ end
+
+ ruby_version_is "2.6" do
+ context "when both begin and end are numerics" do
+ it "returns an instance of Enumerator::ArithmeticSequence" do
+ (1..10).step.class.should == Enumerator::ArithmeticSequence
+ end
+ end
+
+ context "when begin and end are not numerics" do
+ it "returns an instance of Enumerator" do
+ ("a".."z").step.class.should == Enumerator
+ end
+ end
+ end
+ end
end
end
end