-
Notifications
You must be signed in to change notification settings - Fork 4
/
exception_test.rb
executable file
·138 lines (120 loc) · 2.72 KB
/
exception_test.rb
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env ruby
require 'test/unit'
class ExceptionTest < Test::Unit::TestCase
def test_no_exception
assert_nothing_raised { true }
end
def test_raise_runtime_error
assert_raise(RuntimeError) { raise "Bomb" }
end
def test_handle_runtime_error
begin
raise "Bomb"
flunk "Should raise RuntimeError"
rescue
assert_equal(RuntimeError, $!.class)
assert_equal("Bomb", $!.message)
end
end
def test_reraise_runtime_error
begin
begin
raise "Bomb"
flunk "Should raise RuntimeError"
rescue
raise
end
flunk "Should raise RuntimeError"
rescue
assert_equal(RuntimeError, $!.class)
assert_equal("Bomb", $!.message)
end
end
def test_raise_runtime_error_explicit
begin
raise RuntimeError, "Bomb"
flunk "Should raise RuntimeError"
rescue
assert_equal(RuntimeError, $!.class)
assert_equal("Bomb", $!.message)
end
end
def test_custom_exception
begin
raise CustomException.new(13), "Bomb"
flunk "Should raise CustomException"
rescue CustomException => custom
assert_equal(CustomException, custom.class)
assert_equal("Bomb", custom.message)
assert_equal(13, custom.code)
end
end
def test_multiple_rescues
begin
raise CustomException.new(), "Bomb"
flunk "Should raise CustomException"
rescue CustomException => ex
assert_equal(CustomException, ex.class)
rescue RuntimeError => ex
flunk "Should raise CustomException"
end
end
def test_multiple_exceptions_one_rescue
begin
raise CustomException.new(), "Bomb"
flunk "Should raise CustomException"
rescue RuntimeError, CustomException => ex
assert_equal(CustomException, ex.class)
end
end
def test_ensure_with_exception_raised
raised = false
ensured = false
begin
raise RuntimeError
rescue RuntimeError
raised = true
ensure
ensured = true
end
assert(raised)
assert(ensured)
end
def test_ensure_without_exception_raised
ensured = false
begin
rescue RuntimeError
ensure
ensured = true
end
assert(ensured)
end
def test_retry
times = 2
attempts = 0
begin
attempts += 1
raise RuntimeError
rescue RuntimeError
retry if attempts < times
end
assert_equal(2, attempts)
end
def test_catch_throw
x = 0
catch (:done) do
for i in 1..10
x = i
throw :done unless i < 5
end
flunk "Should never get here"
end
assert_equal(5, x)
end
end
class CustomException < RuntimeError
attr :code
def initialize(code = 0)
@code = code
end
end