From 86c0eae56e496584105f550b0d123b666885a99f Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 7 Jan 2013 06:29:48 +0000 Subject: [PATCH] envutil.rb: assert_syntax_error * test/ruby/envutil.rb (Test::Unit::Assertions#assert_syntax_error): new assertion for syntax error and error message. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38723 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/envutil.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/ruby/envutil.rb b/test/ruby/envutil.rb index e9b4b30896..34963df653 100644 --- a/test/ruby/envutil.rb +++ b/test/ruby/envutil.rb @@ -156,6 +156,30 @@ module Test $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)