1 | ##
|
---|
2 | # Exception ISO Test
|
---|
3 |
|
---|
4 | assert('Exception', '15.2.22') do
|
---|
5 | assert_equal Class, Exception.class
|
---|
6 | end
|
---|
7 |
|
---|
8 | assert('Exception.exception', '15.2.22.4.1') do
|
---|
9 | e = Exception.exception('a')
|
---|
10 |
|
---|
11 | assert_equal Exception, e.class
|
---|
12 | end
|
---|
13 |
|
---|
14 | assert('Exception#exception', '15.2.22.5.1') do
|
---|
15 | e = Exception.new
|
---|
16 | re = RuntimeError.new
|
---|
17 | assert_equal e, e.exception
|
---|
18 | assert_equal e, e.exception(e)
|
---|
19 | assert_equal re, re.exception(re)
|
---|
20 | changed_re = re.exception('message has changed')
|
---|
21 | assert_not_equal re, changed_re
|
---|
22 | assert_equal 'message has changed', changed_re.message
|
---|
23 | end
|
---|
24 |
|
---|
25 | assert('Exception#message', '15.2.22.5.2') do
|
---|
26 | e = Exception.exception('a')
|
---|
27 |
|
---|
28 | assert_equal 'a', e.message
|
---|
29 | end
|
---|
30 |
|
---|
31 | assert('Exception#to_s', '15.2.22.5.3') do
|
---|
32 | e = Exception.exception('a')
|
---|
33 |
|
---|
34 | assert_equal 'a', e.to_s
|
---|
35 | end
|
---|
36 |
|
---|
37 | assert('Exception.exception', '15.2.22.4.1') do
|
---|
38 | e = Exception.exception()
|
---|
39 | e.initialize('a')
|
---|
40 |
|
---|
41 | assert_equal 'a', e.message
|
---|
42 | end
|
---|
43 |
|
---|
44 | assert('NameError', '15.2.31') do
|
---|
45 | assert_raise(NameError) do
|
---|
46 | raise NameError.new
|
---|
47 | end
|
---|
48 |
|
---|
49 | e = NameError.new "msg", "name"
|
---|
50 | assert_equal "msg", e.message
|
---|
51 | assert_equal "name", e.name
|
---|
52 | end
|
---|
53 |
|
---|
54 | assert('ScriptError', '15.2.37') do
|
---|
55 | assert_raise(ScriptError) do
|
---|
56 | raise ScriptError.new
|
---|
57 | end
|
---|
58 | end
|
---|
59 |
|
---|
60 | assert('SyntaxError', '15.2.38') do
|
---|
61 | assert_raise(SyntaxError) do
|
---|
62 | raise SyntaxError.new
|
---|
63 | end
|
---|
64 | end
|
---|
65 |
|
---|
66 | # Not ISO specified
|
---|
67 |
|
---|
68 | assert('Exception 1') do
|
---|
69 | r=begin
|
---|
70 | 1+1
|
---|
71 | ensure
|
---|
72 | 2+2
|
---|
73 | end
|
---|
74 | assert_equal 2, r
|
---|
75 | end
|
---|
76 |
|
---|
77 | assert('Exception 2') do
|
---|
78 | r=begin
|
---|
79 | 1+1
|
---|
80 | begin
|
---|
81 | 2+2
|
---|
82 | ensure
|
---|
83 | 3+3
|
---|
84 | end
|
---|
85 | ensure
|
---|
86 | 4+4
|
---|
87 | end
|
---|
88 | assert_equal 4, r
|
---|
89 | end
|
---|
90 |
|
---|
91 | assert('Exception 3') do
|
---|
92 | r=begin
|
---|
93 | 1+1
|
---|
94 | begin
|
---|
95 | 2+2
|
---|
96 | ensure
|
---|
97 | 3+3
|
---|
98 | end
|
---|
99 | ensure
|
---|
100 | 4+4
|
---|
101 | begin
|
---|
102 | 5+5
|
---|
103 | ensure
|
---|
104 | 6+6
|
---|
105 | end
|
---|
106 | end
|
---|
107 | assert_equal 4, r
|
---|
108 | end
|
---|
109 |
|
---|
110 | assert('Exception 4') do
|
---|
111 | a = nil
|
---|
112 | 1.times{|e|
|
---|
113 | begin
|
---|
114 | rescue => err
|
---|
115 | end
|
---|
116 | a = err.class
|
---|
117 | }
|
---|
118 | assert_equal NilClass, a
|
---|
119 | end
|
---|
120 |
|
---|
121 | assert('Exception 5') do
|
---|
122 | $ans = []
|
---|
123 | def m
|
---|
124 | $!
|
---|
125 | end
|
---|
126 | def m2
|
---|
127 | 1.times{
|
---|
128 | begin
|
---|
129 | return
|
---|
130 | ensure
|
---|
131 | $ans << m
|
---|
132 | end
|
---|
133 | }
|
---|
134 | end
|
---|
135 | m2
|
---|
136 | assert_equal [nil], $ans
|
---|
137 | end
|
---|
138 |
|
---|
139 | assert('Exception 6') do
|
---|
140 | $i = 0
|
---|
141 | def m
|
---|
142 | iter{
|
---|
143 | begin
|
---|
144 | $i += 1
|
---|
145 | begin
|
---|
146 | $i += 2
|
---|
147 | break
|
---|
148 | ensure
|
---|
149 |
|
---|
150 | end
|
---|
151 | ensure
|
---|
152 | $i += 4
|
---|
153 | end
|
---|
154 | $i = 0
|
---|
155 | }
|
---|
156 | end
|
---|
157 |
|
---|
158 | def iter
|
---|
159 | yield
|
---|
160 | end
|
---|
161 | m
|
---|
162 | assert_equal 7, $i
|
---|
163 | end
|
---|
164 |
|
---|
165 | assert('Exception 7') do
|
---|
166 | $i = 0
|
---|
167 | def m
|
---|
168 | begin
|
---|
169 | $i += 1
|
---|
170 | begin
|
---|
171 | $i += 2
|
---|
172 | return
|
---|
173 | ensure
|
---|
174 | $i += 3
|
---|
175 | end
|
---|
176 | ensure
|
---|
177 | $i += 4
|
---|
178 | end
|
---|
179 | p :end
|
---|
180 | end
|
---|
181 | m
|
---|
182 | assert_equal 10, $i
|
---|
183 | end
|
---|
184 |
|
---|
185 | assert('Exception 8') do
|
---|
186 | r=begin
|
---|
187 | 1
|
---|
188 | rescue
|
---|
189 | 2
|
---|
190 | else
|
---|
191 | 3
|
---|
192 | end
|
---|
193 | assert_equal 3, r
|
---|
194 | end
|
---|
195 |
|
---|
196 | assert('Exception 9') do
|
---|
197 | r=begin
|
---|
198 | 1+1
|
---|
199 | rescue
|
---|
200 | 2+2
|
---|
201 | else
|
---|
202 | 3+3
|
---|
203 | ensure
|
---|
204 | 4+4
|
---|
205 | end
|
---|
206 | assert_equal 6, r
|
---|
207 | end
|
---|
208 |
|
---|
209 | assert('Exception 10') do
|
---|
210 | r=begin
|
---|
211 | 1+1
|
---|
212 | begin
|
---|
213 | 2+2
|
---|
214 | rescue
|
---|
215 | 3+3
|
---|
216 | else
|
---|
217 | 4+4
|
---|
218 | end
|
---|
219 | rescue
|
---|
220 | 5+5
|
---|
221 | else
|
---|
222 | 6+6
|
---|
223 | ensure
|
---|
224 | 7+7
|
---|
225 | end
|
---|
226 | assert_equal 12, r
|
---|
227 | end
|
---|
228 |
|
---|
229 | assert('Exception 11') do
|
---|
230 | a = :ok
|
---|
231 | begin
|
---|
232 | begin
|
---|
233 | raise Exception
|
---|
234 | rescue
|
---|
235 | a = :ng
|
---|
236 | end
|
---|
237 | rescue Exception
|
---|
238 | end
|
---|
239 | assert_equal :ok, a
|
---|
240 | end
|
---|
241 |
|
---|
242 | assert('Exception 12') do
|
---|
243 | a = :ok
|
---|
244 | begin
|
---|
245 | raise Exception rescue a = :ng
|
---|
246 | rescue Exception
|
---|
247 | end
|
---|
248 | assert_equal :ok, a
|
---|
249 | end
|
---|
250 |
|
---|
251 | assert('Exception 13') do
|
---|
252 | a = :ng
|
---|
253 | begin
|
---|
254 | raise StandardError
|
---|
255 | rescue TypeError, ArgumentError
|
---|
256 | a = :ng
|
---|
257 | rescue
|
---|
258 | a = :ok
|
---|
259 | else
|
---|
260 | a = :ng
|
---|
261 | end
|
---|
262 | assert_equal :ok, a
|
---|
263 | end
|
---|
264 |
|
---|
265 | assert('Exception 14') do
|
---|
266 | def exception_test14; UnknownConstant; end
|
---|
267 | a = :ng
|
---|
268 | begin
|
---|
269 | send(:exception_test14)
|
---|
270 | rescue
|
---|
271 | a = :ok
|
---|
272 | end
|
---|
273 |
|
---|
274 | assert_equal :ok, a
|
---|
275 | end
|
---|
276 |
|
---|
277 | assert('Exception 15') do
|
---|
278 | a = begin
|
---|
279 | :ok
|
---|
280 | rescue
|
---|
281 | :ko
|
---|
282 | end
|
---|
283 | assert_equal :ok, a
|
---|
284 | end
|
---|
285 |
|
---|
286 | assert('Exception 16') do
|
---|
287 | begin
|
---|
288 | raise "foo"
|
---|
289 | false
|
---|
290 | rescue => e
|
---|
291 | assert_equal "foo", e.message
|
---|
292 | end
|
---|
293 | end
|
---|
294 |
|
---|
295 | assert('Exception 17') do
|
---|
296 | r=begin
|
---|
297 | raise "a" # RuntimeError
|
---|
298 | rescue ArgumentError
|
---|
299 | 1
|
---|
300 | rescue StandardError
|
---|
301 | 2
|
---|
302 | else
|
---|
303 | 3
|
---|
304 | ensure
|
---|
305 | 4
|
---|
306 | end
|
---|
307 | assert_equal 2, r
|
---|
308 | end
|
---|
309 |
|
---|
310 | assert('Exception 18') do
|
---|
311 | r=begin
|
---|
312 | 0
|
---|
313 | rescue ArgumentError
|
---|
314 | 1
|
---|
315 | rescue StandardError
|
---|
316 | 2
|
---|
317 | else
|
---|
318 | 3
|
---|
319 | ensure
|
---|
320 | 4
|
---|
321 | end
|
---|
322 | assert_equal 3, r
|
---|
323 | end
|
---|
324 |
|
---|
325 | assert('Exception 19') do
|
---|
326 | class Class4Exception19
|
---|
327 | def a
|
---|
328 | r = @e = false
|
---|
329 | begin
|
---|
330 | b
|
---|
331 | rescue TypeError
|
---|
332 | r = self.z
|
---|
333 | end
|
---|
334 | [ r, @e ]
|
---|
335 | end
|
---|
336 |
|
---|
337 | def b
|
---|
338 | begin
|
---|
339 | 1 * "b"
|
---|
340 | ensure
|
---|
341 | @e = self.z
|
---|
342 | end
|
---|
343 | end
|
---|
344 |
|
---|
345 | def z
|
---|
346 | true
|
---|
347 | end
|
---|
348 | end
|
---|
349 | assert_equal [true, true], Class4Exception19.new.a
|
---|
350 | end
|
---|
351 |
|
---|
352 | assert('Exception#inspect without message') do
|
---|
353 | assert_equal "Exception: Exception", Exception.new.inspect
|
---|
354 | end
|
---|
355 |
|
---|
356 | assert('Exception#backtrace') do
|
---|
357 | assert_nothing_raised do
|
---|
358 | begin
|
---|
359 | raise "get backtrace"
|
---|
360 | rescue => e
|
---|
361 | e.backtrace
|
---|
362 | end
|
---|
363 | end
|
---|
364 | end
|
---|
365 |
|
---|
366 | assert('Raise in ensure') do
|
---|
367 | assert_raise(ArgumentError) do
|
---|
368 | begin
|
---|
369 | raise "" # RuntimeError
|
---|
370 | ensure
|
---|
371 | raise ArgumentError
|
---|
372 | end
|
---|
373 | end
|
---|
374 | end
|
---|
375 |
|
---|
376 | assert('Raise in rescue') do
|
---|
377 | assert_raise(ArgumentError) do
|
---|
378 | begin
|
---|
379 | raise "" # RuntimeError
|
---|
380 | rescue
|
---|
381 | raise ArgumentError
|
---|
382 | end
|
---|
383 | end
|
---|
384 | end
|
---|