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