Skip to content

Commit

Permalink
Item1871: Make print work more like a regular print, when unit test l…
Browse files Browse the repository at this point in the history
…ogging is enabled

git-svn-id: http://svn.foswiki.org/trunk@5142 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelTempest authored and MichaelTempest committed Sep 25, 2009
1 parent 40dfbbd commit ac296e6
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions UnitTestContrib/lib/Unit/Eavesdrop.pm
Expand Up @@ -9,9 +9,19 @@ sub new {
return eval "tie(*$baseName, \$class, \$baseName)";
}

sub finish {
my $this = shift;
my $baseName = delete $this->{baseName};
if ( $baseName ) {
local $^W = 0;
eval "untie( *$baseName )";
die $@ if $@;
}
}

sub DESTROY {
my $this = shift;
{ local $^W = 0; untie( *$this->{baseName} ) }
$this->finish();
}

sub TIEHANDLE {
Expand Down Expand Up @@ -49,9 +59,25 @@ sub PRINT {
$fh = *{ $this->{principal} };
$success = print $fh @_;
}
else {
if ($^W) {
# A real print would generate a warning from the perspective of the caller
my ($package, $filename, $line) = caller;
my $message = "print on closed filehandle ";
$message .= $this->{baseName} if defined $this->{baseName};
$message .= " at $filename line $line\n";
warn $message;
}
}

# This module turns one print into many.
# It is not practical to undo prints already completed if one of them fails
# so try to complete the rest of them anyway.
# The return value is set conservatively - all prints must succeed
# for the result value to indicate success
foreach my $tee ( @{ $this->{tees} } ) {
$fh = *{$tee};
print $fh @_;
print $fh @_ or undef $success;
}

if ($success) {
Expand Down

0 comments on commit ac296e6

Please sign in to comment.