forked from ashb/trycatch
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better error checking, and make try not too reserved
- Loading branch information
Showing
2 changed files
with
104 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,115 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More tests => 6; | ||
use Test::More tests => 8; | ||
use Test::Exception; | ||
use TryCatch; | ||
|
||
BEGIN { use_ok "TryCatch" or BAIL_OUT("Cannot load TryCatch") }; | ||
#use TryCatch; | ||
test_for_error( | ||
qr/^block required after try at .*? line 4$/, | ||
"no block after try", | ||
<<'EOC' ); | ||
use TryCatch; | ||
eval <<'EOC'; | ||
use TryCatch; | ||
sub foo { } | ||
try \&foo | ||
EOC | ||
|
||
sub foo { } | ||
try \&foo | ||
|
||
EOC | ||
test_for_error( | ||
qr/^block required after catch at \(eval \d+\) line 6$/, | ||
"no block after catch", | ||
<<'EOC'); | ||
use TryCatch; | ||
like $@, | ||
qr!^block required after try at \(eval \d+\) line \d+$!, | ||
"no block after try"; | ||
#warn "q{$@}"; | ||
try { 1 } | ||
catch | ||
EOC | ||
|
||
undef $@; | ||
eval <<'EOC'; | ||
use TryCatch; | ||
|
||
try { 1; } | ||
catch | ||
test_for_error( | ||
qr/^Parameter expected near '\^' in '\^Err \$e' at \(eval \d+\) line 4$/, | ||
"invalid catch signature", | ||
<<'EOC'); | ||
use TryCatch; | ||
try { } | ||
catch (^Err $e) {} | ||
EOC | ||
|
||
like $@, | ||
qr!^block required after catch at \(eval \d+\) line \d+$!, | ||
"no block after catch"; | ||
#warn "q{$@}"; | ||
|
||
undef $@; | ||
eval <<'EOC'; | ||
use TryCatch; | ||
TODO: { | ||
local $TODO = "Make this error better"; | ||
|
||
try { } | ||
catch (^Err $e) {} | ||
test_for_error( | ||
qr!^'\)' required after catch signature at \(eval \d+\) line 4!, | ||
"invalid catch signature (missing parenthesis)", | ||
<<'EOC'); | ||
use TryCatch; | ||
try { } | ||
catch ( {} | ||
EOC | ||
|
||
like $@, | ||
qr!^Parameter expected near '\^' in '\^Err \$e' at\E \(eval \d+\) line \d+$!, | ||
"invalid catch signature"; | ||
#warn "q{$@}"; | ||
} | ||
|
||
|
||
undef $@; | ||
eval <<'EOC'; | ||
use TryCatch; | ||
test_for_error( | ||
qr/^Can't locate object method "bar" via package "catch" .*?at \(eval \d+\) line 3\b/, | ||
"bareword between try and catch", | ||
<<'EOC'); | ||
use TryCatch; | ||
try { } | ||
catch ( {} | ||
try { } bar | ||
catch {} | ||
EOC | ||
|
||
TODO: { | ||
local $TODO = "Make this error better"; | ||
like $@, | ||
qr!^'\)' required after catch signature at \(eval \d+\) line \d+!, | ||
"invalid catch signature (missing parenthesis)"; | ||
} | ||
test_for_error( | ||
qr/^Bareword "catch" not allowed while "strict subs" in use at \(eval \d+\) line 3\b/, | ||
"catch is not special", | ||
<<'EOC'); | ||
use TryCatch; | ||
catch; | ||
EOC | ||
|
||
compile_ok("try is not too reserved", <<'EOC'); | ||
use TryCatch; | ||
eval <<'EOC'; | ||
use TryCatch; | ||
try => 1; | ||
EOC | ||
|
||
try { } bar | ||
catch {} | ||
compile_ok( | ||
"catch is not special", | ||
<<'EOC'); | ||
use TryCatch; | ||
catch => 3; | ||
EOC | ||
|
||
like $@, | ||
qr!^Can't locate object method "bar" via package "catch" .*?at \(eval \d+\) line \d+\.?$!, | ||
"bareword between try and catch"; | ||
sub test_for_error { | ||
my ($re, $msg, $code) = @_; | ||
try { | ||
eval $code; | ||
die $@ if $@; | ||
fail($msg); | ||
} | ||
catch ($e) { | ||
like($e, $re, $msg); | ||
} | ||
} | ||
|
||
sub compile_ok { | ||
my ($msg, $code) = @_; | ||
try { | ||
eval $code; | ||
die $@ if $@; | ||
pass($msg); | ||
} | ||
catch ($e) { | ||
diag($e); | ||
fail($msg); | ||
} | ||
} |