John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:
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,
—John
How should the $ot-> methods contain the common code to "fail" or otherwise validate things as it goes?#start the test case. my $x= new blahblah; # what I'm testing. # The above and following method calls will be different for each test +. $x->foo (5000); $x->bar(200); # output_test is my common code I'll use i'll use to validate all the +similar tests. my $ot= output_test->new ("out1"); # ready... different name for each +. my $outfile= $ot->openout(); # get set... (common code to generate fi +le name, open it for output, check for errors) $x->emit ($outfile); # this method call will differ for each test cas +e, so it's not part of the common code. $ot->ok_files_match(); # and back to the common code again to compare + the resulting file with a reference "correct" file. # do all that again, with differences. # ...
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Writing code using Test::More -- how to fail within subs?
by adrianh (Chancellor) on Feb 08, 2003 at 10:13 UTC | |
by John M. Dlugosz (Monsignor) on Feb 09, 2003 at 04:47 UTC | |
by adrianh (Chancellor) on Feb 09, 2003 at 10:06 UTC | |
Re: Writing code using Test::More -- how to fail within subs?
by adrianh (Chancellor) on Feb 08, 2003 at 10:24 UTC | |
by John M. Dlugosz (Monsignor) on Feb 09, 2003 at 04:55 UTC | |
by adrianh (Chancellor) on Feb 09, 2003 at 10:12 UTC |