summaryrefslogtreecommitdiff
path: root/test/ruby/test_keyword.rb
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-09-17 14:32:19 -0700
committerJeremy Evans <code@jeremyevans.net>2019-09-17 16:22:44 -0700
commit775365cbd2bf17195e694771fc1c15698273a640 (patch)
treeffed420e45f7a91d203acf2a22ac66650af4e214 /test/ruby/test_keyword.rb
parent9b35dc38644c10eed008f9ba19a7224f2fb49af2 (diff)
Fix keyword argument separation issues with sym procs when using refinements
Make sure that vm_yield_with_cfunc can correctly set the empty keyword flag by passing 2 as the kw_splat value when calling it in vm_invoke_ifunc_block. Make sure calling.kw_splat is set to 1 and not 128 in vm_sendish, so we can safely check for different kw_splat values. vm_args.c needs to call add_empty_keyword, and to make JIT happy, the function needs to be exported. Rename the function to rb_adjust_argv_kw_splat to more accurately reflect what it does, and mark it as MJIT exported.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2462
Diffstat (limited to 'test/ruby/test_keyword.rb')
-rw-r--r--test/ruby/test_keyword.rb303
1 files changed, 303 insertions, 0 deletions
diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb
index d99a73ff0c..fa65074d17 100644
--- a/test/ruby/test_keyword.rb
+++ b/test/ruby/test_keyword.rb
@@ -2731,3 +2731,306 @@ class TestKeywordArguments < Test::Unit::TestCase
assert_valid_syntax("bug15087(**{}, &nil)")
end
end
+
+class TestKeywordArgumentsSymProcRefinements < Test::Unit::TestCase
+ class C
+ def call(*args, **kw)
+ yield(self, *args, **kw)
+ end
+ end
+ using(Module.new do
+ refine C do
+ def m(*args, **kw)
+ super
+ end
+ end
+ end)
+
+ def test_sym_proc_refine_kwsplat
+ kw = {}
+ h = {:a=>1}
+ h2 = {'a'=>1}
+ h3 = {'a'=>1, :a=>1}
+
+ c = C.new
+ def c.m(*args)
+ args
+ end
+ assert_equal([], c.call(**{}, &:m))
+ assert_equal([], c.call(**kw, &:m))
+ assert_equal([h], c.call(**h, &:m))
+ assert_equal([h], c.call(h, **{}, &:m))
+ assert_equal([h], c.call(a: 1, &:m))
+ assert_equal([h2], c.call(**h2, &:m))
+ assert_equal([h3], c.call(**h3, &:m))
+ assert_equal([h3], c.call(a: 1, **h2, &:m))
+
+ c.singleton_class.remove_method(:m)
+ def c.m; end
+ assert_nil(c.call(**{}, &:m))
+ assert_nil(c.call(**kw, &:m))
+ assert_raise(ArgumentError) { c.call(**h, &:m) }
+ assert_raise(ArgumentError) { c.call(a: 1, &:m) }
+ assert_raise(ArgumentError) { c.call(**h2, &:m) }
+ assert_raise(ArgumentError) { c.call(**h3, &:m) }
+ assert_raise(ArgumentError) { c.call(a: 1, **h2, &:m) }
+
+ c.singleton_class.remove_method(:m)
+ def c.m(args)
+ args
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+ assert_equal(kw, c.call(**{}, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+ assert_equal(kw, c.call(**kw, &:m))
+ end
+ assert_equal(h, c.call(**h, &:m))
+ assert_equal(h, c.call(a: 1, &:m))
+ assert_equal(h2, c.call(**h2, &:m))
+ assert_equal(h3, c.call(**h3, &:m))
+ assert_equal(h3, c.call(a: 1, **h2, &:m))
+
+ c.singleton_class.remove_method(:m)
+ def c.m(**args)
+ args
+ end
+ assert_equal(kw, c.call(**{}, &:m))
+ assert_equal(kw, c.call(**kw, &:m))
+ assert_equal(h, c.call(**h, &:m))
+ assert_equal(h, c.call(a: 1, &:m))
+ assert_equal(h2, c.call(**h2, &:m))
+ assert_equal(h3, c.call(**h3, &:m))
+ assert_equal(h3, c.call(a: 1, **h2, &:m))
+
+ c.singleton_class.remove_method(:m)
+ def c.m(arg, **args)
+ [arg, args]
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+ assert_equal([kw, kw], c.call(**{}, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+ assert_equal([kw, kw], c.call(**kw, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+ assert_equal([h, kw], c.call(**h, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+ assert_equal([h, kw], c.call(a: 1, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+ assert_equal([h2, kw], c.call(**h2, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+ assert_equal([h3, kw], c.call(**h3, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
+ assert_equal([h3, kw], c.call(a: 1, **h2, &:m))
+ end
+
+ c.singleton_class.remove_method(:m)
+ def c.m(arg=1, **args)
+ [arg=1, args]
+ end
+ assert_equal([1, kw], c.call(**{}, &:m))
+ assert_equal([1, kw], c.call(**kw, &:m))
+ assert_equal([1, h], c.call(**h, &:m))
+ assert_equal([1, h], c.call(a: 1, &:m))
+ assert_equal([1, h2], c.call(**h2, &:m))
+ assert_equal([1, h3], c.call(**h3, &:m))
+ assert_equal([1, h3], c.call(a: 1, **h2, &:m))
+ end
+
+ def test_sym_proc_refine_super_method_missing_kwsplat
+ kw = {}
+ h = {:a=>1}
+ h2 = {'a'=>1}
+ h3 = {'a'=>1, :a=>1}
+
+ c = C.new
+ def c.method_missing(_, *args)
+ args
+ end
+ assert_equal([], c.call(**{}, &:m))
+ assert_equal([], c.call(**kw, &:m))
+ assert_equal([h], c.call(**h, &:m))
+ assert_equal([h], c.call(h, **{}, &:m))
+ assert_equal([h], c.call(a: 1, &:m))
+ assert_equal([h2], c.call(**h2, &:m))
+ assert_equal([h3], c.call(**h3, &:m))
+ assert_equal([h3], c.call(a: 1, **h2, &:m))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_) end
+ assert_nil(c.call(**{}, &:m))
+ assert_nil(c.call(**kw, &:m))
+ assert_raise(ArgumentError) { c.call(**h, &:m) }
+ assert_raise(ArgumentError) { c.call(a: 1, &:m) }
+ assert_raise(ArgumentError) { c.call(**h2, &:m) }
+ assert_raise(ArgumentError) { c.call(**h3, &:m) }
+ assert_raise(ArgumentError) { c.call(a: 1, **h2, &:m) }
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, args)
+ args
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal(kw, c.call(**{}, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal(kw, c.call(**kw, &:m))
+ end
+ assert_equal(h, c.call(**h, &:m))
+ assert_equal(h, c.call(a: 1, &:m))
+ assert_equal(h2, c.call(**h2, &:m))
+ assert_equal(h3, c.call(**h3, &:m))
+ assert_equal(h3, c.call(a: 1, **h2, &:m))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, **args)
+ args
+ end
+ assert_equal(kw, c.call(**{}, &:m))
+ assert_equal(kw, c.call(**kw, &:m))
+ assert_equal(h, c.call(**h, &:m))
+ assert_equal(h, c.call(a: 1, &:m))
+ assert_equal(h2, c.call(**h2, &:m))
+ assert_equal(h3, c.call(**h3, &:m))
+ assert_equal(h3, c.call(a: 1, **h2, &:m))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, arg, **args)
+ [arg, args]
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([kw, kw], c.call(**{}, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([kw, kw], c.call(**kw, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h, kw], c.call(**h, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h, kw], c.call(a: 1, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h2, kw], c.call(**h2, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h3, kw], c.call(**h3, &:m))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h3, kw], c.call(a: 1, **h2, &:m))
+ end
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, arg=1, **args)
+ [arg=1, args]
+ end
+ assert_equal([1, kw], c.call(**{}, &:m))
+ assert_equal([1, kw], c.call(**kw, &:m))
+ assert_equal([1, h], c.call(**h, &:m))
+ assert_equal([1, h], c.call(a: 1, &:m))
+ assert_equal([1, h2], c.call(**h2, &:m))
+ assert_equal([1, h3], c.call(**h3, &:m))
+ assert_equal([1, h3], c.call(a: 1, **h2, &:m))
+ end
+
+ def test_sym_proc_refine_method_missing_kwsplat
+ kw = {}
+ h = {:a=>1}
+ h2 = {'a'=>1}
+ h3 = {'a'=>1, :a=>1}
+
+ c = C.new
+ def c.method_missing(_, *args)
+ args
+ end
+ assert_equal([], c.call(**{}, &:m2))
+ assert_equal([], c.call(**kw, &:m2))
+ assert_equal([h], c.call(**h, &:m2))
+ assert_equal([h], c.call(h, **{}, &:m2))
+ assert_equal([h], c.call(a: 1, &:m2))
+ assert_equal([h2], c.call(**h2, &:m2))
+ assert_equal([h3], c.call(**h3, &:m2))
+ assert_equal([h3], c.call(a: 1, **h2, &:m2))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_) end
+ assert_nil(c.call(**{}, &:m2))
+ assert_nil(c.call(**kw, &:m2))
+ assert_raise(ArgumentError) { c.call(**h, &:m2) }
+ assert_raise(ArgumentError) { c.call(a: 1, &:m2) }
+ assert_raise(ArgumentError) { c.call(**h2, &:m2) }
+ assert_raise(ArgumentError) { c.call(**h3, &:m2) }
+ assert_raise(ArgumentError) { c.call(a: 1, **h2, &:m2) }
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, args)
+ args
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal(kw, c.call(**{}, &:m2))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal(kw, c.call(**kw, &:m2))
+ end
+ assert_equal(h, c.call(**h, &:m2))
+ assert_equal(h, c.call(a: 1, &:m2))
+ assert_equal(h2, c.call(**h2, &:m2))
+ assert_equal(h3, c.call(**h3, &:m2))
+ assert_equal(h3, c.call(a: 1, **h2, &:m2))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, **args)
+ args
+ end
+ assert_equal(kw, c.call(**{}, &:m2))
+ assert_equal(kw, c.call(**kw, &:m2))
+ assert_equal(h, c.call(**h, &:m2))
+ assert_equal(h, c.call(a: 1, &:m2))
+ assert_equal(h2, c.call(**h2, &:m2))
+ assert_equal(h3, c.call(**h3, &:m2))
+ assert_equal(h3, c.call(a: 1, **h2, &:m2))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, arg, **args)
+ [arg, args]
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([kw, kw], c.call(**{}, &:m2))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([kw, kw], c.call(**kw, &:m2))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h, kw], c.call(**h, &:m2))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h, kw], c.call(a: 1, &:m2))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h2, kw], c.call(**h2, &:m2))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h3, kw], c.call(**h3, &:m2))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h3, kw], c.call(a: 1, **h2, &:m2))
+ end
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, arg=1, **args)
+ [arg=1, args]
+ end
+ assert_equal([1, kw], c.call(**{}, &:m2))
+ assert_equal([1, kw], c.call(**kw, &:m2))
+ assert_equal([1, h], c.call(**h, &:m2))
+ assert_equal([1, h], c.call(a: 1, &:m2))
+ assert_equal([1, h2], c.call(**h2, &:m2))
+ assert_equal([1, h3], c.call(**h3, &:m2))
+ assert_equal([1, h3], c.call(a: 1, **h2, &:m2))
+ end
+end