I'm writing up the new Testing.pm docs and ran into a snag. Its with
this:
skip {
ok( head("http://www.foo.com"), "www.foo.com is alive" );
ok( head("http://www.foo.com/bar"), " and has bar" );
} "LWP::Simple not installed",
!eval { require LWP::Simple; LWP::Simple->import; 1 };
That is to say, "skip these tests if LWP::Simple isn't installed,
otherwise run them normally." Now that's all fine and good if
LWP::Simple *is* installed. It just runs the two ok() tests normally
producing two ok's as planned.
ok 1 - www.foo.com is alive
ok 2 - and has bar
But what happens if LWP::Simple isn't installed? You expect:
ok 1 - www.foo.com is alive # Skip (LWP::Simple not installed)
ok 2 - and has bar # Skip (LWP::Simple not installed)
Unfortunately, there's a good chance the block of tests will die
prematurely (in this case it'll puke on the first head() call). Even
if run inside an eval block, this means you can't know how many ok()s
were supposed to be run and thus can't output the skip results!
Bummer. Ideas? I really, really, really want to salvage this
interface. (No filters, please) We could make it that skip takes a fourth
argument, the number of tests in the block, but that's clunky and
redundant.
Here's one possibility. Change the syntax of ok():
skip {
ok { head("http://www.foo.com") } "www.foo.com is alive" );
ok { head("http://www.foo.com/bar") } " and has bar" );
} "LWP::Simple not installed",
!eval { require LWP::Simple; LWP::Simple->import; 1 };
Then ok() can safely eval each of its expressions. This might be nice
in general (to prevent a failed test from stopping the whole script)
but it still doesn't solve the problem. What if head() was used
inside the skip() but outside the ok()? What if like() or is() was
used? Back to square one.
Another possibility... use a local __DIE__ handler inside the skip()
to trap and ignore dies. We'd have to be careful not to screw up eval
and $@ in the code we're testing (using $^S), but it will mess up any
code that relies on its own __DIE__ handler. However... since the
purpose here is not to actually run the tests but just to count the
ok's, that doesn't matter!
todo has a similar problem.
Might work. Seems like a long way to go.
--
Michael G. Schwern <[EMAIL PROTECTED]> http://www.pobox.com/~schwern/
Perl6 Quality Assurance <[EMAIL PROTECTED]> Kwalitee Is Job One
The eye opening delightful morning taste of expired cheese bits in sour milk!