From 9b35dc38644c10eed008f9ba19a7224f2fb49af2 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Mon, 16 Sep 2019 13:19:06 -0700 Subject: Pass keyword argument flag when rb_call_super_kw calls method_missing This makes method_missing take a flag for whether keyword arguments were passed. Adds tests both for rb_call_super_kw usage as well as general usage of super calling method_missing in Ruby methods. --- test/ruby/test_keyword.rb | 194 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index 5a85b85f47..d99a73ff0c 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -1,5 +1,6 @@ # frozen_string_literal: false require 'test/unit' +require '-test-/rb_call_super_kw' class TestKeywordArguments < Test::Unit::TestCase def f1(str: "foo", num: 424242) @@ -1518,6 +1519,199 @@ class TestKeywordArguments < Test::Unit::TestCase assert_equal([1, h3], c.m(a: 1, **h2)) end + def test_super_method_missing_kwsplat + kw = {} + h = {:a=>1} + h2 = {'a'=>1} + h3 = {'a'=>1, :a=>1} + + c = Class.new do + def m(*args, **kw) + super + end + end.new + def c.method_missing(_, *args) + args + end + assert_equal([], c.m(**{})) + assert_equal([], c.m(**kw)) + assert_equal([h], c.m(**h)) + assert_equal([h], c.m(a: 1)) + assert_equal([h2], c.m(**h2)) + assert_equal([h3], c.m(**h3)) + assert_equal([h3], c.m(a: 1, **h2)) + + c.singleton_class.remove_method(:method_missing) + def c.method_missing(_); end + assert_nil(c.m(**{})) + assert_nil(c.m(**kw)) + assert_raise(ArgumentError) { c.m(**h) } + assert_raise(ArgumentError) { c.m(a: 1) } + assert_raise(ArgumentError) { c.m(**h2) } + assert_raise(ArgumentError) { c.m(**h3) } + assert_raise(ArgumentError) { c.m(a: 1, **h2) } + + 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.m(**{})) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal(kw, c.m(**kw)) + end + assert_equal(h, c.m(**h)) + assert_equal(h, c.m(a: 1)) + assert_equal(h2, c.m(**h2)) + assert_equal(h3, c.m(**h3)) + assert_equal(h3, c.m(a: 1, **h2)) + + c.singleton_class.remove_method(:method_missing) + def c.method_missing(_, **args) + args + end + assert_equal(kw, c.m(**{})) + assert_equal(kw, c.m(**kw)) + assert_equal(h, c.m(**h)) + assert_equal(h, c.m(a: 1)) + assert_equal(h2, c.m(**h2)) + assert_equal(h3, c.m(a: 1, **h2)) + + 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.m(**{})) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([kw, kw], c.m(**kw)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h, kw], c.m(**h)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h, kw], c.m(a: 1)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h2, kw], c.m(**h2)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h3, kw], c.m(**h3)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h3, kw], c.m(a: 1, **h2)) + end + + c.singleton_class.remove_method(:method_missing) + def c.method_missing(_, arg=1, **args) + [arg=1, args] + end + assert_equal([1, kw], c.m(**{})) + assert_equal([1, kw], c.m(**kw)) + assert_equal([1, h], c.m(**h)) + assert_equal([1, h], c.m(a: 1)) + assert_equal([1, h2], c.m(**h2)) + assert_equal([1, h3], c.m(**h3)) + assert_equal([1, h3], c.m(a: 1, **h2)) + end + + def test_rb_call_super_kw_method_missing_kwsplat + kw = {} + h = {:a=>1} + h2 = {'a'=>1} + h3 = {'a'=>1, :a=>1} + + c = Object.new + c.extend Bug::RbCallSuperKw + def c.method_missing(_, *args) + args + end + assert_equal([], c.m(**{})) + assert_equal([], c.m(**kw)) + assert_equal([h], c.m(**h)) + assert_equal([h], c.m(a: 1)) + assert_equal([h2], c.m(**h2)) + assert_equal([h3], c.m(**h3)) + assert_equal([h3], c.m(a: 1, **h2)) + + c.singleton_class.remove_method(:method_missing) + def c.method_missing(_); end + assert_nil(c.m(**{})) + assert_nil(c.m(**kw)) + assert_raise(ArgumentError) { c.m(**h) } + assert_raise(ArgumentError) { c.m(a: 1) } + assert_raise(ArgumentError) { c.m(**h2) } + assert_raise(ArgumentError) { c.m(**h3) } + assert_raise(ArgumentError) { c.m(a: 1, **h2) } + + 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.m(**{})) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal(kw, c.m(**kw)) + end + assert_equal(h, c.m(**h)) + assert_equal(h, c.m(a: 1)) + assert_equal(h2, c.m(**h2)) + assert_equal(h3, c.m(**h3)) + assert_equal(h3, c.m(a: 1, **h2)) + + c.singleton_class.remove_method(:method_missing) + def c.method_missing(_, **args) + args + end + assert_equal(kw, c.m(**{})) + assert_equal(kw, c.m(**kw)) + assert_equal(h, c.m(**h)) + assert_equal(h, c.m(a: 1)) + assert_equal(h2, c.m(**h2)) + assert_equal(h3, c.m(a: 1, **h2)) + + 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.m(**{})) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([kw, kw], c.m(**kw)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h, kw], c.m(**h)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h, kw], c.m(a: 1)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h2, kw], c.m(**h2)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h3, kw], c.m(**h3)) + end + assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do + assert_equal([h3, kw], c.m(a: 1, **h2)) + end + + c.singleton_class.remove_method(:method_missing) + def c.method_missing(_, arg=1, **args) + [arg=1, args] + end + assert_equal([1, kw], c.m(**{})) + assert_equal([1, kw], c.m(**kw)) + assert_equal([1, h], c.m(**h)) + assert_equal([1, h], c.m(a: 1)) + assert_equal([1, h2], c.m(**h2)) + assert_equal([1, h3], c.m(**h3)) + assert_equal([1, h3], c.m(a: 1, **h2)) + end + def test_define_method_kwsplat kw = {} h = {:a=>1} -- cgit v1.2.3