From 12acc751e3e7fd6f8aec33abf661724ad76c862a Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 17 Mar 2019 05:21:18 +0000 Subject: Numbered parameters [Feature #4475] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67278 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ripper/test_parser_events.rb | 11 ++++++++++- test/ripper/test_scanner_events.rb | 10 ++++++++++ test/ruby/test_parse.rb | 8 ++++---- test/ruby/test_syntax.rb | 13 +++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/ripper/test_parser_events.rb b/test/ripper/test_parser_events.rb index 0591eeb781..e4860ef533 100644 --- a/test/ripper/test_parser_events.rb +++ b/test/ripper/test_parser_events.rb @@ -812,6 +812,12 @@ class TestRipper::ParserEvents < Test::Unit::TestCase assert_equal true, thru_next end + def test_number_arg + thru_number_arg = false + parse('proc {@1}', :on_number_arg) {thru_number_arg = true} + assert_equal true, thru_number_arg + end + def test_opassign thru_opassign = false tree = parse('a += b', :on_opassign) {thru_opassign = true} @@ -1477,8 +1483,11 @@ class TestRipper::ParserEvents < Test::Unit::TestCase assert_equal("unterminated regexp meets end of file", compile_error('/')) end + def test_invalid_numbered_parameter_name + assert_equal("leading zero is not allowed as a numbered parameter", compile_error('proc{@0}')) + end + def test_invalid_instance_variable_name - assert_equal("`@1' is not allowed as an instance variable name", compile_error('@1')) assert_equal("`@' without identifiers is not allowed as an instance variable name", compile_error('@%')) assert_equal("`@' without identifiers is not allowed as an instance variable name", compile_error('@')) end diff --git a/test/ripper/test_scanner_events.rb b/test/ripper/test_scanner_events.rb index 1184b23435..bb98e5d3d9 100644 --- a/test/ripper/test_scanner_events.rb +++ b/test/ripper/test_scanner_events.rb @@ -259,6 +259,8 @@ class TestRipper::ScannerEvents < Test::Unit::TestCase scan('embvar', '"#@ivar"') assert_equal ['#'], scan('embvar', '"#@@cvar"') + assert_equal ['#'], + scan('embvar', '"#@1"') assert_equal [], scan('embvar', '"#lvar"') assert_equal [], @@ -348,6 +350,13 @@ class TestRipper::ScannerEvents < Test::Unit::TestCase scan('ivar', 'm(lvar, @ivar, @@cvar, $gvar)') end + def test_tnumparam + assert_equal [], + scan('tnumparam', '') + assert_equal ['@1'], + scan('tnumparam', 'proc {@1}') + end + def test_kw assert_equal [], scan('kw', '') @@ -742,6 +751,7 @@ class TestRipper::ScannerEvents < Test::Unit::TestCase assert_equal [" E\n\n"], scan('tstring_content', "<<""'E'\n E\n\n"), bug10392 + scan('tstring_content', "tap{<<""EOS}\n""there\n""heredoc\#@1xxx\nEOS") end def test_heredoc_end diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index d21484f4b6..dc4c143241 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -363,7 +363,7 @@ class TestParse < Test::Unit::TestCase def test_dstr_disallowed_variable bug8375 = '[ruby-core:54885] [Bug #8375]' - %w[@ @1 @. @@ @@1 @@. $ $%].each do |src| + %w[@ @. @@ @@1 @@. $ $%].each do |src| src = '#'+src+' ' str = assert_nothing_raised(SyntaxError, "#{bug8375} #{src.dump}") do break eval('"'+src+'"') @@ -378,15 +378,15 @@ class TestParse < Test::Unit::TestCase def assert_disallowed_variable(type, noname, invalid) noname.each do |name| - assert_syntax_error("a = #{name}", "`#{noname[0]}' without identifiers is not allowed as #{type} variable name") + assert_syntax_error("proc{a = #{name} }", "`#{noname[0]}' without identifiers is not allowed as #{type} variable name") end invalid.each do |name| - assert_syntax_error("a = #{name}", "`#{name}' is not allowed as #{type} variable name") + assert_syntax_error("proc {a = #{name} }", "`#{name}' is not allowed as #{type} variable name") end end def test_disallowed_instance_variable - assert_disallowed_variable("an instance", %w[@ @.], %w[@1]) + assert_disallowed_variable("an instance", %w[@ @.], %w[]) end def test_disallowed_class_variable diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index c28bc80bc0..42ad002b8e 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -1291,6 +1291,19 @@ eom assert_valid_syntax('obj::foo (1) {}') end + def test_numbered_parameter + assert_valid_syntax('proc {@1}') + assert_equal(3, eval('[1,2].then {@1+@2}')) + assert_equal("12", eval('[1,2].then {"#@1#@2"}')) + assert_syntax_error('proc {|| @1}', /ordinary parameter is defined/) + assert_syntax_error('proc {|x| @1}', /ordinary parameter is defined/) + assert_syntax_error('proc {@1 = nil}', /Can't assign to numbered parameter @1/) + assert_syntax_error('proc {@01}', /leading zero/) + assert_syntax_error('proc {@1_}', /unexpected/) + assert_syntax_error('proc {@9999999999999999}', /too large/) + assert_syntax_error('@1', /outside block/) + end + private def not_label(x) @result = x; @not_label ||= nil end -- cgit v1.2.3