ruby-changes:57796
From: Jeremy <ko1@a...>
Date: Thu, 19 Sep 2019 03:21:19 +0900 (JST)
Subject: [ruby-changes:57796] 5177dd8ba0 (master): Add and fix some keyword tests
https://git.ruby-lang.org/ruby.git/commit/?id=5177dd8ba0 From 5177dd8ba081dd25bf8ceb0f1c861ea8f83884c3 Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Wed, 18 Sep 2019 11:16:00 -0700 Subject: Add and fix some keyword tests Replace [arg=1, args] with [arg, args] so we can actually test the value correctly. Add some missing tests for **h3 when method accepts (**args). Add tests for passing positional hashes to (**args) methods and check for the expected warnings/errors. diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index fa65074..a9e2b1f 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -232,7 +232,15 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L232 assert_equal(h, c.m(**h)) assert_equal(h, c.m(a: 1)) assert_equal(h2, c.m(**h2)) + assert_equal(h3, c.m(**h3)) assert_equal(h3, c.m(a: 1, **h2)) + assert_warn(/The last argument is used as the keyword parameter.*for `m'/m) do + assert_equal(h, c.m(h)) + end + assert_raise(ArgumentError) { c.m(h2) } + assert_warn(/The last argument is split into positional and keyword parameters.*for `m'/m) do + assert_raise(ArgumentError) { c.m(h3) } + end c.singleton_class.remove_method(:m) def c.m(arg, **args) @@ -259,10 +267,13 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L267 assert_warn(/The keyword argument is passed as the last hash parameter.* for `m'/m) do assert_equal([h3, kw], c.m(a: 1, **h2)) end + assert_equal([h, kw], c.m(h)) + assert_equal([h2, kw], c.m(h2)) + assert_equal([h3, kw], c.m(h3)) c.singleton_class.remove_method(:m) def c.m(arg=1, **args) - [arg=1, args] + [arg, args] end assert_equal([1, kw], c.m(**{})) assert_equal([1, kw], c.m(**kw)) @@ -271,6 +282,13 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L282 assert_equal([1, h2], c.m(**h2)) assert_equal([1, h3], c.m(**h3)) assert_equal([1, h3], c.m(a: 1, **h2)) + assert_warn(/The last argument is used as the keyword parameter.*for `m'/m) do + assert_equal([1, h], c.m(h)) + end + assert_equal([h2, kw], c.m(h2)) + assert_warn(/The last argument is split into positional and keyword parameters.*for `m'/m) do + assert_equal([h2, h], c.m(h3)) + end end def test_implicit_super_kwsplat @@ -338,7 +356,15 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L356 assert_equal(h, c.m(**h)) assert_equal(h, c.m(a: 1)) assert_equal(h2, c.m(**h2)) + assert_equal(h3, c.m(**h3)) assert_equal(h3, c.m(a: 1, **h2)) + assert_warn(/The last argument is used as the keyword parameter.*for `m'/m) do + assert_equal(h, c.m(h)) + end + assert_raise(ArgumentError) { c.m(h2) } + assert_warn(/The last argument is split into positional and keyword parameters.*for `m'/m) do + assert_raise(ArgumentError) { c.m(h3) } + end sc.class_eval do remove_method(:m) @@ -371,7 +397,7 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L397 sc.class_eval do remove_method(:m) def m(arg=1, **args) - [arg=1, args] + [arg, args] end end assert_equal([1, kw], c.m(**{})) @@ -448,7 +474,15 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L474 assert_equal(h, c.m(**h)) assert_equal(h, c.m(a: 1)) assert_equal(h2, c.m(**h2)) + assert_equal(h3, c.m(**h3)) assert_equal(h3, c.m(a: 1, **h2)) + assert_warn(/The last argument is used as the keyword parameter.*for `m'/m) do + assert_equal(h, c.m(h)) + end + assert_raise(ArgumentError) { c.m(h2) } + assert_warn(/The last argument is split into positional and keyword parameters.*for `m'/m) do + assert_raise(ArgumentError) { c.m(h3) } + end sc.class_eval do remove_method(:m) @@ -481,7 +515,7 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L515 sc.class_eval do remove_method(:m) def m(arg=1, **args) - [arg=1, args] + [arg, args] end end assert_equal([1, kw], c.m(**{})) @@ -528,6 +562,13 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L562 assert_equal(h2, f[**h2]) assert_equal(h3, f[**h3]) assert_equal(h3, f[a: 1, **h2]) + assert_warn(/The last argument is used as the keyword parameter.*for `\[\]'/m) do + assert_equal(h, f[h]) + end + assert_raise(ArgumentError) { f[h2] } + assert_warn(/The last argument is split into positional and keyword parameters.*for `\[\]'/m) do + assert_raise(ArgumentError) { f[h3] } + end f = ->(a, **x) { [a,x] } assert_warn(/The keyword argument is passed as the last hash parameter.* for `\[\]'/m) do @@ -600,6 +641,13 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L641 assert_equal(h2, f[**h2]) assert_equal(h3, f[**h3]) assert_equal(h3, f[a: 1, **h2]) + assert_warn(/The last argument is used as the keyword parameter.*for method/m) do + assert_equal(h, f[h]) + end + assert_raise(ArgumentError) { f[h2] } + assert_warn(/The last argument is split into positional and keyword parameters.*for method/m) do + assert_raise(ArgumentError) { f[h3] } + end f = ->(a, **x) { [a,x] } f = f.method(:call) @@ -702,6 +750,13 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L750 assert_equal(h2, c[**h2].args) assert_equal(h3, c[**h3].args) assert_equal(h3, c[a: 1, **h2].args) + assert_warn(/The last argument is used as the keyword parameter.*for `initialize'/m) do + assert_equal(h, c[h].args) + end + assert_raise(ArgumentError) { c[h2].args } + assert_warn(/The last argument is split into positional and keyword parameters.*for `initialize'/m) do + assert_raise(ArgumentError) { c[h3].args } + end c = Class.new(sc) do def initialize(arg, **args) @@ -732,7 +787,7 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L787 c = Class.new(sc) do def initialize(arg=1, **args) - @args = [arg=1, args] + @args = [arg, args] end end assert_equal([1, kw], c[**{}].args) @@ -807,6 +862,13 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L862 assert_equal(h2, c[**h2].args) assert_equal(h3, c[**h3].args) assert_equal(h3, c[a: 1, **h2].args) + assert_warn(/The last argument is used as the keyword parameter.*for `initialize'/m) do + assert_equal(h, c[h].args) + end + assert_raise(ArgumentError) { c[h2].args } + assert_warn(/The last argument is split into positional and keyword parameters.*for `initialize'/m) do + assert_raise(ArgumentError) { c[h3].args } + end c = Class.new(sc) do def initialize(arg, **args) @@ -837,7 +899,7 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L899 c = Class.new(sc) do def initialize(arg=1, **args) - @args = [arg=1, args] + @args = [arg, args] end end.method(:new) assert_equal([1, kw], c[**{}].args) @@ -904,6 +966,13 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L966 assert_equal(h2, c.method(:m)[**h2]) assert_equal(h3, c.method(:m)[**h3]) assert_equal(h3, c.method(:m)[a: 1, **h2]) + assert_warn(/The last argument is used as the keyword parameter.*for `m'/m) do + assert_equal(h, c.method(:m)[h]) + end + assert_raise(ArgumentError) { c.method(:m)[h2] } + assert_warn(/The last argument is split into positional and keyword parameters.*for `m'/m) do + assert_raise(ArgumentError) { c.method(:m)[h3] } + end c.singleton_class.remove_method(:m) def c.m(arg, **args) @@ -933,7 +1002,7 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L1002 c.singleton_class.remove_method(:m) def c.m(arg=1, **args) - [arg=1, args] + [arg, args] end assert_equal([1, kw], c.method(:m)[**{}]) assert_equal([1, kw], c.method(:m)[**kw]) @@ -1000,6 +1069,13 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L1069 assert_equal(h2, sc.instance_method(:m).bind_call(c, **h2)) assert_equal(h3, sc.instance_method(:m).bind_call(c, **h3)) assert_equal(h3, sc.instance_method(:m).bind_call(c, a: 1, **h2)) + assert_warn(/The last argument is used as the keyword parameter.*for `m'/m) do + assert_equal(h, sc.instance_method(:m).bind_call(c, h)) + end + assert_raise(ArgumentError) { sc.instance_method(:m).bind_call(c, h2) } + assert_warn(/The last argument is split into positional and keyword parameters.*for `m'/m) do + assert_raise(ArgumentError) { sc.instance_method(:m).bind_call(c, h3) } + end sc.remove_method(:m) def c.m(arg, **args) @@ -1029,7 +1105,7 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L1105 sc.remove_method(:m) def c.m(arg=1, **args) - [arg=1, args] + [arg, args] end assert_equal([1, kw], sc.inst (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/