fix tests failing on 5.6.x due to differing DESTROY semantics
[p5sagit/Try-Tiny.git] / t / finally.t
CommitLineData
7195fc08 1#!/usr/bin/perl
2
3use strict;
4#use warnings;
5
658a90e5 6use Test::More tests => 24;
7195fc08 7
8BEGIN { use_ok 'Try::Tiny' };
9
10try {
8d2ee831 11 my $a = 1+1;
7195fc08 12} catch {
8d2ee831 13 fail('Cannot go into catch block because we did not throw an exception')
7195fc08 14} finally {
8d2ee831 15 pass('Moved into finally from try');
7195fc08 16};
17
18try {
8d2ee831 19 die('Die');
7195fc08 20} catch {
8d2ee831 21 ok($_ =~ /Die/, 'Error text as expected');
22 pass('Into catch block as we died in try');
7195fc08 23} finally {
8d2ee831 24 pass('Moved into finally from catch');
7195fc08 25};
26
27try {
8d2ee831 28 die('Die');
7195fc08 29} finally {
8d2ee831 30 pass('Moved into finally from catch');
7195fc08 31} catch {
8d2ee831 32 ok($_ =~ /Die/, 'Error text as expected');
7195fc08 33};
34
35try {
8d2ee831 36 die('Die');
7195fc08 37} finally {
8d2ee831 38 pass('Moved into finally block when try throws an exception and we have no catch block');
7195fc08 39};
40
b611f396 41try {
42 die('Die');
43} finally {
44 pass('First finally clause run');
45} finally {
46 pass('Second finally clause run');
47};
48
49try {
50 # do not die
51} finally {
52 if (@_) {
53 fail("errors reported: @_");
54 } else {
55 pass("no error reported") ;
56 }
57};
58
68094f9e 59try {
60 die("Die\n");
61} finally {
62 is_deeply(\@_, [ "Die\n" ], "finally got passed the exception");
63};
7195fc08 64
4c82ac88 65try {
8d2ee831 66 try {
67 die "foo";
68 }
69 catch {
70 die "bar";
71 }
72 finally {
73 pass("finally called");
74 };
4c82ac88 75};
76
658a90e5 77$_ = "foo";
78try {
8d2ee831 79 is($_, "foo", "not localized in try");
658a90e5 80}
81catch {
82}
83finally {
8d2ee831 84 is(scalar(@_), 0, "nothing in \@_ (finally)");
85 is($_, "foo", "\$_ not localized (finally)");
658a90e5 86};
87is($_, "foo", "same afterwards");
88
89$_ = "foo";
90try {
8d2ee831 91 is($_, "foo", "not localized in try");
92 die "bar\n";
658a90e5 93}
94catch {
8d2ee831 95 is($_[0], "bar\n", "error in \@_ (catch)");
96 is($_, "bar\n", "error in \$_ (catch)");
658a90e5 97}
98finally {
8d2ee831 99 is(scalar(@_), 1, "error in \@_ (finally)");
100 is($_[0], "bar\n", "error in \@_ (finally)");
101 is($_, "foo", "\$_ not localized (finally)");
658a90e5 102};
103is($_, "foo", "same afterwards");
7195fc08 1041;