mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* vm_insnhelper.c: set keyword hash on Proc/block calls.
[ruby-core:51172] [Bug #7630] * test/ruby/test_keyword.rb: add tests for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38657 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
b0767cf8c0
commit
371c07f2a2
3 changed files with 161 additions and 23 deletions
|
@ -135,4 +135,123 @@ class TestKeywordArguments < Test::Unit::TestCase
|
|||
assert_equal(["foo", 111111], f[num: 111111])
|
||||
assert_equal(["bar", 111111], f[str: "bar", num: 111111])
|
||||
end
|
||||
|
||||
|
||||
def p1
|
||||
Proc.new do |str: "foo", num: 424242|
|
||||
[str, num]
|
||||
end
|
||||
end
|
||||
|
||||
def test_p1
|
||||
assert_equal(["foo", 424242], p1[])
|
||||
assert_equal(["bar", 424242], p1[str: "bar"])
|
||||
assert_equal(["foo", 111111], p1[num: 111111])
|
||||
assert_equal(["bar", 111111], p1[str: "bar", num: 111111])
|
||||
assert_raise(ArgumentError) { p1[str: "bar", check: true] }
|
||||
assert_equal(["foo", 424242], p1["string"] )
|
||||
end
|
||||
|
||||
|
||||
def p2
|
||||
Proc.new do |x, str: "foo", num: 424242|
|
||||
[x, str, num]
|
||||
end
|
||||
end
|
||||
|
||||
def test_p2
|
||||
assert_equal([nil, "foo", 424242], p2[])
|
||||
assert_equal([:xyz, "foo", 424242], p2[:xyz])
|
||||
end
|
||||
|
||||
|
||||
def p3
|
||||
Proc.new do |str: "foo", num: 424242, **h|
|
||||
[str, num, h]
|
||||
end
|
||||
end
|
||||
|
||||
def test_p3
|
||||
assert_equal(["foo", 424242, {}], p3[])
|
||||
assert_equal(["bar", 424242, {}], p3[str: "bar"])
|
||||
assert_equal(["foo", 111111, {}], p3[num: 111111])
|
||||
assert_equal(["bar", 111111, {}], p3[str: "bar", num: 111111])
|
||||
assert_equal(["bar", 424242, {:check=>true}], p3[str: "bar", check: true])
|
||||
assert_equal(["foo", 424242, {}], p3["string"])
|
||||
end
|
||||
|
||||
|
||||
def p4
|
||||
Proc.new do |str: "foo", num: 424242, **h, &blk|
|
||||
[str, num, h, blk]
|
||||
end
|
||||
end
|
||||
|
||||
def test_p4
|
||||
assert_equal(["foo", 424242, {}, nil], p4[])
|
||||
assert_equal(["bar", 424242, {}, nil], p4[str: "bar"])
|
||||
assert_equal(["foo", 111111, {}, nil], p4[num: 111111])
|
||||
assert_equal(["bar", 111111, {}, nil], p4[str: "bar", num: 111111])
|
||||
assert_equal(["bar", 424242, {:check=>true}, nil], p4[str: "bar", check: true])
|
||||
a = p4.call {|x| x + 42 }
|
||||
assert_equal(["foo", 424242, {}], a[0, 3])
|
||||
assert_equal(43, a.last.call(1))
|
||||
end
|
||||
|
||||
|
||||
def p5
|
||||
Proc.new do |*r, str: "foo", num: 424242, **h|
|
||||
[r, str, num, h]
|
||||
end
|
||||
end
|
||||
|
||||
def test_p5
|
||||
assert_equal([[], "foo", 424242, {}], p5[])
|
||||
assert_equal([[], "bar", 424242, {}], p5[str: "bar"])
|
||||
assert_equal([[], "foo", 111111, {}], p5[num: 111111])
|
||||
assert_equal([[], "bar", 111111, {}], p5[str: "bar", num: 111111])
|
||||
assert_equal([[1], "foo", 424242, {}], p5[1])
|
||||
assert_equal([[1, 2], "foo", 424242, {}], p5[1, 2])
|
||||
assert_equal([[1, 2, 3], "foo", 424242, {}], p5[1, 2, 3])
|
||||
assert_equal([[1], "bar", 424242, {}], p5[1, str: "bar"])
|
||||
assert_equal([[1, 2], "bar", 424242, {}], p5[1, 2, str: "bar"])
|
||||
assert_equal([[1, 2, 3], "bar", 424242, {}], p5[1, 2, 3, str: "bar"])
|
||||
end
|
||||
|
||||
|
||||
def p6
|
||||
Proc.new do |o1, o2=42, *args, p, k: :key, **kw, &b|
|
||||
[o1, o2, args, p, k, kw, b]
|
||||
end
|
||||
end
|
||||
|
||||
def test_p6
|
||||
assert_equal([nil, 42, [], nil, :key, {}, nil], p6[])
|
||||
assert_equal([1, 42, [], 2, :key, {}, nil], p6[1, 2])
|
||||
assert_equal([1, 2, [], 3, :key, {}, nil], p6[1, 2, 3])
|
||||
assert_equal([1, 2, [3], 4, :key, {}, nil], p6[1, 2, 3, 4])
|
||||
assert_equal([1, 2, [3, 4], 5, :key, {str: "bar"}, nil], p6[1, 2, 3, 4, 5, str: "bar"])
|
||||
end
|
||||
|
||||
def test_proc_parameters
|
||||
assert_equal([[:key, :str], [:key, :num]], p1.parameters);
|
||||
assert_equal([[:opt, :x], [:key, :str], [:key, :num]], p2.parameters);
|
||||
assert_equal([[:key, :str], [:key, :num], [:keyrest, :h]], p3.parameters);
|
||||
assert_equal([[:key, :str], [:key, :num], [:keyrest, :h], [:block, :blk]], p4.parameters);
|
||||
assert_equal([[:rest, :r], [:key, :str], [:key, :num], [:keyrest, :h]], p5.parameters);
|
||||
assert_equal([[:opt, :o1], [:opt, :o2], [:rest, :args], [:opt, :p], [:key, :k],
|
||||
[:keyrest, :kw], [:block, :b]], p6.parameters)
|
||||
end
|
||||
|
||||
def m1(*args)
|
||||
yield *args
|
||||
end
|
||||
|
||||
def test_block
|
||||
blk = Proc.new {|str: "foo", num: 424242| [str, num] }
|
||||
assert_equal(["foo", 424242], m1(&blk))
|
||||
assert_equal(["bar", 424242], m1(str: "bar", &blk))
|
||||
assert_equal(["foo", 111111], m1(num: 111111, &blk))
|
||||
assert_equal(["bar", 111111], m1(str: "bar", num: 111111, &blk))
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue