- Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathfail.t
204 lines (171 loc) · 6.12 KB
/
fail.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
useTest;
uselib$?FILE.IO.parent(2).add("packages/Test-Helpers");
useTest::Util;
plan40;
# L<S04/Exceptions/The fail function>
given (Failure.new()) {
.defined;
is.exception.message, "Failed", 'Default message for Failure is "Failed" without $!';
}
{
my$was_after_fail=0;
my$was_before_fail=0;
my$sub= sub { $was_before_fail++; my$exception=fail42; $was_after_fail++ }; #OK not used
my$unthrown_exception=$sub();
# Note: We don't further access $unthrown_exception, so it doesn't get thrown
is$was_before_fail, 1, "fail() doesn't cause our sub to not get executed";
is$was_after_fail, 0, "fail() causes our sub to return (1)";
}
{
my$was_after_fail=0;
my$was_after_sub=0;
my$sub= sub { fail42; $was_after_fail++ };
usefatal;
try { $sub(); $was_after_sub++ };
is$was_after_fail, 0, "fail() causes our sub to return (2)";
is$was_after_sub, 0, "fail() causes our try to die";
}
# https://github.com/Raku/old-issue-tracker/issues/941
{
ourIntsubrt64990 { fail() }
ok rt64990() ~~Failure, 'sub typed Int can fail()';
ourIntsubrepeat { returnfail() }
okrepeat() ~~Failure, 'sub typed Int can return Failure';
}
# https://github.com/Raku/old-issue-tracker/issues/1385
{
subrt70229 { returnfail() }
my$rt70229= rt70229();
ok$rt70229~~Failure, 'got a Failure';
dies-ok { ~$rt70229 }, 'attempt to stringify Failure dies';
}
# https://github.com/Raku/old-issue-tracker/issues/2180
{
subrt77946 { returnfail() }
my$rt77946= rt77946();
isa-ok?$rt77946, Bool, '?Failure returns a Bool';
isa-ok$rt77946.defined, Bool, 'Failure.defined returns a Bool';
}
# https://github.com/Raku/old-issue-tracker/issues/2582
{
my$f= (sub { fail('foo') }).();
is$f.exception, 'foo', 'can extract exception from Failure';
isa-ok$f.exception, Exception, '... and it is an Exception';
}
{
classAnExisException { };
my$f= (subf { fail AnEx.new }).(); #OK not used
isa-ok$f.exception, AnEx, 'can fail() typed exceptions';
}
{
subit-will-fail() { fail'whale' }
dies-ok { usefatal; my$x= it-will-fail(); 1 }, 'use fatal causes call to die';
lives-ok { usefatal; my$x= it-will-fail() //0; 1 }, 'use fatal respects //';
lives-ok { usefatal; my$x= it-will-fail() ||0; 1 }, 'use fatal respects ||';
lives-ok { usefatal; my$x= it-will-fail() &&0; 1 }, 'use fatal respects &&';
lives-ok { usefatal; if it-will-fail() { 1 } else { 0 } }, 'use fatal respects if';
lives-ok { usefatal; unless it-will-fail() { Nil }; 0 }, 'use fatal respects unless';
lives-ok { usefatal; it-will-fail() ??1!!0 }, 'use fatal respects ?? !!';
lives-ok { usefatal; my$x=?it-will-fail(); 1 }, 'use fatal respects ?';
lives-ok { usefatal; my$x=so it-will-fail(); 1 }, 'use fatal respects so';
lives-ok { usefatal; my$x=!it-will-fail(); 1 }, 'use fatal respects !';
lives-ok { usefatal; my$x=not it-will-fail(); 1 }, 'use fatal respects not';
lives-ok { usefatal; my$x=defined it-will-fail(); 1 }, 'use fatal respects defined';
}
# https://github.com/Raku/old-issue-tracker/issues/3183
{
subfatal-scope(&todo) {
usefatal;
todo;
}
subthing-that-fails() {
fail'oh noes';
}
subnon-fatal-scope {
thing-that-fails() or42
}
is fatal-scope(&non-fatal-scope), 42, "Fatal scopes are lexical rather than dynamic";
}
# https://github.com/Raku/old-issue-tracker/issues/2947
{
# We now allow more things in Failure.new than when the original RT
# was filed.
#
# Leaving this here in case anyone can figure out some esoteric way to
# get an X::TypeCheck when Failure.new takes almost any arglist
# throws-like 'Failure.new("foo").()', X::TypeCheck,
# "type check for creating Failure object with '.new' (1)";
}
subs1 {
subs2 {
fail("foo");
}
s2();
CATCH {
default {
ok$_.gist~~ /sub\ss2/,
"Failure reports backtrace from its creation point."
}
}
}
s1();
{
my$died;
my$here;
{
my$dummy=fail'oops';
$here=True;
CATCH { default { $died=$_ } }
}
ok$died~~Exception, 'fail outside of routine just behaves like die (1)';
is~$died, 'oops', 'fail outside of routine just behaves like die (2)';
nok$here, 'fail outside of routine just behaves like die (3)';
}
# https://irclog.perlgeek.de/perl6/2016-12-08#i_13706422
throws-like { nofatal; sink Failure.new; Nil }, Exception,
'sink statement prefix explodes Failures';
{
myclassX::Meow::MeowisException {}
subfoo { failX::Meow::Meow.new }
subbar { foo() orelsefail$_ }
subbaz { foo() orelse.&fail }
fails-like { bar }, X::Meow::Meow,
'fail(Failure:D) re-arms handled Failures';
fails-like { baz }, X::Meow::Meow,
'Failure:D.&fail re-arms handled Failures';
}
# https://github.com/rakudo/rakudo/commit/0a100825dd
subtest 'Failure.self'=> {
plan2;
myclassX::MeowisException { methodmessage { 'meow' } }.new;
subfailer { failX::Meow.new }
throws-like { $= failer.self }, X::Meow, 'unhandled exceptions explode';
somy$f= failer;
is-deeply$f, $f.self, 'handled exceptions are passed through as is';
}
# https://github.com/Raku/old-issue-tracker/issues/3799
is_run 「Failure.new(Exception.new); Nil」, {:out(""), :err(*), :1status},
'Failure.new(Exception.new) does not segfault';
# https://github.com/Raku/old-issue-tracker/issues/6313
{
withoutFailure.new {
is-deeply.raku.EVAL.handled, True,
'Failure:D.raku.EVAL roundtrips `handled` flag';
}
}
# https://github.com/Raku/old-issue-tracker/issues/4661
throws-like {
subs { fail'important failure message' }; myInt$x=s();
}, Exception, message => /important/,
'assigning Failure to typed variable that cannot hold it explodes it';
# https://github.com/rakudo/rakudo/issues/2764
{
CATCH {
isa-ok$_, X::AdHoc, 'did we get a die';
is.payload, "foo", 'did we get the payload expected';
.resume;
}
my$b=Failure.new("foo");
$b.new;
}
# vim: expandtab shiftwidth=4