From 4303919197184a59b086fddfd30863d05d87c043 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Fri, 2 Feb 2024 17:34:41 +0800 Subject: [PATCH] Add SyntaxError#path --- core/exception/fixtures/syntax_error.rb | 1 + core/exception/syntax_error_spec.rb | 26 +++++++++++++++++++++++++ core/kernel/eval_spec.rb | 4 ++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 core/exception/fixtures/syntax_error.rb create mode 100644 core/exception/syntax_error_spec.rb 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:.+/ }