Skip to content

Commit

Permalink
assertLine(String, int) should imply zero number of branches (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed Aug 7, 2018
1 parent bbee039 commit a364dcc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void testCoverageResult() {
assertLine("customValueOfMethod", ICounter.NOT_COVERED); assertLine("customValueOfMethod", ICounter.NOT_COVERED);
assertLine("customValuesMethod", ICounter.NOT_COVERED); assertLine("customValuesMethod", ICounter.NOT_COVERED);


assertLine("const", ICounter.PARTLY_COVERED); assertLine("const", ICounter.PARTLY_COVERED, 1, 1);
assertLine("staticblock", ICounter.FULLY_COVERED); assertLine("staticblock", ICounter.FULLY_COVERED);
assertLine("super", ICounter.FULLY_COVERED); assertLine("super", ICounter.FULLY_COVERED);
assertLine("constructor", ICounter.FULLY_COVERED); assertLine("constructor", ICounter.FULLY_COVERED);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void twoRegions() {
} }
assertLine("twoRegions.2", ICounter.EMPTY); assertLine("twoRegions.2", ICounter.EMPTY);


assertLine("twoRegions.if", ICounter.FULLY_COVERED); assertLine("twoRegions.if", ICounter.FULLY_COVERED, 1, 1);
assertLine("twoRegions.region.1", ICounter.FULLY_COVERED); assertLine("twoRegions.region.1", ICounter.FULLY_COVERED);
assertLine("twoRegions.region.2", ICounter.NOT_COVERED); assertLine("twoRegions.region.2", ICounter.NOT_COVERED);
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public void covered() {
} else { } else {
assertLine("covered.switch", ICounter.PARTLY_COVERED, 2, 7); assertLine("covered.switch", ICounter.PARTLY_COVERED, 2, 7);
} }
assertLine("covered.case1", ICounter.FULLY_COVERED, 0, 0); assertLine("covered.case1", ICounter.FULLY_COVERED);
assertLine("covered.case2", ICounter.FULLY_COVERED, 0, 0); assertLine("covered.case2", ICounter.FULLY_COVERED);
assertLine("covered.case3", ICounter.FULLY_COVERED, 0, 0); assertLine("covered.case3", ICounter.FULLY_COVERED);
assertLine("covered.default", ICounter.FULLY_COVERED, 0, 0); assertLine("covered.default", ICounter.FULLY_COVERED);
} }


/** /**
Expand All @@ -57,7 +57,7 @@ public void notCovered() {
@Test @Test
public void handwritten() { public void handwritten() {
assertLine("handwritten.firstSwitch", ICounter.FULLY_COVERED, 2, 1); assertLine("handwritten.firstSwitch", ICounter.FULLY_COVERED, 2, 1);
assertLine("handwritten.ignored", ICounter.FULLY_COVERED); assertLine("handwritten.ignored", ICounter.FULLY_COVERED, 1, 1);
assertLine("handwritten.secondSwitch", ICounter.FULLY_COVERED, 3, 1); assertLine("handwritten.secondSwitch", ICounter.FULLY_COVERED, 3, 1);
assertLine("handwritten.case1", ICounter.FULLY_COVERED); assertLine("handwritten.case1", ICounter.FULLY_COVERED);
assertLine("handwritten.case2", ICounter.NOT_COVERED); assertLine("handwritten.case2", ICounter.NOT_COVERED);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void returnInBody() {
// https://bugs.openjdk.java.net/browse/JDK-8134759 // https://bugs.openjdk.java.net/browse/JDK-8134759
// javac 7 and 8 up to 8u92 are affected // javac 7 and 8 up to 8u92 are affected
if (JAVA_VERSION.isBefore("1.8.0_92")) { if (JAVA_VERSION.isBefore("1.8.0_92")) {
assertLine("returnInBody.close", ICounter.FULLY_COVERED, 0, 0); assertLine("returnInBody.close", ICounter.FULLY_COVERED);
} else { } else {
assertLine("returnInBody.close", ICounter.EMPTY); assertLine("returnInBody.close", ICounter.EMPTY);
} }
Expand Down Expand Up @@ -130,10 +130,10 @@ public void nested() {
} else { } else {
assertLine("nested.try3", ICounter.EMPTY); assertLine("nested.try3", ICounter.EMPTY);
} }
assertLine("nested.open3", ICounter.FULLY_COVERED, 0, 0); assertLine("nested.open3", ICounter.FULLY_COVERED);
assertLine("nested.body3", ICounter.FULLY_COVERED, 0, 0); assertLine("nested.body3", ICounter.FULLY_COVERED);
assertLine("nested.catch3", ICounter.NOT_COVERED); assertLine("nested.catch3", ICounter.NOT_COVERED);
assertLine("nested.finally3", ICounter.FULLY_COVERED, 0, 0); assertLine("nested.finally3", ICounter.FULLY_COVERED);


// without filter next lines have branches: // without filter next lines have branches:
assertLine("nested.close3", ICounter.EMPTY); assertLine("nested.close3", ICounter.EMPTY);
Expand Down Expand Up @@ -186,14 +186,14 @@ public void empty() {
assertLine("empty.try", ICounter.EMPTY); assertLine("empty.try", ICounter.EMPTY);
} }
assertLine("empty.open", ICounter.FULLY_COVERED); assertLine("empty.open", ICounter.FULLY_COVERED);
// empty when EJC: if (!isJDKCompiler) {
if (isJDKCompiler) { assertLine("empty.close", ICounter.PARTLY_COVERED, 7, 1);
if (JAVA_VERSION.isBefore("9")) { } else if (JAVA_VERSION.isBefore("8")) {
// branches with javac 7 and 8 assertLine("empty.close", ICounter.PARTLY_COVERED, 6, 2);
assertLine("empty.close", ICounter.PARTLY_COVERED); } else if (JAVA_VERSION.isBefore("9")) {
} else { assertLine("empty.close", ICounter.PARTLY_COVERED, 2, 2);
assertLine("empty.close", ICounter.FULLY_COVERED, 0, 0); } else {
} assertLine("empty.close", ICounter.FULLY_COVERED);
} }
} }


Expand All @@ -204,7 +204,11 @@ public void empty() {
public void throwInBody() { public void throwInBody() {
// not filtered // not filtered
assertLine("throwInBody.try", ICounter.NOT_COVERED); assertLine("throwInBody.try", ICounter.NOT_COVERED);
if (!isJDKCompiler || JAVA_VERSION.isBefore("11")) { if (!isJDKCompiler){
assertLine("throwInBody.close", ICounter.NOT_COVERED, 6, 0);
} else if (JAVA_VERSION.isBefore("9")) {
assertLine("throwInBody.close", ICounter.NOT_COVERED, 4, 0);
} else if (JAVA_VERSION.isBefore("11")) {
assertLine("throwInBody.close", ICounter.NOT_COVERED); assertLine("throwInBody.close", ICounter.NOT_COVERED);
} else { } else {
assertLine("throwInBody.close", ICounter.EMPTY); assertLine("throwInBody.close", ICounter.EMPTY);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -131,22 +131,28 @@ protected void assertMethodCount(final int expectedTotal) {
} }


protected void assertLine(final String tag, final int status) { protected void assertLine(final String tag, final int status) {
final int nr = source.getLineNumber(tag); privateAssertLine(tag, status, 0, 0);
final ILine line = sourceCoverage.getLine(nr);
final String msg = String.format("Status in line %s: %s",
Integer.valueOf(nr), source.getLine(nr));
final int insnStatus = line.getInstructionCounter().getStatus();
assertEquals(msg, STATUS_NAME[status], STATUS_NAME[insnStatus]);
} }


protected void assertLine(final String tag, final int status, protected void assertLine(final String tag, final int status,
final int missedBranches, final int coveredBranches) { final int missedBranches, final int coveredBranches) {
assertLine(tag, status); if (missedBranches == 0 && coveredBranches == 0) {
throw new IllegalArgumentException(
"Omit redundant specification of zero number of branches");
}
privateAssertLine(tag, status, missedBranches, coveredBranches);
}

private void privateAssertLine(final String tag, final int status,
final int missedBranches, final int coveredBranches) {
final int nr = source.getLineNumber(tag); final int nr = source.getLineNumber(tag);
final ILine line = sourceCoverage.getLine(nr); final ILine line = sourceCoverage.getLine(nr);
final String msg = String.format("Branches in line %s: %s", final String lineMsg = String.format("line %s: %s", Integer.valueOf(nr),
Integer.valueOf(nr), source.getLine(nr)); source.getLine(nr));
assertEquals(msg + " branches", final int insnStatus = line.getInstructionCounter().getStatus();
assertEquals("Instructions in " + lineMsg, STATUS_NAME[status],
STATUS_NAME[insnStatus]);
assertEquals("Branches in " + lineMsg,
CounterImpl.getInstance(missedBranches, coveredBranches), CounterImpl.getInstance(missedBranches, coveredBranches),
line.getBranchCounter()); line.getBranchCounter());
} }
Expand Down

0 comments on commit a364dcc

Please sign in to comment.