summaryrefslogtreecommitdiff
path: root/spec/ruby/core/exception/top_level_spec.rb
blob: b47648425eeb01ceee303c27bce06a18d3eb18dd (plain)
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
require_relative '../../spec_helper'

describe "An Exception reaching the top level" do
  it "is printed on STDERR" do
    ruby_exe('raise "foo"', args: "2>&1", exit_status: 1).should.include?("in `<main>': foo (RuntimeError)")
  end

  it "the Exception#cause is printed to STDERR with backtraces" do
    code = <<-RUBY
    def raise_cause
      raise "the cause"
    end
    def raise_wrapped
      raise "wrapped"
    end
    begin
      raise_cause
    rescue
      raise_wrapped
    end
    RUBY
    lines = ruby_exe(code, args: "2>&1", exit_status: 1).lines
    lines.reject! { |l| l.include?('rescue in') }
    lines.map! { |l| l.chomp[/:(in.+)/, 1] }
    lines.should == ["in `raise_wrapped': wrapped (RuntimeError)",
                     "in `<main>'",
                     "in `raise_cause': the cause (RuntimeError)",
                     "in `<main>'"]
  end

  describe "with a custom backtrace" do
    it "is printed on STDERR" do
      code = <<-RUBY
      raise RuntimeError, "foo", [
        "/dir/foo.rb:10:in `raising'",
        "/dir/bar.rb:20:in `caller'",
      ]
      RUBY
      ruby_exe(code, args: "2>&1", exit_status: 1).should == <<-EOS
/dir/foo.rb:10:in `raising': foo (RuntimeError)
\tfrom /dir/bar.rb:20:in `caller'
      EOS
    end
  end
end