/
exceptionhandler.t
322 lines (261 loc) · 6.56 KB
/
exceptionhandler.t
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!./parrot
# Copyright (C) 2006-2010, Parrot Foundation.
=head1 NAME
t/pmc/exception_handler.t - test ExceptionHandler PMC
=head1 SYNOPSIS
% prove t/pmc/exceptionhandler.t
=head1 DESCRIPTION
Tests the ExceptionHandler PMC.
=cut
.include 'except_severity.pasm'
.include 'except_types.pasm'
.sub main :main
.include 'test_more.pir'
# If test exited with "bad plan" MyHandlerCan.can_handle wasn't invoked.
plan(19)
test_bool()
test_int()
.local pmc eh, eh2
eh = new ['ExceptionHandler']
ok(1, 'Instantiated ExceptionHandler')
set_label eh, nonfatal_handler_one
eh.'min_severity'(.EXCEPT_NORMAL)
eh.'max_severity'(.EXCEPT_WARNING)
push_eh eh
eh2 = new ['ExceptionHandler']
set_label eh2, error_handler_one
eh2.'min_severity'(.EXCEPT_ERROR)
eh2.'max_severity'(.EXCEPT_FATAL)
push_eh eh2
.local int i
i = eh.'min_severity'()
is(i, .EXCEPT_NORMAL, 'get min_severity - 1')
i = eh.'max_severity'()
is(i, .EXCEPT_WARNING, 'get max_severity - 1')
i = eh2.'min_severity'()
is(i, .EXCEPT_ERROR, 'get min_severity - 2')
i = eh2.'max_severity'()
is(i, .EXCEPT_FATAL, 'get max_severity - 2')
$P0 = new ['Exception']
$P0['severity'] = .EXCEPT_NORMAL
throw $P0
$P0 = new ['Exception']
$P0['severity'] = .EXCEPT_SEVERE
throw $P0
pop_eh
pop_eh
goto more_tests
nonfatal_handler_one:
.local pmc e, c
.get_results (e)
ok(1, 'Min and Max severity for exception handlers')
c = e['resume']
eh = 0
c()
error_handler_one:
.get_results (e)
ok(1, 'Min and Max severity for exception handlers')
c = e['resume']
eh = 0
c()
more_tests:
eh = new ['ExceptionHandler']
set_label eh, typed_handler_one
eh.'handle_types'(.CONTROL_OK, .CONTROL_BREAK)
push_eh eh
eh = new ['ExceptionHandler']
set_label eh, typed_handler_two
eh.'handle_types'(.EXCEPTION_SYNTAX_ERROR, .EXCEPTION_UNEXPECTED_NULL)
push_eh eh
$P0 = new ['Exception']
$P0['type'] = .CONTROL_OK
throw $P0
$P0 = new ['Exception']
$P0['type'] = .CONTROL_BREAK
throw $P0
pop_eh
pop_eh
test_handle_types_except()
goto init_int
typed_handler_one:
.get_results (e)
ok(1, 'Exception Handler type checks work')
c = e['resume']
eh = 0
c()
typed_handler_two:
.get_results (e)
ok(0, 'Exception Handler type checks work')
c = e['resume']
eh = 0
c()
init_int:
eh = new ['ExceptionHandler'], .CONTROL_BREAK
set_label eh, init_int_eh
push_eh eh
$P0 = new ['Exception']
$P0['type'] = .CONTROL_BREAK
throw $P0
$I0 = 0
goto init_int_done
init_int_eh:
pop_eh
$I0 = 1
init_int_done:
ok($I0, "init_int handler correctly caught exception")
subclass_handler:
.local pmc myhandler, myhandlercan
myhandler = subclass_exception_handler()
myhandlercan = subclass_exception_handler_can()
$I0 = subclass_handler_pop(myhandler)
ok($I0, 'Exception Handler subclass popped')
$I0 = subclass_handler_catches_can(myhandlercan)
ok($I0, 'Exception Handler subclass with can_handle method catch exception')
# This test is not expected to die now.
# Please report to TT #154 if it must be skipped again.
#skip(1,'Exception Handler subclass causes segfault: TT #154')
$I0 = 0
push_eh outcatch
$I0 = subclass_handler_catches(myhandler)
outcatch:
ok($I0, 'Exception Handler subclass catch exception')
.end
.sub test_bool
$P0 = new 'ExceptionHandler'
nok($P0,'ExceptionHandler without address is false')
set_label $P0, _handler
ok($P0,'ExceptionHandler with address is true')
_handler:
.end
.sub test_int
$P0 = new 'ExceptionHandler'
set_label $P0, _handler
push_eh $P0
$I0 = $P0
ok(1,'get_integer on ExceptionHandler ')
.return()
_handler:
say "howdy int!"
.end
.sub subclass_exception_handler
.local pmc myhandler
myhandler = subclass 'ExceptionHandler', [ 'MyHandler' ]
.return(myhandler)
.end
.sub subclass_exception_handler_can
.local pmc myhandler
myhandler = subclass 'ExceptionHandler', [ 'MyHandlerCan' ]
.return(myhandler)
.end
.sub subclass_handler_pop
.param pmc myhandler
.local pmc eh
eh = new ['ExceptionHandler']
set_label eh, subclassed_popped
push_eh eh
.local pmc myeh
new myeh, myhandler
set_label myeh, subclassed_handler
push_eh myeh
pop_eh
$P0 = new ['Exception']
throw $P0
subclassed_popped:
.return(1)
subclassed_handler:
.return(0)
.end
.sub subclass_handler_catches_can
.param pmc myhandler
.local pmc eh
eh = new ['ExceptionHandler']
set_label eh, subclassed_failed
push_eh eh
.local pmc myeh
new myeh, myhandler
set_label myeh, subclassed_handler
push_eh myeh
$P0 = new ['Exception']
throw $P0
subclassed_failed:
.get_results($P0)
.return(0)
subclassed_handler:
.get_results($P0)
.return(1)
.end
.sub subclass_handler_catches
.param pmc myhandler
.local pmc eh
eh = new ['ExceptionHandler']
set_label eh, subclassed_failed
push_eh eh
.local pmc myeh
new myeh, myhandler
set_label myeh, subclassed_handler
push_eh myeh
$P0 = new ['Exception']
throw $P0
subclassed_failed:
.get_results($P0)
.return(0)
subclassed_handler:
.get_results($P0)
.return(1)
.end
.namespace [ 'MyHandler' ]
.namespace [ 'MyHandlerCan' ]
.sub can_handle :method
.param pmc ex
ok(1, 'MyHandlerCan.can_handle invoked')
.return(1)
.end
.namespace [ ]
.sub 'test_handle_types_except'
.local pmc badeh, eh, ex
.local int i
.const int TYPEUSED = .EXCEPTION_UNEXPECTED_NULL
.const int TYPEOTHER = .EXCEPTION_SYNTAX_ERROR
i = 0
eh = new ['ExceptionHandler']
badeh = new ['ExceptionHandler']
eh.'handle_types_except'(TYPEUSED)
set_label eh, catch
set_label badeh, badcatch
push_eh badeh
push_eh eh
ex = new ['Exception']
ex['type'] = TYPEOTHER
throw ex
goto report1
badcatch:
finalize eh
goto report1
catch:
finalize eh
i = 1
report1:
ok(i, 'type not in except is list is caught')
i = 0
set_label badeh, catchall
set_label eh, dontcatch
ex = new ['Exception']
ex['type'] = TYPEUSED
throw ex
goto report2
catchall:
finalize eh
i = 1
goto report2
dontcatch:
finalize eh
report2:
pop_eh
pop_eh
ok(i, 'type in except is list is not caught')
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: