1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#!/usr/bin/perl
use lib 'lib', '../lib';
BEGIN {
package My::Common;
*throw_something = $0 =~ /_ok/ ? sub () { 0 } : sub () { 1 };
};
{
package My::EvalDieObject;
sub throw {
my %args = @_;
die bless {%args}, shift;
};
};
our %tests = (
'01_EvalDieScalar' => sub {
eval {
die 'Message' if My::Common::throw_something;
};
if ($@ =~ /^Message/) {
1;
};
},
'02_EvalDieObject' => sub {
eval {
My::EvalDieObject->throw if My::Common::throw_something;
};
if ($@) {
if (ref $@ && $@->isa('My::EvalDieObject')) {
my $e = $@;
1;
}
}
},
);
eval {
eval q{use Exception::Base 'Exception::My'};
$tests{ '03_ExceptionEval' } = sub {
eval {
Exception::My->throw(message=>'Message') if My::Common::throw_something;
};
if ($@) {
my $e = Exception::Base->catch;
if ($e->isa('Exception::My') and $e->matches('Message')) {
1;
};
};
};
$tests{ '04_Exception1Eval' } = sub {
eval {
Exception::My->throw(message=>'Message', verbosity=>1) if My::Common::throw_something;
};
if ($@) {
my $e = Exception::Base->catch;
if ($e->isa('Exception::My') and $e->matches('Message')) {
1;
};
};
};
};
eval q{
die 1;
package My::Error;
BEGIN {
eval {
require Error;
Error->import(':try');
};
};
Error->VERSION or die;
$main::tests{'05_Error'} = sub {
try {
Error::Simple->throw('Message') if My::Common::throw_something;
}
Error->catch(with {
my $e = $_[0];
if ($e->text eq 'Message') {
1;
}
});
};
};
eval {
package My::ClassThrowable;
require Class::Throwable;
Class::Throwable->import;
$main::tests{'06_ClassThrowable'} = sub {
eval {
Class::Throwable->throw('Message') if My::Common::throw_something;
};
if ($@) {
my $e = $@;
if (ref $e and $e->isa('Class::Throwable') and $e->getMessage eq 'Message') {
1;
};
};
};
};
eval {
package My::ExceptionClass;
require Exception::Class;
Exception::Class->import('MyException');
$main::tests{'07_ExceptionClass'} = sub {
eval {
MyException->throw(error=>'Message') if My::Common::throw_something;
};
my $e;
if ($e = Exception::Class->caught('MyException') and $e->error eq 'Message') {
1;
};
};
};
eval q{
package My::ExceptionClassTC;
BEGIN {
eval {
require Exception::Class;
Exception::Class->import('MyException');
require Exception::Class::TryCatch;
Exception::Class::TryCatch->import;
};
};
Exception::Class::TryCatch->VERSION or die;
$main::tests{'08_ExceptionClassTC'} = sub {
try eval {
MyException->throw(error=>'Message') if My::Common::throw_something;
};
if (catch my $e) {
if ($e->isa('MyException') and $e->error eq 'Message') {
1;
}
};
};
};
{
package My::TryCatch;
use TryCatch;
$main::tests{'09_TryCatch'} = sub {
try {
die 'Message this message' if My::Common::throw_something;
}
catch ($e) {
if ($@ =~ /^Message/) {
1;
};
};
};
};
use Benchmark ':all';
print "Benchmark for ", (My::Common::throw_something ? "FAIL" : "OK"), "\n";
my $result = timethese($ARGV[0] || -1, { %tests });
cmpthese($result);
|