summaryrefslogtreecommitdiff
path: root/spec/ruby/core/exception/exception_spec.rb
blob: d6f5283bd9e47a55a202f879e5de875794cf87fd (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require_relative '../../spec_helper'
require_relative 'fixtures/common'
require_relative 'shared/new'

describe "Exception.exception" do
  it_behaves_like :exception_new, :exception
end

describe "Exception#exception" do
  it "returns self when passed no argument" do
    e = RuntimeError.new
    e.should == e.exception
  end

  it "returns self when passed self as an argument" do
    e = RuntimeError.new
    e.should == e.exception(e)
  end

  it "returns an exception of the same class as self with the message given as argument" do
    e = RuntimeError.new
    e2 = e.exception("message")
    e2.should be_an_instance_of(RuntimeError)
    e2.message.should == "message"
  end

  it "when raised will be rescued as the new exception" do
    begin
      begin
        raised_first = StandardError.new('first')
        raise raised_first
      rescue => caught_first
        raised_second = raised_first.exception('second')
        raise raised_second
      end
    rescue => caught_second
    end

    raised_first.should == caught_first
    raised_second.should == caught_second
  end

  it "captures an exception into $!" do
    exception = begin
      raise
    rescue RuntimeError
      $!
    end

    exception.class.should == RuntimeError
    exception.message.should == ""
    exception.backtrace.first.should =~ /exception_spec/
  end

  class CustomArgumentError < StandardError
    attr_reader :val
    def initialize(val)
      @val = val
    end
  end

  it "returns an exception of the same class as self with the message given as argument, but without reinitializing" do
    e = CustomArgumentError.new(:boom)
    e2 = e.exception("message")
    e2.should be_an_instance_of(CustomArgumentError)
    e2.val.should == :boom
    e2.message.should == "message"
  end
end