Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

my $class = ref($class) || $class;

In order to write a test that would cover this, you would have to include the following in your test code:

my $foo = Foo->new();
my $foo2 = $foo->new();
my $foo3 = Foo::new();

Right off the bat, that's a missleading statement.

Code coverage tools can only tell you what lines are executed or not executed, I've never seen one smart enough that you can run it on a test suite, and have it tell you if all the side effects of every executed line of code is also executed.

If I was running Devel::Cover on some unit tests I wrote, and it told me there was a conditional in which one branch wasn't getting run, I would never assume that just writting a test that executed that branch is enough to consider that branch "tested". I would also look at any state modified in that branch, and make sure that my test included any other code that refrenced that state after the branch.

That's not just an important thing to remember about Code Coverage -- it's important to remember about any unit tests. I'm constently seeing people design/modify classes or objects which contain state, and write little micro-unit-tests which test each method individually, without ever testing "chains" of method invocations. The worst example I ever saw was an implimentatin of a glorified Hash that had methods like "add($key,$val)</code, <code>get($key), remove($key) and listKeys()" This is what the unit tests looked like...

function testAdd() { $hash = new Hash; assert($OK == $hash->add("foo","bar")); } function testGet() { $hash = new Hash; $hash->add("foo","bar"); assert("bar" == $hash->get("foo")); } function testList() { $hash = new Hash; $hash->add("foo","bar"); assert("foo" == $hash->listKeys()); } function testRemove() { $hash = new Hash; $hash->add("foo","bar"); assert($OK == $hash->remove("foo")); }

The unit tests all passed, the code was handed off to the client, and the client send back the following set of tests *they* had written to prove that the damn library was a load of crap...

function testGetShouldNotDieHorriblyIfKeyNotFound() { $hash = new Hash; $hash->get("foo"); # this was causing a core dump } function testRemoveShouldAcctuallyRemove() { $hash = new Hash; $hash->add("foo","bar"); $hash->remove("foo"); assert("bar" != $hash->get("foo")); # hey look, still there }

Just becuase you're code is getting executed, doesn't mean it works, or that all of the permutations of use cases are getting executed.


In reply to Re: Is "ref($class) || $class" a bad thing? by hossman
in thread Is "ref($class) || $class" a bad thing? by stvn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found