diff options
author | Jeremy Evans <code@jeremyevans.net> | 2019-09-29 17:47:17 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2019-09-29 18:31:08 -0700 |
commit | 649a64ae29318501472f74798a12eb60871ab990 (patch) | |
tree | 8295ea066c04d326bb7462a4f7569d60d393c5b4 /test/-ext- | |
parent | fba8627dc1c5b191713edeb5fc21cbe0ddde9e3c (diff) |
Add three more C-API functions for handling keywords
This adds rb_funcall_passing_block_kw, rb_funcallv_public_kw,
and rb_yield_splat_kw. This functions are necessary to easily
handle cases where rb_funcall_passing_block, rb_funcallv_public,
and rb_yield_splat are currently used and a keyword argument
separation warning is raised.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/2507
Diffstat (limited to 'test/-ext-')
-rw-r--r-- | test/-ext-/funcall/test_passing_block.rb | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/test/-ext-/funcall/test_passing_block.rb b/test/-ext-/funcall/test_passing_block.rb index 5112bc0925..c2d7639b52 100644 --- a/test/-ext-/funcall/test_passing_block.rb +++ b/test/-ext-/funcall/test_passing_block.rb @@ -3,8 +3,8 @@ require 'test/unit' class TestFuncall < Test::Unit::TestCase module Relay - def self.target(*args, &block) - yield(*args) if block + def self.target(*args, **kw, &block) + yield(*args, **kw) if block end end require '-test-/funcall' @@ -20,4 +20,56 @@ class TestFuncall < Test::Unit::TestCase Relay.with_funcall_passing_block("feature#4504") {|arg| ok = arg || true} assert_equal("feature#4504", ok) end + + def test_with_funcall_passing_block_kw + block = ->(*a, **kw) { [a, kw] } + assert_equal([[1], {}], Relay.with_funcall_passing_block_kw(0, 1, &block)) + assert_equal([[], {a: 1}], Relay.with_funcall_passing_block_kw(1, a: 1, &block)) + assert_equal([[1], {a: 1}], Relay.with_funcall_passing_block_kw(1, 1, a: 1, &block)) + assert_equal([[{}], {}], Relay.with_funcall_passing_block_kw(2, {}, **{}, &block)) + assert_equal([[], {a: 1}], Relay.with_funcall_passing_block_kw(3, a: 1, &block)) + assert_equal([[{a: 1}], {}], Relay.with_funcall_passing_block_kw(3, {a: 1}, **{}, &block)) + assert_warn(/warning: The keyword argument is passed as the last hash parameter.*for method/m) do + assert_equal({}, Relay.with_funcall_passing_block_kw(3, **{}, &->(a){a})) + end + end + + def test_with_funcallv_public_kw + o = Object.new + def o.foo(*args, **kw) + [args, kw] + end + def o.bar(*args, **kw) + [args, kw] + end + o.singleton_class.send(:private, :bar) + def o.baz(arg) + arg + end + assert_equal([[1], {}], Relay.with_funcallv_public_kw(o, :foo, 0, 1)) + assert_equal([[], {a: 1}], Relay.with_funcallv_public_kw(o, :foo, 1, a: 1)) + assert_equal([[1], {a: 1}], Relay.with_funcallv_public_kw(o, :foo, 1, 1, a: 1)) + assert_equal([[{}], {}], Relay.with_funcallv_public_kw(o, :foo, 2, {}, **{})) + assert_equal([[], {a: 1}], Relay.with_funcallv_public_kw(o, :foo, 3, a: 1)) + assert_equal([[{a: 1}], {}], Relay.with_funcallv_public_kw(o, :foo, 3, {a: 1}, **{})) + assert_raise(NoMethodError) { Relay.with_funcallv_public_kw(o, :bar, 3, {a: 1}, **{}) } + assert_warn(/warning: The keyword argument is passed as the last hash parameter.*for `baz'/m) do + assert_equal({}, Relay.with_funcallv_public_kw(o, :baz, 3, **{})) + end + end + + def test_with_yield_splat_kw + block = ->(*a, **kw) { [a, kw] } + assert_equal([[1], {}], Relay.with_yield_splat_kw(0, [1], &block)) + assert_equal([[], {a: 1}], Relay.with_yield_splat_kw(1, [{a: 1}], &block)) + assert_equal([[1], {a: 1}], Relay.with_yield_splat_kw(1, [1, {a: 1}], &block)) + assert_equal([[{}], {}], Relay.with_yield_splat_kw(2, [{}], **{}, &block)) + assert_warn(/warning: The last argument is used as the keyword parameter.*for method/m) do + assert_equal([[], {a: 1}], Relay.with_yield_splat_kw(3, [{a: 1}], &block)) + end + assert_equal([[{a: 1}], {}], Relay.with_yield_splat_kw(3, [{a: 1}], **{}, &block)) + assert_warn(/warning: The keyword argument is passed as the last hash parameter/) do + assert_equal({}, Relay.with_yield_splat_kw(3, [], **{}, &->(a){a})) + end + end end |