Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: TDD with Coverage Analysis. Wow.

by tlm (Prior)
on Aug 27, 2005 at 13:22 UTC ( [id://487141]=note: print w/replies, xml ) Need Help??


in reply to TDD with Coverage Analysis. Wow.

D::C is cool. I also agree xdg's observations. It can definitely seduce one into obsessing over achieving 100% coverage on all fronts, which, given the current limitations of D::C, is not always reasonable.

In addition to the ones already mentioned, one (admittedly pretty darn obscure) situation I haven't figured out how to cover is code like this:

$SIG{ __DIE__ } = sub { if ( $^S ) { # exceptions being caught } else { # uncoverable? die; } }
In tests, the __DIE__ handler would always run within the dynamic scope of an eval, for obvious reasons, so $^S (aka, $EXCEPTIONS_BEING_CAUGHT) will always be true.

Update: I added a die statement to the second branch of the conditional. Thanks to davidrw, whose reply alerted me to the omission.

the lowliest monk

Replies are listed 'Best First'.
Re^2: TDD with Coverage Analysis. Wow.
by davidrw (Prior) on Aug 27, 2005 at 15:21 UTC
    Can you invoke it directly in the test script as &{$SIG{__DIE__}} and test accordingly from there?

    Also ditto on the above .. we started using Devel::Cover recently at work and besides being very cool in itself, is very great thing in terms of forcing you to really know your own code in order to write thorough test coverage for it. On more than one occaison has made me rethink/rework code, and also revealed hidden bugs.

    As for coverage --> 100%, for me it is also usually the error conditions (e.g. </c>$dbh->prepare() or do { ... }</c>) that are the holes.. and while 100% isn't strictly necessary, it is nice feeling to get a completely green board ;)


    As for another work around (this might not be possible depending on what the function actually does) for one of xdg's points, this might work in some cases:
    # original my $filename = prompt_for_filename() || default_filename(); # potential workaround use constant DEFAULT_FILENAME => 'foo.txt'; my $filename = prompt_for_filename() || DEFAULT_FILENAME;

      Can you invoke it directly in the test script as &{$SIG{__DIE__}} and test accordingly from there?

      Only if the !$^S branch doesn't die (and doesn't call something that does). I should have made that clearer in my original post. Will fix. Thanks.

      # potential workaround use constant DEFAULT_FILENAME => 'foo.txt'; my $filename = prompt_for_filename() || DEFAULT_FILENAME;

      I don't see how the second alternative of the || could ever be false (i.e. the coverage would never be 100%).

      Update: Ah, I see.

      the lowliest monk

        I don't see how the second alternative of the || could ever be false (i.e. the coverage would never be 100%).
        It can't, but just like with xdg's note about my $value = $some_other_value || undef;, Devel::Cover is also smart enough to realize that DEFAULT_FILENAME is a constant and removes the truth table entries of it being false, so that you can get to 100% coverage for that line.
Re^2: TDD with Coverage Analysis. Wow.
by DrWhy (Chaplain) on Aug 29, 2005 at 06:01 UTC
    Perhaps you could test the non $^S branch by embedding the code under test in a separate litttle script and arrange to have this code executed in a separate process executed using an IPC::Run::<something> call and/or system(). You could then capture any error output/exit code as needed to verify correct behavior?

    --DrWhy

    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

      Perhaps you could test the non $^S branch by embedding the code under test in a separate litttle script and arrange to have this code executed in a separate process executed using an IPC::Run::<something> call and/or system(). You could then capture any error output/exit code as needed to verify correct behavior?

      Yes, I did try something like this, and it worked, but Devel::Cover did not detect it. So the branch is not really uncoverable, I originally wrote, but rather "uncoverable as far as Devel::Cover can tell." :-)

      the lowliest monk

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://487141]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found