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