mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
7567fbf080
ex) def m;yield 1, 2; end; m{|v| p v} #=> 1 * parse.y: apply above change for "for" statement. * test/ruby/test_assignment.rb: ditto * test/ruby/test_basicinstructions.rb: ditto. * test/ruby/test_iterator.rb: ditto. * test/ruby/test_yield.rb: ditto. * compile.c (iseq_compile_each): fix debug. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12544 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
67 lines
1.6 KiB
Ruby
67 lines
1.6 KiB
Ruby
require 'test/unit'
|
|
|
|
class TestRubyYield < Test::Unit::TestCase
|
|
|
|
def test_ary_each
|
|
ary = [1]
|
|
ary.each {|a, b, c, d| assert_equal [1,nil,nil,nil], [a,b,c,d] }
|
|
ary.each {|a, b, c| assert_equal [1,nil,nil], [a,b,c] }
|
|
ary.each {|a, b| assert_equal [1,nil], [a,b] }
|
|
ary.each {|a| assert_equal 1, a }
|
|
end
|
|
|
|
def test_hash_each
|
|
h = {:a => 1}
|
|
h.each do |k, v|
|
|
assert_equal :a, k
|
|
assert_equal 1, v
|
|
end
|
|
h.each do |k|
|
|
assert_equal :a, k # changed at 1.9
|
|
end
|
|
end
|
|
|
|
def test_yield_0
|
|
assert_equal 1, iter0 { 1 }
|
|
assert_equal 2, iter0 { 2 }
|
|
end
|
|
|
|
def iter0
|
|
yield
|
|
end
|
|
|
|
def test_yield_1
|
|
iter1([]) {|a, b| assert_equal [nil,nil], [a, b] }
|
|
iter1([1]) {|a, b| assert_equal [1,nil], [a, b] }
|
|
iter1([1, 2]) {|a, b| assert_equal [1,2], [a,b] }
|
|
iter1([1, 2, 3]) {|a, b| assert_equal [1,2], [a,b] }
|
|
|
|
iter1([]) {|a| assert_equal [], a }
|
|
iter1([1]) {|a| assert_equal [1], a }
|
|
iter1([1, 2]) {|a| assert_equal [1,2], a }
|
|
iter1([1, 2, 3]) {|a| assert_equal [1,2,3], a }
|
|
end
|
|
|
|
def iter1(args)
|
|
yield args
|
|
end
|
|
|
|
def test_yield2
|
|
def iter2_1() yield 1, *[2, 3] end
|
|
iter2_1 {|a, b, c| assert_equal [1,2,3], [a,b,c] }
|
|
def iter2_2() yield 1, *[] end
|
|
iter2_2 {|a, b, c| assert_equal [1,nil,nil], [a,b,c] }
|
|
def iter2_3() yield 1, *[2] end
|
|
iter2_3 {|a, b, c| assert_equal [1,2,nil], [a,b,c] }
|
|
end
|
|
|
|
def test_yield_nested
|
|
[[1, [2, 3]]].each {|a, (b, c)|
|
|
assert_equal [1,2,3], [a,b,c]
|
|
}
|
|
[[1, [2, 3]]].map {|a, (b, c)|
|
|
assert_equal [1,2,3], [a,b,c]
|
|
}
|
|
end
|
|
|
|
end
|