Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 195 lines (166 sloc) 5.087 kB
58135b3 [t/spec] changed 'use v6-alpha;' to 'use v6;'
moritz authored
1 use v6;
b957ad1 [t/spec] Refactored control_flow/try.t tests under S04
cosimo authored
2
3 use Test;
4
5 # L<S04/"Statement parsing"/"or try {...}">
6
1ad644d @skids tests for some enhancements to die/fail
skids authored
7 plan 40;
b957ad1 [t/spec] Refactored control_flow/try.t tests under S04
cosimo authored
8
9 {
10 # simple try
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
11 my $lived = Mu;
b957ad1 [t/spec] Refactored control_flow/try.t tests under S04
cosimo authored
12 try { die "foo" };
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
13 ok($! ~~ /foo/, "error var was set");
b957ad1 [t/spec] Refactored control_flow/try.t tests under S04
cosimo authored
14 };
15
cbe1ad7 @moritz explicitly test return value of try { }
moritz authored
16 # try should return Nil if an exception was caught
a03dd1a @colomon Fudge for Niecza.
colomon authored
17 #?niecza skip "=== and Nil NYI"
cbe1ad7 @moritz explicitly test return value of try { }
moritz authored
18 {
19 ok (try { die 'foo' }) === Nil, 'try returns Nil when exception was caught';
20 ok (try { die 'foo'; CATCH { default { } } }) === Nil, '... even when there was a CATCH block';
21 }
22
b957ad1 [t/spec] Refactored control_flow/try.t tests under S04
cosimo authored
23 # try should work when returning an array or hash
24 {
25 my @array = try { 42 };
26 is +@array, 1, '@array = try {...} worked (1)';
27 is ~@array, "42", '@array = try {...} worked (2)';
28 }
29
30 {
31 my @array = try { (42,) };
32 is +@array, 1, '@array = try {...} worked (3)';
33 is ~@array, "42", '@array = try {...} worked (4)';
34 }
35
36 {
37 my %hash = try { 'a', 1 };
38 is +%hash, 1, '%hash = try {...} worked (1)';
39 is ~%hash.keys, "a", '%hash = try {...} worked (2)';
40 }
41
42 {
43 my %hash = try { hash("a", 1) };
44 is +%hash, 1, '%hash = try {...} worked (5)';
45 is ~%hash.keys, "a", '%hash = try {...} worked (6)';
46 }
47
48 {
49 my %hash;
50 try { %hash = try { a => 3 } };
d4b5038 [t/spec] Fudgified try.t spec test
cosimo authored
51 is +%hash, 1, '%hash = try {...} worked (7)';
52 is ~%hash.keys, "a", '%hash = try {...} worked (8)';
53 is ~%hash<a>, 3, '%hash = try {...} worked (9)';
b957ad1 [t/spec] Refactored control_flow/try.t tests under S04
cosimo authored
54 }
55
56 # return inside try{}-blocks
57 # PIL2JS *seems* to work, but it does not, actually:
58 # The "return 42" works without problems, and the caller actually sees the
59 # return value 42. But when the end of the test is reached, &try will
60 # **resume after the return**, effectively running the tests twice.
61 # (Therefore I moved the tests to the end, so not all tests are rerun).
02c58e2 [spec]
moritz authored
62
b957ad1 [t/spec] Refactored control_flow/try.t tests under S04
cosimo authored
63 {
02c58e2 [spec]
moritz authored
64 my $was_in_foo = 0;
b957ad1 [t/spec] Refactored control_flow/try.t tests under S04
cosimo authored
65 sub foo {
66 $was_in_foo++;
67 try { return 42 };
68 $was_in_foo++;
69 return 23;
70 }
71 is foo(), 42, 'return() inside try{}-blocks works (1)';
72 is $was_in_foo, 1, 'return() inside try{}-blocks works (2)';
73 }
74
75 {
02c58e2 [spec]
moritz authored
76 sub test1 {
b957ad1 [t/spec] Refactored control_flow/try.t tests under S04
cosimo authored
77 try { return 42 };
78 return 23;
79 }
80
02c58e2 [spec]
moritz authored
81 sub test2 {
b957ad1 [t/spec] Refactored control_flow/try.t tests under S04
cosimo authored
82 test1();
83 die 42;
84 }
85
709e414 @lizmat s/dies_ok/dies-ok/g
lizmat authored
86 dies-ok { test2() },
013f8db [t/spec] one of tests description was in double quotes which caused u…
bbkr authored
87 'return() inside a try{}-block should cause following exceptions to really die';
b957ad1 [t/spec] Refactored control_flow/try.t tests under S04
cosimo authored
88 }
89
37c4e73 [t/spec] Test for RT #65358
kyle authored
90 {
91 sub argcount { return +@_ }
92 is argcount( try { 17 }, 23, 99 ), 3, 'try gets a block, nothing more';
93 }
94
b13cd8a [t/spec] Add test by KyleHa++ that didn't seem to have made it from t…
jnthn authored
95 {
96 my $catches = 0;
97 try {
98 try {
99 die 'catch!';
100 CATCH {
101 die 'caught' if ! $catches++;
102 }
103 }
104 }
105 is $catches, 1, 'CATCH does not catch exceptions thrown within it';
106 }
107
09ecc5e @FROGGS add test for Exception.resume in CATCH
FROGGS authored
108 #?niecza '.resume does not actually resume'
109 {
110 my $resumed = 0;
111 try {
112 die "ohh";
113 $resumed = 1;
114 CATCH { default { .resume } }
115 }
116 is $resumed, 1, 'CATCH allows to resume to right after the exception';
117 }
118
f8f23b9 added test for RT #68728
Jimmy Zhuo authored
119 {
120 my $str = '';
121 try {
d3a9312 @moritz [try.t] change compile time to run time error
moritz authored
122 ().abc;
f8f23b9 added test for RT #68728
Jimmy Zhuo authored
123 CATCH {
2881bb5 @jnthn Ensure a CATCH actually handles the exception in question.
jnthn authored
124 default {
125 $str ~= 'A';
126 if 'foo' ~~ /foo/ {
127 $str ~= 'B';
128 $str ~= $/;
129 }
f8f23b9 added test for RT #68728
Jimmy Zhuo authored
130 }
131 }
132 }
96654ce s/tab/space/ for tests
jimmy authored
133 is $str, 'ABfoo', 'block including if structure and printing $/ ok';
f8f23b9 added test for RT #68728
Jimmy Zhuo authored
134 }
da13ad1 @moritz tests for &die, try and X::AdHoc
moritz authored
135
136 #?niecza skip 'new exception stuff'
137 {
138 class MyPayload {
139 method Str() { 'something exceptional' }
140 };
141 my $p = MyPayload.new;
142 try die $p;
bb0a8fa @lizmat Use isa-ok instead of isa_ok
lizmat authored
143 isa-ok $!, X::AdHoc, 'die($non-exception) creates an X::AdHoc';
da13ad1 @moritz tests for &die, try and X::AdHoc
moritz authored
144 ok $!.payload === $p, '$!.payload is the argument to &die';
145 is $!.Str, 'something exceptional', '$!.Str uses the payload';
5c0802d @moritz more exception tests
moritz authored
146
1ad644d @skids tests for some enhancements to die/fail
skids authored
147 try die($p,42);
148 isa-ok $!, X::AdHoc, 'die($,$) creates an X::AdHoc';
149 ok $!.payload[0] === $p, '$!.payload[0] is the first argument to &die';
150 ok $!.payload[1] == 42, '$!.payload[1] is the second argument to &die';
151 is $!.Str, 'something exceptional42', '$!.Str culls whitespace';
152
153 try die(X::NYI.new(:feature<fee>),"fee");
154 isa-ok $!, X::AdHoc, 'die(Exception,$) creates an X::AdHoc';
155 ok $!.payload[0] ~~ X::NYI, '$!.payload[0] is the Exception';
156
157 sub a { # new $!
158 try die();
159 is $!.Str, 'Died', 'When $! not set, die() has default message "Died"';
160 try die("fee");
161 die();
162 CATCH {
163 default {
164 is $_.Str, 'fee', 'When $! is set, die() is die($!).';
165 }
166 };
167 }
168 a();
169
5c0802d @moritz more exception tests
moritz authored
170 class MyEx is Exception {
171 has $.s;
172 }
173 try MyEx.new(s => 'bar').throw;
bb0a8fa @lizmat Use isa-ok instead of isa_ok
lizmat authored
174 isa-ok $!, MyEx, 'Can throw subtypes of Exception and get them back';
5c0802d @moritz more exception tests
moritz authored
175 is $!.s, 'bar', '... and got the right object back';
da13ad1 @moritz tests for &die, try and X::AdHoc
moritz authored
176 }
c070484 @moritz test for RT #111704, try-blocks plus statement modifiers gobble up $_
moritz authored
177
178 # RT #111704
179 {
180 my $x = 0;
181 try { $x = $_ } given '42';
182 is $x, '42', 'try block in statement-modifying contextualizer';
183 }
9699743 @jnthn Add tests for RT #RT #123053; currently skipped.
jnthn authored
184
185 # RT #123053
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
186 lives-ok { try +'foo' }, 'Failure does not escape try (statement form)';
187 lives-ok { try { +'foo' } }, 'Failure does not escape try (block form)';
188 lives-ok { try { +'foo'; CATCH { default { } } } }, 'Failure does not escape try (block form with CATCH)';
9699743 @jnthn Add tests for RT #RT #123053; currently skipped.
jnthn authored
189
800eff0 @jnthn Tests for RT #117217.
jnthn authored
190 # RT #117217
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
191 lives-ok { try ... }, '... failure does not escape try (statement form)';
192 lives-ok { try { ... } }, '... failure does not escape try (block form)';
800eff0 @jnthn Tests for RT #117217.
jnthn authored
193
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
194 # vim: ft=perl6
Something went wrong with that request. Please try again.