summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-09-08 02:16:15 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-09-27 21:53:01 +0900
commite1250a5f9778f463a541bc1ee5a951f64c131bbf (patch)
treebff420527442756e3cceec28b9463b2f59d47e21 /test/ruby
parent50520cc1930331bccdb94730e17ddc01798f2be0 (diff)
Syntax check of block exits in the parser
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/test_ast.rb33
-rw-r--r--test/ruby/test_rubyoptions.rb3
2 files changed, 36 insertions, 0 deletions
diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb
index b4bcc03cfe..bb6f98c4a9 100644
--- a/test/ruby/test_ast.rb
+++ b/test/ruby/test_ast.rb
@@ -214,6 +214,39 @@ class TestAst < Test::Unit::TestCase
end
end
+ def assert_parse(code)
+ node = RubyVM::AbstractSyntaxTree.parse(code)
+ assert_kind_of(RubyVM::AbstractSyntaxTree::Node, node)
+ end
+
+ def assert_invalid_parse(msg, code)
+ assert_raise_with_message(SyntaxError, msg, code) do
+ RubyVM::AbstractSyntaxTree.parse(code)
+ end
+ end
+
+ def test_invalid_exit
+ [
+ "break",
+ "break true",
+ "next",
+ "next true",
+ "redo",
+ ].each do |code, *args|
+ msg = /Invalid #{code[/\A\w+/]}/
+ assert_parse("while false; #{code}; end")
+ assert_parse("until true; #{code}; end")
+ assert_parse("begin #{code}; end while false")
+ assert_parse("begin #{code}; end until true")
+ assert_parse("->{#{code}}")
+ assert_parse("->{class X; #{code}; end}")
+ assert_invalid_parse(msg, "#{code}")
+ assert_invalid_parse(msg, "def m; #{code}; end")
+ assert_invalid_parse(msg, "begin; #{code}; end")
+ assert_parse("END {#{code}}")
+ end
+ end
+
def test_node_id_for_location
exception = begin
raise
diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb
index 4cc0b707d4..2e2fdca76f 100644
--- a/test/ruby/test_rubyoptions.rb
+++ b/test/ruby/test_rubyoptions.rb
@@ -369,6 +369,9 @@ class TestRubyOptions < Test::Unit::TestCase
def test_syntax_check
assert_in_out_err(%w(-c -e a=1+1 -e !a), "", ["Syntax OK"], [])
+ assert_in_out_err(%w(-c -e break), "", [], ["-e:1: Invalid break", :*])
+ assert_in_out_err(%w(-c -e next), "", [], ["-e:1: Invalid next", :*])
+ assert_in_out_err(%w(-c -e redo), "", [], ["-e:1: Invalid redo", :*])
end
def test_invalid_option