From b5dff926e710a4fb069596f1911c998a28e3262a Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 11 Nov 2019 09:16:00 +0900 Subject: Prefer assert_syntax_error and assert_valid_syntax --- test/ruby/test_parse.rb | 326 +++++++++++++++++------------------------------ test/ruby/test_syntax.rb | 85 ++++-------- 2 files changed, 143 insertions(+), 268 deletions(-) (limited to 'test') diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index b4b03467f6..ee5f815fd6 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -27,10 +27,10 @@ class TestParse < Test::Unit::TestCase end def test_alias_backref - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /can't make alias/) do + begin; alias $foo $1 - END + end; end end @@ -85,10 +85,10 @@ class TestParse < Test::Unit::TestCase assert_equal([42, 42], [o.Foo, o.Bar]) assert_equal([42, 42], [o::baz, o::qux]) - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /Can't set variable/) do + begin; $1 ||= t.foo 42 - END + end; end def t.bar(x); x + yield; end @@ -153,67 +153,65 @@ class TestParse < Test::Unit::TestCase end def test_dynamic_constant_assignment - assert_raise(SyntaxError) do - Object.new.instance_eval <<-END, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /dynamic constant/) do + begin; def foo self::FOO, self::BAR = 1, 2 ::FOO, ::BAR = 1, 2 end - END + end; end - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /Can't set variable/) do + begin; $1, $2 = 1, 2 - END + end; end - assert_raise(SyntaxError) do - Object.new.instance_eval <<-END, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /dynamic constant/) do + begin; def foo ::FOO = 1 end - END + end; end c = Class.new c.freeze - assert_nothing_raised(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 - if false + assert_valid_syntax("#{<<~"begin;"}\n#{<<~'end;'}") do + begin; c::FOO &= 1 ::FOO &= 1 - end - END + end; end - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /Can't set variable/) do + begin; $1 &= 1 - END + end; end end def test_class_module - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /must be CONSTANT/) do + begin; class foo; end - END + end; end - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /in method body/) do + begin; def foo class Foo; end module Bar; end end - END + end; end - assert_nothing_raised(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_valid_syntax("#{<<~"begin;"}\n#{<<~'end;'}") do + begin; class Foo 1; end - END + end; end end @@ -273,37 +271,34 @@ class TestParse < Test::Unit::TestCase end def test_bad_arg - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /cannot be a constant/) do + begin; def foo(FOO); end - END + end; end - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /cannot be an instance variable/) do + begin; def foo(@foo); end - END + end; end - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /cannot be a global variable/) do + begin; def foo($foo); end - END + end; end - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /cannot be a class variable/) do + begin; def foo(@@foo); end - END + end; end - o = Object.new - def o.foo(*r); yield(*r); end - - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 - o.foo 1 {|; @a| @a = 42 } - END + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /cannot be an instance variable/) do + begin; + o.foo {|; @a| @a = 42 } + end; end end @@ -436,30 +431,30 @@ class TestParse < Test::Unit::TestCase end def test_duplicate_argument - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", '') do + begin; 1.times {|&b?| } - END + end; end - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /duplicated argument/) do + begin; 1.times {|a, a|} - END + end; end - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /duplicated argument/) do + begin; def foo(a, a); end - END + end; end end def test_define_singleton_error - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /singleton method for literals/) do + begin; def ("foo").foo; end - END + end; end end @@ -569,15 +564,14 @@ class TestParse < Test::Unit::TestCase end def test_question - assert_raise(SyntaxError) { eval('?') } - assert_raise(SyntaxError) { eval('? ') } - assert_raise(SyntaxError) { eval("?\n") } - assert_raise(SyntaxError) { eval("?\t") } - assert_raise(SyntaxError) { eval("?\v") } - assert_raise(SyntaxError) { eval("?\r") } - assert_raise(SyntaxError) { eval("?\f") } - assert_raise(SyntaxError) { eval("?\f") } - assert_raise(SyntaxError) { eval(" ?a\x8a".force_encoding("utf-8")) } + assert_syntax_error('?', /incomplete/) + assert_syntax_error('? ', /unexpected/) + assert_syntax_error("?\n", /unexpected/) + assert_syntax_error("?\t", /unexpected/) + assert_syntax_error("?\v", /unexpected/) + assert_syntax_error("?\r", /unexpected/) + assert_syntax_error("?\f", /unexpected/) + assert_syntax_error(" ?a\x8a".force_encoding("utf-8"), /invalid multibyte/) assert_equal("\u{1234}", eval("?\u{1234}")) assert_equal("\u{1234}", eval('?\u{1234}')) assert_equal("\u{1234}", eval('?\u1234')) @@ -601,9 +595,9 @@ class TestParse < Test::Unit::TestCase def test_percent assert_equal(:foo, eval('%s(foo)')) - assert_raise(SyntaxError) { eval('%s') } - assert_raise(SyntaxError) { eval('%ss') } - assert_raise(SyntaxError) { eval('%z()') } + assert_syntax_error('%s', /unterminated quoted string/) + assert_syntax_error('%ss', /unknown type/) + assert_syntax_error('%z()', /unknown type/) end def test_symbol @@ -630,21 +624,13 @@ class TestParse < Test::Unit::TestCase end def test_parse_string - assert_raise(SyntaxError) do - eval <<-END, nil, __FILE__, __LINE__+1 -/ - END - end + assert_syntax_error("/\n", /unterminated/) end def test_here_document x = nil - assert_raise(SyntaxError) do - eval %Q( -<\