summaryrefslogtreecommitdiff
path: root/spec/ruby/core/range
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/range')
-rw-r--r--spec/ruby/core/range/begin_spec.rb6
-rw-r--r--spec/ruby/core/range/bsearch_spec.rb466
-rw-r--r--spec/ruby/core/range/case_compare_spec.rb19
-rw-r--r--spec/ruby/core/range/clone_spec.rb26
-rw-r--r--spec/ruby/core/range/count_spec.rb12
-rw-r--r--spec/ruby/core/range/cover_spec.rb14
-rw-r--r--spec/ruby/core/range/dup_spec.rb23
-rw-r--r--spec/ruby/core/range/each_spec.rb101
-rw-r--r--spec/ruby/core/range/end_spec.rb6
-rw-r--r--spec/ruby/core/range/eql_spec.rb10
-rw-r--r--spec/ruby/core/range/equal_value_spec.rb18
-rw-r--r--spec/ruby/core/range/exclude_end_spec.rb19
-rw-r--r--spec/ruby/core/range/first_spec.rb53
-rw-r--r--spec/ruby/core/range/fixtures/classes.rb90
-rw-r--r--spec/ruby/core/range/frozen_spec.rb25
-rw-r--r--spec/ruby/core/range/hash_spec.rb24
-rw-r--r--spec/ruby/core/range/include_spec.rb14
-rw-r--r--spec/ruby/core/range/initialize_spec.rb41
-rw-r--r--spec/ruby/core/range/inspect_spec.rb29
-rw-r--r--spec/ruby/core/range/last_spec.rb57
-rw-r--r--spec/ruby/core/range/max_spec.rb115
-rw-r--r--spec/ruby/core/range/member_spec.rb10
-rw-r--r--spec/ruby/core/range/min_spec.rb88
-rw-r--r--spec/ruby/core/range/minmax_spec.rb130
-rw-r--r--spec/ruby/core/range/new_spec.rb77
-rw-r--r--spec/ruby/core/range/overlap_spec.rb89
-rw-r--r--spec/ruby/core/range/percent_spec.rb16
-rw-r--r--spec/ruby/core/range/range_spec.rb7
-rw-r--r--spec/ruby/core/range/reverse_each_spec.rb103
-rw-r--r--spec/ruby/core/range/shared/begin.rb10
-rw-r--r--spec/ruby/core/range/shared/cover.rb193
-rw-r--r--spec/ruby/core/range/shared/cover_and_include.rb86
-rw-r--r--spec/ruby/core/range/shared/end.rb10
-rw-r--r--spec/ruby/core/range/shared/equal_value.rb51
-rw-r--r--spec/ruby/core/range/shared/include.rb91
-rw-r--r--spec/ruby/core/range/size_spec.rb92
-rw-r--r--spec/ruby/core/range/step_spec.rb683
-rw-r--r--spec/ruby/core/range/to_a_spec.rb39
-rw-r--r--spec/ruby/core/range/to_s_spec.rb23
-rw-r--r--spec/ruby/core/range/to_set_spec.rb55
40 files changed, 3021 insertions, 0 deletions
diff --git a/spec/ruby/core/range/begin_spec.rb b/spec/ruby/core/range/begin_spec.rb
new file mode 100644
index 0000000000..ab82b45b7e
--- /dev/null
+++ b/spec/ruby/core/range/begin_spec.rb
@@ -0,0 +1,6 @@
+require_relative '../../spec_helper'
+require_relative 'shared/begin'
+
+describe "Range#begin" do
+ it_behaves_like :range_begin, :begin
+end
diff --git a/spec/ruby/core/range/bsearch_spec.rb b/spec/ruby/core/range/bsearch_spec.rb
new file mode 100644
index 0000000000..5254ab756c
--- /dev/null
+++ b/spec/ruby/core/range/bsearch_spec.rb
@@ -0,0 +1,466 @@
+require_relative '../../spec_helper'
+require_relative '../enumerable/shared/enumeratorized'
+
+describe "Range#bsearch" do
+ it "returns an Enumerator when not passed a block" do
+ (0..1).bsearch.should be_an_instance_of(Enumerator)
+ end
+
+ it_behaves_like :enumeratorized_with_unknown_size, :bsearch, (1..3)
+
+ it "raises a TypeError if the block returns an Object" do
+ -> { (0..1).bsearch { Object.new } }.should raise_error(TypeError, "wrong argument type Object (must be numeric, true, false or nil)")
+ end
+
+ it "raises a TypeError if the block returns a String and boundaries are Integer values" do
+ -> { (0..1).bsearch { "1" } }.should raise_error(TypeError, "wrong argument type String (must be numeric, true, false or nil)")
+ end
+
+ it "raises a TypeError if the block returns a String and boundaries are Float values" do
+ -> { (0.0..1.0).bsearch { "1" } }.should raise_error(TypeError, "wrong argument type String (must be numeric, true, false or nil)")
+ end
+
+ it "raises a TypeError if the Range has Object values" do
+ value = mock("range bsearch")
+ r = Range.new value, value
+
+ -> { r.bsearch { true } }.should raise_error(TypeError, "can't do binary search for MockObject")
+ end
+
+ it "raises a TypeError if the Range has String values" do
+ -> { ("a".."e").bsearch { true } }.should raise_error(TypeError, "can't do binary search for String")
+ end
+
+ it "raises TypeError when non-Numeric begin/end and block not passed" do
+ -> { ("a".."e").bsearch }.should raise_error(TypeError, "can't do binary search for String")
+ end
+
+ context "with Integer values" do
+ context "with a block returning true or false" do
+ it "returns nil if the block returns false for every element" do
+ (0...3).bsearch { |x| x > 3 }.should be_nil
+ end
+
+ it "returns nil if the block returns nil for every element" do
+ (0..3).bsearch { |x| nil }.should be_nil
+ end
+
+ it "returns minimum element if the block returns true for every element" do
+ (-2..4).bsearch { |x| x < 4 }.should == -2
+ end
+
+ it "returns the smallest element for which block returns true" do
+ (0..4).bsearch { |x| x >= 2 }.should == 2
+ (-1..4).bsearch { |x| x >= 1 }.should == 1
+ end
+
+ it "returns the last element if the block returns true for the last element" do
+ (0..4).bsearch { |x| x >= 4 }.should == 4
+ (0...4).bsearch { |x| x >= 3 }.should == 3
+ end
+ end
+
+ context "with a block returning negative, zero, positive numbers" do
+ it "returns nil if the block returns less than zero for every element" do
+ (0..3).bsearch { |x| x <=> 5 }.should be_nil
+ end
+
+ it "returns nil if the block returns greater than zero for every element" do
+ (0..3).bsearch { |x| x <=> -1 }.should be_nil
+
+ end
+
+ it "returns nil if the block never returns zero" do
+ (0..3).bsearch { |x| x < 2 ? 1 : -1 }.should be_nil
+ end
+
+ it "accepts (+/-)Float::INFINITY from the block" do
+ (0..4).bsearch { |x| Float::INFINITY }.should be_nil
+ (0..4).bsearch { |x| -Float::INFINITY }.should be_nil
+ end
+
+ it "returns an element at an index for which block returns 0.0" do
+ result = (0..4).bsearch { |x| x < 2 ? 1.0 : x > 2 ? -1.0 : 0.0 }
+ result.should == 2
+ end
+
+ it "returns an element at an index for which block returns 0" do
+ result = (0..4).bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 }
+ [1, 2].should include(result)
+ end
+ end
+
+ it "returns nil for empty ranges" do
+ (0...0).bsearch { true }.should == nil
+ (0...0).bsearch { false }.should == nil
+ (0...0).bsearch { 1 }.should == nil
+ (0...0).bsearch { 0 }.should == nil
+ (0...0).bsearch { -1 }.should == nil
+
+ (4..2).bsearch { true }.should == nil
+ (4..2).bsearch { 1 }.should == nil
+ (4..2).bsearch { 0 }.should == nil
+ (4..2).bsearch { -1 }.should == nil
+ end
+
+ it "returns enumerator when block not passed" do
+ (0...3).bsearch.kind_of?(Enumerator).should == true
+ end
+ end
+
+ context "with Float values" do
+ context "with a block returning true or false" do
+ it "returns nil if the block returns false for every element" do
+ (0.1...2.3).bsearch { |x| x > 3 }.should be_nil
+ end
+
+ it "returns nil if the block returns nil for every element" do
+ (-0.0..2.3).bsearch { |x| nil }.should be_nil
+ end
+
+ it "returns minimum element if the block returns true for every element" do
+ (-0.2..4.8).bsearch { |x| x < 5 }.should == -0.2
+ end
+
+ it "returns the smallest element for which block returns true" do
+ (0..4.2).bsearch { |x| x >= 2 }.should == 2
+ (-1.2..4.3).bsearch { |x| x >= 1 }.should == 1
+ end
+
+ it "returns a boundary element if appropriate" do
+ (1.0..3.0).bsearch { |x| x >= 3.0 }.should == 3.0
+ (1.0...3.0).bsearch { |x| x >= 3.0.prev_float }.should == 3.0.prev_float
+ (1.0..3.0).bsearch { |x| x >= 1.0 }.should == 1.0
+ (1.0...3.0).bsearch { |x| x >= 1.0 }.should == 1.0
+ end
+
+ it "works with infinity bounds" do
+ inf = Float::INFINITY
+ (0..inf).bsearch { |x| x == inf }.should == inf
+ (0...inf).bsearch { |x| x == inf }.should == nil
+ (-inf..0).bsearch { |x| x != -inf }.should == -Float::MAX
+ (-inf...0).bsearch { |x| x != -inf }.should == -Float::MAX
+ (inf..inf).bsearch { |x| true }.should == inf
+ (inf...inf).bsearch { |x| true }.should == nil
+ (-inf..-inf).bsearch { |x| true }.should == -inf
+ (-inf...-inf).bsearch { |x| true }.should == nil
+ (inf..0).bsearch { true }.should == nil
+ (inf...0).bsearch { true }.should == nil
+ (0..-inf).bsearch { true }.should == nil
+ (0...-inf).bsearch { true }.should == nil
+ (inf..-inf).bsearch { true }.should == nil
+ (inf...-inf).bsearch { true }.should == nil
+ (0..inf).bsearch { |x| x >= 3 }.should == 3.0
+ (0...inf).bsearch { |x| x >= 3 }.should == 3.0
+ (-inf..0).bsearch { |x| x >= -3 }.should == -3.0
+ (-inf...0).bsearch { |x| x >= -3 }.should == -3.0
+ (-inf..inf).bsearch { |x| x >= 3 }.should == 3.0
+ (-inf...inf).bsearch { |x| x >= 3 }.should == 3.0
+ (0..inf).bsearch { |x| x >= Float::MAX }.should == Float::MAX
+ (0...inf).bsearch { |x| x >= Float::MAX }.should == Float::MAX
+ end
+ end
+
+ context "with a block returning negative, zero, positive numbers" do
+ it "returns nil if the block returns less than zero for every element" do
+ (-2.0..3.2).bsearch { |x| x <=> 5 }.should be_nil
+ end
+
+ it "returns nil if the block returns greater than zero for every element" do
+ (0.3..3.0).bsearch { |x| x <=> -1 }.should be_nil
+ end
+
+ it "returns nil if the block never returns zero" do
+ (0.2..2.3).bsearch { |x| x < 2 ? 1 : -1 }.should be_nil
+ end
+
+ it "accepts (+/-)Float::INFINITY from the block" do
+ (0.1..4.5).bsearch { |x| Float::INFINITY }.should be_nil
+ (-5.0..4.0).bsearch { |x| -Float::INFINITY }.should be_nil
+ end
+
+ it "returns an element at an index for which block returns 0.0" do
+ result = (0.0..4.0).bsearch { |x| x < 2 ? 1.0 : x > 2 ? -1.0 : 0.0 }
+ result.should == 2
+ end
+
+ it "returns an element at an index for which block returns 0" do
+ result = (0.1..4.9).bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 }
+ result.should >= 1
+ result.should <= 3
+ end
+
+ it "returns an element at an index for which block returns 0 (small numbers)" do
+ result = (0.1..0.3).bsearch { |x| x < 0.1 ? 1 : x > 0.3 ? -1 : 0 }
+ result.should >= 0.1
+ result.should <= 0.3
+ end
+
+ it "returns a boundary element if appropriate" do
+ (1.0..3.0).bsearch { |x| 3.0 - x }.should == 3.0
+ (1.0...3.0).bsearch { |x| 3.0.prev_float - x }.should == 3.0.prev_float
+ (1.0..3.0).bsearch { |x| 1.0 - x }.should == 1.0
+ (1.0...3.0).bsearch { |x| 1.0 - x }.should == 1.0
+ end
+
+ it "works with infinity bounds" do
+ inf = Float::INFINITY
+ (0..inf).bsearch { |x| x == inf ? 0 : 1 }.should == inf
+ (0...inf).bsearch { |x| x == inf ? 0 : 1 }.should == nil
+ (-inf...0).bsearch { |x| x == -inf ? 0 : -1 }.should == -inf
+ (-inf..0).bsearch { |x| x == -inf ? 0 : -1 }.should == -inf
+ (inf..inf).bsearch { 0 }.should == inf
+ (inf...inf).bsearch { 0 }.should == nil
+ (-inf..-inf).bsearch { 0 }.should == -inf
+ (-inf...-inf).bsearch { 0 }.should == nil
+ (inf..0).bsearch { 0 }.should == nil
+ (inf...0).bsearch { 0 }.should == nil
+ (0..-inf).bsearch { 0 }.should == nil
+ (0...-inf).bsearch { 0 }.should == nil
+ (inf..-inf).bsearch { 0 }.should == nil
+ (inf...-inf).bsearch { 0 }.should == nil
+ (-inf..inf).bsearch { |x| 3 - x }.should == 3.0
+ (-inf...inf).bsearch { |x| 3 - x }.should == 3.0
+ (0...inf).bsearch { |x| x >= Float::MAX ? 0 : 1 }.should == Float::MAX
+ end
+ end
+
+ it "returns enumerator when block not passed" do
+ (0.1...2.3).bsearch.kind_of?(Enumerator).should == true
+ end
+ end
+
+ context "with endless ranges and Integer values" do
+ context "with a block returning true or false" do
+ it "returns minimum element if the block returns true for every element" do
+ eval("(-2..)").bsearch { |x| true }.should == -2
+ end
+
+ it "returns the smallest element for which block returns true" do
+ eval("(0..)").bsearch { |x| x >= 2 }.should == 2
+ eval("(-1..)").bsearch { |x| x >= 1 }.should == 1
+ end
+ end
+
+ context "with a block returning negative, zero, positive numbers" do
+ it "returns nil if the block returns less than zero for every element" do
+ eval("(0..)").bsearch { |x| -1 }.should be_nil
+ end
+
+ it "returns nil if the block never returns zero" do
+ eval("(0..)").bsearch { |x| x > 5 ? -1 : 1 }.should be_nil
+ end
+
+ it "accepts -Float::INFINITY from the block" do
+ eval("(0..)").bsearch { |x| -Float::INFINITY }.should be_nil
+ end
+
+ it "returns an element at an index for which block returns 0.0" do
+ result = eval("(0..)").bsearch { |x| x < 2 ? 1.0 : x > 2 ? -1.0 : 0.0 }
+ result.should == 2
+ end
+
+ it "returns an element at an index for which block returns 0" do
+ result = eval("(0..)").bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 }
+ [1, 2, 3].should include(result)
+ end
+ end
+
+ it "returns enumerator when block not passed" do
+ eval("(-2..)").bsearch.kind_of?(Enumerator).should == true
+ end
+ end
+
+ context "with endless ranges and Float values" do
+ context "with a block returning true or false" do
+ it "returns nil if the block returns false for every element" do
+ eval("(0.1..)").bsearch { |x| x < 0.0 }.should be_nil
+ eval("(0.1...)").bsearch { |x| x < 0.0 }.should be_nil
+ end
+
+ it "returns nil if the block returns nil for every element" do
+ eval("(-0.0..)").bsearch { |x| nil }.should be_nil
+ eval("(-0.0...)").bsearch { |x| nil }.should be_nil
+ end
+
+ it "returns minimum element if the block returns true for every element" do
+ eval("(-0.2..)").bsearch { |x| true }.should == -0.2
+ eval("(-0.2...)").bsearch { |x| true }.should == -0.2
+ end
+
+ it "returns the smallest element for which block returns true" do
+ eval("(0..)").bsearch { |x| x >= 2 }.should == 2
+ eval("(-1.2..)").bsearch { |x| x >= 1 }.should == 1
+ end
+
+ it "works with infinity bounds" do
+ inf = Float::INFINITY
+ eval("(inf..)").bsearch { |x| true }.should == inf
+ eval("(inf...)").bsearch { |x| true }.should == nil
+ eval("(-inf..)").bsearch { |x| true }.should == -inf
+ eval("(-inf...)").bsearch { |x| true }.should == -inf
+ end
+ end
+
+ context "with a block returning negative, zero, positive numbers" do
+ it "returns nil if the block returns less than zero for every element" do
+ eval("(-2.0..)").bsearch { |x| -1 }.should be_nil
+ eval("(-2.0...)").bsearch { |x| -1 }.should be_nil
+ end
+
+ it "returns nil if the block returns greater than zero for every element" do
+ eval("(0.3..)").bsearch { |x| 1 }.should be_nil
+ eval("(0.3...)").bsearch { |x| 1 }.should be_nil
+ end
+
+ it "returns nil if the block never returns zero" do
+ eval("(0.2..)").bsearch { |x| x < 2 ? 1 : -1 }.should be_nil
+ end
+
+ it "accepts (+/-)Float::INFINITY from the block" do
+ eval("(0.1..)").bsearch { |x| Float::INFINITY }.should be_nil
+ eval("(-5.0..)").bsearch { |x| -Float::INFINITY }.should be_nil
+ end
+
+ it "returns an element at an index for which block returns 0.0" do
+ result = eval("(0.0..)").bsearch { |x| x < 2 ? 1.0 : x > 2 ? -1.0 : 0.0 }
+ result.should == 2
+ end
+
+ it "returns an element at an index for which block returns 0" do
+ result = eval("(0.1..)").bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 }
+ result.should >= 1
+ result.should <= 3
+ end
+
+ it "works with infinity bounds" do
+ inf = Float::INFINITY
+ eval("(inf..)").bsearch { |x| 1 }.should == nil
+ eval("(inf...)").bsearch { |x| 1 }.should == nil
+ eval("(inf..)").bsearch { |x| x == inf ? 0 : 1 }.should == inf
+ eval("(inf...)").bsearch { |x| x == inf ? 0 : 1 }.should == nil
+ eval("(-inf..)").bsearch { |x| x == -inf ? 0 : -1 }.should == -inf
+ eval("(-inf...)").bsearch { |x| x == -inf ? 0 : -1 }.should == -inf
+ eval("(-inf..)").bsearch { |x| 3 - x }.should == 3
+ eval("(-inf...)").bsearch { |x| 3 - x }.should == 3
+ eval("(0.0...)").bsearch { 0 }.should != inf
+ end
+ end
+
+ it "returns enumerator when block not passed" do
+ eval("(0.1..)").bsearch.kind_of?(Enumerator).should == true
+ end
+ end
+
+ context "with beginless ranges and Integer values" do
+ context "with a block returning true or false" do
+ it "returns the smallest element for which block returns true" do
+ (..10).bsearch { |x| x >= 2 }.should == 2
+ (...-1).bsearch { |x| x >= -10 }.should == -10
+ end
+ end
+
+ context "with a block returning negative, zero, positive numbers" do
+ it "returns nil if the block returns greater than zero for every element" do
+ (..0).bsearch { |x| 1 }.should be_nil
+ end
+
+ it "returns nil if the block never returns zero" do
+ (..0).bsearch { |x| x > 5 ? -1 : 1 }.should be_nil
+ end
+
+ it "accepts Float::INFINITY from the block" do
+ (..0).bsearch { |x| Float::INFINITY }.should be_nil
+ end
+
+ it "returns an element at an index for which block returns 0.0" do
+ result = (..10).bsearch { |x| x < 2 ? 1.0 : x > 2 ? -1.0 : 0.0 }
+ result.should == 2
+ end
+
+ it "returns an element at an index for which block returns 0" do
+ result = (...10).bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 }
+ [1, 2, 3].should include(result)
+ end
+ end
+
+ it "returns enumerator when block not passed" do
+ (..10).bsearch.kind_of?(Enumerator).should == true
+ end
+ end
+
+ context "with beginless ranges and Float values" do
+ context "with a block returning true or false" do
+ it "returns nil if the block returns true for every element" do
+ (..-0.1).bsearch { |x| x > 0.0 }.should be_nil
+ (...-0.1).bsearch { |x| x > 0.0 }.should be_nil
+ end
+
+ it "returns nil if the block returns nil for every element" do
+ (..-0.1).bsearch { |x| nil }.should be_nil
+ (...-0.1).bsearch { |x| nil }.should be_nil
+ end
+
+ it "returns the smallest element for which block returns true" do
+ (..10).bsearch { |x| x >= 2 }.should == 2
+ (..10).bsearch { |x| x >= 1 }.should == 1
+ end
+
+ it "works with infinity bounds" do
+ inf = Float::INFINITY
+ (..inf).bsearch { |x| true }.should == -inf
+ (...inf).bsearch { |x| true }.should == -inf
+ (..-inf).bsearch { |x| true }.should == -inf
+ (...-inf).bsearch { |x| true }.should == nil
+ end
+ end
+
+ context "with a block returning negative, zero, positive numbers" do
+ it "returns nil if the block returns less than zero for every element" do
+ (..5.0).bsearch { |x| -1 }.should be_nil
+ (...5.0).bsearch { |x| -1 }.should be_nil
+ end
+
+ it "returns nil if the block returns greater than zero for every element" do
+ (..1.1).bsearch { |x| 1 }.should be_nil
+ (...1.1).bsearch { |x| 1 }.should be_nil
+ end
+
+ it "returns nil if the block never returns zero" do
+ (..6.3).bsearch { |x| x < 2 ? 1 : -1 }.should be_nil
+ end
+
+ it "accepts (+/-)Float::INFINITY from the block" do
+ (..5.0).bsearch { |x| Float::INFINITY }.should be_nil
+ (..7.0).bsearch { |x| -Float::INFINITY }.should be_nil
+ end
+
+ it "returns an element at an index for which block returns 0.0" do
+ result = (..8.0).bsearch { |x| x < 2 ? 1.0 : x > 2 ? -1.0 : 0.0 }
+ result.should == 2
+ end
+
+ it "returns an element at an index for which block returns 0" do
+ result = (..8.0).bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 }
+ result.should >= 1
+ result.should <= 3
+ end
+
+ it "works with infinity bounds" do
+ inf = Float::INFINITY
+ (..-inf).bsearch { |x| 1 }.should == nil
+ (...-inf).bsearch { |x| 1 }.should == nil
+ (..inf).bsearch { |x| x == inf ? 0 : 1 }.should == inf
+ (...inf).bsearch { |x| x == inf ? 0 : 1 }.should == nil
+ (..-inf).bsearch { |x| x == -inf ? 0 : -1 }.should == -inf
+ (...-inf).bsearch { |x| x == -inf ? 0 : -1 }.should == nil
+ (..inf).bsearch { |x| 3 - x }.should == 3
+ (...inf).bsearch { |x| 3 - x }.should == 3
+ end
+ end
+
+ it "returns enumerator when block not passed" do
+ (..-0.1).bsearch.kind_of?(Enumerator).should == true
+ end
+ end
+end
diff --git a/spec/ruby/core/range/case_compare_spec.rb b/spec/ruby/core/range/case_compare_spec.rb
new file mode 100644
index 0000000000..c9b253f0a5
--- /dev/null
+++ b/spec/ruby/core/range/case_compare_spec.rb
@@ -0,0 +1,19 @@
+require_relative '../../spec_helper'
+require_relative 'shared/cover_and_include'
+require_relative 'shared/cover'
+
+describe "Range#===" do
+ it "returns the result of calling #cover? on self" do
+ range = RangeSpecs::WithoutSucc.new(0)..RangeSpecs::WithoutSucc.new(10)
+ (range === RangeSpecs::WithoutSucc.new(2)).should == true
+ end
+
+ it_behaves_like :range_cover_and_include, :===
+ it_behaves_like :range_cover, :===
+
+ ruby_bug "#19533", ""..."3.3" do
+ it "returns true on any value if begin and end are both nil" do
+ (nil..nil).should === 1
+ end
+ end
+end
diff --git a/spec/ruby/core/range/clone_spec.rb b/spec/ruby/core/range/clone_spec.rb
new file mode 100644
index 0000000000..cf6ce74da0
--- /dev/null
+++ b/spec/ruby/core/range/clone_spec.rb
@@ -0,0 +1,26 @@
+require_relative '../../spec_helper'
+
+describe "Range#clone" do
+ it "duplicates the range" do
+ original = (1..3)
+ copy = original.clone
+ copy.begin.should == 1
+ copy.end.should == 3
+ copy.should_not.exclude_end?
+ copy.should_not.equal? original
+
+ original = ("a"..."z")
+ copy = original.clone
+ copy.begin.should == "a"
+ copy.end.should == "z"
+ copy.should.exclude_end?
+ copy.should_not.equal? original
+ end
+
+ it "maintains the frozen state" do
+ (1..2).clone.frozen?.should == (1..2).frozen?
+ (1..).clone.frozen?.should == (1..).frozen?
+ Range.new(1, 2).clone.frozen?.should == Range.new(1, 2).frozen?
+ Class.new(Range).new(1, 2).clone.frozen?.should == Class.new(Range).new(1, 2).frozen?
+ end
+end
diff --git a/spec/ruby/core/range/count_spec.rb b/spec/ruby/core/range/count_spec.rb
new file mode 100644
index 0000000000..24d4a9caf3
--- /dev/null
+++ b/spec/ruby/core/range/count_spec.rb
@@ -0,0 +1,12 @@
+require_relative '../../spec_helper'
+
+describe "Range#count" do
+ it "returns Infinity for beginless ranges without arguments or blocks" do
+ inf = Float::INFINITY
+ eval("('a'...)").count.should == inf
+ eval("(7..)").count.should == inf
+ (...'a').count.should == inf
+ (...nil).count.should == inf
+ (..10.0).count.should == inf
+ end
+end
diff --git a/spec/ruby/core/range/cover_spec.rb b/spec/ruby/core/range/cover_spec.rb
new file mode 100644
index 0000000000..c05bb50614
--- /dev/null
+++ b/spec/ruby/core/range/cover_spec.rb
@@ -0,0 +1,14 @@
+# encoding: binary
+require_relative '../../spec_helper'
+require_relative 'shared/cover_and_include'
+require_relative 'shared/cover'
+
+describe "Range#cover?" do
+ it_behaves_like :range_cover_and_include, :cover?
+ it_behaves_like :range_cover, :cover?
+ it_behaves_like :range_cover_subrange, :cover?
+
+ it "covers U+9995 in the range U+0999..U+9999" do
+ ("\u{999}".."\u{9999}").cover?("\u{9995}").should be_true
+ end
+end
diff --git a/spec/ruby/core/range/dup_spec.rb b/spec/ruby/core/range/dup_spec.rb
new file mode 100644
index 0000000000..fab3c3f1b2
--- /dev/null
+++ b/spec/ruby/core/range/dup_spec.rb
@@ -0,0 +1,23 @@
+require_relative '../../spec_helper'
+
+describe "Range#dup" do
+ it "duplicates the range" do
+ original = (1..3)
+ copy = original.dup
+ copy.begin.should == 1
+ copy.end.should == 3
+ copy.should_not.exclude_end?
+ copy.should_not.equal?(original)
+
+ copy = ("a"..."z").dup
+ copy.begin.should == "a"
+ copy.end.should == "z"
+ copy.should.exclude_end?
+ end
+
+ it "creates an unfrozen range" do
+ (1..2).dup.should_not.frozen?
+ (1..).dup.should_not.frozen?
+ Range.new(1, 2).dup.should_not.frozen?
+ end
+end
diff --git a/spec/ruby/core/range/each_spec.rb b/spec/ruby/core/range/each_spec.rb
new file mode 100644
index 0000000000..f10330d61d
--- /dev/null
+++ b/spec/ruby/core/range/each_spec.rb
@@ -0,0 +1,101 @@
+require_relative '../../spec_helper'
+require_relative '../enumerable/shared/enumeratorized'
+
+describe "Range#each" do
+ it "passes each element to the given block by using #succ" do
+ a = []
+ (-5..5).each { |i| a << i }
+ a.should == [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
+
+ a = []
+ ('A'..'D').each { |i| a << i }
+ a.should == ['A','B','C','D']
+
+ a = []
+ ('A'...'D').each { |i| a << i }
+ a.should == ['A','B','C']
+
+ a = []
+ (0xfffd...0xffff).each { |i| a << i }
+ a.should == [0xfffd, 0xfffe]
+
+ y = mock('y')
+ x = mock('x')
+ x.should_receive(:<=>).with(y).any_number_of_times.and_return(-1)
+ x.should_receive(:<=>).with(x).any_number_of_times.and_return(0)
+ x.should_receive(:succ).any_number_of_times.and_return(y)
+ y.should_receive(:<=>).with(x).any_number_of_times.and_return(1)
+ y.should_receive(:<=>).with(y).any_number_of_times.and_return(0)
+
+ a = []
+ (x..y).each { |i| a << i }
+ a.should == [x, y]
+ end
+
+ it "works for non-ASCII ranges" do
+ a = []
+ ('Σ'..'Ω').each { |i| a << i }
+ a.should == ["Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω"]
+ end
+
+ it "works with endless ranges" do
+ a = []
+ (-2..).each { |x| break if x > 2; a << x }
+ a.should == [-2, -1, 0, 1, 2]
+
+ a = []
+ (-2...).each { |x| break if x > 2; a << x }
+ a.should == [-2, -1, 0, 1, 2]
+ end
+
+ it "works with String endless ranges" do
+ a = []
+ ('A'..).each { |x| break if x > "D"; a << x }
+ a.should == ["A", "B", "C", "D"]
+
+ a = []
+ ('A'...).each { |x| break if x > "D"; a << x }
+ a.should == ["A", "B", "C", "D"]
+ end
+
+ it "raises a TypeError beginless ranges" do
+ -> { (..2).each { |x| x } }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if the first element does not respond to #succ" do
+ -> { (0.5..2.4).each { |i| i } }.should raise_error(TypeError)
+
+ b = mock('x')
+ (a = mock('1')).should_receive(:<=>).with(b).and_return(1)
+
+ -> { (a..b).each { |i| i } }.should raise_error(TypeError)
+ end
+
+ it "returns self" do
+ range = 1..10
+ range.each{}.should equal(range)
+ end
+
+ it "returns an enumerator when no block given" do
+ enum = (1..3).each
+ enum.should be_an_instance_of(Enumerator)
+ enum.to_a.should == [1, 2, 3]
+ end
+
+ it "supports Time objects that respond to #succ" do
+ t = Time.utc(1970)
+ def t.succ; self + 1 end
+ t_succ = t.succ
+ def t_succ.succ; self + 1; end
+
+ (t..t_succ).to_a.should == [Time.utc(1970), Time.utc(1970, nil, nil, nil, nil, 1)]
+ (t...t_succ).to_a.should == [Time.utc(1970)]
+ end
+
+ it "passes each Symbol element by using #succ" do
+ (:aa..:ac).each.to_a.should == [:aa, :ab, :ac]
+ (:aa...:ac).each.to_a.should == [:aa, :ab]
+ end
+
+ it_behaves_like :enumeratorized_with_origin_size, :each, (1..3)
+end
diff --git a/spec/ruby/core/range/end_spec.rb b/spec/ruby/core/range/end_spec.rb
new file mode 100644
index 0000000000..9e5e6f7d43
--- /dev/null
+++ b/spec/ruby/core/range/end_spec.rb
@@ -0,0 +1,6 @@
+require_relative '../../spec_helper'
+require_relative 'shared/end'
+
+describe "Range#end" do
+ it_behaves_like :range_end, :end
+end
diff --git a/spec/ruby/core/range/eql_spec.rb b/spec/ruby/core/range/eql_spec.rb
new file mode 100644
index 0000000000..fa6c71840e
--- /dev/null
+++ b/spec/ruby/core/range/eql_spec.rb
@@ -0,0 +1,10 @@
+require_relative '../../spec_helper'
+require_relative 'shared/equal_value'
+
+describe "Range#eql?" do
+ it_behaves_like :range_eql, :eql?
+
+ it "returns false if the endpoints are not eql?" do
+ (0..1).should_not eql(0..1.0)
+ end
+end
diff --git a/spec/ruby/core/range/equal_value_spec.rb b/spec/ruby/core/range/equal_value_spec.rb
new file mode 100644
index 0000000000..83dcf5cec8
--- /dev/null
+++ b/spec/ruby/core/range/equal_value_spec.rb
@@ -0,0 +1,18 @@
+require_relative '../../spec_helper'
+require_relative 'shared/equal_value'
+
+describe "Range#==" do
+ it_behaves_like :range_eql, :==
+
+ it "returns true if the endpoints are ==" do
+ (0..1).should == (0..1.0)
+ end
+
+ it "returns true if the endpoints are == for endless ranges" do
+ eval("(1.0..)").should == eval("(1.0..)")
+ end
+
+ it "returns true if the endpoints are == for beginless ranges" do
+ (...10).should == (...10)
+ end
+end
diff --git a/spec/ruby/core/range/exclude_end_spec.rb b/spec/ruby/core/range/exclude_end_spec.rb
new file mode 100644
index 0000000000..c4006fea78
--- /dev/null
+++ b/spec/ruby/core/range/exclude_end_spec.rb
@@ -0,0 +1,19 @@
+require_relative '../../spec_helper'
+
+describe "Range#exclude_end?" do
+ it "returns false if the range does not exclude the end value" do
+ (-2..2).should_not.exclude_end?
+ ('A'..'B').should_not.exclude_end?
+ (0.5..2.4).should_not.exclude_end?
+ (0xfffd..0xffff).should_not.exclude_end?
+ Range.new(0, 1).should_not.exclude_end?
+ end
+
+ it "returns true if the range excludes the end value" do
+ (0...5).should.exclude_end?
+ ('A'...'B').should.exclude_end?
+ (0.5...2.4).should.exclude_end?
+ (0xfffd...0xffff).should.exclude_end?
+ Range.new(0, 1, true).should.exclude_end?
+ end
+end
diff --git a/spec/ruby/core/range/first_spec.rb b/spec/ruby/core/range/first_spec.rb
new file mode 100644
index 0000000000..2af935f439
--- /dev/null
+++ b/spec/ruby/core/range/first_spec.rb
@@ -0,0 +1,53 @@
+require_relative '../../spec_helper'
+require_relative 'shared/begin'
+
+describe "Range#first" do
+ it_behaves_like :range_begin, :first
+
+ it "returns the specified number of elements from the beginning" do
+ (0..2).first(2).should == [0, 1]
+ end
+
+ it "returns an empty array for an empty Range" do
+ (0...0).first(2).should == []
+ end
+
+ it "returns an empty array when passed zero" do
+ (0..2).first(0).should == []
+ end
+
+ it "returns all elements in the range when count exceeds the number of elements" do
+ (0..2).first(4).should == [0, 1, 2]
+ end
+
+ it "raises an ArgumentError when count is negative" do
+ -> { (0..2).first(-1) }.should raise_error(ArgumentError)
+ end
+
+ it "calls #to_int to convert the argument" do
+ obj = mock_int(2)
+ (3..7).first(obj).should == [3, 4]
+ end
+
+ it "raises a TypeError if #to_int does not return an Integer" do
+ obj = mock("to_int")
+ obj.should_receive(:to_int).and_return("1")
+ -> { (2..3).first(obj) }.should raise_error(TypeError)
+ end
+
+ it "truncates the value when passed a Float" do
+ (2..9).first(2.8).should == [2, 3]
+ end
+
+ it "raises a TypeError when passed nil" do
+ -> { (2..3).first(nil) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError when passed a String" do
+ -> { (2..3).first("1") }.should raise_error(TypeError)
+ end
+
+ it "raises a RangeError when called on an beginless range" do
+ -> { (..1).first }.should raise_error(RangeError)
+ end
+end
diff --git a/spec/ruby/core/range/fixtures/classes.rb b/spec/ruby/core/range/fixtures/classes.rb
new file mode 100644
index 0000000000..3a1df010b2
--- /dev/null
+++ b/spec/ruby/core/range/fixtures/classes.rb
@@ -0,0 +1,90 @@
+module RangeSpecs
+ class TenfoldSucc
+ include Comparable
+
+ attr_reader :n
+
+ def initialize(n)
+ @n = n
+ end
+
+ def <=>(other)
+ @n <=> other.n
+ end
+
+ def succ
+ self.class.new(@n * 10)
+ end
+ end
+
+ # Custom Range classes Xs and Ys
+ class Custom
+ include Comparable
+ attr_reader :length
+
+ def initialize(n)
+ @length = n
+ end
+
+ def eql?(other)
+ inspect.eql? other.inspect
+ end
+ alias :== :eql?
+
+ def inspect
+ 'custom'
+ end
+
+ def <=>(other)
+ @length <=> other.length
+ 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)
+ end
+
+ def inspect
+ 'x' * @length
+ end
+ end
+
+ class Ys < Custom # represent a string of 'y's
+ def succ
+ Ys.new(@length + 1)
+ end
+
+ def inspect
+ 'y' * @length
+ end
+ end
+
+ class MyRange < Range
+ end
+
+ class ComparisonError < RuntimeError
+ end
+end
diff --git a/spec/ruby/core/range/frozen_spec.rb b/spec/ruby/core/range/frozen_spec.rb
new file mode 100644
index 0000000000..8dab5e5339
--- /dev/null
+++ b/spec/ruby/core/range/frozen_spec.rb
@@ -0,0 +1,25 @@
+require_relative '../../spec_helper'
+
+# There is no Range#frozen? method but this feels like the best place for these specs
+describe "Range#frozen?" do
+ it "is true for literal ranges" do
+ (1..2).should.frozen?
+ (1..).should.frozen?
+ (..1).should.frozen?
+ end
+
+ it "is true for Range.new" do
+ Range.new(1, 2).should.frozen?
+ Range.new(1, nil).should.frozen?
+ Range.new(nil, 1).should.frozen?
+ end
+
+ it "is false for instances of a subclass of Range" do
+ sub_range = Class.new(Range).new(1, 2)
+ sub_range.should_not.frozen?
+ end
+
+ it "is false for Range.allocate" do
+ Range.allocate.should_not.frozen?
+ end
+end
diff --git a/spec/ruby/core/range/hash_spec.rb b/spec/ruby/core/range/hash_spec.rb
new file mode 100644
index 0000000000..4f2681e86e
--- /dev/null
+++ b/spec/ruby/core/range/hash_spec.rb
@@ -0,0 +1,24 @@
+require_relative '../../spec_helper'
+
+describe "Range#hash" do
+ it "is provided" do
+ (0..1).respond_to?(:hash).should == true
+ ('A'..'Z').respond_to?(:hash).should == true
+ (0xfffd..0xffff).respond_to?(:hash).should == true
+ (0.5..2.4).respond_to?(:hash).should == true
+ end
+
+ it "generates the same hash values for Ranges with the same start, end and exclude_end? values" do
+ (0..1).hash.should == (0..1).hash
+ (0...10).hash.should == (0...10).hash
+ (0..10).hash.should_not == (0...10).hash
+ end
+
+ it "generates an Integer for the hash value" do
+ (0..0).hash.should be_an_instance_of(Integer)
+ (0..1).hash.should be_an_instance_of(Integer)
+ (0...10).hash.should be_an_instance_of(Integer)
+ (0..10).hash.should be_an_instance_of(Integer)
+ end
+
+end
diff --git a/spec/ruby/core/range/include_spec.rb b/spec/ruby/core/range/include_spec.rb
new file mode 100644
index 0000000000..449e18985b
--- /dev/null
+++ b/spec/ruby/core/range/include_spec.rb
@@ -0,0 +1,14 @@
+# encoding: binary
+require_relative '../../spec_helper'
+require_relative 'shared/cover_and_include'
+require_relative 'shared/include'
+require_relative 'shared/cover'
+
+describe "Range#include?" do
+ it_behaves_like :range_cover_and_include, :include?
+ it_behaves_like :range_include, :include?
+
+ it "does not include U+9995 in the range U+0999..U+9999" do
+ ("\u{999}".."\u{9999}").include?("\u{9995}").should be_false
+ end
+end
diff --git a/spec/ruby/core/range/initialize_spec.rb b/spec/ruby/core/range/initialize_spec.rb
new file mode 100644
index 0000000000..c653caf0c6
--- /dev/null
+++ b/spec/ruby/core/range/initialize_spec.rb
@@ -0,0 +1,41 @@
+require_relative '../../spec_helper'
+
+describe "Range#initialize" do
+ before do
+ @range = Range.allocate
+ end
+
+ it "is private" do
+ Range.should have_private_instance_method("initialize")
+ end
+
+ it "initializes correctly the Range object when given 2 arguments" do
+ -> { @range.send(:initialize, 0, 1) }.should_not raise_error
+ end
+
+ it "initializes correctly the Range object when given 3 arguments" do
+ -> { @range.send(:initialize, 0, 1, true) }.should_not raise_error
+ end
+
+ it "raises an ArgumentError if passed without or with only one argument" do
+ -> { @range.send(:initialize) }.should raise_error(ArgumentError)
+ -> { @range.send(:initialize, 1) }.should raise_error(ArgumentError)
+ end
+
+ it "raises an ArgumentError if passed with four or more arguments" do
+ -> { @range.send(:initialize, 1, 3, 5, 7) }.should raise_error(ArgumentError)
+ -> { @range.send(:initialize, 1, 3, 5, 7, 9) }.should raise_error(ArgumentError)
+ end
+
+ it "raises a FrozenError if called on an already initialized Range" do
+ -> { (0..1).send(:initialize, 1, 3) }.should raise_error(FrozenError)
+ -> { (0..1).send(:initialize, 1, 3, true) }.should raise_error(FrozenError)
+ end
+
+ it "raises an ArgumentError if arguments don't respond to <=>" do
+ o1 = Object.new
+ o2 = Object.new
+
+ -> { @range.send(:initialize, o1, o2) }.should raise_error(ArgumentError)
+ end
+end
diff --git a/spec/ruby/core/range/inspect_spec.rb b/spec/ruby/core/range/inspect_spec.rb
new file mode 100644
index 0000000000..072de123b7
--- /dev/null
+++ b/spec/ruby/core/range/inspect_spec.rb
@@ -0,0 +1,29 @@
+require_relative '../../spec_helper'
+
+describe "Range#inspect" do
+ it "provides a printable form, using #inspect to convert the start and end objects" do
+ ('A'..'Z').inspect.should == '"A".."Z"'
+ ('A'...'Z').inspect.should == '"A"..."Z"'
+
+ (0..21).inspect.should == "0..21"
+ (-8..0).inspect.should == "-8..0"
+ (-411..959).inspect.should == "-411..959"
+ (0xfff..0xfffff).inspect.should == "4095..1048575"
+ (0.5..2.4).inspect.should == "0.5..2.4"
+ end
+
+ it "works for endless ranges" do
+ eval("(1..)").inspect.should == "1.."
+ eval("(0.1...)").inspect.should == "0.1..."
+ end
+
+ it "works for beginless ranges" do
+ (..1).inspect.should == "..1"
+ (...0.1).inspect.should == "...0.1"
+ end
+
+ it "works for nil ... nil ranges" do
+ (..nil).inspect.should == "nil..nil"
+ eval("(nil...)").inspect.should == "nil...nil"
+ end
+end
diff --git a/spec/ruby/core/range/last_spec.rb b/spec/ruby/core/range/last_spec.rb
new file mode 100644
index 0000000000..82b3e2ff53
--- /dev/null
+++ b/spec/ruby/core/range/last_spec.rb
@@ -0,0 +1,57 @@
+require_relative '../../spec_helper'
+require_relative 'shared/end'
+
+describe "Range#last" do
+ it_behaves_like :range_end, :last
+
+ it "returns the specified number of elements from the end" do
+ (1..5).last(3).should == [3, 4, 5]
+ end
+
+ it "returns the specified number if elements for single element inclusive range" do
+ (1..1).last(1).should == [1]
+ end
+
+ it "returns an empty array for an empty Range" do
+ (0...0).last(2).should == []
+ end
+
+ it "returns an empty array when passed zero" do
+ (0..2).last(0).should == []
+ end
+
+ it "returns all elements in the range when count exceeds the number of elements" do
+ (2..4).last(5).should == [2, 3, 4]
+ end
+
+ it "raises an ArgumentError when count is negative" do
+ -> { (0..2).last(-1) }.should raise_error(ArgumentError)
+ end
+
+ it "calls #to_int to convert the argument" do
+ obj = mock_int(2)
+ (3..7).last(obj).should == [6, 7]
+ end
+
+ it "raises a TypeError if #to_int does not return an Integer" do
+ obj = mock("to_int")
+ obj.should_receive(:to_int).and_return("1")
+ -> { (2..3).last(obj) }.should raise_error(TypeError)
+ end
+
+ it "truncates the value when passed a Float" do
+ (2..9).last(2.8).should == [8, 9]
+ end
+
+ it "raises a TypeError when passed nil" do
+ -> { (2..3).last(nil) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError when passed a String" do
+ -> { (2..3).last("1") }.should raise_error(TypeError)
+ end
+
+ it "raises a RangeError when called on an endless range" do
+ -> { eval("(1..)").last }.should raise_error(RangeError)
+ end
+end
diff --git a/spec/ruby/core/range/max_spec.rb b/spec/ruby/core/range/max_spec.rb
new file mode 100644
index 0000000000..09371f5298
--- /dev/null
+++ b/spec/ruby/core/range/max_spec.rb
@@ -0,0 +1,115 @@
+require_relative '../../spec_helper'
+
+describe "Range#max" do
+ it "returns the maximum value in the range when called with no arguments" do
+ (1..10).max.should == 10
+ (1...10).max.should == 9
+ (0...2**64).max.should == 18446744073709551615
+ ('f'..'l').max.should == 'l'
+ ('a'...'f').max.should == 'e'
+ end
+
+ it "returns the maximum value in the Float range when called with no arguments" do
+ (303.20..908.1111).max.should == 908.1111
+ end
+
+ it "raises TypeError when called on an exclusive range and a non Integer value" do
+ -> { (303.20...908.1111).max }.should raise_error(TypeError)
+ end
+
+ it "returns nil when the endpoint is less than the start point" do
+ (100..10).max.should be_nil
+ ('z'..'l').max.should be_nil
+ end
+
+ it "returns nil when the endpoint equals the start point and the range is exclusive" do
+ (5...5).max.should be_nil
+ end
+
+ it "returns the endpoint when the endpoint equals the start point and the range is inclusive" do
+ (5..5).max.should equal(5)
+ end
+
+ it "returns nil when the endpoint is less than the start point in a Float range" do
+ (3003.20..908.1111).max.should be_nil
+ end
+
+ it "returns end point when the range is Time..Time(included end point)" do
+ time_start = Time.now
+ time_end = Time.now + 1.0
+ (time_start..time_end).max.should equal(time_end)
+ end
+
+ it "raises TypeError when called on a Time...Time(excluded end point)" do
+ time_start = Time.now
+ time_end = Time.now + 1.0
+ -> { (time_start...time_end).max }.should raise_error(TypeError)
+ end
+
+ it "raises RangeError when called on an endless range" do
+ -> { eval("(1..)").max }.should raise_error(RangeError)
+ end
+
+ it "returns the end point for beginless ranges" do
+ (..1).max.should == 1
+ (..1.0).max.should == 1.0
+ end
+
+ ruby_version_is ""..."4.0" do
+ it "raises for an exclusive beginless Integer range" do
+ -> {
+ (...1).max
+ }.should raise_error(TypeError, 'cannot exclude end value with non Integer begin value')
+ end
+ end
+
+ ruby_version_is "4.0" do
+ it "returns the end point for exclusive beginless Integer ranges" do
+ (...1).max.should == 0
+ end
+ end
+
+ it "raises for an exclusive beginless non Integer range" do
+ -> {
+ (...1.0).max
+ }.should raise_error(TypeError, 'cannot exclude non Integer end value')
+ end
+end
+
+describe "Range#max given a block" do
+ it "passes each pair of values in the range to the block" do
+ acc = []
+ (1..10).max {|a,b| acc << [a,b]; a }
+ acc.flatten!
+ (1..10).each do |value|
+ acc.include?(value).should be_true
+ end
+ end
+
+ it "passes each pair of elements to the block in reversed order" do
+ acc = []
+ (1..5).max {|a,b| acc << [a,b]; a }
+ acc.should == [[2,1],[3,2], [4,3], [5, 4]]
+ end
+
+ it "calls #> and #< on the return value of the block" do
+ obj = mock('obj')
+ obj.should_receive(:>).exactly(2).times
+ obj.should_receive(:<).exactly(2).times
+ (1..3).max {|a,b| obj }
+ end
+
+ it "returns the element the block determines to be the maximum" do
+ (1..3).max {|a,b| -3 }.should == 1
+ end
+
+ it "returns nil when the endpoint is less than the start point" do
+ (100..10).max {|x,y| x <=> y}.should be_nil
+ ('z'..'l').max {|x,y| x <=> y}.should be_nil
+ (5...5).max {|x,y| x <=> y}.should be_nil
+ end
+
+ it "raises RangeError when called with custom comparison method on an beginless range" do
+ -> { (..1).max {|a, b| a} }.should raise_error(RangeError)
+ end
+end
diff --git a/spec/ruby/core/range/member_spec.rb b/spec/ruby/core/range/member_spec.rb
new file mode 100644
index 0000000000..78299ae9e5
--- /dev/null
+++ b/spec/ruby/core/range/member_spec.rb
@@ -0,0 +1,10 @@
+# encoding: binary
+require_relative '../../spec_helper'
+require_relative 'shared/cover_and_include'
+require_relative 'shared/include'
+require_relative 'shared/cover'
+
+describe "Range#member?" do
+ it_behaves_like :range_cover_and_include, :member?
+ it_behaves_like :range_include, :member?
+end
diff --git a/spec/ruby/core/range/min_spec.rb b/spec/ruby/core/range/min_spec.rb
new file mode 100644
index 0000000000..89310ee589
--- /dev/null
+++ b/spec/ruby/core/range/min_spec.rb
@@ -0,0 +1,88 @@
+require_relative '../../spec_helper'
+
+describe "Range#min" do
+ it "returns the minimum value in the range when called with no arguments" do
+ (1..10).min.should == 1
+ ('f'..'l').min.should == 'f'
+ end
+
+ it "returns the minimum value in the Float range when called with no arguments" do
+ (303.20..908.1111).min.should == 303.20
+ end
+
+ it "returns nil when the start point is greater than the endpoint" do
+ (100..10).min.should be_nil
+ ('z'..'l').min.should be_nil
+ end
+
+ it "returns nil when the endpoint equals the start point and the range is exclusive" do
+ (7...7).min.should be_nil
+ end
+
+ it "returns the start point when the endpoint equals the start point and the range is inclusive" do
+ (7..7).min.should equal(7)
+ end
+
+ it "returns nil when the start point is greater than the endpoint in a Float range" do
+ (3003.20..908.1111).min.should be_nil
+ end
+
+ it "returns start point when the range is Time..Time(included end point)" do
+ time_start = Time.now
+ time_end = Time.now + 1.0
+ (time_start..time_end).min.should equal(time_start)
+ end
+
+ it "returns start point when the range is Time...Time(excluded end point)" do
+ time_start = Time.now
+ time_end = Time.now + 1.0
+ (time_start...time_end).min.should equal(time_start)
+ end
+
+ it "returns the start point for endless ranges" do
+ eval("(1..)").min.should == 1
+ eval("(1.0...)").min.should == 1.0
+ end
+
+ it "raises RangeError when called on an beginless range" do
+ -> { (..1).min }.should raise_error(RangeError)
+ end
+end
+
+describe "Range#min given a block" do
+ it "passes each pair of values in the range to the block" do
+ acc = []
+ (1..10).min {|a,b| acc << [a,b]; a }
+ acc.flatten!
+ (1..10).each do |value|
+ acc.include?(value).should be_true
+ end
+ end
+
+ it "passes each pair of elements to the block where the first argument is the current element, and the last is the first element" do
+ acc = []
+ (1..5).min {|a,b| acc << [a,b]; a }
+ acc.should == [[2, 1], [3, 1], [4, 1], [5, 1]]
+ end
+
+ it "calls #> and #< on the return value of the block" do
+ obj = mock('obj')
+ obj.should_receive(:>).exactly(2).times
+ obj.should_receive(:<).exactly(2).times
+ (1..3).min {|a,b| obj }
+ end
+
+ it "returns the element the block determines to be the minimum" do
+ (1..3).min {|a,b| -3 }.should == 3
+ end
+
+ it "returns nil when the start point is greater than the endpoint" do
+ (100..10).min {|x,y| x <=> y}.should be_nil
+ ('z'..'l').min {|x,y| x <=> y}.should be_nil
+ (7...7).min {|x,y| x <=> y}.should be_nil
+ end
+
+ it "raises RangeError when called with custom comparison method on an endless range" do
+ -> { eval("(1..)").min {|a, b| a} }.should raise_error(RangeError)
+ end
+end
diff --git a/spec/ruby/core/range/minmax_spec.rb b/spec/ruby/core/range/minmax_spec.rb
new file mode 100644
index 0000000000..6651ae3726
--- /dev/null
+++ b/spec/ruby/core/range/minmax_spec.rb
@@ -0,0 +1,130 @@
+require_relative '../../spec_helper'
+
+describe 'Range#minmax' do
+ before(:each) do
+ @x = mock('x')
+ @y = mock('y')
+
+ @x.should_receive(:<=>).with(@y).any_number_of_times.and_return(-1) # x < y
+ @x.should_receive(:<=>).with(@x).any_number_of_times.and_return(0) # x == x
+ @y.should_receive(:<=>).with(@x).any_number_of_times.and_return(1) # y > x
+ @y.should_receive(:<=>).with(@y).any_number_of_times.and_return(0) # y == y
+ end
+
+ describe 'on an inclusive range' do
+ it 'should raise RangeError on an endless range without iterating the range' do
+ @x.should_not_receive(:succ)
+
+ range = (@x..)
+
+ -> { range.minmax }.should raise_error(RangeError, 'cannot get the maximum of endless range')
+ end
+
+ it 'raises RangeError or ArgumentError on a beginless range' do
+ range = (..@x)
+
+ -> { range.minmax }.should raise_error(StandardError) { |e|
+ if RangeError === e
+ # error from #min
+ -> { raise e }.should raise_error(RangeError, 'cannot get the minimum of beginless range')
+ else
+ # error from #max
+ -> { raise e }.should raise_error(ArgumentError, 'comparison of NilClass with MockObject failed')
+ end
+ }
+ end
+
+ it 'should return beginning of range if beginning and end are equal without iterating the range' do
+ @x.should_not_receive(:succ)
+
+ (@x..@x).minmax.should == [@x, @x]
+ end
+
+ it 'should return nil pair if beginning is greater than end without iterating the range' do
+ @y.should_not_receive(:succ)
+
+ (@y..@x).minmax.should == [nil, nil]
+ end
+
+ it 'should return the minimum and maximum values for a non-numeric range without iterating the range' do
+ @x.should_not_receive(:succ)
+
+ (@x..@y).minmax.should == [@x, @y]
+ end
+
+ it 'should return the minimum and maximum values for a numeric range' do
+ (1..3).minmax.should == [1, 3]
+ end
+
+ it 'should return the minimum and maximum values for a numeric range without iterating the range' do
+ # We cannot set expectations on integers,
+ # so we "prevent" iteration by picking a value that would iterate until the spec times out.
+ range_end = Float::INFINITY
+
+ (1..range_end).minmax.should == [1, range_end]
+ end
+
+ it 'should return the minimum and maximum values according to the provided block by iterating the range' do
+ @x.should_receive(:succ).once.and_return(@y)
+
+ (@x..@y).minmax { |x, y| - (x <=> y) }.should == [@y, @x]
+ end
+ end
+
+ describe 'on an exclusive range' do
+ it 'should raise RangeError on an endless range' do
+ @x.should_not_receive(:succ)
+ range = (@x...)
+
+ -> { range.minmax }.should raise_error(RangeError, 'cannot get the maximum of endless range')
+ end
+
+ it 'should raise RangeError on a beginless range' do
+ range = (...@x)
+
+ -> { range.minmax }.should raise_error(RangeError,
+ /cannot get the maximum of beginless range with custom comparison method|cannot get the minimum of beginless range/)
+ end
+
+ it 'should return nil pair if beginning and end are equal without iterating the range' do
+ @x.should_not_receive(:succ)
+
+ (@x...@x).minmax.should == [nil, nil]
+ end
+
+ it 'should return nil pair if beginning is greater than end without iterating the range' do
+ @y.should_not_receive(:succ)
+
+ (@y...@x).minmax.should == [nil, nil]
+ end
+
+ it 'should return the minimum and maximum values for a non-numeric range by iterating the range' do
+ @x.should_receive(:succ).once.and_return(@y)
+
+ (@x...@y).minmax.should == [@x, @x]
+ end
+
+ it 'should return the minimum and maximum values for a numeric range' do
+ (1...3).minmax.should == [1, 2]
+ end
+
+ it 'should return the minimum and maximum values for a numeric range without iterating the range' do
+ # We cannot set expectations on integers,
+ # so we "prevent" iteration by picking a value that would iterate until the spec times out.
+ range_end = bignum_value
+
+ (1...range_end).minmax.should == [1, range_end - 1]
+ end
+
+ it 'raises TypeError if the end value is not an integer' do
+ range = (0...Float::INFINITY)
+ -> { range.minmax }.should raise_error(TypeError, 'cannot exclude non Integer end value')
+ end
+
+ it 'should return the minimum and maximum values according to the provided block by iterating the range' do
+ @x.should_receive(:succ).once.and_return(@y)
+
+ (@x...@y).minmax { |x, y| - (x <=> y) }.should == [@x, @x]
+ end
+ end
+end
diff --git a/spec/ruby/core/range/new_spec.rb b/spec/ruby/core/range/new_spec.rb
new file mode 100644
index 0000000000..3cab887799
--- /dev/null
+++ b/spec/ruby/core/range/new_spec.rb
@@ -0,0 +1,77 @@
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
+
+describe "Range.new" do
+ it "constructs a range using the given start and end" do
+ range = Range.new('a', 'c')
+ range.should == ('a'..'c')
+
+ range.first.should == 'a'
+ range.last.should == 'c'
+ end
+
+ it "includes the end object when the third parameter is omitted or false" do
+ Range.new('a', 'c').to_a.should == ['a', 'b', 'c']
+ Range.new(1, 3).to_a.should == [1, 2, 3]
+
+ Range.new('a', 'c', false).to_a.should == ['a', 'b', 'c']
+ Range.new(1, 3, false).to_a.should == [1, 2, 3]
+
+ Range.new('a', 'c', true).to_a.should == ['a', 'b']
+ Range.new(1, 3, 1).to_a.should == [1, 2]
+
+ Range.new(1, 3, mock('[1,2]')).to_a.should == [1, 2]
+ Range.new(1, 3, :test).to_a.should == [1, 2]
+ end
+
+ it "raises an ArgumentError when the given start and end can't be compared by using #<=>" do
+ -> { Range.new(1, mock('x')) }.should raise_error(ArgumentError)
+ -> { Range.new(mock('x'), mock('y')) }.should raise_error(ArgumentError)
+
+ b = mock('x')
+ (a = mock('nil')).should_receive(:<=>).with(b).and_return(nil)
+ -> { Range.new(a, b) }.should raise_error(ArgumentError)
+ end
+
+ it "does not rescue exception raised in #<=> when compares the given start and end" do
+ b = mock('a')
+ a = mock('b')
+ a.should_receive(:<=>).with(b).and_raise(RangeSpecs::ComparisonError)
+
+ -> { Range.new(a, b) }.should raise_error(RangeSpecs::ComparisonError)
+ end
+
+ describe "beginless/endless range" do
+ it "allows beginless left boundary" do
+ range = Range.new(nil, 1)
+ range.begin.should == nil
+ end
+
+ it "distinguishes ranges with included and excluded right boundary" do
+ range_exclude = Range.new(nil, 1, true)
+ range_include = Range.new(nil, 1, false)
+
+ range_exclude.should_not == range_include
+ end
+
+ 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
+
+ it "creates a frozen range if the class is Range.class" do
+ Range.new(1, 2).should.frozen?
+ end
+
+ it "does not create a frozen range if the class is not Range.class" do
+ Class.new(Range).new(1, 2).should_not.frozen?
+ end
+ end
+end
diff --git a/spec/ruby/core/range/overlap_spec.rb b/spec/ruby/core/range/overlap_spec.rb
new file mode 100644
index 0000000000..9b6fc13493
--- /dev/null
+++ b/spec/ruby/core/range/overlap_spec.rb
@@ -0,0 +1,89 @@
+require_relative '../../spec_helper'
+
+ruby_version_is '3.3' do
+ describe "Range#overlap?" do
+ it "returns true if other Range overlaps self" do
+ (0..2).overlap?(1..3).should == true
+ (1..3).overlap?(0..2).should == true
+ (0..2).overlap?(0..2).should == true
+ (0..3).overlap?(1..2).should == true
+ (1..2).overlap?(0..3).should == true
+
+ ('a'..'c').overlap?('b'..'d').should == true
+ end
+
+ it "returns false if other Range does not overlap self" do
+ (0..2).overlap?(3..4).should == false
+ (0..2).overlap?(-4..-1).should == false
+
+ ('a'..'c').overlap?('d'..'f').should == false
+ end
+
+ it "raises TypeError when called with non-Range argument" do
+ -> {
+ (0..2).overlap?(1)
+ }.should raise_error(TypeError, "wrong argument type Integer (expected Range)")
+ end
+
+ it "returns true when beginningless and endless Ranges overlap" do
+ (0..2).overlap?(..3).should == true
+ (0..2).overlap?(..1).should == true
+ (0..2).overlap?(..0).should == true
+
+ (..3).overlap?(0..2).should == true
+ (..1).overlap?(0..2).should == true
+ (..0).overlap?(0..2).should == true
+
+ (0..2).overlap?(-1..).should == true
+ (0..2).overlap?(1..).should == true
+ (0..2).overlap?(2..).should == true
+
+ (-1..).overlap?(0..2).should == true
+ (1..).overlap?(0..2).should == true
+ (2..).overlap?(0..2).should == true
+
+ (0..).overlap?(2..).should == true
+ (..0).overlap?(..2).should == true
+ end
+
+ it "returns false when beginningless and endless Ranges do not overlap" do
+ (0..2).overlap?(..-1).should == false
+ (0..2).overlap?(3..).should == false
+
+ (..-1).overlap?(0..2).should == false
+ (3..).overlap?(0..2).should == false
+ end
+
+ it "returns false when Ranges are not compatible" do
+ (0..2).overlap?('a'..'d').should == false
+ end
+
+ it "return false when self is empty" do
+ (2..0).overlap?(1..3).should == false
+ (2...2).overlap?(1..3).should == false
+ (1...1).overlap?(1...1).should == false
+ (2..0).overlap?(2..0).should == false
+
+ ('c'..'a').overlap?('b'..'d').should == false
+ ('a'...'a').overlap?('b'..'d').should == false
+ ('b'...'b').overlap?('b'...'b').should == false
+ ('c'...'a').overlap?('c'...'a').should == false
+ end
+
+ it "return false when other Range is empty" do
+ (1..3).overlap?(2..0).should == false
+ (1..3).overlap?(2...2).should == false
+
+ ('b'..'d').overlap?('c'..'a').should == false
+ ('b'..'d').overlap?('c'...'c').should == false
+ end
+
+ it "takes into account exclusive end" do
+ (0...2).overlap?(2..4).should == false
+ (2..4).overlap?(0...2).should == false
+
+ ('a'...'c').overlap?('c'..'e').should == false
+ ('c'..'e').overlap?('a'...'c').should == false
+ 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..5ec6770ddb
--- /dev/null
+++ b/spec/ruby/core/range/percent_spec.rb
@@ -0,0 +1,16 @@
+require_relative '../../spec_helper'
+
+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
diff --git a/spec/ruby/core/range/range_spec.rb b/spec/ruby/core/range/range_spec.rb
new file mode 100644
index 0000000000..8e9433f8c1
--- /dev/null
+++ b/spec/ruby/core/range/range_spec.rb
@@ -0,0 +1,7 @@
+require_relative '../../spec_helper'
+
+describe "Range" do
+ it "includes Enumerable" do
+ Range.include?(Enumerable).should == true
+ end
+end
diff --git a/spec/ruby/core/range/reverse_each_spec.rb b/spec/ruby/core/range/reverse_each_spec.rb
new file mode 100644
index 0000000000..56390cc0da
--- /dev/null
+++ b/spec/ruby/core/range/reverse_each_spec.rb
@@ -0,0 +1,103 @@
+require_relative '../../spec_helper'
+
+ruby_version_is "3.3" do
+ describe "Range#reverse_each" do
+ it "traverses the Range in reverse order and passes each element to block" do
+ a = []
+ (1..3).reverse_each { |i| a << i }
+ a.should == [3, 2, 1]
+
+ a = []
+ (1...3).reverse_each { |i| a << i }
+ a.should == [2, 1]
+ end
+
+ it "returns self" do
+ r = (1..3)
+ r.reverse_each { |x| }.should equal(r)
+ end
+
+ it "returns an Enumerator if no block given" do
+ enum = (1..3).reverse_each
+ enum.should be_an_instance_of(Enumerator)
+ enum.to_a.should == [3, 2, 1]
+ end
+
+ it "raises a TypeError for endless Ranges of Integers" do
+ -> {
+ (1..).reverse_each.take(3)
+ }.should raise_error(TypeError, "can't iterate from NilClass")
+ end
+
+ it "raises a TypeError for endless Ranges of non-Integers" do
+ -> {
+ ("a"..).reverse_each.take(3)
+ }.should raise_error(TypeError, "can't iterate from NilClass")
+ end
+
+ context "Integer boundaries" do
+ it "supports beginningless Ranges" do
+ (..5).reverse_each.take(3).should == [5, 4, 3]
+ end
+ end
+
+ context "non-Integer boundaries" do
+ it "uses #succ to iterate a Range of non-Integer elements" do
+ y = mock('y')
+ x = mock('x')
+
+ x.should_receive(:succ).any_number_of_times.and_return(y)
+ x.should_receive(:<=>).with(y).any_number_of_times.and_return(-1)
+ x.should_receive(:<=>).with(x).any_number_of_times.and_return(0)
+ y.should_receive(:<=>).with(x).any_number_of_times.and_return(1)
+ y.should_receive(:<=>).with(y).any_number_of_times.and_return(0)
+
+ a = []
+ (x..y).each { |i| a << i }
+ a.should == [x, y]
+ end
+
+ it "uses #succ to iterate a Range of Strings" do
+ a = []
+ ('A'..'D').reverse_each { |i| a << i }
+ a.should == ['D','C','B','A']
+ end
+
+ it "uses #succ to iterate a Range of Symbols" do
+ a = []
+ (:A..:D).reverse_each { |i| a << i }
+ a.should == [:D, :C, :B, :A]
+ end
+
+ it "raises a TypeError when `begin` value does not respond to #succ" do
+ -> { (Time.now..Time.now).reverse_each { |x| x } }.should raise_error(TypeError, /can't iterate from Time/)
+ -> { (//..//).reverse_each { |x| x } }.should raise_error(TypeError, /can't iterate from Regexp/)
+ -> { ([]..[]).reverse_each { |x| x } }.should raise_error(TypeError, /can't iterate from Array/)
+ end
+
+ it "does not support beginningless Ranges" do
+ -> {
+ (..'a').reverse_each { |x| x }
+ }.should raise_error(TypeError, /can't iterate from NilClass/)
+ end
+ end
+
+ context "when no block is given" do
+ describe "returned Enumerator size" do
+ it "returns the Range size when Range size is finite" do
+ (1..3).reverse_each.size.should == 3
+ end
+
+ ruby_bug "#20936", "3.4"..."4.0" do
+ it "returns Infinity when Range size is infinite" do
+ (..3).reverse_each.size.should == Float::INFINITY
+ end
+ end
+
+ it "returns nil when Range size is unknown" do
+ ('a'..'z').reverse_each.size.should == nil
+ end
+ end
+ end
+ end
+end
diff --git a/spec/ruby/core/range/shared/begin.rb b/spec/ruby/core/range/shared/begin.rb
new file mode 100644
index 0000000000..f660e3faf9
--- /dev/null
+++ b/spec/ruby/core/range/shared/begin.rb
@@ -0,0 +1,10 @@
+describe :range_begin, shared: true do
+ it "returns the first element of self" do
+ (-1..1).send(@method).should == -1
+ (0..1).send(@method).should == 0
+ (0xffff...0xfffff).send(@method).should == 65535
+ ('Q'..'T').send(@method).should == 'Q'
+ ('Q'...'T').send(@method).should == 'Q'
+ (0.5..2.4).send(@method).should == 0.5
+ end
+end
diff --git a/spec/ruby/core/range/shared/cover.rb b/spec/ruby/core/range/shared/cover.rb
new file mode 100644
index 0000000000..eaefb45942
--- /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 be_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
+ 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
+ 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
+ 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 be_false
+ end
+
+ it "returns true if other is equal as first element" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should be_true
+ end
+
+ it "returns true if other is matched by element.succ" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should be_true
+ end
+
+ it "returns true if other is not matched by element.succ" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should be_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
+ 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
+ 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 be_false
+ end
+
+ it "returns true if other is equal as first element" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should be_true
+ end
+
+ it "returns true if other is matched by element.succ" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should be_true
+ end
+
+ it "returns true if other is not matched by element.succ" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should be_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
+ 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
+ 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 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
+
+ it "allows self to be a beginless range" do
+ (...10).send(@method, (3..7)).should be_true
+ (...10).send(@method, (3..15)).should be_false
+
+ (..7.9).send(@method, (2.5..6.5)).should be_true
+ (..7.9).send(@method, (2.5..8.5)).should be_false
+
+ (..'i').send(@method, ('d'..'f')).should be_true
+ (..'i').send(@method, ('d'..'z')).should be_false
+ end
+
+ it "allows self to be a endless range" do
+ eval("(0...)").send(@method, (3..7)).should be_true
+ eval("(5...)").send(@method, (3..15)).should be_false
+
+ eval("(1.1..)").send(@method, (2.5..6.5)).should be_true
+ eval("(3.3..)").send(@method, (2.5..8.5)).should be_false
+
+ eval("('a'..)").send(@method, ('d'..'f')).should be_true
+ eval("('p'..)").send(@method, ('d'..'z')).should be_false
+ end
+
+ it "accepts beginless range argument" do
+ (..10).send(@method, (...10)).should be_true
+ (0..10).send(@method, (...10)).should be_false
+
+ (1.1..7.9).send(@method, (...10.5)).should be_false
+
+ ('c'..'i').send(@method, (..'i')).should be_false
+ end
+
+ it "accepts endless range argument" do
+ eval("(0..)").send(@method, eval("(0...)")).should be_true
+ (0..10).send(@method, eval("(0...)")).should be_false
+
+ (1.1..7.9).send(@method, eval("(0.8...)")).should be_false
+
+ ('c'..'i').send(@method, eval("('a'..)")).should be_false
+ end
+end
diff --git a/spec/ruby/core/range/shared/cover_and_include.rb b/spec/ruby/core/range/shared/cover_and_include.rb
new file mode 100644
index 0000000000..13fc5e1790
--- /dev/null
+++ b/spec/ruby/core/range/shared/cover_and_include.rb
@@ -0,0 +1,86 @@
+# encoding: binary
+require_relative '../../../spec_helper'
+
+describe :range_cover_and_include, shared: true do
+ it "returns true if other is an element of self" do
+ (0..5).send(@method, 2).should == true
+ (-5..5).send(@method, 0).should == true
+ (-1...1).send(@method, 10.5).should == false
+ (-10..-2).send(@method, -2.5).should == true
+ ('C'..'X').send(@method, 'M').should == true
+ ('C'..'X').send(@method, 'A').should == false
+ ('B'...'W').send(@method, 'W').should == false
+ ('B'...'W').send(@method, 'Q').should == true
+ (0xffff..0xfffff).send(@method, 0xffffd).should == true
+ (0xffff..0xfffff).send(@method, 0xfffd).should == false
+ (0.5..2.4).send(@method, 2).should == true
+ (0.5..2.4).send(@method, 2.5).should == false
+ (0.5..2.4).send(@method, 2.4).should == true
+ (0.5...2.4).send(@method, 2.4).should == false
+ end
+
+ it "returns true if other is an element of self for endless ranges" do
+ (1..).send(@method, 2.4).should == true
+ (0.5...).send(@method, 2.4).should == true
+ end
+
+ it "returns true if other is an element of self for beginless ranges" do
+ (..10).send(@method, 2.4).should == true
+ (...10.5).send(@method, 2.4).should == true
+ end
+
+ it "returns false if values are not comparable" do
+ (1..10).send(@method, nil).should == false
+ (1...10).send(@method, nil).should == false
+
+ (..10).send(@method, nil).should == false
+ (...10).send(@method, nil).should == false
+
+ (1..).send(@method, nil).should == false
+ (1...).send(@method, nil).should == false
+ end
+
+ it "compares values using <=>" do
+ rng = (1..5)
+ m = mock("int")
+ m.should_receive(:coerce).and_return([1, 2])
+ m.should_receive(:<=>).and_return(1)
+
+ rng.send(@method, m).should be_false
+ end
+
+ it "raises an ArgumentError without exactly one argument" do
+ ->{ (1..2).send(@method) }.should raise_error(ArgumentError)
+ ->{ (1..2).send(@method, 1, 2) }.should raise_error(ArgumentError)
+ end
+
+ it "returns true if argument is equal to the first value of the range" do
+ (0..5).send(@method, 0).should be_true
+ ('f'..'s').send(@method, 'f').should be_true
+ end
+
+ it "returns true if argument is equal to the last value of the range" do
+ (0..5).send(@method, 5).should be_true
+ (0...5).send(@method, 4).should be_true
+ ('f'..'s').send(@method, 's').should be_true
+ end
+
+ it "returns true if argument is less than the last value of the range and greater than the first value" do
+ (20..30).send(@method, 28).should be_true
+ ('e'..'h').send(@method, 'g').should be_true
+ end
+
+ it "returns true if argument is sole element in the range" do
+ (30..30).send(@method, 30).should be_true
+ end
+
+ it "returns false if range is empty" do
+ (30...30).send(@method, 30).should be_false
+ (30...30).send(@method, nil).should be_false
+ end
+
+ it "returns false if the range does not contain the argument" do
+ ('A'..'C').send(@method, 20.9).should be_false
+ ('A'...'C').send(@method, 'C').should be_false
+ end
+end
diff --git a/spec/ruby/core/range/shared/end.rb b/spec/ruby/core/range/shared/end.rb
new file mode 100644
index 0000000000..b26394fe31
--- /dev/null
+++ b/spec/ruby/core/range/shared/end.rb
@@ -0,0 +1,10 @@
+describe :range_end, shared: true do
+ it "end returns the last element of self" do
+ (-1..1).send(@method).should == 1
+ (0..1).send(@method).should == 1
+ ("A".."Q").send(@method).should == "Q"
+ ("A"..."Q").send(@method).should == "Q"
+ (0xffff...0xfffff).send(@method).should == 1048575
+ (0.5..2.4).send(@method).should == 2.4
+ end
+end
diff --git a/spec/ruby/core/range/shared/equal_value.rb b/spec/ruby/core/range/shared/equal_value.rb
new file mode 100644
index 0000000000..363c6be558
--- /dev/null
+++ b/spec/ruby/core/range/shared/equal_value.rb
@@ -0,0 +1,51 @@
+require_relative '../fixtures/classes'
+
+describe :range_eql, shared: true do
+ it "returns true if other has same begin, end, and exclude_end? values" do
+ (0..2).send(@method, 0..2).should == true
+ ('G'..'M').send(@method,'G'..'M').should == true
+ (0.5..2.4).send(@method, 0.5..2.4).should == true
+ (5..10).send(@method, Range.new(5,10)).should == true
+ ('D'..'V').send(@method, Range.new('D','V')).should == true
+ (0.5..2.4).send(@method, Range.new(0.5, 2.4)).should == true
+ (0xffff..0xfffff).send(@method, 0xffff..0xfffff).should == true
+ (0xffff..0xfffff).send(@method, Range.new(0xffff,0xfffff)).should == true
+
+ a = RangeSpecs::Xs.new(3)..RangeSpecs::Xs.new(5)
+ b = Range.new(RangeSpecs::Xs.new(3), RangeSpecs::Xs.new(5))
+ a.send(@method, b).should == true
+ end
+
+ it "returns false if one of the attributes differs" do
+ ('Q'..'X').send(@method, 'A'..'C').should == false
+ ('Q'...'X').send(@method, 'Q'..'W').should == false
+ ('Q'..'X').send(@method, 'Q'...'X').should == false
+ (0.5..2.4).send(@method, 0.5...2.4).should == false
+ (1482..1911).send(@method, 1482...1911).should == false
+ (0xffff..0xfffff).send(@method, 0xffff...0xfffff).should == false
+
+ a = RangeSpecs::Xs.new(3)..RangeSpecs::Xs.new(5)
+ b = Range.new(RangeSpecs::Ys.new(3), RangeSpecs::Ys.new(5))
+ a.send(@method, b).should == false
+ end
+
+ it "returns false if other is not a Range" do
+ (1..10).send(@method, 1).should == false
+ (1..10).send(@method, 'a').should == false
+ (1..10).send(@method, mock('x')).should == false
+ end
+
+ it "returns true for subclasses of Range" do
+ Range.new(1, 2).send(@method, RangeSpecs::MyRange.new(1, 2)).should == true
+
+ a = Range.new(RangeSpecs::Xs.new(3), RangeSpecs::Xs.new(5))
+ b = RangeSpecs::MyRange.new(RangeSpecs::Xs.new(3), RangeSpecs::Xs.new(5))
+ a.send(@method, b).should == true
+ end
+
+ it "works for endless Ranges" do
+ eval("(1..)").send(@method, eval("(1..)")).should == true
+ eval("(0.5...)").send(@method, eval("(0.5...)")).should == true
+ eval("(1..)").send(@method, eval("(1...)")).should == false
+ end
+end
diff --git a/spec/ruby/core/range/shared/include.rb b/spec/ruby/core/range/shared/include.rb
new file mode 100644
index 0000000000..15a0e5fb9f
--- /dev/null
+++ b/spec/ruby/core/range/shared/include.rb
@@ -0,0 +1,91 @@
+# encoding: binary
+require_relative '../../../spec_helper'
+require_relative '../fixtures/classes'
+
+describe :range_include, shared: true do
+ 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
+ end
+
+ it "returns false if other is not matched by element.succ" do
+ ('a'..'c').send(@method, 'bc').should be_false
+ ('a'...'c').send(@method, 'bc').should be_false
+ 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 be_false
+ end
+
+ it "returns true if other is equal as first element" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should be_true
+ end
+
+ it "returns true if other is matched by element.succ" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should be_true
+ end
+
+ it "returns false if other is not matched by element.succ" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should be_false
+ 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
+ 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
+ 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 be_false
+ end
+
+ it "returns true if other is equal as first element" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should be_true
+ end
+
+ it "returns true if other is matched by element.succ" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should be_true
+ end
+
+ it "returns false if other is not matched by element.succ" do
+ @range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should be_false
+ 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
+ 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
+ end
+ end
+ end
+
+ describe "with Time endpoints" do
+ it "uses cover? logic" do
+ now = Time.now
+ range = (now..(now + 60))
+
+ range.include?(now).should == true
+ range.include?(now - 1).should == false
+ range.include?(now + 60).should == true
+ range.include?(now + 61).should == false
+ end
+ end
+end
diff --git a/spec/ruby/core/range/size_spec.rb b/spec/ruby/core/range/size_spec.rb
new file mode 100644
index 0000000000..1a3ddd197e
--- /dev/null
+++ b/spec/ruby/core/range/size_spec.rb
@@ -0,0 +1,92 @@
+require_relative '../../spec_helper'
+
+describe "Range#size" do
+ it "returns the number of elements in the range" do
+ (1..16).size.should == 16
+ (1...16).size.should == 15
+ end
+
+ it "returns 0 if last is less than first" do
+ (16..0).size.should == 0
+ end
+
+ it 'returns Float::INFINITY for increasing, infinite ranges' do
+ (0..Float::INFINITY).size.should == Float::INFINITY
+ end
+
+ it 'returns Float::INFINITY for endless ranges if the start is numeric' do
+ eval("(1..)").size.should == Float::INFINITY
+ end
+
+ it 'returns nil for endless ranges if the start is not numeric' do
+ eval("('z'..)").size.should == nil
+ end
+
+ ruby_version_is ""..."3.4" do
+ it 'returns Float::INFINITY for all beginless ranges if the end is numeric' do
+ (..1).size.should == Float::INFINITY
+ (...0.5).size.should == Float::INFINITY
+ end
+
+ it 'returns nil for all beginless ranges if the end is not numeric' do
+ (...'o').size.should == nil
+ end
+
+ it 'returns nil if the start and the end is both nil' do
+ (nil..nil).size.should == nil
+ end
+ end
+
+ ruby_version_is ""..."3.4" do
+ it "returns the number of elements in the range" do
+ (1.0..16.0).size.should == 16
+ (1.0...16.0).size.should == 15
+ (1.0..15.9).size.should == 15
+ (1.1..16.0).size.should == 15
+ (1.1..15.9).size.should == 15
+ end
+
+ it "returns 0 if last is less than first" do
+ (16.0..0.0).size.should == 0
+ (Float::INFINITY..0).size.should == 0
+ end
+
+ it 'returns Float::INFINITY for increasing, infinite ranges' do
+ (-Float::INFINITY..0).size.should == Float::INFINITY
+ (-Float::INFINITY..Float::INFINITY).size.should == Float::INFINITY
+ end
+
+ it 'returns Float::INFINITY for endless ranges if the start is numeric' do
+ eval("(0.5...)").size.should == Float::INFINITY
+ end
+
+ it 'returns nil for endless ranges if the start is not numeric' do
+ eval("([]...)").size.should == nil
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it 'raises TypeError if a range is not iterable' do
+ -> { (1.0..16.0).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (1.0...16.0).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (1.0..15.9).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (1.1..16.0).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (1.1..15.9).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (16.0..0.0).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (Float::INFINITY..0).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (-Float::INFINITY..0).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (-Float::INFINITY..Float::INFINITY).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (..1).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (...0.5).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (..nil).size }.should raise_error(TypeError, /can't iterate from/)
+ -> { (...'o').size }.should raise_error(TypeError, /can't iterate from/)
+ -> { eval("(0.5...)").size }.should raise_error(TypeError, /can't iterate from/)
+ -> { eval("([]...)").size }.should raise_error(TypeError, /can't iterate from/)
+ end
+ end
+
+ it "returns nil if first and last are not Numeric" do
+ (:a..:z).size.should be_nil
+ ('a'..'z').size.should be_nil
+ end
+end
diff --git a/spec/ruby/core/range/step_spec.rb b/spec/ruby/core/range/step_spec.rb
new file mode 100644
index 0000000000..0d0caf746d
--- /dev/null
+++ b/spec/ruby/core/range/step_spec.rb
@@ -0,0 +1,683 @@
+require_relative '../../spec_helper'
+
+describe "Range#step" do
+ before :each do
+ ScratchPad.record []
+ end
+
+ it "returns self" do
+ r = 1..2
+ r.step { }.should equal(r)
+ end
+
+ ruby_version_is ""..."3.4" do
+ it "calls #to_int to coerce step to an Integer" do
+ obj = mock("Range#step")
+ obj.should_receive(:to_int).and_return(1)
+
+ (1..2).step(obj) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([1, 2])
+ end
+
+ it "raises a TypeError if step does not respond to #to_int" do
+ obj = mock("Range#step non-integer")
+
+ -> { (1..2).step(obj) { } }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if #to_int does not return an Integer" do
+ obj = mock("Range#step non-integer")
+ obj.should_receive(:to_int).and_return("1")
+
+ -> { (1..2).step(obj) { } }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if the first element does not respond to #succ" do
+ obj = mock("Range#step non-comparable")
+ obj.should_receive(:<=>).with(obj).and_return(1)
+
+ -> { (obj..obj).step { |x| x } }.should raise_error(TypeError)
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "calls #coerce to coerce step to an Integer" do
+ obj = mock("Range#step")
+ obj.should_receive(:coerce).at_least(:once).and_return([1, 2])
+
+ (1..3).step(obj) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([1, 3])
+ end
+
+ it "raises a TypeError if step does not respond to #coerce" do
+ obj = mock("Range#step non-coercible")
+
+ -> { (1..2).step(obj) { } }.should raise_error(TypeError)
+ end
+ end
+
+ it "raises an ArgumentError if step is 0" do
+ -> { (-1..1).step(0) { |x| x } }.should raise_error(ArgumentError)
+ end
+
+ it "raises an ArgumentError if step is 0.0" do
+ -> { (-1..1).step(0.0) { |x| x } }.should raise_error(ArgumentError)
+ end
+
+ ruby_version_is "3.4" do
+ it "does not raise an ArgumentError if step is 0 for non-numeric ranges" do
+ t = Time.utc(2023, 2, 24)
+ -> { (t..t+1).step(0) { break } }.should_not raise_error(ArgumentError)
+ end
+ end
+
+ ruby_version_is ""..."3.4" do
+ it "raises an ArgumentError if step is negative" do
+ -> { (-1..1).step(-2) { |x| x } }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "with inclusive end" do
+ describe "and Integer values" do
+ it "yields Integer values incremented by 1 and less than or equal to end when not passed a step" do
+ (-2..2).step { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-2, -1, 0, 1, 2])
+ end
+
+ it "yields Integer values incremented by an Integer step" do
+ (-5..5).step(2) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-5, -3, -1, 1, 3, 5])
+ end
+
+ it "yields Float values incremented by a Float step" do
+ (-2..2).step(1.5) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
+ end
+
+ ruby_version_is "3.4" do
+ it "does not iterate if step is negative for forward range" do
+ (-1..1).step(-1) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([])
+ end
+
+ it "iterates backward if step is negative for backward range" do
+ (1..-1).step(-1) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([1, 0, -1])
+ end
+ end
+ end
+
+ describe "and Float values" do
+ it "yields Float values incremented by 1 and less than or equal to end when not passed a step" do
+ (-2.0..2.0).step { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0, 2.0])
+ end
+
+ it "yields Float values incremented by an Integer step" do
+ (-5.0..5.0).step(2) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0, 5.0])
+ end
+
+ it "yields Float values incremented by a Float step" do
+ (-1.0..1.0).step(0.5) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5, 1.0])
+ end
+
+ it "returns Float values of 'step * n + begin <= end'" do
+ (1.0..6.4).step(1.8) { |x| ScratchPad << x }
+ (1.0..12.7).step(1.3) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([1.0, 2.8, 4.6, 6.4, 1.0, 2.3, 3.6,
+ 4.9, 6.2, 7.5, 8.8, 10.1, 11.4, 12.7])
+ end
+
+ it "handles infinite values at either end" do
+ (-Float::INFINITY..0.0).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
+ ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY])
+
+ ScratchPad.record []
+ (0.0..Float::INFINITY).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
+ ScratchPad.recorded.should eql([0.0, 2.0, 4.0])
+ end
+ end
+
+ describe "and Integer, Float values" do
+ it "yields Float values incremented by 1 and less than or equal to end when not passed a step" do
+ (-2..2.0).step { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0, 2.0])
+ end
+
+ it "yields Float values incremented by an Integer step" do
+ (-5..5.0).step(2) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0, 5.0])
+ end
+
+ it "yields Float values incremented by a Float step" do
+ (-1..1.0).step(0.5) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5, 1.0])
+ end
+ end
+
+ describe "and Float, Integer values" do
+ it "yields Float values incremented by 1 and less than or equal to end when not passed a step" do
+ (-2.0..2).step { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0, 2.0])
+ end
+
+ it "yields Float values incremented by an Integer step" do
+ (-5.0..5).step(2) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0, 5.0])
+ end
+
+ it "yields Float values incremented by a Float step" do
+ (-1.0..1).step(0.5) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5, 1.0])
+ end
+ end
+
+ describe "and String values" do
+ it "yields String values incremented by #succ and less than or equal to end when not passed a step" do
+ ("A".."E").step { |x| ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "B", "C", "D", "E"]
+ end
+
+ it "yields String values incremented by #succ called Integer step times" do
+ ("A".."G").step(2) { |x| ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "C", "E", "G"]
+ end
+
+ it "raises a TypeError when passed a Float step" do
+ -> { ("A".."G").step(2.0) { } }.should raise_error(TypeError)
+ end
+
+ ruby_version_is ""..."3.4" do
+ it "calls #succ on begin and each element returned by #succ" do
+ obj = mock("Range#step String start")
+ obj.should_receive(:<=>).exactly(3).times.and_return(-1, -1, -1, 0)
+ obj.should_receive(:succ).exactly(2).times.and_return(obj)
+
+ (obj..obj).step { |x| ScratchPad << x }
+ ScratchPad.recorded.should == [obj, obj, obj]
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "yields String values adjusted by step and less than or equal to end" do
+ ("A".."AAA").step("A") { |x| ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "AA", "AAA"]
+ end
+
+ it "raises a TypeError when passed an incompatible type step" do
+ -> { ("A".."G").step([]) { } }.should raise_error(TypeError)
+ end
+
+ it "calls #+ on begin and each element returned by #+" do
+ start = mock("Range#step String start")
+ stop = mock("Range#step String stop")
+
+ mid1 = mock("Range#step String mid1")
+ mid2 = mock("Range#step String mid2")
+
+ step = mock("Range#step String step")
+
+ # Deciding on the direction of iteration
+ start.should_receive(:<=>).with(stop).at_least(:twice).and_return(-1)
+ # Deciding whether the step moves iteration in the right direction
+ start.should_receive(:<=>).with(mid1).and_return(-1)
+ # Iteration 1
+ start.should_receive(:+).at_least(:once).with(step).and_return(mid1)
+ # Iteration 2
+ mid1.should_receive(:<=>).with(stop).and_return(-1)
+ mid1.should_receive(:+).with(step).and_return(mid2)
+ # Iteration 3
+ mid2.should_receive(:<=>).with(stop).and_return(0)
+
+ (start..stop).step(step) { |x| ScratchPad << x }
+ ScratchPad.recorded.should == [start, mid1, mid2]
+ end
+
+ it "iterates backward if the step is decreasing values, and the range is backward" do
+ start = mock("Range#step String start")
+ stop = mock("Range#step String stop")
+
+ mid1 = mock("Range#step String mid1")
+ mid2 = mock("Range#step String mid2")
+
+ step = mock("Range#step String step")
+
+ # Deciding on the direction of iteration
+ start.should_receive(:<=>).with(stop).at_least(:twice).and_return(1)
+ # Deciding whether the step moves iteration in the right direction
+ start.should_receive(:<=>).with(mid1).and_return(1)
+ # Iteration 1
+ start.should_receive(:+).at_least(:once).with(step).and_return(mid1)
+ # Iteration 2
+ mid1.should_receive(:<=>).with(stop).and_return(1)
+ mid1.should_receive(:+).with(step).and_return(mid2)
+ # Iteration 3
+ mid2.should_receive(:<=>).with(stop).and_return(0)
+
+ (start..stop).step(step) { |x| ScratchPad << x }
+ ScratchPad.recorded.should == [start, mid1, mid2]
+ end
+
+ it "does no iteration of the direction of the range and of the step don't match" do
+ start = mock("Range#step String start")
+ stop = mock("Range#step String stop")
+
+ mid1 = mock("Range#step String mid1")
+ mid2 = mock("Range#step String mid2")
+
+ step = mock("Range#step String step")
+
+ # Deciding on the direction of iteration: stop > start
+ start.should_receive(:<=>).with(stop).at_least(:twice).and_return(1)
+ # Deciding whether the step moves iteration in the right direction
+ # start + step < start, the direction is opposite to the range's
+ start.should_receive(:+).with(step).and_return(mid1)
+ start.should_receive(:<=>).with(mid1).and_return(-1)
+
+ (start..stop).step(step) { |x| ScratchPad << x }
+ ScratchPad.recorded.should == []
+ end
+ end
+ end
+ end
+
+ describe "with exclusive end" do
+ describe "and Integer values" do
+ it "yields Integer values incremented by 1 and less than end when not passed a step" do
+ (-2...2).step { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-2, -1, 0, 1])
+ end
+
+ it "yields Integer values incremented by an Integer step" do
+ (-5...5).step(2) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-5, -3, -1, 1, 3])
+ end
+
+ it "yields Float values incremented by a Float step" do
+ (-2...2).step(1.5) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
+ end
+ end
+
+ describe "and Float values" do
+ it "yields Float values incremented by 1 and less than end when not passed a step" do
+ (-2.0...2.0).step { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
+ end
+
+ it "yields Float values incremented by an Integer step" do
+ (-5.0...5.0).step(2) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
+ end
+
+ it "yields Float values incremented by a Float step" do
+ (-1.0...1.0).step(0.5) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
+ end
+
+ it "returns Float values of 'step * n + begin < end'" do
+ (1.0...6.4).step(1.8) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([1.0, 2.8, 4.6])
+ end
+
+ it "correctly handles values near the upper limit" do # https://bugs.ruby-lang.org/issues/16612
+ (1.0...55.6).step(18.2) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([1.0, 19.2, 37.4, 55.599999999999994])
+
+ (1.0...55.6).step(18.2).size.should == 4
+ end
+
+ it "handles infinite values at either end" do
+ (-Float::INFINITY...0.0).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
+ ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY])
+
+ ScratchPad.record []
+ (0.0...Float::INFINITY).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
+ ScratchPad.recorded.should eql([0.0, 2.0, 4.0])
+ end
+ end
+
+ describe "and Integer, Float values" do
+ it "yields Float values incremented by 1 and less than end when not passed a step" do
+ (-2...2.0).step { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
+ end
+
+ it "yields Float values incremented by an Integer step" do
+ (-5...5.0).step(2) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
+ end
+
+ it "yields an Float and then Float values incremented by a Float step" do
+ (-1...1.0).step(0.5) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
+ end
+ end
+
+ describe "and Float, Integer values" do
+ it "yields Float values incremented by 1 and less than end when not passed a step" do
+ (-2.0...2).step { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
+ end
+
+ it "yields Float values incremented by an Integer step" do
+ (-5.0...5).step(2) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
+ end
+
+ it "yields Float values incremented by a Float step" do
+ (-1.0...1).step(0.5) { |x| ScratchPad << x }
+ ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
+ end
+ end
+
+ describe "and String values" do
+ ruby_version_is ""..."3.4" do
+ it "yields String values incremented by #succ and less than or equal to end when not passed a step" do
+ ("A"..."E").step { |x| ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "B", "C", "D"]
+ end
+
+ it "yields String values incremented by #succ called Integer step times" do
+ ("A"..."G").step(2) { |x| ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "C", "E"]
+ end
+
+ it "raises a TypeError when passed a Float step" do
+ -> { ("A"..."G").step(2.0) { } }.should raise_error(TypeError)
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "yields String values adjusted by step and less than or equal to end" do
+ ("A"..."AAA").step("A") { |x| ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "AA"]
+ end
+
+ it "raises a TypeError when passed an incompatible type step" do
+ -> { ("A".."G").step([]) { } }.should raise_error(TypeError)
+ end
+ end
+ end
+ end
+
+ describe "with an endless range" do
+ describe "and Integer values" do
+ it "yield Integer values incremented by 1 when not passed a step" do
+ (-2..).step { |x| break if x > 2; ScratchPad << x }
+ ScratchPad.recorded.should eql([-2, -1, 0, 1, 2])
+
+ ScratchPad.record []
+ (-2...).step { |x| break if x > 2; ScratchPad << x }
+ ScratchPad.recorded.should eql([-2, -1, 0, 1, 2])
+ end
+
+ it "yields Integer values incremented by an Integer step" do
+ (-5..).step(2) { |x| break if x > 3; ScratchPad << x }
+ ScratchPad.recorded.should eql([-5, -3, -1, 1, 3])
+
+ ScratchPad.record []
+ (-5...).step(2) { |x| break if x > 3; ScratchPad << x }
+ ScratchPad.recorded.should eql([-5, -3, -1, 1, 3])
+ end
+
+ it "yields Float values incremented by a Float step" do
+ (-2..).step(1.5) { |x| break if x > 1.0; ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
+
+ ScratchPad.record []
+ (-2..).step(1.5) { |x| break if x > 1.0; ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
+ end
+ end
+
+ describe "and Float values" do
+ it "yields Float values incremented by 1 and less than end when not passed a step" do
+ (-2.0..).step { |x| break if x > 1.5; ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
+
+ ScratchPad.record []
+ (-2.0...).step { |x| break if x > 1.5; ScratchPad << x }
+ ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
+ end
+
+ it "yields Float values incremented by an Integer step" do
+ (-5.0..).step(2) { |x| break if x > 3.5; ScratchPad << x }
+ ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
+
+ ScratchPad.record []
+ (-5.0...).step(2) { |x| break if x > 3.5; ScratchPad << x }
+ ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
+ end
+
+ it "yields Float values incremented by a Float step" do
+ (-1.0..).step(0.5) { |x| break if x > 0.6; ScratchPad << x }
+ ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
+
+ ScratchPad.record []
+ (-1.0...).step(0.5) { |x| break if x > 0.6; ScratchPad << x }
+ ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
+ end
+
+ it "handles infinite values at the start" do
+ (-Float::INFINITY..).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
+ ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY])
+
+ ScratchPad.record []
+ (-Float::INFINITY...).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
+ ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY])
+ end
+ end
+
+ describe "and String values" do
+ it "yields String values incremented by #succ and less than or equal to end when not passed a step" do
+ ('A'..).step { |x| break if x > "D"; ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "B", "C", "D"]
+
+ ScratchPad.record []
+ ('A'...).step { |x| break if x > "D"; ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "B", "C", "D"]
+ end
+
+ it "yields String values incremented by #succ called Integer step times" do
+ ('A'..).step(2) { |x| break if x > "F"; ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "C", "E"]
+
+ ScratchPad.record []
+ ('A'...).step(2) { |x| break if x > "F"; ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "C", "E"]
+ end
+
+ it "raises a TypeError when passed a Float step" do
+ -> { ('A'..).step(2.0) { } }.should raise_error(TypeError)
+ -> { ('A'...).step(2.0) { } }.should raise_error(TypeError)
+ end
+
+ ruby_version_is "3.4" do
+ it "yields String values adjusted by step" do
+ ('A'..).step("A") { |x| break if x > "AAA"; ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "AA", "AAA"]
+
+ ScratchPad.record []
+ ('A'...).step("A") { |x| break if x > "AAA"; ScratchPad << x }
+ ScratchPad.recorded.should == ["A", "AA", "AAA"]
+ end
+
+ it "raises a TypeError when passed an incompatible type step" do
+ -> { ('A'..).step([]) { } }.should raise_error(TypeError)
+ -> { ('A'...).step([]) { } }.should raise_error(TypeError)
+ end
+ end
+ end
+ end
+
+ describe "when no block is given" do
+ it "raises an ArgumentError if step is 0" do
+ -> { (-1..1).step(0) }.should raise_error(ArgumentError)
+ end
+
+ describe "returned Enumerator" do
+ describe "size" do
+ ruby_version_is ""..."3.4" do
+ it "raises a TypeError if step does not respond to #to_int" do
+ obj = mock("Range#step non-integer")
+ -> { (1..2).step(obj) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if #to_int does not return an Integer" do
+ obj = mock("Range#step non-integer")
+ obj.should_receive(:to_int).and_return("1")
+ -> { (1..2).step(obj) }.should raise_error(TypeError)
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "does not raise if step is incompatible" do
+ obj = mock("Range#step non-integer")
+ -> { (1..2).step(obj) }.should_not raise_error
+ end
+ end
+
+ it "returns the ceil of range size divided by the number of steps" do
+ (1..10).step(4).size.should == 3
+ (1..10).step(3).size.should == 4
+ (1..10).step(2).size.should == 5
+ (1..10).step(1).size.should == 10
+ (-5..5).step(2).size.should == 6
+ (1...10).step(4).size.should == 3
+ (1...10).step(3).size.should == 3
+ (1...10).step(2).size.should == 5
+ (1...10).step(1).size.should == 9
+ (-5...5).step(2).size.should == 5
+ end
+
+ it "returns the ceil of range size divided by the number of steps even if step is negative" do
+ (-1..1).step(-1).size.should == 0
+ (1..-1).step(-1).size.should == 3
+ end
+
+ it "returns the correct number of steps when one of the arguments is a float" do
+ (-1..1.0).step(0.5).size.should == 5
+ (-1.0...1.0).step(0.5).size.should == 4
+ end
+
+ it "returns the range size when there's no step_size" do
+ (-2..2).step.size.should == 5
+ (-2.0..2.0).step.size.should == 5
+ (-2..2.0).step.size.should == 5
+ (-2.0..2).step.size.should == 5
+ (1.0..6.4).step(1.8).size.should == 4
+ (1.0..12.7).step(1.3).size.should == 10
+ (-2...2).step.size.should == 4
+ (-2.0...2.0).step.size.should == 4
+ (-2...2.0).step.size.should == 4
+ (-2.0...2).step.size.should == 4
+ (1.0...6.4).step(1.8).size.should == 3
+ end
+
+ ruby_version_is ""..."3.4" do
+ it "returns nil with begin and end are String" do
+ ("A".."E").step(2).size.should == nil
+ ("A"..."E").step(2).size.should == nil
+ ("A".."E").step.size.should == nil
+ ("A"..."E").step.size.should == nil
+ end
+
+ it "return nil and not raises a TypeError if the first element does not respond to #succ" do
+ obj = mock("Range#step non-comparable")
+ obj.should_receive(:<=>).with(obj).and_return(1)
+ enum = (obj..obj).step
+ -> { enum.size }.should_not raise_error
+ enum.size.should == nil
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "returns nil with begin and end are String" do
+ ("A".."E").step("A").size.should == nil
+ ("A"..."E").step("A").size.should == nil
+ end
+
+ it "return nil and not raises a TypeError if the first element is not of compatible type" do
+ obj = mock("Range#step non-comparable")
+ obj.should_receive(:<=>).with(obj).and_return(1)
+ enum = (obj..obj).step(obj)
+ -> { enum.size }.should_not raise_error
+ enum.size.should == nil
+ end
+ end
+ end
+
+ # We use .take below to ensure the enumerator works
+ # because that's an Enumerable method and so it uses the Enumerator behavior
+ # not just a method overridden in Enumerator::ArithmeticSequence.
+ describe "type" 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
+ (1..10).step(3).take(4).should == [1, 4, 7, 10]
+ end
+ end
+
+ context "when begin is not defined and end is numeric" do
+ it "returns an instance of Enumerator::ArithmeticSequence" do
+ (..10).step.class.should == Enumerator::ArithmeticSequence
+ end
+ end
+
+ context "when range is endless" do
+ it "returns an instance of Enumerator::ArithmeticSequence when begin is numeric" do
+ (1..).step.class.should == Enumerator::ArithmeticSequence
+ (1..).step(2).take(3).should == [1, 3, 5]
+ end
+
+ ruby_version_is ""..."3.4" do
+ it "returns an instance of Enumerator when begin is not numeric" do
+ ("a"..).step.class.should == Enumerator
+ ("a"..).step(2).take(3).should == %w[a c e]
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "returns an instance of Enumerator when begin is not numeric" do
+ ("a"..).step("a").class.should == Enumerator
+ ("a"..).step("a").take(3).should == %w[a aa aaa]
+ end
+ end
+ end
+
+ context "when range is beginless and endless" do
+ ruby_version_is ""..."3.4" do
+ it "returns an instance of Enumerator" do
+ Range.new(nil, nil).step.class.should == Enumerator
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "raises an ArgumentError" do
+ -> { Range.new(nil, nil).step(1) }.should raise_error(ArgumentError)
+ end
+ end
+ end
+
+ context "when begin and end are not numerics" do
+ ruby_version_is ""..."3.4" do
+ it "returns an instance of Enumerator" do
+ ("a".."z").step.class.should == Enumerator
+ ("a".."z").step(3).take(4).should == %w[a d g j]
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "returns an instance of Enumerator" do
+ ("a".."z").step("a").class.should == Enumerator
+ ("a".."z").step("a").take(4).should == %w[a aa aaa aaaa]
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/ruby/core/range/to_a_spec.rb b/spec/ruby/core/range/to_a_spec.rb
new file mode 100644
index 0000000000..b1d3de32db
--- /dev/null
+++ b/spec/ruby/core/range/to_a_spec.rb
@@ -0,0 +1,39 @@
+require_relative '../../spec_helper'
+
+describe "Range#to_a" do
+ it "converts self to an array" do
+ (-5..5).to_a.should == [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
+ ('A'..'D').to_a.should == ['A','B','C','D']
+ ('A'...'D').to_a.should == ['A','B','C']
+ (0xfffd...0xffff).to_a.should == [0xfffd,0xfffe]
+ -> { (0.5..2.4).to_a }.should raise_error(TypeError)
+ end
+
+ it "returns empty array for descending-ordered" do
+ (5..-5).to_a.should == []
+ ('D'..'A').to_a.should == []
+ ('D'...'A').to_a.should == []
+ (0xffff...0xfffd).to_a.should == []
+ end
+
+ it "works with Ranges of 64-bit integers" do
+ large = 1 << 40
+ (large..large+1).to_a.should == [1099511627776, 1099511627777]
+ end
+
+ it "works with Ranges of Symbols" do
+ (:A..:z).to_a.size.should == 58
+ end
+
+ it "works for non-ASCII ranges" do
+ ('Σ'..'Ω').to_a.should == ["Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω"]
+ end
+
+ it "throws an exception for endless ranges" do
+ -> { eval("(1..)").to_a }.should raise_error(RangeError)
+ end
+
+ it "throws an exception for beginless ranges" do
+ -> { (..1).to_a }.should raise_error(TypeError)
+ end
+end
diff --git a/spec/ruby/core/range/to_s_spec.rb b/spec/ruby/core/range/to_s_spec.rb
new file mode 100644
index 0000000000..460c330912
--- /dev/null
+++ b/spec/ruby/core/range/to_s_spec.rb
@@ -0,0 +1,23 @@
+require_relative '../../spec_helper'
+
+describe "Range#to_s" do
+ it "provides a printable form of self" do
+ (0..21).to_s.should == "0..21"
+ (-8..0).to_s.should == "-8..0"
+ (-411..959).to_s.should == "-411..959"
+ ('A'..'Z').to_s.should == 'A..Z'
+ ('A'...'Z').to_s.should == 'A...Z'
+ (0xfff..0xfffff).to_s.should == "4095..1048575"
+ (0.5..2.4).to_s.should == "0.5..2.4"
+ end
+
+ it "can show endless ranges" do
+ eval("(1..)").to_s.should == "1.."
+ eval("(1.0...)").to_s.should == "1.0..."
+ end
+
+ it "can show beginless ranges" do
+ (..1).to_s.should == "..1"
+ (...1.0).to_s.should == "...1.0"
+ end
+end
diff --git a/spec/ruby/core/range/to_set_spec.rb b/spec/ruby/core/range/to_set_spec.rb
new file mode 100644
index 0000000000..589c0e9aed
--- /dev/null
+++ b/spec/ruby/core/range/to_set_spec.rb
@@ -0,0 +1,55 @@
+require_relative '../../spec_helper'
+require_relative '../enumerable/fixtures/classes'
+
+describe "Enumerable#to_set" do
+ it "returns a new Set created from self" do
+ (1..4).to_set.should == Set[1, 2, 3, 4]
+ (1...4).to_set.should == Set[1, 2, 3]
+ end
+
+ it "passes down passed blocks" do
+ (1..3).to_set { |x| x * x }.should == Set[1, 4, 9]
+ end
+
+ ruby_version_is "4.0" do
+ it "raises a RangeError if the range is infinite" do
+ -> { (1..).to_set }.should raise_error(RangeError, "cannot convert endless range to a set")
+ -> { (1...).to_set }.should raise_error(RangeError, "cannot convert endless range to a set")
+ end
+ end
+
+ ruby_version_is ""..."4.0" do
+ it "instantiates an object of provided as the first argument set class" do
+ set = (1..3).to_set(EnumerableSpecs::SetSubclass)
+ set.should be_kind_of(EnumerableSpecs::SetSubclass)
+ set.to_a.sort.should == [1, 2, 3]
+ end
+ end
+
+ ruby_version_is "4.0"..."4.1" do
+ it "instantiates an object of provided as the first argument set class and warns" do
+ set = nil
+ proc {
+ set = (1..3).to_set(EnumerableSpecs::SetSubclass)
+ }.should complain(/Enumerable#to_set/)
+ set.should be_kind_of(EnumerableSpecs::SetSubclass)
+ set.to_a.sort.should == [1, 2, 3]
+ end
+ end
+
+ ruby_version_is "4.1" do
+ it "does not accept any positional argument" do
+ -> {
+ (1..3).to_set(EnumerableSpecs::SetSubclass)
+ }.should raise_error(ArgumentError, 'wrong number of arguments (given 1, expected 0)')
+ end
+ end
+
+ it "does not need explicit `require 'set'`" do
+ output = ruby_exe(<<~RUBY, options: '--disable-gems', args: '2>&1')
+ puts (1..3).to_set.to_a.inspect
+ RUBY
+
+ output.chomp.should == "[1, 2, 3]"
+ end
+end