Permalink
Browse files

Turns out that require resets $@ and $!, which is bad for our purposes.

So wrap all require's in a _try() block to mask that.

Add a die_on_fail option to _try() so our requires will fail when they
fail.
  • Loading branch information...
1 parent 99e80a9 commit 9c56ac75f0097f1f219937594eb3bc15c93f6224 @schwern schwern committed Oct 23, 2008
Showing with 33 additions and 19 deletions.
  1. +15 −8 lib/Test/Builder.pm
  2. +18 −11 t/Builder/try.t
View
@@ -457,7 +457,7 @@ sub _unoverload {
my $self = shift;
my $type = shift;
- $self->_try( sub { require overload } ) || return;
+ $self->_try(sub { require overload; }, die_on_fail => 1);
foreach my $thing (@_) {
if( $self->_is_object($$thing) ) {
@@ -1042,14 +1042,21 @@ It is suggested you use this in place of eval BLOCK.
=cut
sub _try {
- my( $self, $code ) = @_;
+ my( $self, $code, %opts ) = @_;
- local $!; # eval can mess up $!
- local $@; # don't set $@ in the test
- local $SIG{__DIE__}; # don't trip an outside DIE handler.
- my $return = eval { $code->() };
+ my $error;
+ my $return;
+ {
+ local $!; # eval can mess up $!
+ local $@; # don't set $@ in the test
+ local $SIG{__DIE__}; # don't trip an outside DIE handler.
+ $return = eval { $code->() };
+ $error = $@;
+ }
+
+ die $error if $error and $opts{die_on_fail};
- return wantarray ? ( $return, $@ ) : $return;
+ return wantarray ? ( $return, $error ) : $return;
}
=end private
@@ -1296,7 +1303,7 @@ sub explain {
return map {
ref $_
? do {
- require Data::Dumper;
+ $self->_try(sub { require Data::Dumper }, die_on_fail => 1);
my $dumper = Data::Dumper->new( [$_] );
$dumper->Indent(1)->Terse(1);
View
@@ -18,19 +18,26 @@ use Test::More 'no_plan';
require Test::Builder;
my $tb = Test::Builder->new;
-local $SIG{__DIE__} = sub { fail("DIE handler called: @_") };
-# These should not change;
-local $@ = 42;
-local $! = 23;
+# Test that _try() has no effect on $@ and $! and is not effected by
+# __DIE__
+{
+ local $SIG{__DIE__} = sub { fail("DIE handler called: @_") };
+ local $@ = 42;
+ local $! = 23;
-is $tb->_try(sub { 2 }), 2;
-is $tb->_try(sub { return '' }), '';
+ is $tb->_try(sub { 2 }), 2;
+ is $tb->_try(sub { return '' }), '';
-is $tb->_try(sub { die; }), undef;
+ is $tb->_try(sub { die; }), undef;
-is_deeply [$tb->_try(sub { die "Foo\n" }, undef)],
- [undef, "Foo\n"];
+ is_deeply [$tb->_try(sub { die "Foo\n" })], [undef, "Foo\n"];
-is $@, 42;
-cmp_ok $!, '==', 23;
+ is $@, 42;
+ cmp_ok $!, '==', 23;
+}
+
+ok !eval {
+ $tb->_try(sub { die "Died\n" }, die_on_fail => 1);
+};
+is $@, "Died\n";

0 comments on commit 9c56ac7

Please sign in to comment.