diff options
Diffstat (limited to 'test/ripper/test_parser_events.rb')
-rw-r--r-- | test/ripper/test_parser_events.rb | 114 |
1 files changed, 101 insertions, 13 deletions
diff --git a/test/ripper/test_parser_events.rb b/test/ripper/test_parser_events.rb index 5bb8f120f2..dc94da01ee 100644 --- a/test/ripper/test_parser_events.rb +++ b/test/ripper/test_parser_events.rb @@ -16,7 +16,7 @@ class TestRipper::ParserEvents < Test::Unit::TestCase end def parse(str, nm = nil, &bl) - dp = DummyParser.new(str) + dp = TestRipper::DummyParser.new(str) dp.hook(*nm, &bl) if nm dp.parse.to_s end @@ -152,6 +152,45 @@ class TestRipper::ParserEvents < Test::Unit::TestCase thru_args_forward = false parse(code, :on_args_forward) {thru_args_forward = true} assert_equal true, thru_args_forward, "no args_forward for: #{code}" + parse(code, :on_params) {|*, block| assert_nil(block)} + end + end + + def test_anonymous_block_forwarding + thru_args_add_block = false + parse('def b(&); c(&); end', :on_args_add_block) {thru_args_add_block = true} + assert_equal true, thru_args_add_block + assert_match "no anonymous block parameter", compile_error('def b; c(&); end') + end + + def test_anonymous_rest_forwarding + [ + 'c(*)', + 'c(*, *)', + ].each do |code| + thru_args_add_star = false + src = "def b(*); #{code} end" + parse(src, :on_args_add_star) {thru_args_add_star = true} + assert_equal true, thru_args_add_star, src + + src = "def b; #{code} end" + assert_match "no anonymous rest parameter", compile_error(src), src + end + end + + def test_anonymous_keyword_rest_forwarding + [ + 'c(**)', + 'c(k: 1, **)', + 'c(**, k: 1)', + ].each do |code| + thru_assoc_splat = false + src = "def b(**); #{code} end" + parse(src, :on_assoc_splat) {thru_assoc_splat = true} + assert_equal true, thru_assoc_splat, src + + src = "def b; #{code} end" + assert_match "no anonymous keyword rest parameter", compile_error(src), src end end @@ -230,15 +269,27 @@ class TestRipper::ParserEvents < Test::Unit::TestCase def test_assign_error_backref thru_assign_error = false result = - parse('$` = 1', :on_assign_error) {thru_assign_error = true} + parse('$& = 1', :on_assign_error) {thru_assign_error = true} + assert_equal true, thru_assign_error + assert_equal '[assign(assign_error(var_field($&)),1)]', result + + thru_assign_error = false + result = + parse('$&, _ = 1', :on_assign_error) {thru_assign_error = true} assert_equal true, thru_assign_error - assert_equal '[assign(assign_error(var_field($`)),1)]', result + assert_equal '[massign([assign_error(var_field($&)),var_field(_)],1)]', result thru_assign_error = false result = - parse('$`, _ = 1', :on_assign_error) {thru_assign_error = true} + parse('$& += 1', :on_assign_error) {thru_assign_error = true} assert_equal true, thru_assign_error - assert_equal '[massign([assign_error(var_field($`)),var_field(_)],1)]', result + assert_equal '[assign_error(opassign(var_field($&),+=,1))]', result + + thru_assign_error = false + result = + parse('$& += cmd 1, 2', :on_assign_error) {thru_assign_error = true} + assert_equal true, thru_assign_error + assert_equal '[assign_error(opassign(var_field($&),+=,command(cmd,[1,2])))]', result end def test_assign_error_const_qualified @@ -461,6 +512,23 @@ class TestRipper::ParserEvents < Test::Unit::TestCase assert_equal "[call(ref(self),&.,foo,[])]", tree end + def test_call_colon2 + hook = Module.new do + def on_op(op) + super("(op: #{op.inspect})") + end + def on_call(recv, name, *args) + super(recv, "(method: #{name})", *args) + end + def on_ident(name) + super("(ident: #{name.inspect})") + end + end + + parser = TestRipper::DummyParser.new("a::b").extend(hook) + assert_equal '[call(vcall((ident: "a")),(method: (op: "::")),(ident: "b"))]', parser.parse.to_s + end + def test_excessed_comma thru_excessed_comma = false parse("proc{|x,|}", :on_excessed_comma) {thru_excessed_comma = true} @@ -1588,20 +1656,20 @@ class TestRipper::ParserEvents < Test::Unit::TestCase end def test_invalid_instance_variable_name - assert_equal("`@1' is not allowed as an instance variable name", compile_error('proc{@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('@')) + assert_equal("'@1' is not allowed as an instance variable name", compile_error('proc{@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 def test_invalid_class_variable_name - assert_equal("`@@1' is not allowed as a class variable name", compile_error('@@1')) - assert_equal("`@@' without identifiers is not allowed as a class variable name", compile_error('@@%')) - assert_equal("`@@' without identifiers is not allowed as a class variable name", compile_error('@@')) + assert_equal("'@@1' is not allowed as a class variable name", compile_error('@@1')) + assert_equal("'@@' without identifiers is not allowed as a class variable name", compile_error('@@%')) + assert_equal("'@@' without identifiers is not allowed as a class variable name", compile_error('@@')) end def test_invalid_global_variable_name - assert_equal("`$%' is not allowed as a global variable name", compile_error('$%')) - assert_equal("`$' without identifiers is not allowed as a global variable name", compile_error('$')) + assert_equal("'$%' is not allowed as a global variable name", compile_error('$%')) + assert_equal("'$' without identifiers is not allowed as a global variable name", compile_error('$')) end def test_warning_ignored_magic_comment @@ -1616,6 +1684,26 @@ class TestRipper::ParserEvents < Test::Unit::TestCase assert_equal(%w"frozen_string_literal nottrue", args) end + def test_warning_duplicated_when_clause + fmt, *args = warning(<<~STR) + a = 1 + case a + when 1 + when 1 + when 2 + else + end + STR + assert_match(/duplicated 'when' clause/, fmt) + assert_equal([3], args) + end + + def test_warn_duplicated_hash_keys + fmt, *args = warn("{ a: 1, a: 2 }") + assert_match(/is duplicated and overwritten on line/, fmt) + assert_equal([:a, 1], args) + end + def test_warn_cr_in_middle fmt = nil assert_warn("") {fmt, = warn("\r;")} |