Merge pull request #15 from Flimm/master
[p5sagit/Try-Tiny.git] / t / context.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 use Try::Tiny;
7
8 plan 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 ;
14
15 my $ctx_index = {
16   VOID => undef,
17   LIST => 1,
18   SCALAR => '',
19 };
20 my ($ctx, $die);
21
22 for (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
45 sub 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 {
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     }
61     return 'finally';
62   }
63   finally {
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     }
69     return 'finally';
70   };
71 }