1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

assert_valid_syntax

* test/ruby/test_syntax.rb (assert_valid_syntax): new assertion to
  validate syntax.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34910 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-03-05 16:17:29 +00:00
parent bc4ef96c9e
commit 0827c3848a

View file

@ -1,21 +1,24 @@
require 'test/unit' require 'test/unit'
class TestSyntax < Test::Unit::TestCase class TestSyntax < Test::Unit::TestCase
def valid_syntax?(code, fname) def assert_valid_syntax(code, fname, mesg = fname)
code = code.dup.force_encoding("ascii-8bit") code = code.dup.force_encoding("ascii-8bit")
code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n) { code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n) {
"#$&#{"\n" if $1 && !$2}BEGIN{throw tag, :ok}\n" "#$&#{"\n" if $1 && !$2}BEGIN{throw tag, :ok}\n"
} }
code.force_encoding("us-ascii") code.force_encoding("us-ascii")
catch {|tag| eval(code, binding, fname, 0)} verbose, $VERBOSE = $VERBOSE, nil
rescue SyntaxError assert_nothing_raised(SyntaxError, mesg) do
false assert_equal(:ok, catch {|tag| eval(code, binding, fname, 0)}, mesg)
end
ensure
$VERBOSE = verbose
end end
def test_syntax def test_syntax
assert_nothing_raised(Exception) do assert_nothing_raised(Exception) do
for script in Dir[File.expand_path("../../../{lib,sample,ext,test}/**/*.rb", __FILE__)].sort for script in Dir[File.expand_path("../../../{lib,sample,ext,test}/**/*.rb", __FILE__)].sort
assert(valid_syntax?(IO::read(script), script), script) assert_valid_syntax(IO::read(script), script)
end end
end end
end end
@ -56,9 +59,7 @@ class TestSyntax < Test::Unit::TestCase
bug = '[ruby-dev:45292]' bug = '[ruby-dev:45292]'
["", "a", "a, b"].product(["", ";x", [";", "x"]]) do |params| ["", "a", "a, b"].product(["", ";x", [";", "x"]]) do |params|
params = ["|", *params, "|"].join("\n") params = ["|", *params, "|"].join("\n")
assert_nothing_raised(SyntaxError, NameError, "#{bug} #{params.inspect}") do assert_valid_syntax("1.times{#{params}}", __FILE__, "#{bug} #{params.inspect}")
eval("1.times{#{params}}")
end
end end
end end