aboutsummaryrefslogtreecommitdiffstats
path: root/test/ruby/test_keyword.rb
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-09-05 10:36:28 -0700
committerJeremy Evans <code@jeremyevans.net>2019-09-05 17:47:12 -0700
commitd1ef73b59cede58f2173fa0f4ff7480a820f25d6 (patch)
tree39e3f12eb37469d60d4f027da3f0aa6f68936bf5 /test/ruby/test_keyword.rb
parent55b96c5d2d7d8bcc2953484bd2f9c9519b252dae (diff)
downloadruby-d1ef73b59cede58f2173fa0f4ff7480a820f25d6.tar.gz
Always remove empty keyword hashes when calling methods
While doing so is not backwards compatible with Ruby 2.6, it is necessary for generic argument forwarding to work for all methods: ```ruby def foo(*args, **kw, &block) bar(*args, **kw, &block) end ``` If you do not remove empty keyword hashes, and bar does not accept keyword arguments, then a call to foo without keyword arguments calls bar with an extra positional empty hash argument.
Diffstat (limited to 'test/ruby/test_keyword.rb')
-rw-r--r--test/ruby/test_keyword.rb48
1 files changed, 16 insertions, 32 deletions
diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb
index 71b72bd4ed..b337343420 100644
--- a/test/ruby/test_keyword.rb
+++ b/test/ruby/test_keyword.rb
@@ -185,9 +185,7 @@ class TestKeywordArguments < Test::Unit::TestCase
f = -> { true }
assert_equal(true, f[**{}])
- assert_warn(/The keyword argument is passed as the last hash parameter/m) do
- assert_raise(ArgumentError) { f[**kw] }
- end
+ assert_equal(true, f[**kw])
assert_warn(/The keyword argument is passed as the last hash parameter/m) do
assert_raise(ArgumentError) { f[**h] }
end
@@ -203,9 +201,7 @@ class TestKeywordArguments < Test::Unit::TestCase
f = ->(a) { a }
assert_raise(ArgumentError) { f[**{}] }
- assert_warn(/The keyword argument is passed as the last hash parameter/m) do
- assert_equal(kw, f[**kw])
- end
+ assert_raise(ArgumentError) { f[**kw] }
assert_warn(/The keyword argument is passed as the last hash parameter/m) do
assert_equal(h, f[**h])
end
@@ -283,7 +279,7 @@ class TestKeywordArguments < Test::Unit::TestCase
end
end
assert_equal([], c[**{}].args)
- assert_equal([{}], c[**kw].args)
+ assert_equal([], c[**kw].args)
assert_equal([h], c[**h].args)
assert_equal([h], c[a: 1].args)
assert_equal([h2], c[**h2].args)
@@ -294,7 +290,7 @@ class TestKeywordArguments < Test::Unit::TestCase
def initialize; end
end
assert_nil(c[**{}].args)
- assert_raise(ArgumentError) { c[**kw] }
+ assert_nil(c[**kw].args)
assert_raise(ArgumentError) { c[**h] }
assert_raise(ArgumentError) { c[a: 1] }
assert_raise(ArgumentError) { c[**h2] }
@@ -307,7 +303,7 @@ class TestKeywordArguments < Test::Unit::TestCase
end
end
assert_raise(ArgumentError) { c[**{}] }
- assert_equal(kw, c[**kw].args)
+ assert_raise(ArgumentError) { c[**kw] }
assert_equal(h, c[**h].args)
assert_equal(h, c[a: 1].args)
assert_equal(h2, c[**h2].args)
@@ -333,7 +329,7 @@ class TestKeywordArguments < Test::Unit::TestCase
end
end
assert_raise(ArgumentError) { c[**{}] }
- assert_equal([kw, kw], c[**kw].args)
+ assert_raise(ArgumentError) { c[**kw] }
assert_equal([h, kw], c[**h].args)
assert_equal([h, kw], c[a: 1].args)
assert_equal([h2, kw], c[**h2].args)
@@ -365,7 +361,7 @@ class TestKeywordArguments < Test::Unit::TestCase
args
end
assert_equal([], c.method(:m)[**{}])
- assert_equal([{}], c.method(:m)[**kw])
+ assert_equal([], c.method(:m)[**kw])
assert_equal([h], c.method(:m)[**h])
assert_equal([h], c.method(:m)[a: 1])
assert_equal([h2], c.method(:m)[**h2])
@@ -375,7 +371,7 @@ class TestKeywordArguments < Test::Unit::TestCase
c.singleton_class.remove_method(:m)
def c.m; end
assert_nil(c.method(:m)[**{}])
- assert_raise(ArgumentError) { c.method(:m)[**kw] }
+ assert_nil(c.method(:m)[**kw])
assert_raise(ArgumentError) { c.method(:m)[**h] }
assert_raise(ArgumentError) { c.method(:m)[a: 1] }
assert_raise(ArgumentError) { c.method(:m)[**h2] }
@@ -387,7 +383,7 @@ class TestKeywordArguments < Test::Unit::TestCase
args
end
assert_raise(ArgumentError) { c.method(:m)[**{}] }
- assert_equal(kw, c.method(:m)[**kw])
+ assert_raise(ArgumentError) { c.method(:m)[**kw] }
assert_equal(h, c.method(:m)[**h])
assert_equal(h, c.method(:m)[a: 1])
assert_equal(h2, c.method(:m)[**h2])
@@ -411,7 +407,7 @@ class TestKeywordArguments < Test::Unit::TestCase
[arg, args]
end
assert_raise(ArgumentError) { c.method(:m)[**{}] }
- assert_equal([kw, kw], c.method(:m)[**kw])
+ assert_raise(ArgumentError) { c.method(:m)[**kw] }
assert_equal([h, kw], c.method(:m)[**h])
assert_equal([h, kw], c.method(:m)[a: 1])
assert_equal([h2, kw], c.method(:m)[**h2])
@@ -487,9 +483,7 @@ class TestKeywordArguments < Test::Unit::TestCase
[arg, args]
end
assert_raise(ArgumentError) { c.send(:m, **{}) }
- assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
- assert_equal([kw, kw], c.send(:m, **kw))
- end
+ assert_raise(ArgumentError) { c.send(:m, **kw) }
assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do
assert_equal([h, kw], c.send(:m, **h))
end
@@ -576,9 +570,7 @@ class TestKeywordArguments < Test::Unit::TestCase
[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_raise(ArgumentError) { :m.to_proc.call(c, **kw) }
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
@@ -664,9 +656,7 @@ class TestKeywordArguments < Test::Unit::TestCase
[arg, args]
end
assert_raise(ArgumentError) { c.m(**{}) }
- 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_raise(ArgumentError) { c.m(**kw) }
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
@@ -707,9 +697,7 @@ class TestKeywordArguments < Test::Unit::TestCase
define_method(:m) { }
end
assert_nil(c.m(**{}))
- assert_warn(/The keyword argument is passed as the last hash parameter/m) do
- assert_raise(ArgumentError) { c.m(**kw) }
- end
+ assert_nil(c.m(**kw))
assert_warn(/The keyword argument is passed as the last hash parameter/m) do
assert_raise(ArgumentError) { c.m(**h) }
end
@@ -731,9 +719,7 @@ class TestKeywordArguments < Test::Unit::TestCase
define_method(:m) {|arg| arg }
end
assert_raise(ArgumentError) { c.m(**{}) }
- assert_warn(/The keyword argument is passed as the last hash parameter/m) do
- assert_equal(kw, c.m(**kw))
- end
+ assert_raise(ArgumentError) { c.m(**kw) }
assert_warn(/The keyword argument is passed as the last hash parameter/m) do
assert_equal(h, c.m(**h))
end
@@ -779,9 +765,7 @@ class TestKeywordArguments < Test::Unit::TestCase
define_method(:m) {|arg, **opt| [arg, opt] }
end
assert_raise(ArgumentError) { c.m(**{}) }
- assert_warn(/The keyword argument is passed as the last hash parameter/m) do
- assert_equal([kw, kw], c.m(**kw))
- end
+ assert_raise(ArgumentError) { c.m(**kw) }
assert_warn(/The keyword argument is passed as the last hash parameter/m) do
assert_equal([h, kw], c.m(**h))
end