diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2019-10-19 03:05:03 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2019-10-22 02:35:43 +0900 |
commit | 62d43828770211470bcacb9e943876f981b5a1b4 (patch) | |
tree | 97c1cdebd90cff8bf28320d5ad053b80cc53df53 /test/ruby | |
parent | 35f90bf1b9b06f93e919af3c4095b7aff903d799 (diff) | |
download | ruby-62d43828770211470bcacb9e943876f981b5a1b4.tar.gz |
Arguments forwarding [Feature #16253]
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/test_syntax.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index 4b9be497d0..5b933d9682 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -1471,6 +1471,46 @@ eom assert_valid_syntax("tap {a = (break unless true)}") end + def test_argument_forwarding + assert_valid_syntax('def foo(...) bar(...) end') + assert_valid_syntax('def foo(...) end') + assert_syntax_error('iter do |...| end', /unexpected/) + assert_syntax_error('iter {|...|}', /unexpected/) + assert_syntax_error('def foo(x, y, z) bar(...); end', /unexpected/) + assert_syntax_error('def foo(x, y, z) super(...); end', /unexpected/) + assert_syntax_error('def foo(...) yield(...); end', /unexpected/) + assert_syntax_error('def foo(...) return(...); end', /unexpected/) + assert_syntax_error('def foo(...) a = (...); end', /unexpected/) + assert_syntax_error('def foo(...) [...]; end', /unexpected/) + assert_syntax_error('def foo(...) foo[...]; end', /unexpected/) + assert_syntax_error('def foo(...) foo[...] = x; end', /unexpected/) + assert_syntax_error('def foo(...) foo(...) { }; end', /both block arg and actual block given/) + assert_syntax_error('def foo(...) defined?(...); end', /unexpected/) + + obj1 = Object.new + def obj1.bar(*args, **kws, &block) + block.call(args, kws) + end + obj1.instance_eval('def foo(...) bar(...) end') + + klass = Class.new { + def foo(*args, **kws, &block) + block.call(args, kws) + end + } + obj2 = klass.new + obj2.instance_eval('def foo(...) super(...) end') + + [obj1, obj2].each do |obj| + assert_equal([[1, 2, 3], {k1: 4, k2: 5}], obj.foo(1, 2, 3, k1: 4, k2: 5) {|*x| x}) + assert_equal(-1, obj.:foo.arity) + parameters = obj.:foo.parameters + assert_equal(:rest, parameters.dig(0, 0)) + assert_equal(:keyrest, parameters.dig(1, 0)) + assert_equal(:block, parameters.dig(2, 0)) + end + end + private def not_label(x) @result = x; @not_label ||= nil end |