summaryrefslogtreecommitdiff
path: root/test/ruby/test_proc.rb
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-09-25 11:22:14 -0700
committerJeremy Evans <code@jeremyevans.net>2019-09-26 08:01:53 -0700
commit760893d2f85682a3c8c00be4b29dea0f6b2bb4f8 (patch)
treec08a72928c4e5145422cd2f452fa4baebc9c9399 /test/ruby/test_proc.rb
parent0aa267f985084e69c3e45cc3e94698eaacab5c36 (diff)
Fix keyword argument separation issues in Proc#{<<,>>}
This requires adding rb_proc_call_with_block_kw.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2484
Diffstat (limited to 'test/ruby/test_proc.rb')
-rw-r--r--test/ruby/test_proc.rb136
1 files changed, 136 insertions, 0 deletions
diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb
index fbd3a6c68a..b7d644345d 100644
--- a/test/ruby/test_proc.rb
+++ b/test/ruby/test_proc.rb
@@ -1512,3 +1512,139 @@ class TestProc < Test::Unit::TestCase
def m1(&b) b end; def m2(); m1 { next 42 } end }.m2.call)
end
end
+
+class TestProcKeywords < Test::Unit::TestCase
+ def test_compose_keywords
+ f = ->(**kw) { kw.merge(:a=>1) }
+ g = ->(kw) { kw.merge(:a=>2) }
+
+ assert_equal(2, (f >> g).call(a: 3)[:a])
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (f << g).call(a: 3)[:a])
+ end
+ assert_equal(2, (f >> g).call(a: 3)[:a])
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (f << g).call({a: 3})[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(2, (f >> g).call({a: 3})[:a])
+ end
+ assert_equal(2, (g << f).call(a: 3)[:a])
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (g >> f).call(a: 3)[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(2, (g << f).call({a: 3})[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (g >> f).call({a: 3})[:a])
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.*The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (f << g).call(**{})[:a])
+ end
+ assert_equal(2, (f >> g).call(**{})[:a])
+ end
+
+ def test_compose_keywords_method
+ f = ->(**kw) { kw.merge(:a=>1) }.method(:call)
+ g = ->(kw) { kw.merge(:a=>2) }.method(:call)
+
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (f << g).call(a: 3)[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(2, (f >> g).call(a: 3)[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (f << g).call({a: 3})[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(2, (f >> g).call({a: 3})[:a])
+ end
+ assert_equal(2, (g << f).call(a: 3)[:a])
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (g >> f).call(a: 3)[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(2, (g << f).call({a: 3})[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (g >> f).call({a: 3})[:a])
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.*The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (f << g).call(**{})[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(2, (f >> g).call(**{})[:a])
+ end
+ end
+
+ def test_compose_keywords_non_proc
+ f = ->(**kw) { kw.merge(:a=>1) }
+ g = Object.new
+ def g.call(kw) kw.merge(:a=>2) end
+ def g.to_proc; method(:call).to_proc; end
+ def g.<<(f) to_proc << f end
+ def g.>>(f) to_proc >> f end
+
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (f << g).call(a: 3)[:a])
+ end
+ assert_equal(2, (f >> g).call(a: 3)[:a])
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (f << g).call({a: 3})[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(2, (f >> g).call({a: 3})[:a])
+ end
+ assert_equal(2, (g << f).call(a: 3)[:a])
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (g >> f).call(a: 3)[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(2, (g << f).call({a: 3})[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
+ assert_equal(1, (g >> f).call({a: 3})[:a])
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.*for `call'/m) do
+ assert_equal(1, (f << g).call(**{})[:a])
+ end
+ assert_equal(2, (f >> g).call(**{})[:a])
+
+ f = ->(kw) { kw.merge(:a=>1) }
+ g = Object.new
+ def g.call(**kw) kw.merge(:a=>2) end
+ def g.to_proc; method(:call).to_proc; end
+ def g.<<(f) to_proc << f end
+ def g.>>(f) to_proc >> f end
+
+ assert_equal(1, (f << g).call(a: 3)[:a])
+ assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
+ assert_equal(2, (f >> g).call(a: 3)[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
+ assert_equal(1, (f << g).call({a: 3})[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
+ assert_equal(2, (f >> g).call({a: 3})[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
+ assert_equal(2, (g << f).call(a: 3)[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
+ assert_equal(1, (g >> f).call(a: 3)[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
+ assert_equal(2, (g << f).call({a: 3})[:a])
+ end
+ assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
+ assert_equal(1, (g >> f).call({a: 3})[:a])
+ end
+ assert_equal(1, (f << g).call(**{})[:a])
+ assert_warn(/The keyword argument is passed as the last hash parameter.*The last argument is used as the keyword parameter.*for `call'/m) do
+ assert_equal(2, (f >> g).call(**{})[:a])
+ end
+ end
+end
+