summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorBarrett Ingram <bingram@eab.com>2020-03-21 10:53:27 -0500
committerBarrett Ingram <bingram@eab.com>2020-03-21 11:17:27 -0500
commitf6736d526260b2aa9f50b61150a67fcfa2045893 (patch)
tree824683180ee5da0ab33f8bc4698d358097bd53f3 /spec
parent2208ed30f246b4dbeb512745021e00052781ece6 (diff)
downloadpry-f6736d526260b2aa9f50b61150a67fcfa2045893.tar.gz
Display all syntax error messages when catching SyntaxException
Previously when catching syntax errors in the REPL, we were only showing the last syntax error displayed by the ruby output. However, ruby can generate multiple syntax error messages within a single SyntaxException. For example, this code generates multiple syntax error messages: ``` $ ruby -e 'puts {"key"=>"val"}.to_json' -e:1: syntax error, unexpected =>, expecting '}' puts {"key"=>"val"}.to_json -e:1: syntax error, unexpected '}', expecting end-of-input puts {"key"=>"val"}.to_json ``` We can't predict which error message would be most helpful for the consumer - we should show both of them. This commit modifies the string replacement we're doing when printing SyntaxExceptions so any number of syntax error lines will be shown correctly. Issue: https://github.com/pry/pry/issues/2102 The error message of SyntaxError is different from Ruby's one
Diffstat (limited to 'spec')
-rw-r--r--spec/syntax_checking_spec.rb28
1 files changed, 23 insertions, 5 deletions
diff --git a/spec/syntax_checking_spec.rb b/spec/syntax_checking_spec.rb
index ec7f5b36..ca75ba9f 100644
--- a/spec/syntax_checking_spec.rb
+++ b/spec/syntax_checking_spec.rb
@@ -23,17 +23,21 @@ describe Pry do
end
end
- ([
+ [
["end"],
["puts )("],
["1 1"],
- ["puts :"]
- ] + [
+ ["puts :"],
+
# in this case the syntax error is "expecting ')'".
["def", "method(1"],
+
# in this case the syntax error is "expecting keyword_end".
- ["o = Object.new.tap{ def o.render;", "'MEH'", "}"]
- ]).compact.each do |foo|
+ ["o = Object.new.tap{ def o.render;", "'MEH'", "}"],
+
+ # multiple syntax errors reported in one SyntaxException
+ ["puts {'key'=>'val'}.to_json"]
+ ].compact.each do |foo|
it "should raise an error on invalid syntax like #{foo.inspect}" do
redirect_pry_io(InputTester.new(*foo), @str_output) do
Pry.start
@@ -41,6 +45,20 @@ describe Pry do
expect(@str_output.string).to match(/SyntaxError/)
end
+
+ it "should display correct number of errors on invalid syntax like #{foo.inspect}" do
+ begin
+ # rubocop:disable Security/Eval
+ eval(foo.join("\n"))
+ # rubocop:enable Security/Eval
+ rescue SyntaxError => e
+ error_count = e.message.scan(/syntax error/).count
+ end
+ expect(error_count).not_to be_nil
+
+ pry_output = mock_pry(*foo)
+ expect(pry_output.scan(/SyntaxError/).count).to eq(error_count)
+ end
end
it "should not intefere with syntax errors explicitly raised" do