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

ruby-changes:26672

From: nobu <ko1@a...>
Date: Mon, 7 Jan 2013 15:29:56 +0900 (JST)
Subject: [ruby-changes:26672] nobu:r38723 (trunk): envutil.rb: assert_syntax_error

nobu	2013-01-07 15:29:48 +0900 (Mon, 07 Jan 2013)

  New Revision: 38723

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38723

  Log:
    envutil.rb: assert_syntax_error
    
    * test/ruby/envutil.rb (Test::Unit::Assertions#assert_syntax_error):
      new assertion for syntax error and error message.

  Modified files:
    trunk/test/ruby/envutil.rb

Index: test/ruby/envutil.rb
===================================================================
--- test/ruby/envutil.rb	(revision 38722)
+++ test/ruby/envutil.rb	(revision 38723)
@@ -156,6 +156,30 @@ module Test https://github.com/ruby/ruby/blob/trunk/test/ruby/envutil.rb#L156
         $VERBOSE = verbose
       end
 
+      def assert_syntax_error(code, error, fname = caller_locations(1, 1)[0], mesg = fname.to_s)
+        code = code.dup.force_encoding("ascii-8bit")
+        code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n) {
+          "#$&#{"\n" if $1 && !$2}BEGIN{throw tag, :ng}\n"
+        }
+        code.force_encoding("us-ascii")
+        verbose, $VERBOSE = $VERBOSE, nil
+        yield if defined?(yield)
+        case
+        when Array === fname
+          fname, line = *fname
+        when defined?(fname.path) && defined?(fname.lineno)
+          fname, line = fname.path, fname.lineno
+        else
+          line = 0
+        end
+        e = assert_raise(SyntaxError, mesg) do
+          catch {|tag| eval(code, binding, fname, line)}
+        end
+        assert_match(error, e.message, mesg)
+      ensure
+        $VERBOSE = verbose
+      end
+
       def assert_normal_exit(testsrc, message = '', opt = {})
         assert_valid_syntax(testsrc, caller_locations(1, 1)[0])
         if opt.include?(:child_env)

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

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