There's more than one way to do things | |
PerlMonks |
Writing code using Test::More -- how to fail within subs?by John M. Dlugosz (Monsignor) |
on Feb 08, 2003 at 06:11 UTC ( [id://233656]=perlquestion: print w/replies, xml ) | Need Help?? |
John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:
I wrote a test, then want a bunch more almost like it but with different data. So I put all the guts into reusable sub's and drive the actual test case with a couple calls and only code that's really different for this case. The code in the sub's will do things like open ... or die. If I change the die to a fail then it doesn't stop trying to go on, when it needs to jump to the next whole test case. If I put a eval around the top-level test case code, I have more duplication in the "catch" part. Hmm, I suppose I can always fail (printing the right message) and then die in the common code, but that's less elegant than the "...or die" idiom. It turns into an if block, or more cryptically a do block in the "or" part. If I make a die-ing fail wrapper, I'd need to do similar to every Test::More handy function. There are lots of ways to write it on one line anyway, and maybe a few ways to reuse the code. How would you do it? I'm interested in generalizing the idiom to work with any of the test primitives, and keeping the code clear and short.
TIMTOWTDI,
How should the $ot-> methods contain the common code to "fail" or otherwise validate things as it goes? P.S. Perhaps I must have a "catch" (if ($@) { ...) for this top-level code that contains the fail, because the code I'm calling (e.g. emit()) might die, and that's not specially-written test code.
Back to
Seekers of Perl Wisdom
|
|