Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expect_syntax_error spec helper #1188

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion language/BEGIN_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
end

it "must appear in a top-level context" do
-> { eval "1.times { BEGIN { 1 } }" }.should raise_error(SyntaxError)
expect_syntax_error("1.times { BEGIN { 1 } }")
end

it "uses top-level for self" do
Expand Down
38 changes: 15 additions & 23 deletions language/block_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,9 @@ def obj.to_ary; raise "Exception raised in #to_ary" end

describe "taking identically-named arguments" do
it "raises a SyntaxError for standard arguments" do
-> { eval "lambda { |x,x| }" }.should raise_error(SyntaxError)
-> { eval "->(x,x) {}" }.should raise_error(SyntaxError)
-> { eval "Proc.new { |x,x| }" }.should raise_error(SyntaxError)
expect_syntax_error("lambda { |x,x| }")
expect_syntax_error("->(x,x) {}")
expect_syntax_error("Proc.new { |x,x| }")
end

it "accepts unnamed arguments" do
Expand Down Expand Up @@ -790,29 +790,23 @@ def obj.to_ary; raise "Exception raised in #to_ary" end
end

it "can not have the same name as one of the standard parameters" do
-> { eval "[1].each {|foo; foo| }" }.should raise_error(SyntaxError)
-> { eval "[1].each {|foo, bar; glark, bar| }" }.should raise_error(SyntaxError)
expect_syntax_error("[1].each {|foo; foo| }")
expect_syntax_error("[1].each {|foo, bar; glark, bar| }")
end

it "can not be prefixed with an asterisk" do
-> { eval "[1].each {|foo; *bar| }" }.should raise_error(SyntaxError)
-> do
eval "[1].each {|foo, bar; glark, *fnord| }"
end.should raise_error(SyntaxError)
expect_syntax_error("[1].each {|foo; *bar| }")
expect_syntax_error("[1].each {|foo, bar; glark, *fnord| }")
end

it "can not be prefixed with an ampersand" do
-> { eval "[1].each {|foo; &bar| }" }.should raise_error(SyntaxError)
-> do
eval "[1].each {|foo, bar; glark, &fnord| }"
end.should raise_error(SyntaxError)
expect_syntax_error("[1].each {|foo; &bar| }")
expect_syntax_error("[1].each {|foo, bar; glark, &fnord| }")
end

it "can not be assigned default values" do
-> { eval "[1].each {|foo; bar=1| }" }.should raise_error(SyntaxError)
-> do
eval "[1].each {|foo, bar; glark, fnord=:fnord| }"
end.should raise_error(SyntaxError)
expect_syntax_error("[1].each {|foo; bar=1| }")
expect_syntax_error("[1].each {|foo, bar; glark, fnord=:fnord| }")
end

it "need not be preceded by standard parameters" do
Expand All @@ -821,8 +815,8 @@ def obj.to_ary; raise "Exception raised in #to_ary" end
end

it "only allow a single semi-colon in the parameter list" do
-> { eval "[1].each {|foo; bar; glark| }" }.should raise_error(SyntaxError)
-> { eval "[1].each {|; bar; glark| }" }.should raise_error(SyntaxError)
expect_syntax_error("[1].each {|foo; bar; glark| }")
expect_syntax_error("[1].each {|; bar; glark| }")
end

it "override shadowed variables from the outer scope" do
Expand Down Expand Up @@ -963,9 +957,7 @@ def obj.to_ary; raise "Exception raised in #to_ary" end
ruby_version_is ""..."3.4" do
it "raises a SyntaxError if using the argument in its default value" do
a = 1
-> {
eval "proc { |a=a| a }"
}.should raise_error(SyntaxError)
expect_syntax_error "proc { |a=a| a }"
end
end

Expand Down Expand Up @@ -1011,7 +1003,7 @@ def c(&); yield :non_null end
end

it "requires the anonymous block parameter to be declared if directly passing a block" do
-> { eval "def a; b(&); end; def b; end" }.should raise_error(SyntaxError)
expect_syntax_error("def a; b(&); end; def b; end")
end

it "works when it's the only declared parameter" do
Expand Down
8 changes: 2 additions & 6 deletions language/break_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,17 @@ def mid(&b)

describe "The break statement in a method" do
it "is invalid and raises a SyntaxError" do
-> {
eval("def m; break; end")
}.should raise_error(SyntaxError)
expect_syntax_error("def m; break; end")
end
end

describe "The break statement in a module literal" do
it "is invalid and raises a SyntaxError" do
code = <<~RUBY
expect_syntax_error <<~RUBY
module BreakSpecs:ModuleWithBreak
break
end
RUBY

-> { eval(code) }.should raise_error(SyntaxError)
end
end

Expand Down
12 changes: 4 additions & 8 deletions language/case_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,26 +261,22 @@ def bar; @calls << :bar; end
end

it "raises a SyntaxError when 'else' is used when no 'when' is given" do
-> {
eval <<-CODE
expect_syntax_error <<-CODE
case 4
else
true
end
CODE
}.should raise_error(SyntaxError)
CODE
end

it "raises a SyntaxError when 'else' is used before a 'when' was given" do
-> {
eval <<-CODE
expect_syntax_error <<-CODE
case 4
else
true
when 4; false
end
CODE
}.should raise_error(SyntaxError)
CODE
end

it "supports nested case statements" do
Expand Down
10 changes: 5 additions & 5 deletions language/def_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def foo(a, b, c, d, e, *f); [a, b, c, d, e, f]; end
end

it "allows only a single * argument" do
-> { eval 'def foo(a, *b, *c); end' }.should raise_error(SyntaxError)
expect_syntax_error('def foo(a, *b, *c); end')
end

it "requires the presence of any arguments that precede the *" do
Expand Down Expand Up @@ -199,11 +199,11 @@ def foo(a, b = 2, *args)

ruby_version_is ""..."3.4" do
it "raises a SyntaxError if using the argument in its default value" do
-> {
eval "def foo(bar = bar)
expect_syntax_error <<~RUBY
def foo(bar = bar)
bar
end"
}.should raise_error(SyntaxError)
end
RUBY
end
end

Expand Down
2 changes: 1 addition & 1 deletion language/encoding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
end

it "raises a SyntaxError if assigned to" do
-> { eval("__ENCODING__ = 1") }.should raise_error(SyntaxError)
expect_syntax_error("__ENCODING__ = 1")
end
end
14 changes: 6 additions & 8 deletions language/ensure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,12 @@ class EnsureInClassExample

describe "An ensure block inside {} block" do
it "is not allowed" do
-> {
eval <<-ruby
lambda {
raise
ensure
}
ruby
}.should raise_error(SyntaxError)
expect_syntax_error <<-ruby
lambda {
raise
ensure
}
ruby
end
end

Expand Down
2 changes: 1 addition & 1 deletion language/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe "The __FILE__ pseudo-variable" do
it "raises a SyntaxError if assigned to" do
-> { eval("__FILE__ = 1") }.should raise_error(SyntaxError)
expect_syntax_error("__FILE__ = 1")
end

ruby_version_is ""..."3.3" do
Expand Down
6 changes: 3 additions & 3 deletions language/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
end

it "with '==>' in the middle raises SyntaxError" do
-> { eval("{:a ==> 1}") }.should raise_error(SyntaxError)
expect_syntax_error("{:a ==> 1}")
end

it "recognizes '!' at the end of the key" do
Expand All @@ -95,7 +95,7 @@
end

it "raises a SyntaxError if there is no space between `!` and `=>`" do
-> { eval("{:a!=> 1}") }.should raise_error(SyntaxError)
expect_syntax_error("{:a!=> 1}")
end

it "recognizes '?' at the end of the key" do
Expand All @@ -107,7 +107,7 @@
end

it "raises a SyntaxError if there is no space between `?` and `=>`" do
-> { eval("{:a?=> 1}") }.should raise_error(SyntaxError)
expect_syntax_error("{:a?=> 1}")
end

it "constructs a new hash with the given elements" do
Expand Down
4 changes: 1 addition & 3 deletions language/heredoc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@
end

it 'raises SyntaxError if quoted HEREDOC identifier is ending not on same line' do
-> {
eval %{<<"HERE\n"\nraises syntax error\nHERE}
}.should raise_error(SyntaxError)
expect_syntax_error %{<<"HERE\n"\nraises syntax error\nHERE}
end

it "allows HEREDOC with <<~'identifier', allowing to indent identifier and content" do
Expand Down
4 changes: 1 addition & 3 deletions language/lambda_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,7 @@ def create_lambda
ruby_version_is ""..."3.4" do
it "raises a SyntaxError if using the argument in its default value" do
a = 1
-> {
eval "-> (a=a) { a }"
}.should raise_error(SyntaxError)
expect_syntax_error("-> (a=a) { a }")
end
end

Expand Down
2 changes: 1 addition & 1 deletion language/line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe "The __LINE__ pseudo-variable" do
it "raises a SyntaxError if assigned to" do
-> { eval("__LINE__ = 1") }.should raise_error(SyntaxError)
expect_syntax_error("__LINE__ = 1")
end

before :each do
Expand Down
9 changes: 2 additions & 7 deletions language/method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1230,13 +1230,8 @@ def n(value, &block)

context "when the argument looks like an argument list" do
it "raises a syntax error" do
-> {
eval("m (1, 2)")
}.should raise_error(SyntaxError)

-> {
eval("m (1, 2, 3)")
}.should raise_error(SyntaxError)
expect_syntax_error("m (1, 2)")
expect_syntax_error("m (1, 2, 3)")
end
end

Expand Down
4 changes: 1 addition & 3 deletions language/next_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ def self.enclosing_method
describe "The next statement" do
describe "in a method" do
it "is invalid and raises a SyntaxError" do
-> {
eval("def m; next; end")
}.should raise_error(SyntaxError)
expect_syntax_error("def m; next; end")
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions language/numbers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

it "must have a digit before the decimal point" do
0.75.should == 0.75
-> { eval(".75") }.should raise_error(SyntaxError)
-> { eval("-.75") }.should raise_error(SyntaxError)
expect_syntax_error(".75")
expect_syntax_error("-.75")
end

it "can have an exponent" do
Expand Down
16 changes: 8 additions & 8 deletions language/precedence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ def >=(a); 0; end;
end

it "<=> == === != =~ !~ are non-associative" do
-> { eval("1 <=> 2 <=> 3") }.should raise_error(SyntaxError)
-> { eval("1 == 2 == 3") }.should raise_error(SyntaxError)
-> { eval("1 === 2 === 3") }.should raise_error(SyntaxError)
-> { eval("1 != 2 != 3") }.should raise_error(SyntaxError)
-> { eval("1 =~ 2 =~ 3") }.should raise_error(SyntaxError)
-> { eval("1 !~ 2 !~ 3") }.should raise_error(SyntaxError)
expect_syntax_error("1 <=> 2 <=> 3")
expect_syntax_error("1 == 2 == 3")
expect_syntax_error("1 === 2 === 3")
expect_syntax_error("1 != 2 != 3")
expect_syntax_error("1 =~ 2 =~ 3")
expect_syntax_error("1 !~ 2 !~ 3")
end

it "<=> == === != =~ !~ have higher precedence than &&" do
Expand Down Expand Up @@ -290,8 +290,8 @@ class FalseClass; undef_method :=~; end
end

it ".. ... are non-associative" do
-> { eval("1..2..3") }.should raise_error(SyntaxError)
-> { eval("1...2...3") }.should raise_error(SyntaxError)
expect_syntax_error("1..2..3")
expect_syntax_error("1...2...3")
end

it ".. ... have higher precedence than ? :" do
Expand Down
8 changes: 4 additions & 4 deletions language/predefined_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ def obj.foo2; yield; end
end

it "raises a SyntaxError if assigned to" do
-> { eval("nil = true") }.should raise_error(SyntaxError)
expect_syntax_error("nil = true")
end
end

Expand All @@ -1110,7 +1110,7 @@ def obj.foo2; yield; end
end

it "raises a SyntaxError if assigned to" do
-> { eval("true = false") }.should raise_error(SyntaxError)
expect_syntax_error("true = false")
end
end

Expand All @@ -1120,13 +1120,13 @@ def obj.foo2; yield; end
end

it "raises a SyntaxError if assigned to" do
-> { eval("false = nil") }.should raise_error(SyntaxError)
expect_syntax_error("false = nil")
end
end

describe "The self pseudo-variable" do
it "raises a SyntaxError if assigned to" do
-> { eval("self = 1") }.should raise_error(SyntaxError)
expect_syntax_error("self = 1")
end
end

Expand Down
4 changes: 1 addition & 3 deletions language/redo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@

describe "in a method" do
it "is invalid and raises a SyntaxError" do
-> {
eval("def m; redo; end")
}.should raise_error(SyntaxError)
expect_syntax_error("def m; redo; end")
end
end
end
2 changes: 1 addition & 1 deletion language/regexp/character_classes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
/[^[:lower:]A-C]+/.match("abcABCDEF123def").to_a.should == ["DEF123"] # negated character class
/[:alnum:]+/.match("a:l:n:u:m").to_a.should == ["a:l:n:u:m"] # should behave like regular character class composed of the individual letters
/[\[:alnum:]+/.match("[:a:l:n:u:m").to_a.should == ["[:a:l:n:u:m"] # should behave like regular character class composed of the individual letters
-> { eval('/[[:alpha:]-[:digit:]]/') }.should raise_error(SyntaxError) # can't use character class as a start value of range
expect_syntax_error('/[[:alpha:]-[:digit:]]/')
end

it "matches ASCII characters with [[:ascii:]]" do
Expand Down
Loading