itay / node-cover

Native JavaScript Code Coverage
http://www.cover.io
165 stars 17 forks source link

Add decision/condition coverage #5

Open goatslacker opened 12 years ago

itay commented 12 years ago

Can you give an example of what you want here? What statistics do you want to collect?

goatslacker commented 12 years ago

Here's a simple example:

var a;
if (true) {
  a = 1;
}

This will report as 100% coverage. I covered all lines and all blocks however I did not cover all branches. What if the if statement evaluates to false? That is currently not being reported.

Here's another example with ternary operator:

function what(yes) {
  return yes ? 1 : 2;
}

// test:
what(true);

cover tells me it's 100% coverage when in fact I'm not covering return 2

itay commented 12 years ago

In the first case, you are covering all branches, no? Your 'if' condition evaluates to true. I agree that if it was false, you would have had less than 100% coverage, but I think it would report it as less than a 100%, no?

For the second case, you're right, we're misreporting to some degree. If you had return yes ? a() : b(), we'd report it more correctly.

I think I have a fix in mind to give more correct stats for some of these cases. Open to ideas/suggestions.

itay commented 12 years ago

Can you try installing the new version (0.2.1)? I think I resolved the issue.

goatslacker commented 12 years ago

The first case was a trivial example. But yes, I do in fact have 100% line coverage but my testing code is "flawed" in the sense that I'm not ever checking if (false)

Btw I don't mind checking out and building from git so you don't have to be publishing packages to npm.

itay commented 12 years ago

Thanks - let me know if the fix worked for you :)

goatslacker commented 12 years ago

So, the fix worked fine for the ternary operator example. If I were to nitpick though, it told me I missed a line when it should really tell me that I missed a branch

itay commented 12 years ago

I see. There are a couple of ways to look at it:

In this case, it's telling you that you missed a partial line, because you covered some parts of the line but not others (that's why it is in yellow). There are other cases that aren't really "branches". Consider for example: if (true || 2) { .. }. In this case, you miss the 2, because the true will always evaluate.

I do think that it might be interesting to add secondary stats beyond line and block coverage. For example, we could add function-coverage and branch-coverage.

I'll think about adding those. Any suggestions are welcome!

goatslacker commented 12 years ago

function coverage would be great. I'll keep posting more issues as I find them.