diff options
author | Kyrylo Silin <silin@kyrylo.org> | 2019-04-14 19:14:16 +0300 |
---|---|---|
committer | Kyrylo Silin <silin@kyrylo.org> | 2019-04-15 00:08:24 +0300 |
commit | 95d3a74e9b22c44ea48ce870171640f0471db09b (patch) | |
tree | 5dae8107c718d96c11a0340d7b3c55a550fab0d0 /spec | |
parent | 9306e69bda2f44985944cbc162db4723cb8a0ec4 (diff) | |
download | pry-95d3a74e9b22c44ea48ce870171640f0471db09b.tar.gz |
config: factor out exception_handler hook to a separate classexception-handler-refactoring
Diffstat (limited to 'spec')
-rw-r--r-- | spec/exception_handler_spec.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/spec/exception_handler_spec.rb b/spec/exception_handler_spec.rb new file mode 100644 index 00000000..62138153 --- /dev/null +++ b/spec/exception_handler_spec.rb @@ -0,0 +1,64 @@ +RSpec.describe Pry::ExceptionHandler do + describe ".handle_exception" do + let(:output) { StringIO.new } + let(:pry_instance) { Pry.new } + + context "when exception is a UserError and a SyntaxError" do + let(:exception) do + SyntaxError.new('cool syntax error, dude').extend(Pry::UserError) + end + + it "prints the syntax error with customized message" do + described_class.handle_exception(output, exception, pry_instance) + expect(output.string).to start_with("SyntaxError: dude\n") + end + end + + context "when exception is a standard error" do + let(:exception) do + error = StandardError.new('oops') + error.set_backtrace(["/bin/pry:23:in `<main>'"]) + error + end + + it "prints standard error message" do + described_class.handle_exception(output, exception, pry_instance) + expect(output.string) + .to eq("StandardError: oops\nfrom /bin/pry:23:in `<main>'\n") + end + end + + context "when exception is a nested standard error" do + let(:exception) do + error = nil + begin + begin + raise 'nested oops' + rescue # rubocop:disable Style/RescueStandardError + raise 'outer oops' + end + rescue StandardError => outer_error + error = outer_error + end + + error + end + + before do + if RUBY_VERSION.start_with?('1.9', '2.0') + skip("Ruby #{RUBY_VERSION} doesn't support nested exceptions") + end + end + + it "prints standard error message" do + described_class.handle_exception(output, exception, pry_instance) + expect(output.string).to match( + /RuntimeError:\souter\soops\n + from\s.+\n + Caused\sby\sRuntimeError:\snested\soops\n + from.+/x + ) + end + end + end +end |