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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |