diff options
author | Jeremy Evans <code@jeremyevans.net> | 2019-09-27 13:35:29 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2020-03-08 20:49:09 -0700 |
commit | f4394bbca361c2bb500f586ba0bf1bef8b919910 (patch) | |
tree | fea4e00e12d0dcbf6a76d7d4b7e95e6db34583c1 /test | |
parent | 040cfc89b9a110cd0fb2abdcd35e8215b4a71f60 (diff) | |
download | ruby-f4394bbca361c2bb500f586ba0bf1bef8b919910.tar.gz |
Do not autosplat when calling procs that accept rest and keywords
When providing a single array to a block that takes a splat, pass the
array as one argument of the splat instead of as the splat itself,
even if the block also accepts keyword arguments. Previously, this
behavior was only used for blocks that did not accept keywords.
Implements [Feature#16166]
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_keyword.rb | 19 | ||||
-rw-r--r-- | test/ruby/test_proc.rb | 27 |
2 files changed, 39 insertions, 7 deletions
diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index db7d696644..48d6a7336a 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -3284,15 +3284,20 @@ class TestKeywordArguments < Test::Unit::TestCase bug7665 = '[ruby-core:51278]' bug8463 = '[ruby-core:55203] [Bug #8463]' a = [*%w[foo bar], {zzz: 42}] - expect = a + [{}] - assert_equal(expect, rest_keyrest(*a), bug7665) + splat_expect = a + [{}] + nonsplat_expect = [a, {}] + assert_equal(splat_expect, rest_keyrest(*a), bug7665) + assert_equal(nonsplat_expect, rest_keyrest(a), bug7665) + pr = proc {|*args, **opt| next *args, opt} - assert_equal(expect, pr.call(*a), bug7665) - assert_equal(expect, pr.call(a), bug8463) + assert_equal(splat_expect, pr.call(*a), bug7665) + assert_equal(nonsplat_expect, pr.call(a), bug8463) + pr = proc {|a, *b, **opt| next a, *b, opt} - assert_equal(expect, pr.call(a), bug8463) + assert_equal(splat_expect, pr.call(a), bug8463) + pr = proc {|a, **opt| next a, opt} - assert_equal(expect.values_at(0, -1), pr.call(expect), bug8463) + assert_equal(splat_expect.values_at(0, -1), pr.call(splat_expect), bug8463) end def req_plus_keyword(x, **h) @@ -3662,7 +3667,7 @@ class TestKeywordArguments < Test::Unit::TestCase def test_nonsymbol_key result = m(["a" => 10]) { |a = nil, **b| [a, b] } - assert_equal([{"a" => 10}, {}], result) + assert_equal([[{"a" => 10}], {}], result) end def method_for_test_to_hash_call_during_setup_complex_parameters k1:, k2:, **rest_kw diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index b00f42d81a..14b79380e2 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -784,6 +784,33 @@ class TestProc < Test::Unit::TestCase assert_equal [[1, 2], Proc, :x], (pr.call(1, 2){|x| x}) end + def test_proc_args_only_rest + pr = proc {|*c| c } + assert_equal [], pr.call() + assert_equal [1], pr.call(1) + assert_equal [[1]], pr.call([1]) + assert_equal [1, 2], pr.call(1,2) + assert_equal [[1, 2]], pr.call([1,2]) + end + + def test_proc_args_rest_kw + pr = proc {|*c, a: 1| [c, a] } + assert_equal [[], 1], pr.call() + assert_equal [[1], 1], pr.call(1) + assert_equal [[[1]], 1], pr.call([1]) + assert_equal [[1, 2], 1], pr.call(1,2) + assert_equal [[[1, 2]], 1], pr.call([1,2]) + end + + def test_proc_args_rest_kwsplat + pr = proc {|*c, **kw| [c, kw] } + assert_equal [[], {}], pr.call() + assert_equal [[1], {}], pr.call(1) + assert_equal [[[1]], {}], pr.call([1]) + assert_equal [[1, 2], {}], pr.call(1,2) + assert_equal [[[1, 2]], {}], pr.call([1,2]) + end + def test_proc_args_pos_rest_post_block pr = proc {|a,b,*c,d,e,&f| [a, b, c, d, e, f.class, f&&f.call(:x)] |