diff --git a/core/exception/fixtures/syntax_error.rb b/core/exception/fixtures/syntax_error.rb new file mode 100644 index 000000000..578a61ad6 --- /dev/null +++ b/core/exception/fixtures/syntax_error.rb @@ -0,0 +1 @@ +1+1=2 diff --git a/core/exception/syntax_error_spec.rb b/core/exception/syntax_error_spec.rb new file mode 100644 index 000000000..60e60ad6a --- /dev/null +++ b/core/exception/syntax_error_spec.rb @@ -0,0 +1,26 @@ +require_relative '../../spec_helper' + + +ruby_version_is "3.2" do + describe "SyntaxError#path" do + it "returns the file path provided to eval" do + expected = 'speccing.rb' + -> { + eval('if true', TOPLEVEL_BINDING, expected) + }.should raise_error(SyntaxError) { |e| + e.path.should == expected + } + end + + it "returns the file path that raised an exception" do + expected_path = fixture(__FILE__, 'syntax_error.rb') + -> { + require_relative 'fixtures/syntax_error' + }.should raise_error(SyntaxError) {|e| e.path.should == expected_path } + end + + it "returns nil when constructed directly" do + SyntaxError.new.path.should == nil + end + end +end diff --git a/core/kernel/eval_spec.rb b/core/kernel/eval_spec.rb index 15c9d511f..eab8b72fe 100644 --- a/core/kernel/eval_spec.rb +++ b/core/kernel/eval_spec.rb @@ -135,7 +135,7 @@ it "includes file and line information in syntax error" do expected = 'speccing.rb' -> { - eval('if true',TOPLEVEL_BINDING, expected) + eval('if true', TOPLEVEL_BINDING, expected) }.should raise_error(SyntaxError) { |e| e.message.should =~ /#{expected}:1:.+/ } @@ -144,7 +144,7 @@ it "evaluates string with given filename and negative linenumber" do expected_file = 'speccing.rb' -> { - eval('if true',TOPLEVEL_BINDING, expected_file, -100) + eval('if true', TOPLEVEL_BINDING, expected_file, -100) }.should raise_error(SyntaxError) { |e| e.message.should =~ /#{expected_file}:-100:.+/ }