summaryrefslogtreecommitdiff
path: root/spec/ruby/shared/kernel/raise.rb
blob: 70d638fff9ebab882915583e4b3743d71036649e (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
describe :kernel_raise, shared: true do
  before :each do
    ScratchPad.clear
  end

  it "aborts execution" do
    lambda do
      @object.raise Exception, "abort"
      ScratchPad.record :no_abort
    end.should raise_error(Exception, "abort")

    ScratchPad.recorded.should be_nil
  end

  it "raises RuntimeError if no exception class is given" do
    lambda { @object.raise }.should raise_error(RuntimeError)
  end

  it "raises a given Exception instance" do
    error = RuntimeError.new
    lambda { @object.raise(error) }.should raise_error(error)
  end

  it "raises a RuntimeError if string given" do
    lambda { @object.raise("a bad thing") }.should raise_error(RuntimeError)
  end

  it "raises a TypeError when passed a non-Exception object" do
    lambda { @object.raise(Object.new) }.should raise_error(TypeError)
  end

  it "raises a TypeError when passed true" do
    lambda { @object.raise(true) }.should raise_error(TypeError)
  end

  it "raises a TypeError when passed false" do
    lambda { @object.raise(false) }.should raise_error(TypeError)
  end

  it "raises a TypeError when passed nil" do
    lambda { @object.raise(nil) }.should raise_error(TypeError)
  end

  it "re-raises the rescued exception" do
    lambda do
      begin
        raise Exception, "outer"
        ScratchPad.record :no_abort
      rescue
        begin
          raise StandardError, "inner"
        rescue
        end

        @object.raise
        ScratchPad.record :no_reraise
      end
    end.should raise_error(Exception, "outer")

    ScratchPad.recorded.should be_nil
  end

  it "allows Exception, message, and backtrace parameters" do
    lambda do
      @object.raise(ArgumentError, "message", caller)
    end.should raise_error(ArgumentError, "message")
  end
end