[前][次][番号順一覧][スレッド一覧]

ruby-changes:58701

From: Nobuyoshi <ko1@a...>
Date: Mon, 11 Nov 2019 09:18:55 +0900 (JST)
Subject: [ruby-changes:58701] b5dff926e7 (master): Prefer assert_syntax_error and assert_valid_syntax

https://git.ruby-lang.org/ruby.git/commit/?id=b5dff926e7

From b5dff926e710a4fb069596f1911c998a28e3262a Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Mon, 11 Nov 2019 09:16:00 +0900
Subject: Prefer assert_syntax_error and assert_valid_syntax


diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb
index b4b0346..ee5f815 100644
--- a/test/ruby/test_parse.rb
+++ b/test/ruby/test_parse.rb
@@ -27,10 +27,10 @@ class TestParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L27
   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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L85
     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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L153
   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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L271
   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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L431
   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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L564
   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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L595
 
   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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L624
   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(
-<\<FOO
-      )
-    end
+    assert_syntax_error("<\<FOO\n", /can't find string "FOO"/)
 
     assert_nothing_raised(SyntaxError) do
       x = eval %q(
@@ -655,23 +641,11 @@ FOO https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L641
     end
     assert_equal "\#$\n", x
 
-    assert_raise(SyntaxError) do
-      eval %Q(
-<\<\"
-      )
-    end
+    assert_syntax_error("<\<\"\n", /unterminated here document identifier/)
 
-    assert_raise(SyntaxError) do
-      eval %q(
-<<``
-      )
-    end
+    assert_syntax_error("<<``\n", /can't find string ""/)
 
-    assert_raise(SyntaxError) do
-      eval %q(
-<<--
-      )
-    end
+    assert_syntax_error("<<--\n", /unexpected <</)
 
     assert_nothing_raised(SyntaxError) do
       x = eval %q(
@@ -734,17 +708,13 @@ x = __ENCODING__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L708
   end
 
   def test_embedded_rd
-    assert_raise(SyntaxError) do
-      eval <<-END, nil, __FILE__, __LINE__+1
-=begin
-      END
-    end
+    assert_valid_syntax("=begin\n""=end")
   end
 
   def test_float
     assert_equal(1.0/0, eval("1e10000"))
-    assert_raise(SyntaxError) { eval('1_E') }
-    assert_raise(SyntaxError) { eval('1E1E1') }
+    assert_syntax_error('1_E', /trailing `_'/)
+    assert_syntax_error('1E1E1', /unexpected constant/)
   end
 
   def test_global_variable
@@ -754,21 +724,19 @@ x = __ENCODING__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L724
     $test_parse_foobarbazqux = nil
     assert_equal(nil, $&)
     assert_equal(nil, eval('alias $& $preserve_last_match'))
-    assert_raise_with_message(SyntaxError, /as a global variable name\na = \$\ (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]