whitespace
[p5sagit/Try-Tiny.git] / t / context.t
CommitLineData
f115dfa6 1use strict;
2use warnings;
3
4use Test::More;
01b96275 5use Try::Tiny;
f115dfa6 6
01b96275 7plan tests =>
8 (4+1) * 2 # list/scalar with exception (try + catch + 2 x finally) + is_deeply
9+ 4 # void with exception
10+ (3+1) * 2 # list/scalar no exception (try + 2 x finally) + is_deeply
11+ 3 # void no exception
12;
f115dfa6 13
14my $ctx_index = {
15 VOID => undef,
16 LIST => 1,
17 SCALAR => '',
18};
19my ($ctx, $die);
20
21for (sort keys %$ctx_index) {
22 $ctx = $_;
23 for (0,1) {
24 $die = $_;
25 if ($ctx_index->{$ctx}) {
26 is_deeply(
27 [ run() ],
28 [ $die ? 'catch' : 'try' ],
29 );
30 }
31 elsif (defined $ctx_index->{$ctx}) {
32 is_deeply(
33 [ scalar run() ],
34 [ $die ? 'catch' : 'try' ],
35 );
36 }
37 else {
38 run();
39 1;
40 }
41 }
42}
43
44sub run {
45 try {
46 is (wantarray, $ctx_index->{$ctx}, "Proper context $ctx in try{}");
47 die if $die;
48 return 'try';
49 }
50 catch {
51 is (wantarray, $ctx_index->{$ctx}, "Proper context $ctx in catch{}");
52 return 'catch';
53 }
54 finally {
2531688c 55 SKIP: {
56 skip "DESTROY() not called in void context on perl $]", 1
8c34b0ef 57 if "$]" < '5.008';
2531688c 58 is (wantarray, undef, "Proper VOID context in finally{} 1");
59 }
f115dfa6 60 return 'finally';
61 }
62 finally {
2531688c 63 SKIP: {
64 skip "DESTROY() not called in void context on perl $]", 1
8c34b0ef 65 if "$]" < '5.008';
2531688c 66 is (wantarray, undef, "Proper VOID context in finally{} 2");
67 }
f115dfa6 68 return 'finally';
69 };
70}