Subject: | Overloaded Exceptions Without Fallback |
Hello.
Sometimes exception classes are annoyingly overloaded like so:
package MyException;
use overload q{""} => \&to_string;
i.e. without "fallback => 1" in there. This causes an error when they
are used with Test::Exception with the dies_ok function that attempts to
"ne" them, and that overload is not defined.
Attached is a simple patch (and corrisponding patch to the test suite)
that allows these semi-broken Exception objects to work with Test::Exception.
There may be other methods that suffer from sililar problems, but I haven't encountered them yet.
Thanks.
Mark.
diff -urN Test-Exception-0.13/Exception.pm Test-Exception-0.13.foo/Exception.pm
--- Test-Exception-0.13/Exception.pm 2003-01-06 22:17:45.000000000 +0000
+++ Test-Exception-0.13.foo/Exception.pm 2003-01-28 12:25:18.000000000 +0000
@@ -75,7 +75,7 @@
sub dies_ok (&;$) {
my ($sub, $test_name) = @_;
my $exception = _try_as_caller($sub);
- my $ok = $Tester->ok($exception ne '', $test_name);
+ my $ok = $Tester->ok("$exception" ne '', $test_name);
$@ = $exception;
return($ok);
}
diff -urN Test-Exception-0.13/t/Exception.t Test-Exception-0.13.foo/t/Exception.t
--- Test-Exception-0.13/t/Exception.t 2003-01-06 22:13:57.000000000 +0000
+++ Test-Exception-0.13.foo/t/Exception.t 2003-01-28 12:25:06.000000000 +0000
@@ -2,7 +2,7 @@
use strict;
-use Test::Builder::Tester tests => 15;
+use Test::Builder::Tester tests => 16;
BEGIN {
my $module = 'Test::Exception';
@@ -32,9 +32,16 @@
use overload q{""} => sub { "overloaded" }, fallback => 1;
};
+{
+ package Local::Error::OverloadNoFallback;
+ use base qw(Local::Error::Simple);
+ use overload q{""} => sub { "overloaded" };
+};
+
my $SIMPLE = Local::Error::Simple->new();
my $TEST = Local::Error::Test->new();
my $OVERLOAD = Local::Error::Overload->new();
+my $OVERLOAD_NO_FALLBACK = Local::Error::OverloadNoFallback->new();
sub error {
my $type = shift;
@@ -44,6 +51,8 @@
die $TEST;
} elsif ($type eq "overload") {
die $OVERLOAD;
+ } elsif ($type eq "overload no fallback") {
+ die $OVERLOAD_NO_FALLBACK;
} elsif ($type eq "die") {
die "a normal die\n";
} else {
@@ -132,3 +141,7 @@
test_out("ok 1");
throws_ok { error("none") } qr/^$/;
test_test("throws_ok: normal exit matched");
+
+test_out("ok 1");
+dies_ok { error("overload no fallback") };
+test_test("dies_ok: overload without fallback");