Search in sources :

Example 21 with ICounter

use of org.jacoco.core.analysis.ICounter in project jacoco by jacoco.

the class CyclomaticComplexityTest method testSwitch4.

@Test
public void testSwitch4() throws Exception {
    instrument(Switch.class);
    target.test(0);
    target.test(1);
    target.test(2);
    final ICounter complexity = analyze();
    assertEquals(CounterImpl.getInstance(0, 3), complexity);
}
Also used : ICounter(org.jacoco.core.analysis.ICounter) Test(org.junit.Test)

Example 22 with ICounter

use of org.jacoco.core.analysis.ICounter in project jacoco by jacoco.

the class BarColumn method item.

public void item(final HTMLElement td, final ITableItem item, final Resources resources, final ReportOutputFolder base) throws IOException {
    if (max > 0) {
        final ICounter counter = item.getNode().getCounter(entity);
        final int missed = counter.getMissedCount();
        bar(td, missed, Resources.REDBAR, resources, base);
        final int covered = counter.getCoveredCount();
        bar(td, covered, Resources.GREENBAR, resources, base);
    }
}
Also used : ICounter(org.jacoco.core.analysis.ICounter)

Example 23 with ICounter

use of org.jacoco.core.analysis.ICounter in project jacoco by jacoco.

the class PercentageColumn method cell.

private void cell(final HTMLElement td, final ICoverageNode node) throws IOException {
    final ICounter counter = node.getCounter(entity);
    final int total = counter.getTotalCount();
    if (total == 0) {
        td.text("n/a");
    } else {
        td.text(format(counter.getCoveredRatio()));
    }
}
Also used : ICounter(org.jacoco.core.analysis.ICounter)

Example 24 with ICounter

use of org.jacoco.core.analysis.ICounter in project bazel by bazelbuild.

the class JacocoLCOVFormatter method createVisitor.

public IReportVisitor createVisitor(final File output, final Map<String, BranchCoverageDetail> branchCoverageDetail) {
    return new IReportVisitor() {

        private Map<String, Map<String, IClassCoverage>> sourceToClassCoverage = new TreeMap<>();

        private Map<String, ISourceFileCoverage> sourceToFileCoverage = new TreeMap<>();

        @Override
        public void visitInfo(List<SessionInfo> sessionInfos, Collection<ExecutionData> executionData) throws IOException {
        }

        @Override
        public void visitEnd() throws IOException {
            try (FileWriter fileWriter = new FileWriter(output, true);
                PrintWriter printWriter = new PrintWriter(fileWriter)) {
                for (String sourceFile : sourceToClassCoverage.keySet()) {
                    processSourceFile(printWriter, sourceFile);
                }
            }
        }

        @Override
        public void visitBundle(IBundleCoverage bundle, ISourceFileLocator locator) throws IOException {
            // information and process everything at the end.
            for (IPackageCoverage pkgCoverage : bundle.getPackages()) {
                for (IClassCoverage clsCoverage : pkgCoverage.getClasses()) {
                    String fileName = clsCoverage.getPackageName() + "/" + clsCoverage.getSourceFileName();
                    if (!sourceToClassCoverage.containsKey(fileName)) {
                        sourceToClassCoverage.put(fileName, new TreeMap<String, IClassCoverage>());
                    }
                    sourceToClassCoverage.get(fileName).put(clsCoverage.getName(), clsCoverage);
                }
                for (ISourceFileCoverage srcCoverage : pkgCoverage.getSourceFiles()) {
                    sourceToFileCoverage.put(srcCoverage.getPackageName() + "/" + srcCoverage.getName(), srcCoverage);
                }
            }
        }

        @Override
        public IReportGroupVisitor visitGroup(String name) throws IOException {
            return null;
        }

        private void processSourceFile(PrintWriter writer, String sourceFile) {
            writer.printf("SF:%s\n", sourceFile);
            ISourceFileCoverage srcCoverage = sourceToFileCoverage.get(sourceFile);
            if (srcCoverage != null) {
                // List methods, including methods from nested classes, in FN/FNDA pairs
                for (IClassCoverage clsCoverage : sourceToClassCoverage.get(sourceFile).values()) {
                    for (IMethodCoverage mthCoverage : clsCoverage.getMethods()) {
                        String name = constructFunctionName(mthCoverage, clsCoverage.getName());
                        writer.printf("FN:%d,%s\n", mthCoverage.getFirstLine(), name);
                        writer.printf("FNDA:%d,%s\n", mthCoverage.getMethodCounter().getCoveredCount(), name);
                    }
                }
                for (IClassCoverage clsCoverage : sourceToClassCoverage.get(sourceFile).values()) {
                    BranchCoverageDetail detail = branchCoverageDetail.get(clsCoverage.getName());
                    if (detail != null) {
                        for (int line : detail.linesWithBranches()) {
                            int numBranches = detail.getBranches(line);
                            boolean executed = detail.getExecutedBit(line);
                            if (executed) {
                                for (int branchIdx = 0; branchIdx < numBranches; branchIdx++) {
                                    if (detail.getTakenBit(line, branchIdx)) {
                                        // executed, taken
                                        writer.printf("BA:%d,%d\n", line, 2);
                                    } else {
                                        // executed, not taken
                                        writer.printf("BA:%d,%d\n", line, 1);
                                    }
                                }
                            } else {
                                for (int branchIdx = 0; branchIdx < numBranches; branchIdx++) {
                                    // not executed
                                    writer.printf("BA:%d,%d\n", line, 0);
                                }
                            }
                        }
                    }
                }
                // List of DA entries matching source lines
                int firstLine = srcCoverage.getFirstLine();
                int lastLine = srcCoverage.getLastLine();
                for (int line = firstLine; line <= lastLine; line++) {
                    ICounter instructionCounter = srcCoverage.getLine(line).getInstructionCounter();
                    if (instructionCounter.getTotalCount() != 0) {
                        writer.printf("DA:%d,%d\n", line, instructionCounter.getCoveredCount());
                    }
                }
            }
            writer.println("end_of_record");
        }

        private String constructFunctionName(IMethodCoverage mthCoverage, String clsName) {
            // lcov_merger doesn't seem to care about these entries.
            return clsName + "::" + mthCoverage.getName() + " " + mthCoverage.getDesc();
        }
    };
}
Also used : IMethodCoverage(org.jacoco.core.analysis.IMethodCoverage) FileWriter(java.io.FileWriter) ISourceFileLocator(org.jacoco.report.ISourceFileLocator) IReportVisitor(org.jacoco.report.IReportVisitor) IPackageCoverage(org.jacoco.core.analysis.IPackageCoverage) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) ICounter(org.jacoco.core.analysis.ICounter) Collection(java.util.Collection) IBundleCoverage(org.jacoco.core.analysis.IBundleCoverage) List(java.util.List) TreeMap(java.util.TreeMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 25 with ICounter

use of org.jacoco.core.analysis.ICounter in project jacoco by jacoco.

the class CounterImplTest method testGetMissedStatus1.

@Test
public void testGetMissedStatus1() {
    ICounter c = CounterImpl.getInstance(0, 0);
    assertEquals(ICounter.EMPTY, c.getStatus());
}
Also used : ICounter(org.jacoco.core.analysis.ICounter) Test(org.junit.Test)

Aggregations

ICounter (org.jacoco.core.analysis.ICounter)50 Test (org.junit.Test)40 CounterEntity (org.jacoco.core.analysis.ICoverageNode.CounterEntity)2 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 IBundleCoverage (org.jacoco.core.analysis.IBundleCoverage)1 IClassCoverage (org.jacoco.core.analysis.IClassCoverage)1 ILine (org.jacoco.core.analysis.ILine)1 IMethodCoverage (org.jacoco.core.analysis.IMethodCoverage)1 IPackageCoverage (org.jacoco.core.analysis.IPackageCoverage)1 ISourceFileCoverage (org.jacoco.core.analysis.ISourceFileCoverage)1 Instruction (org.jacoco.core.internal.flow.Instruction)1 IReportVisitor (org.jacoco.report.IReportVisitor)1 ISourceFileLocator (org.jacoco.report.ISourceFileLocator)1 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)1