2 # Copyright (C) 2009-2010, Parrot Foundation.
7 t/pmc/exception.t - Exception Handling
11 % prove t/pmc/exception.t
15 Tests C<Exception> and C<ExceptionHandler> PMCs.
19 .include 'except_types.pasm'
22 .include 'test_more.pir'
30 test_setattribute_wrong()
34 test_push_pop_eh_long()
46 ok($P1,'Exception object return true')
53 is($I0, 42, 'set/get integer on Exception')
56 .sub test_integer_keyed
59 ex = new ['Exception']
62 is(value, 0, 'get type default value')
63 ex['type'] = .EXCEPTION_SYNTAX_ERROR
65 is(value, .EXCEPTION_SYNTAX_ERROR, 'get type value changed')
67 value = ex['exit_code']
68 is(value, 0, 'get exit_code default value')
70 value = ex['exit_code']
71 is(value, 127, 'get exit_code value changed')
74 is(value, 0, 'get handled default is false')
77 is(value, 1, 'get handled value changed')
79 eh = new ['ExceptionHandler']
80 eh.'handle_types'(.EXCEPTION_ATTRIB_NOT_FOUND)
84 ex['the droids you are looking for'] = 42
88 is(value, 1, 'set invalid key throws')
92 value = ex['the droids you are looking for']
96 is(value, 1, 'get invalid key throws')
99 .sub test_string_keyed
102 .const string TEST_VALUE = 'fubar'
103 ex = new ['Exception']
104 ex['message'] = TEST_VALUE
105 value = ex['message']
106 is(value, TEST_VALUE, 'set/get string_keyed')
112 .param string attrname
114 $P0 = new ['Integer'], value
115 setattribute obj, attrname, $P0
120 .param string attrname
124 setattribute obj, attrname, $P0
129 .param string attrname
130 .param int checkvalue
132 $S0 = 'got ' . attrname
133 $P1 = getattribute obj, attrname
135 is($I1, checkvalue, $S0)
140 .param string attrname
141 .param string checkvalue
143 $S0 = 'got ' . attrname
144 $P1 = getattribute obj, attrname
146 is($S1, checkvalue, $S0)
151 $P0 = new 'ExceptionHandler'
152 set_addr $P0, _handler
157 getattribute $P1, $P0, 'type'
159 getattribute $P2, $P0, 'handled'
160 is($P2,0,'got handled')
161 getattribute $P3, $P0, 'exit_code'
162 is($P2,0,'got exit_code')
163 getattribute $P4, $P0, 'severity'
168 getattribute $P5, $P0, 'foo'
175 ok($I0, "Can't fetch non-existent attribute")
179 $P1 = new ['Exception']
181 setattr_int($P1, 'type', 5)
182 setattr_int($P1, 'severity', 6)
183 setattr_int($P1, 'exit_code', 7)
184 setattr_int($P1, 'handled', 1)
185 setattr_string($P1, 'message', "just pining")
186 setattr_string($P1, 'payload', "additional payload")
188 $P8 = new ['ResizablePMCArray']
190 $P8[0] = 'backtrace line 1'
191 $P8[1] = 'backtrace line 2'
192 setattribute $P1, 'backtrace', $P8
196 ok(0, "throwing exception failed")
197 skip(7, "because of throwing exception failed")
203 gotattr_int($P0, 'type', 5)
204 gotattr_int($P0, 'severity', 6)
205 gotattr_int($P0, 'exit_code', 7)
206 gotattr_int($P0, 'handled', 1)
207 gotattr_string($P0, 'message', "just pining")
208 gotattr_string($P0, 'payload', "additional payload")
210 $P28 = getattribute $P0, 'backtrace'
212 is($P30, "backtrace line 1", 'got backtrace data')
215 is($P31, "backtrace line 2", 'more backtrace data')
218 .sub test_setattribute_wrong
221 ex = new ['Exception']
222 eh = new ['ExceptionHandler']
223 eh.'handle_types'(.EXCEPTION_ATTRIB_NOT_FOUND)
227 setattribute ex, 'wrong attribute', eh
233 is(result, 1, 'setting a wrong attribute throws')
238 ex = new ['Exception']
242 bt = getattribute ex, 'birthtime'
244 is(nbt, n, 'get and set birthtime')
247 .sub test_handler_ctx
248 .local pmc ex, eh, hc
250 ex = new ['Exception']
251 eh = new ['ExceptionHandler']
252 eh.'handle_types'(.EXCEPTION_INVALID_OPERATION)
255 set_label eh, catch_get
257 hc = getattribute ex, 'handler_ctx'
263 is(result, 1, 'get handler_ctx invalid operation')
266 set_label eh, catch_set
267 setattribute ex, 'handler_ctx', ex
273 is(result, 1, 'set handler_ctx invalid operation')
276 .sub test_push_pop_eh
278 ok(1,'push_eh works')
285 say "i am the decider"
288 .sub test_push_eh_throw
290 $P0 = new ['Exception']
292 ok(0,'throw does not throw')
295 ok(1,'throw can throw')
298 .sub test_push_pop_eh_long
299 $P0 = new ['ExceptionHandler']
300 set_addr $P0, handler
302 ok(1,'push_eh works (long)')
305 ok(1,'pop_eh works (long)')
309 say "i am the decider"
322 new $P20, ['ExceptionHandler']
323 set_addr $P20, _handler
325 new $P30, ['Exception']
329 ok(1,'caught exception object thrown')
332 # Test clone vtable function
335 .local pmc ex, exclone
336 ex = new ['Exception']
337 ex['type'] = .EXCEPTION_SYNTAX_ERROR
340 result = iseq ex, exclone
341 is(result, 1, 'cloned Exception is equal to original')
342 exclone['type'] = .EXCEPTION_ERR_OVERFLOW
343 result = iseq ex, exclone
344 is(result, 0, 'cloned and modified Exception is not equal to original')
347 .sub test_throw_clone
348 .local pmc ex, exclone, eh, ehguard
350 ex = new ['Exception']
351 ex['type'] = .EXCEPTION_SYNTAX_ERROR
354 ehguard = new ['ExceptionHandler']
355 set_label ehguard, catchall
357 eh = new ['ExceptionHandler']
358 eh.'handle_types'(.EXCEPTION_SYNTAX_ERROR)
369 is(result, 1, 'caught a cloned Exception')
373 .local pmc pay, getpay, exc
374 set_label ehguard, catchall2
377 pay = new ['Integer'], 9875
380 result = iseq ex, exclone
381 is(result, 1, 'cloned Exception with payload is equal to original')
388 getpay = exc['payload']
390 if $I0 != 9875 goto catchall2
393 is(result, 1, 'caught a cloned Exception with payload')
398 ex = new ['Exception']
399 bt = ex.'backtrace'()
401 is($I0, 0, 'got backtrace from unthrow Exception')
404 .sub test_annotations
406 ex = new ['Exception']
407 ann = ex.'annotations'()
409 is($I0, 0, 'got annotations from unthrow Exception')
411 is($I0, 0, 'annotations from unthrow Exception are empty')
418 # vim: expandtab shiftwidth=4 ft=pir: