summaryrefslogtreecommitdiff
path: root/test/ruby/test_iterator.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_iterator.rb')
-rw-r--r--test/ruby/test_iterator.rb89
1 files changed, 56 insertions, 33 deletions
diff --git a/test/ruby/test_iterator.rb b/test/ruby/test_iterator.rb
index 362becc0e0..1bb655d52e 100644
--- a/test/ruby/test_iterator.rb
+++ b/test/ruby/test_iterator.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: false
require 'test/unit'
class Array
@@ -11,28 +12,23 @@ class Array
end
class TestIterator < Test::Unit::TestCase
- def ttt
- assert(iterator?)
- end
-
- def test_iterator
- assert(!iterator?)
-
- ttt{}
-
- # yield at top level !! here's not toplevel
- assert(!defined?(yield))
+ def test_yield_at_toplevel
+ assert_separately([],"#{<<~"begin;"}\n#{<<~'end;'}")
+ begin;
+ assert(!block_given?)
+ assert(!defined?(yield))
+ end;
end
def test_array
- $x = [1, 2, 3, 4]
- $y = []
+ x = [1, 2, 3, 4]
+ y = []
# iterator over array
- for i in $x
- $y.push i
+ for i in x
+ y.push i
end
- assert_equal($x, $y)
+ assert_equal(x, y)
end
def tt
@@ -73,42 +69,52 @@ class TestIterator < Test::Unit::TestCase
def test_break
done = true
loop{
- break
+ break if true
done = false # should not reach here
}
assert(done)
done = false
- $bad = false
+ bad = false
loop {
break if done
done = true
- next
- $bad = true # should not reach here
+ next if true
+ bad = true # should not reach here
}
- assert(!$bad)
+ assert(!bad)
done = false
- $bad = false
+ bad = false
loop {
break if done
done = true
- redo
- $bad = true # should not reach here
+ redo if true
+ bad = true # should not reach here
}
- assert(!$bad)
+ assert(!bad)
- $x = []
+ x = []
for i in 1 .. 7
- $x.push i
+ x.push i
end
- assert_equal(7, $x.size)
- assert_equal([1, 2, 3, 4, 5, 6, 7], $x)
+ assert_equal(7, x.size)
+ assert_equal([1, 2, 3, 4, 5, 6, 7], x)
+ end
+
+ def test_array_for_masgn
+ a = [Struct.new(:to_ary).new([1,2])]
+ x = []
+ a.each {|i,j|x << [i,j]}
+ assert_equal([[1,2]], x)
+ x = []
+ for i,j in a; x << [i,j]; end
+ assert_equal([[1,2]], x)
end
def test_append_method_to_built_in_class
- $x = [[1,2],[3,4],[5,6]]
- assert_equal($x.iter_test1{|x|x}, $x.iter_test2{|x|x})
+ x = [[1,2],[3,4],[5,6]]
+ assert_equal(x.iter_test1{|e|e}, x.iter_test2{|e|e})
end
class IterTest
@@ -169,10 +175,13 @@ class TestIterator < Test::Unit::TestCase
end
def test_block_given
+ verbose_bak, $VERBOSE = $VERBOSE, nil
assert(m1{p 'test'})
assert(m2{p 'test'})
assert(!m1())
assert(!m2())
+ ensure
+ $VERBOSE = verbose_bak
end
def m3(var, &block)
@@ -278,6 +287,9 @@ class TestIterator < Test::Unit::TestCase
def proc_call(&b)
b.call
end
+ def proc_call2(b)
+ b.call
+ end
def proc_yield()
yield
end
@@ -299,6 +311,18 @@ class TestIterator < Test::Unit::TestCase
def test_ljump
assert_raise(LocalJumpError) {get_block{break}.call}
+ begin
+ verbose_bak, $VERBOSE = $VERBOSE, nil
+ # See the commit https://github.com/ruby/ruby/commit/7d8a415bc2d08a1b5e9d1ea802493b6eeb99c219
+ # This block is not used but this is intentional.
+ # |
+ # +-----------------------------------------------------+
+ # |
+ # vv
+ assert_raise(LocalJumpError) {proc_call2(get_block{break}){}}
+ ensure
+ $VERBOSE = verbose_bak
+ end
# cannot use assert_nothing_raised due to passing block.
begin
@@ -329,8 +353,7 @@ class TestIterator < Test::Unit::TestCase
marity_test(:marity_test)
marity_test(:p)
- lambda(&method(:assert)).call(true)
- lambda(&get_block{|a,n| assert(a,n)}).call(true, "marity")
+ get_block{|a,n| assert(a,n)}.call(true, "marity")
end
def foo