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

ruby-changes:47172

From: nobu <ko1@a...>
Date: Sat, 8 Jul 2017 11:22:24 +0900 (JST)
Subject: [ruby-changes:47172] nobu:r59287 (trunk): assertions.rb: syntax_check for other impl

nobu	2017-07-08 11:22:20 +0900 (Sat, 08 Jul 2017)

  New Revision: 59287

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59287

  Log:
    assertions.rb: syntax_check for other impl
    
    * test/lib/test/unit/assertions.rb (syntax_check): use eval
      instead of RubyVM::InstructionSequence.compile so that other
      implementations can share the tests.
      [ruby-core:81935] [Bug #13723]

  Modified files:
    trunk/test/lib/test/unit/assertions.rb
Index: test/lib/test/unit/assertions.rb
===================================================================
--- test/lib/test/unit/assertions.rb	(revision 59286)
+++ test/lib/test/unit/assertions.rb	(revision 59287)
@@ -481,8 +481,24 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit/assertions.rb#L481
       # compatibility with test-unit
       alias pend skip
 
+      if defined?(RubyVM::InstructionSequence)
+        def syntax_check(code, fname, line)
+          code = code.dup.force_encoding(Encoding::UTF_8)
+          RubyVM::InstructionSequence.compile(code, fname, fname, line)
+          :ok
+        end
+      else
+        def syntax_check(code, fname, line)
+          code = code.b
+          code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n) {
+            "#$&#{"\n" if $1 && !$2}BEGIN{throw tag, :ok}\n"
+          }
+          code = code.force_encoding(Encoding::UTF_8)
+          catch {|tag| eval(code, binding, fname, line - 1)}
+        end
+      end
+
       def prepare_syntax_check(code, fname = caller_locations(2, 1)[0], mesg = fname.to_s, verbose: nil)
-        code = code.dup.force_encoding(Encoding::UTF_8)
         verbose, $VERBOSE = $VERBOSE, verbose
         case
         when Array === fname
@@ -501,7 +517,7 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit/assertions.rb#L517
         prepare_syntax_check(code, *args) do |src, fname, line, mesg|
           yield if defined?(yield)
           assert_nothing_raised(SyntaxError, mesg) do
-            RubyVM::InstructionSequence.compile(src, fname, fname, line)
+            assert_equal(:ok, syntax_check(src, fname, line), mesg)
           end
         end
       end
@@ -510,7 +526,7 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit/assertions.rb#L526
         prepare_syntax_check(code, *args) do |src, fname, line, mesg|
           yield if defined?(yield)
           e = assert_raise(SyntaxError, mesg) do
-            RubyVM::InstructionSequence.compile(src, fname, fname, line)
+            syntax_check(src, fname, line)
           end
           assert_match(error, e.message, mesg)
           e

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

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