summaryrefslogtreecommitdiff
path: root/test/ruby/test_range.rb
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-02-09 20:37:34 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-02-09 20:37:34 +0900
commitda4464b824857d7610f9865ceb452ce0ead49164 (patch)
treefce8cb0636b401a5db8a225a3ef5c9fee9854d3b /test/ruby/test_range.rb
parent8edd350bda66a9ecb2c1043627679f2dc48d6f66 (diff)
[Bug #19426] Fix endless `Range#step` with `#succ` method
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/7277
Diffstat (limited to 'test/ruby/test_range.rb')
-rw-r--r--test/ruby/test_range.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb
index bb5ef6df8f..0a131644e5 100644
--- a/test/ruby/test_range.rb
+++ b/test/ruby/test_range.rb
@@ -392,6 +392,26 @@ class TestRange < Test::Unit::TestCase
assert_equal(4, (1.0...5.6).step(1.5).to_a.size)
end
+ def test_step_with_succ
+ c = Struct.new(:i) do
+ def succ; self.class.new(i+1); end
+ def <=>(other) i <=> other.i;end
+ end.new(0)
+
+ result = []
+ (c..c.succ).step(2) do |d|
+ result << d.i
+ end
+ assert_equal([0], result)
+
+ result = []
+ (c..).step(2) do |d|
+ result << d.i
+ break if d.i >= 4
+ end
+ assert_equal([0, 2, 4], result)
+ end
+
def test_each
a = []
(0..10).each {|x| a << x }
@@ -456,6 +476,26 @@ class TestRange < Test::Unit::TestCase
assert_equal(["a", "b", "c"], a)
end
+ def test_each_with_succ
+ c = Struct.new(:i) do
+ def succ; self.class.new(i+1); end
+ def <=>(other) i <=> other.i;end
+ end.new(0)
+
+ result = []
+ (c..c.succ).each do |d|
+ result << d.i
+ end
+ assert_equal([0, 1], result)
+
+ result = []
+ (c..).each do |d|
+ result << d.i
+ break if d.i >= 4
+ end
+ assert_equal([0, 1, 2, 3, 4], result)
+ end
+
def test_begin_end
assert_equal(0, (0..1).begin)
assert_equal(1, (0..1).end)