summaryrefslogtreecommitdiff
path: root/test/ruby/test_keyword.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_keyword.rb')
-rw-r--r--test/ruby/test_keyword.rb76
1 files changed, 70 insertions, 6 deletions
diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb
index 34a80c3729..c836abd0c6 100644
--- a/test/ruby/test_keyword.rb
+++ b/test/ruby/test_keyword.rb
@@ -193,7 +193,7 @@ class TestKeywordArguments < Test::Unit::TestCase
# cfunc call
assert_equal(nil, p(**nil))
- def self.a0; end
+ def self.a0(&); end
assert_equal(nil, a0(**nil))
assert_equal(nil, :a0.to_proc.call(self, **nil))
assert_equal(nil, a0(**nil, &:block))
@@ -2424,6 +2424,21 @@ class TestKeywordArguments < Test::Unit::TestCase
assert_raise(ArgumentError) { m.call(42, a: 1, **h2) }
end
+ def test_ruby2_keywords_post_arg
+ def self.a(*c, **kw) [c, kw] end
+ def self.b(*a, b) a(*a, b) end
+ assert_warn(/Skipping set of ruby2_keywords flag for b \(method accepts keywords or post arguments or method does not accept argument splat\)/) do
+ assert_nil(singleton_class.send(:ruby2_keywords, :b))
+ end
+ assert_equal([[{foo: 1}, {bar: 1}], {}], b({foo: 1}, bar: 1))
+
+ b = ->(*a, b){a(*a, b)}
+ assert_warn(/Skipping set of ruby2_keywords flag for proc \(proc accepts keywords or post arguments or proc does not accept argument splat\)/) do
+ b.ruby2_keywords
+ end
+ assert_equal([[{foo: 1}, {bar: 1}], {}], b.({foo: 1}, bar: 1))
+ end
+
def test_proc_ruby2_keywords
h1 = {:a=>1}
foo = ->(*args, &block){block.call(*args)}
@@ -2436,8 +2451,8 @@ class TestKeywordArguments < Test::Unit::TestCase
assert_raise(ArgumentError) { foo.call(:a=>1, &->(arg, **kw){[arg, kw]}) }
assert_equal(h1, foo.call(:a=>1, &->(arg){arg}))
- [->(){}, ->(arg){}, ->(*args, **kw){}, ->(*args, k: 1){}, ->(*args, k: ){}].each do |pr|
- assert_warn(/Skipping set of ruby2_keywords flag for proc \(proc accepts keywords or proc does not accept argument splat\)/) do
+ [->(){}, ->(arg){}, ->(*args, x){}, ->(*args, **kw){}, ->(*args, k: 1){}, ->(*args, k: ){}].each do |pr|
+ assert_warn(/Skipping set of ruby2_keywords flag for proc \(proc accepts keywords or post arguments or proc does not accept argument splat\)/) do
pr.ruby2_keywords
end
end
@@ -2790,10 +2805,27 @@ class TestKeywordArguments < Test::Unit::TestCase
assert_equal(:opt, o.clear_last_opt(a: 1))
assert_nothing_raised(ArgumentError) { o.clear_last_empty_method(a: 1) }
- assert_warn(/Skipping set of ruby2_keywords flag for bar \(method accepts keywords or method does not accept argument splat\)/) do
+ assert_warn(/Skipping set of ruby2_keywords flag for bar \(method accepts keywords or post arguments or method does not accept argument splat\)/) do
assert_nil(c.send(:ruby2_keywords, :bar))
end
+ c.class_eval do
+ def bar_post(*a, x) = nil
+ define_method(:bar_post_bmethod) { |*a, x| }
+ end
+ assert_warn(/Skipping set of ruby2_keywords flag for bar_post \(method accepts keywords or post arguments or method does not accept argument splat\)/) do
+ assert_nil(c.send(:ruby2_keywords, :bar_post))
+ end
+ assert_warn(/Skipping set of ruby2_keywords flag for bar_post_bmethod \(method accepts keywords or post arguments or method does not accept argument splat\)/) do
+ assert_nil(c.send(:ruby2_keywords, :bar_post_bmethod))
+ end
+
+ utf16_sym = "abcdef".encode("UTF-16LE").to_sym
+ c.send(:define_method, utf16_sym, c.instance_method(:itself))
+ assert_warn(/abcdef/) do
+ assert_nil(c.send(:ruby2_keywords, utf16_sym))
+ end
+
o = Object.new
class << o
alias bar p
@@ -2848,8 +2880,22 @@ class TestKeywordArguments < Test::Unit::TestCase
assert_equal(1, process(:foo, bar: :baz))
end
+ def test_ruby2_keywords_bug_20679
+ c = Class.new do
+ def self.get(_, _, h, &block)
+ h[1]
+ end
+
+ ruby2_keywords def get(*args, &block)
+ self.class.get(*args, &block)
+ end
+ end
+
+ assert_equal 2, c.new.get(true, {}, 1 => 2)
+ end
+
def test_top_ruby2_keywords
- assert_in_out_err([], <<-INPUT, ["[1, 2, 3]", "{:k=>1}"], [])
+ assert_in_out_err([], <<-INPUT, ["[1, 2, 3]", "{k: 1}"], [])
def bar(*a, **kw)
p a, kw
end
@@ -4013,7 +4059,7 @@ class TestKeywordArguments < Test::Unit::TestCase
tap { m }
GC.start
tap { m }
- }, bug8964
+ }, bug8964, timeout: 30
assert_normal_exit %q{
prc = Proc.new {|a: []|}
GC.stress = true
@@ -4523,6 +4569,24 @@ class TestKeywordArgumentsSymProcRefinements < Test::Unit::TestCase
assert_equal({one: 1, two: 2}, f.call(one:, two:))
end
+ def m_bug20570(*a, **nil)
+ a
+ end
+
+ def test_splat_arg_with_prohibited_keyword
+ assert_equal([], m_bug20570(*[]))
+ assert_equal([1], m_bug20570(*[1]))
+ assert_equal([1, 2], m_bug20570(*[1, 2]))
+ h = nil
+ assert_equal([], m_bug20570(*[], **h))
+ assert_equal([1], m_bug20570(*[1], **h))
+ assert_equal([1, 2], m_bug20570(*[1, 2], **h))
+
+ assert_equal([], m_bug20570(*[], **nil))
+ assert_equal([1], m_bug20570(*[1], **nil))
+ assert_equal([1, 2], m_bug20570(*[1, 2], **nil))
+ end
+
private def one
1
end