e25a09cd0c4e32906fda3444fc4b2af9bbb5c4a0
[p5sagit/Try-Tiny.git] / t / context.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 BEGIN {
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
18 my $ctx_index = {
19   VOID => undef,
20   LIST => 1,
21   SCALAR => '',
22 };
23 my ($ctx, $die);
24
25 for (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
48 sub 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 {
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     }
64     return 'finally';
65   }
66   finally {
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     }
72     return 'finally';
73   };
74 }