diff options
author | Jeremy Evans <code@jeremyevans.net> | 2019-09-03 14:54:37 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2019-09-05 17:47:12 -0700 |
commit | 6f9b86616a8ad60cfed2979e2a0f8398a12e7c85 (patch) | |
tree | 5fc2b4bd2ac4d30512df59efb39ee406248d0804 /test | |
parent | 38dae1d510b931516ba1229a1ffbe5f6e470e292 (diff) | |
download | ruby-6f9b86616a8ad60cfed2979e2a0f8398a12e7c85.tar.gz |
Make Symbol#to_proc calls handle keyword arguments
Make rb_sym_proc_call take a flag for whether a keyword argument
is used, and use the new rb_funcall_with_block_kw function to
pass that information.
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_keyword.rb | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index b8d8736c5f..91e53e1dee 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -447,6 +447,79 @@ class TestKeywordArguments < Test::Unit::TestCase assert_equal([1, h3], c.send(:m, **h3)) end + def test_sym_proc_kwsplat + kw = {} + h = {'a'=>1} + h2 = {'a'=>1} + h3 = {'a'=>1, :a=>1} + + c = Object.new + def c.m(*args) + args + end + assert_equal([], :m.to_proc.call(c, **{})) + assert_equal([], :m.to_proc.call(c, **kw)) + assert_equal([h], :m.to_proc.call(c, **h)) + assert_equal([h2], :m.to_proc.call(c, **h2)) + assert_equal([h3], :m.to_proc.call(c, **h3)) + + c.singleton_class.remove_method(:m) + def c.m; end + assert_nil(:m.to_proc.call(c, **{})) + assert_nil(:m.to_proc.call(c, **kw)) + assert_raise(ArgumentError) { :m.to_proc.call(c, **h) } + assert_raise(ArgumentError) { :m.to_proc.call(c, **h2) } + assert_raise(ArgumentError) { :m.to_proc.call(c, **h3) } + + c.singleton_class.remove_method(:m) + def c.m(args) + args + end + assert_raise(ArgumentError) { :m.to_proc.call(c, **{}) } + assert_raise(ArgumentError) { :m.to_proc.call(c, **kw) } + assert_equal(h, :m.to_proc.call(c, **h)) + assert_equal(h2, :m.to_proc.call(c, **h2)) + assert_equal(h3, :m.to_proc.call(c, **h3)) + + c.singleton_class.remove_method(:m) + def c.m(**args) + args + end + assert_equal(kw, :m.to_proc.call(c, **{})) + assert_equal(kw, :m.to_proc.call(c, **kw)) + assert_equal(h, :m.to_proc.call(c, **h)) + assert_equal(h2, :m.to_proc.call(c, **h2)) + assert_equal(h3, :m.to_proc.call(c, **h3)) + + c.singleton_class.remove_method(:m) + def c.m(arg, **args) + [arg, args] + end + assert_raise(ArgumentError) { :m.to_proc.call(c, **{}) } + assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do + assert_equal([kw, kw], :m.to_proc.call(c, **kw)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do + assert_equal([h, kw], :m.to_proc.call(c, **h)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do + assert_equal([h2, kw], :m.to_proc.call(c, **h2)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do + assert_equal([h3, kw], :m.to_proc.call(c, **h3)) + end + + c.singleton_class.remove_method(:m) + def c.m(arg=1, **args) + [arg=1, args] + end + assert_equal([1, kw], :m.to_proc.call(c, **{})) + assert_equal([1, kw], :m.to_proc.call(c, **kw)) + assert_equal([1, h], :m.to_proc.call(c, **h)) + assert_equal([1, h2], :m.to_proc.call(c, **h2)) + assert_equal([1, h3], :m.to_proc.call(c, **h3)) + end + def test_method_missing_kwsplat kw = {} h = {'a'=>1} |