Skip to content

Commit

Permalink
Count line hits for PIT correctly
Browse files Browse the repository at this point in the history
Only killed mutations count as hits, and only lived or uncovered mutations
count as misses.
  • Loading branch information
thejohnfreeman committed Feb 8, 2017
1 parent 3b695cb commit 7d52867
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ class PITSourceReportFactory implements SourceReportFactory {
Map<String, Map<Integer, Integer>> hitsPerLineMapPerFile = [:]

mutations.mutation.each() { mutation ->
def status = mutation.@status
Integer hit
if (status == 'KILLED') {
hit = 1
} else if (status == 'LIVED' || status == 'NO_COVERAGE') {
hit = 0
} else {
// Leave null for lines that could not be mutated.
return
}

// PIT reports only the source file's base name, which might not be
// unique. Assume the source file's directory can be derived from
// the package name.
Expand All @@ -43,10 +54,11 @@ class PITSourceReportFactory implements SourceReportFactory {
}
def packageName = matcher.group(0)
def filename = packageName.replace('.', File.separator) + mutation.sourceFile.text()

def hitsPerLine = hitsPerLineMapPerFile.get(filename, [:])
Integer lineNumber = mutation.lineNumber.text().toInteger() - 1
Integer hits = hitsPerLine.get(lineNumber, 0)
hitsPerLine[lineNumber] = hits + 1
hitsPerLine[lineNumber] = hits + hit
}

List<SourceReport> reports = new ArrayList<SourceReport>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ class PITSourceReportFactoryTest {

assertEquals 'src/test/fixture/org/kt3k/bankaccount/BankAccount.java', reports[0].name
assertEquals 30, reports[0].coverage.size()
assertEquals 4, reports[0].coverage.findAll{ it != null }.sum()
assertEquals 4, reports[0].coverage.findAll{ it != null }.size()
assertEquals 3, reports[0].coverage.findAll{ it != null }.sum()
assertEquals 'src/test/fixture/org/kt3k/bankaccount/TransferContext.java', reports[1].name
assertEquals 45, reports[1].coverage.size()
assertEquals 4, reports[1].coverage.findAll{ it != null }.size()
assertEquals 4, reports[1].coverage.findAll{ it != null }.sum()
}

Expand Down

0 comments on commit 7d52867

Please sign in to comment.