summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-04-19 22:19:41 +0900
committerJeremy Evans <code@jeremyevans.net>2019-08-30 12:39:31 -0700
commit6a9ce1fea89bc5c6518dd6bb7ff3b824a9321976 (patch)
treed1e954802cfcb8fafb71b2e16dd7d957326d70d7 /test/ruby
parentafae8555da07b2349a245d6646e3b36a49f027d5 (diff)
Support **nil syntax for specifying a method does not accept keyword arguments
This syntax means the method should be treated as a method that uses keyword arguments, but no specific keyword arguments are supported, and therefore calling the method with keyword arguments will raise an ArgumentError. It is still allowed to double splat an empty hash when calling the method, as that does not pass any keyword arguments.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2395
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/test_keyword.rb28
-rw-r--r--test/ruby/test_syntax.rb16
2 files changed, 44 insertions, 0 deletions
diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb
index 791d60b70a..1e707170fd 100644
--- a/test/ruby/test_keyword.rb
+++ b/test/ruby/test_keyword.rb
@@ -126,6 +126,34 @@ class TestKeywordArguments < Test::Unit::TestCase
assert_equal(1, f10(b: 42))
end
+ def f11(**nil)
+ local_variables
+ end
+
+ def test_f11
+ h = {}
+
+ assert_equal([], f11)
+ assert_equal([], f11(**{}))
+ assert_equal([], f11(**h))
+ end
+
+ def f12(**nil, &b)
+ [b, local_variables]
+ end
+
+ def test_f12
+ h = {}
+ b = proc{}
+
+ assert_equal([nil, [:b]], f12)
+ assert_equal([nil, [:b]], f12(**{}))
+ assert_equal([nil, [:b]], f12(**h))
+ assert_equal([b, [:b]], f12(&b))
+ assert_equal([b, [:b]], f12(**{}, &b))
+ assert_equal([b, [:b]], f12(**h, &b))
+ end
+
def test_method_parameters
assert_equal([[:key, :str], [:key, :num]], method(:f1).parameters);
assert_equal([[:req, :x], [:key, :str], [:key, :num]], method(:f2).parameters);
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index 02a95bc60b..72a3cc2fc7 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -249,6 +249,22 @@ class TestSyntax < Test::Unit::TestCase
assert_syntax_error('def o.foo(@@foo: a) end', /class variable/)
end
+ def test_keywords_specified_and_not_accepted
+ assert_syntax_error('def o.foo(a:, **nil) end', /unexpected/)
+ assert_syntax_error('def o.foo(a:, **nil, &b) end', /unexpected/)
+ assert_syntax_error('def o.foo(**a, **nil) end', /unexpected/)
+ assert_syntax_error('def o.foo(**a, **nil, &b) end', /unexpected/)
+ assert_syntax_error('def o.foo(**nil, **a) end', /unexpected/)
+ assert_syntax_error('def o.foo(**nil, **a, &b) end', /unexpected/)
+
+ assert_syntax_error('proc do |a:, **nil| end', /unexpected/)
+ assert_syntax_error('proc do |a:, **nil, &b| end', /unexpected/)
+ assert_syntax_error('proc do |**a, **nil| end', /unexpected/)
+ assert_syntax_error('proc do |**a, **nil, &b| end', /unexpected/)
+ assert_syntax_error('proc do |**nil, **a| end', /unexpected/)
+ assert_syntax_error('proc do |**nil, **a, &b| end', /unexpected/)
+ end
+
def test_optional_self_reference
bug9593 = '[ruby-core:61299] [Bug #9593]'
o = Object.new