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 70 71 72 73 74 75 76 77 78 79 80 81 82
|
require 'test/unit'
class TestExceptions < Test::Unit::TestCase
def testBasic
begin
raise "this must be handled"
fail "Should have raised exception"
rescue
assert(true)
end
end
def testBasicWithRetry
again = true
begin
raise "this must be handled no.2"
rescue
if again
again = false
retry
fail "should have retried"
end
end
assert(!again)
end
def testExceptionInRescueClause
string = "this must be handled no.3"
begin
begin
raise "exception in rescue clause"
rescue
raise string
end
fail "should have raised exception"
rescue
assert_equal(string, $!.message)
end
end
def testExceptionInEnsureClause
begin
begin
raise "this must be handled no.4"
ensure
raise "exception in ensure clause"
end
fail "exception should have been raised"
rescue
assert(true)
end
end
def testEnsureInNestedException
bad = true
begin
begin
raise "this must be handled no.5"
ensure
bad = false
end
rescue
end
assert(!bad)
end
def testEnsureTriggeredByBreak
bad = true
while true
begin
break
ensure
bad = false
end
end
assert(!bad)
end
end
|