summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-09-16 13:19:06 -0700
committerJeremy Evans <code@jeremyevans.net>2019-09-17 16:22:44 -0700
commit9b35dc38644c10eed008f9ba19a7224f2fb49af2 (patch)
tree9c6c97cfd1cd828d8796145956621d6ac08ea6e8 /test/ruby
parent0785469a400b00eb4576fedbf93b614c70eaf760 (diff)
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.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2462
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/test_keyword.rb194
1 files changed, 194 insertions, 0 deletions
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}