whitespace
[p5sagit/Try-Tiny.git] / t / context.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Try::Tiny;
6
7 plan 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 ;
13
14 my $ctx_index = {
15   VOID => undef,
16   LIST => 1,
17   SCALAR => '',
18 };
19 my ($ctx, $die);
20
21 for (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
44 sub 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 {
55     SKIP: {
56       skip "DESTROY() not called in void context on perl $]", 1
57         if "$]" < '5.008';
58       is (wantarray, undef, "Proper VOID context in finally{} 1");
59     }
60     return 'finally';
61   }
62   finally {
63     SKIP: {
64       skip "DESTROY() not called in void context on perl $]", 1
65         if "$]" < '5.008';
66       is (wantarray, undef, "Proper VOID context in finally{} 2");
67     }
68     return 'finally';
69   };
70 }