/
try.t
175 lines (149 loc) · 4.27 KB
/
try.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
use v6;
use Test;
# L<S04/"Statement parsing"/"or try {...}">
plan 32;
{
# simple try
my $lived = Mu;
try { die "foo" };
ok($! ~~ /foo/, "error var was set");
};
# try should return Nil if an exception was caught
#?niecza skip "=== and Nil NYI"
{
ok (try { die 'foo' }) === Nil, 'try returns Nil when exception was caught';
ok (try { die 'foo'; CATCH { default { } } }) === Nil, '... even when there was a CATCH block';
}
# try should work when returning an array or hash
{
my @array = try { 42 };
is +@array, 1, '@array = try {...} worked (1)';
is ~@array, "42", '@array = try {...} worked (2)';
}
{
my @array = try { (42,) };
is +@array, 1, '@array = try {...} worked (3)';
is ~@array, "42", '@array = try {...} worked (4)';
}
{
my %hash = try { 'a', 1 };
is +%hash, 1, '%hash = try {...} worked (1)';
is ~%hash.keys, "a", '%hash = try {...} worked (2)';
}
{
my %hash = try { hash("a", 1) };
is +%hash, 1, '%hash = try {...} worked (5)';
is ~%hash.keys, "a", '%hash = try {...} worked (6)';
}
{
my %hash;
try { %hash = try { a => 3 } };
is +%hash, 1, '%hash = try {...} worked (7)';
is ~%hash.keys, "a", '%hash = try {...} worked (8)';
is ~%hash<a>, 3, '%hash = try {...} worked (9)';
}
# return inside try{}-blocks
# PIL2JS *seems* to work, but it does not, actually:
# The "return 42" works without problems, and the caller actually sees the
# return value 42. But when the end of the test is reached, &try will
# **resume after the return**, effectively running the tests twice.
# (Therefore I moved the tests to the end, so not all tests are rerun).
{
my $was_in_foo = 0;
sub foo {
$was_in_foo++;
try { return 42 };
$was_in_foo++;
return 23;
}
is foo(), 42, 'return() inside try{}-blocks works (1)';
is $was_in_foo, 1, 'return() inside try{}-blocks works (2)';
}
{
sub test1 {
try { return 42 };
return 23;
}
sub test2 {
test1();
die 42;
}
dies_ok { test2() },
'return() inside a try{}-block should cause following exceptions to really die';
}
{
sub argcount { return +@_ }
is argcount( try { 17 }, 23, 99 ), 3, 'try gets a block, nothing more';
}
{
my $catches = 0;
try {
try {
die 'catch!';
CATCH {
die 'caught' if ! $catches++;
}
}
}
is $catches, 1, 'CATCH does not catch exceptions thrown within it';
}
#?niecza '.resume does not actually resume'
{
my $resumed = 0;
try {
die "ohh";
$resumed = 1;
CATCH { default { .resume } }
}
is $resumed, 1, 'CATCH allows to resume to right after the exception';
}
{
my $str = '';
try {
().abc;
CATCH {
default {
$str ~= 'A';
if 'foo' ~~ /foo/ {
$str ~= 'B';
$str ~= $/;
}
}
}
}
is $str, 'ABfoo', 'block including if structure and printing $/ ok';
}
#?niecza skip 'new exception stuff'
{
class MyPayload {
method Str() { 'something exceptional' }
};
my $p = MyPayload.new;
try die $p;
isa-ok $!, X::AdHoc, 'die($non-exception) creates an X::AdHoc';
ok $!.payload === $p, '$!.payload is the argument to &die';
is $!.Str, 'something exceptional', '$!.Str uses the payload';
class MyEx is Exception {
has $.s;
}
try MyEx.new(s => 'bar').throw;
isa-ok $!, MyEx, 'Can throw subtypes of Exception and get them back';
is $!.s, 'bar', '... and got the right object back';
}
# RT #111704
#?rakudo.moar todo 'RT #111704'
#?rakudo.jvm todo 'RT #111704'
{
my $x = 0;
try { $x = $_ } given '42';
is $x, '42', 'try block in statement-modifying contextualizer';
}
# RT #123053
lives_ok { try +'foo' }, 'Failure does not escape try (statement form)';
lives_ok { try { +'foo' } }, 'Failure does not escape try (block form)';
lives_ok { try { +'foo'; CATCH { default { } } } }, 'Failure does not escape try (block form with CATCH)';
# RT #117217
lives_ok { try ... }, '... failure does not escape try (statement form)';
lives_ok { try { ... } }, '... failure does not escape try (block form)';
done;
# vim: ft=perl6