Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails / Cobertura report - conditional coverage doesn't make sense

What could cause something like this:

coverage report fragment

The line def result shows only 2/6 branches covered, even though it has NO BRANCHES, while the following line, which actually has a conditional, is ok.

What's going on? I verified that the cobertura.ser is getting cleaned up between builds.

like image 534
wrschneider Avatar asked Sep 20 '13 17:09

wrschneider


3 Answers

any idea where I can get the source for that JAR?

jar and souce code for disableOptimizationsTransformation

Also - any idea how to include that JAR on classpath ONLY for the test-app build phase?

// Remove the jar before the war is bundled
grails.war.resources = { stagingDir ->
  delete(file:"${stagingDir}/WEB-INF/lib/DisableOptimizationsTransformation-0.1-SNAPSHOT.jar")
}

from other post here

like image 133
lapolonio Avatar answered Oct 22 '22 18:10

lapolonio


Same discussion also appeared in the official forum, see Branch coverage issues .

@rgarcia gave an excellent little tool jar to disable the AST optimization so that Cobertura is able to compute the coverage correctly.

To use the jar, just put it in your myapp\lib folder and then test-app -coverage:)

like image 43
shihpeng Avatar answered Oct 22 '22 16:10

shihpeng


I've noticed the same thing in our grails projects - I think this is caused by the "optimization" branches the groovy compiler creates.

For example - this code

def deleteSomething(params) {
   def result
   if(params.something && params.somethingelse)
      result = "something"
   else result = "something else"
}

looks like this when compiled

public Object deleteSomething(Object params)
{
   CallSite[] arrayOfCallSite = $getCallSiteArray(); Object result = null; if ((!BytecodeInterface8.isOrigZ()) || (__$stMC) || (BytecodeInterface8.disabledStandardMetaClass())) {
      if (((DefaultTypeTransformation.booleanUnbox(arrayOfCallSite[2].callGetProperty(params))) && (DefaultTypeTransformation.booleanUnbox(arrayOfCallSite[3].callGetProperty(params))) ? 1 : 0) != 0) {
         String str1 = "something"; result = str1; return str1; } else {
         String str2 = "something else"; result = str2; return str2;
      }
   }
   else if (((DefaultTypeTransformation.booleanUnbox(arrayOfCallSite[4].callGetProperty(params))) && (DefaultTypeTransformation.booleanUnbox(arrayOfCallSite[5].callGetProperty(params))) ? 1 : 0) != 0) {
      String str3 = "something"; result = str3; return str3; } else {
      String str4 = "something else"; result = str4; return str4; } return null;
}

More discussion here.

like image 4
Becca Gaspard Avatar answered Oct 22 '22 18:10

Becca Gaspard