File: exception_handler_spec.rb

package info (click to toggle)
pry 0.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,772 kB
  • sloc: ruby: 21,692; sh: 17; makefile: 11
file content (81 lines) | stat: -rw-r--r-- 2,455 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

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)
        if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.2')
          expect(output.string)
            .to include("StandardError: oops\nfrom /bin/pry:23:in `<main>'\n")
        else
          message = "StandardError: oops (StandardError)\nfrom /bin/pry:23:in `<main>'\n"
          expect(output.string)
            .to include(message)
        end
      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?('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)
        if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.2')
          expect(output.string).to match(
            /RuntimeError:\souter\soops\n
            from\s.+\n
            Caused\sby\sRuntimeError:\snested\soops\n
            from.+/x
          )
        else
          expect(output.string).to match(
            /RuntimeError:\souter\soops\s\(RuntimeError\)\n
            from\s.+\n
            Caused\sby\sRuntimeError:\snested\soops\n
            from\s.+/x
          )
        end
      end
    end
  end
end