| 1 |
assert 'mrb_protect' do |
| 2 |
# no failure in protect returns [result, false] |
| 3 |
assert_equal ['test', false] do |
| 4 |
ExceptionTest.mrb_protect { 'test' } |
| 5 |
end |
| 6 |
# failure in protect returns [exception, true] |
| 7 |
result = ExceptionTest.mrb_protect { raise 'test' } |
| 8 |
assert_kind_of RuntimeError, result[0] |
| 9 |
assert_true result[1] |
| 10 |
end |
| 11 |
|
| 12 |
assert 'mrb_ensure' do |
| 13 |
a = false |
| 14 |
assert_equal 'test' do |
| 15 |
ExceptionTest.mrb_ensure Proc.new { 'test' }, Proc.new { a = true } |
| 16 |
end |
| 17 |
assert_true a |
| 18 |
|
| 19 |
a = false |
| 20 |
assert_raise RuntimeError do |
| 21 |
ExceptionTest.mrb_ensure Proc.new { raise 'test' }, Proc.new { a = true } |
| 22 |
end |
| 23 |
assert_true a |
| 24 |
end |
| 25 |
|
| 26 |
assert 'mrb_rescue' do |
| 27 |
assert_equal 'test' do |
| 28 |
ExceptionTest.mrb_rescue Proc.new { 'test' }, Proc.new {} |
| 29 |
end |
| 30 |
|
| 31 |
class CustomExp < Exception |
| 32 |
end |
| 33 |
|
| 34 |
assert_raise CustomExp do |
| 35 |
ExceptionTest.mrb_rescue Proc.new { raise CustomExp.new 'test' }, Proc.new { 'rescue' } |
| 36 |
end |
| 37 |
|
| 38 |
assert_equal 'rescue' do |
| 39 |
ExceptionTest.mrb_rescue Proc.new { raise 'test' }, Proc.new { 'rescue' } |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
assert 'mrb_rescue_exceptions' do |
| 44 |
assert_equal 'test' do |
| 45 |
ExceptionTest.mrb_rescue_exceptions Proc.new { 'test' }, Proc.new {} |
| 46 |
end |
| 47 |
|
| 48 |
assert_raise RangeError do |
| 49 |
ExceptionTest.mrb_rescue_exceptions Proc.new { raise RangeError.new 'test' }, Proc.new { 'rescue' } |
| 50 |
end |
| 51 |
|
| 52 |
assert_equal 'rescue' do |
| 53 |
ExceptionTest.mrb_rescue_exceptions Proc.new { raise TypeError.new 'test' }, Proc.new { 'rescue' } |
| 54 |
end |
| 55 |
end |