summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi/exception_spec.rb
blob: 62d7d3706e1e57e651a268cf5d2ea4080d6bf86e (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
require_relative 'spec_helper'

load_extension("exception")

describe "C-API Exception function" do
  before :each do
    @s = CApiExceptionSpecs.new
  end

  describe "rb_exc_new" do
    it "creates an exception from a C string and length" do
      @s.rb_exc_new('foo').to_s.should == 'foo'
    end
  end

  describe "rb_exc_new2" do
    it "creates an exception from a C string" do
      @s.rb_exc_new2('foo').to_s.should == 'foo'
    end
  end

  describe "rb_exc_new3" do
    it "creates an exception from a Ruby string" do
      @s.rb_exc_new3('foo').to_s.should == 'foo'
    end
  end

  describe "rb_exc_raise" do
    it "raises passed exception" do
      runtime_error = RuntimeError.new '42'
      -> { @s.rb_exc_raise(runtime_error) }.should raise_error(RuntimeError, '42')
    end

    it "raises an exception with an empty backtrace" do
      runtime_error = RuntimeError.new '42'
      runtime_error.set_backtrace []
      -> { @s.rb_exc_raise(runtime_error) }.should raise_error(RuntimeError, '42')
    end
  end

  describe "rb_set_errinfo" do
    after :each do
      @s.rb_set_errinfo(nil)
    end

    it "accepts nil" do
      @s.rb_set_errinfo(nil).should be_nil
    end

    it "accepts an Exception instance" do
      @s.rb_set_errinfo(Exception.new).should be_nil
    end

    it "raises a TypeError if the object is not nil or an Exception instance" do
      -> { @s.rb_set_errinfo("error") }.should raise_error(TypeError)
    end
  end
end