mockit.coverage.reporting.lineCoverage.LineSegmentsFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for automated developer testing.
It contains mocking/faking APIs and a code coverage tool, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
/*
* Copyright (c) 2006 JMockit developers
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.coverage.reporting.lineCoverage;
import java.util.*;
import javax.annotation.*;
import static java.lang.Character.*;
import mockit.coverage.*;
import mockit.coverage.lines.*;
import mockit.coverage.reporting.*;
import mockit.coverage.reporting.parsing.*;
final class LineSegmentsFormatter
{
@Nullable private final ListOfCallPoints listOfCallPoints;
@Nonnull private final StringBuilder formattedLine;
// Helper fields:
private int lineNumber;
private int segmentIndex;
private LineSegmentData segmentData;
@Nullable private LineElement element;
LineSegmentsFormatter(boolean withCallPoints, @Nonnull StringBuilder formattedLine) {
listOfCallPoints = withCallPoints ? new ListOfCallPoints() : null;
this.formattedLine = formattedLine;
}
void formatSegments(@Nonnull LineParser lineParser, @Nonnull LineCoverageData lineData) {
lineNumber = lineParser.getNumber();
List branchData = lineData.getBranches();
int numSegments = lineData.getNumberOfSegments();
element = lineParser.getInitialElement().appendUntilNextCodeElement(formattedLine);
segmentIndex = 0;
segmentData = lineData;
appendUntilNextBranchingPoint();
while (element != null && segmentIndex < numSegments) {
segmentData = segmentIndex == 0 ? lineData : branchData.get(segmentIndex - 1);
element = element.appendUntilNextCodeElement(formattedLine);
appendUntilNextBranchingPoint();
}
if (element != null) {
element.appendAllBefore(formattedLine, null);
}
formattedLine.append("
");
if (listOfCallPoints != null) {
formattedLine.append(listOfCallPoints.getContents());
}
}
private void appendUntilNextBranchingPoint() {
if (element != null) {
LineElement firstElement = element;
element = element.findNextBranchingPoint();
appendToFormattedLine(firstElement);
if (element != null && element.isBranchingElement()) {
formattedLine.append(element.getText());
element = element.getNext();
}
}
}
private void appendToFormattedLine(@Nonnull LineElement firstElement) {
if (firstElement != element) {
appendStartTag();
firstElement.appendAllBefore(formattedLine, element);
appendEndTag();
segmentIndex++;
}
}
private void appendStartTag() {
formattedLine.append("");
}
else {
formattedLine.append("class='covered'>");
}
}
else {
formattedLine.append("class='uncovered'>");
}
}
private void appendEndTag() {
int i = formattedLine.length() - 1;
while (isWhitespace(formattedLine.charAt(i))) {
i--;
}
formattedLine.insert(i + 1, "");
if (listOfCallPoints != null) {
List