The use of done_testing implies a dependency of Test::More 0.88 or
[p5sagit/Try-Tiny.git] / t / finally.t
index abfb9cb..2624cc9 100644 (file)
@@ -1,41 +1,41 @@
 #!/usr/bin/perl
 
 use strict;
-#use warnings;
+use warnings;
 
-use Test::More tests => 24;
+use Test::More tests => 27;
 
-BEGIN { use_ok 'Try::Tiny' };
+use Try::Tiny;
 
 try {
-       my $a = 1+1;
+  my $a = 1+1;
 } catch {
-       fail('Cannot go into catch block because we did not throw an exception')
+  fail('Cannot go into catch block because we did not throw an exception')
 } finally {
-       pass('Moved into finally from try');
+  pass('Moved into finally from try');
 };
 
 try {
-       die('Die');
+  die('Die');
 } catch {
-       ok($_ =~ /Die/, 'Error text as expected');
-       pass('Into catch block as we died in try');
+  ok($_ =~ /Die/, 'Error text as expected');
+  pass('Into catch block as we died in try');
 } finally {
-       pass('Moved into finally from catch');
+  pass('Moved into finally from catch');
 };
 
 try {
-       die('Die');
+  die('Die');
 } finally {
-       pass('Moved into finally from catch');
+  pass('Moved into finally from catch');
 } catch {
-       ok($_ =~ /Die/, 'Error text as expected');
+  ok($_ =~ /Die/, 'Error text as expected');
 };
 
 try {
-       die('Die');
+  die('Die');
 } finally {
-       pass('Moved into finally block when try throws an exception and we have no catch block');
+  pass('Moved into finally block when try throws an exception and we have no catch block');
 };
 
 try {
@@ -63,42 +63,69 @@ try {
 };
 
 try {
-    try {
-        die "foo";
-    }
-    catch {
-        die "bar";
-    }
-    finally {
-        pass("finally called");
-    };
+  try {
+    die "foo";
+  }
+  catch {
+    die "bar";
+  }
+  finally {
+    pass("finally called");
+  };
 };
 
 $_ = "foo";
 try {
-    is($_, "foo", "not localized in try");
+  is($_, "foo", "not localized in try");
 }
 catch {
 }
 finally {
-    is(scalar(@_), 0, "nothing in \@_ (finally)");
-    is($_, "foo", "\$_ not localized (finally)");
+  is(scalar(@_), 0, "nothing in \@_ (finally)");
+  is($_, "foo", "\$_ not localized (finally)");
 };
 is($_, "foo", "same afterwards");
 
 $_ = "foo";
 try {
-    is($_, "foo", "not localized in try");
-    die "bar\n";
+  is($_, "foo", "not localized in try");
+  die "bar\n";
 }
 catch {
-    is($_[0], "bar\n", "error in \@_ (catch)");
-    is($_, "bar\n", "error in \$_ (catch)");
+  is($_[0], "bar\n", "error in \@_ (catch)");
+  is($_, "bar\n", "error in \$_ (catch)");
 }
 finally {
-    is(scalar(@_), 1, "error in \@_ (finally)");
-    is($_[0], "bar\n", "error in \@_ (finally)");
-    is($_, "foo", "\$_ not localized (finally)");
+  is(scalar(@_), 1, "error in \@_ (finally)");
+  is($_[0], "bar\n", "error in \@_ (finally)");
+  is($_, "foo", "\$_ not localized (finally)");
 };
 is($_, "foo", "same afterwards");
+
+{
+  my @warnings;
+  local $SIG{__WARN__} = sub {
+    $_[0] =~ /\QExecution of finally() block CODE(0x\E.+\Q) resulted in an exception/
+      ? push @warnings, @_
+      : warn @_
+  };
+
+  try {
+    die 'tring'
+  } finally {
+    die 'fin 1'
+  } finally {
+    pass('fin 2 called')
+  } finally {
+    die 'fin 3'
+  };
+
+  is( scalar @warnings, 2, 'warnings from both fatal finally blocks' );
+
+  my @originals = sort map { $_ =~ /Original exception text follows:\n\n(.+)/s } @warnings;
+
+  like $originals[0], qr/fin 1 at/, 'First warning contains original exception';
+  like $originals[1], qr/fin 3 at/, 'Second warning contains original exception';
+}
+
 1;