All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.coverage.reporting.pathCoverage.PathCoverageFormatter Maven / Gradle / Ivy

Go to download

JMockit Coverage is a code coverage tool with several metrics (line, path, data) capable of generating HTML reports. It is designed with ease of use in mind, avoiding the need for complex configuration. Instead, smart (but overridable) defaults are employed, such as the selection of which classes to consider for coverage, and where to find sources files for report generation.

There is a newer version: 1.23
Show newest version
/*
 * Copyright (c) 2006-2013 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.coverage.reporting.pathCoverage;

import java.io.*;
import java.util.*;

import org.jetbrains.annotations.*;

import mockit.coverage.paths.*;

final class PathCoverageFormatter
{
   @NotNull private final PrintWriter output;
   @NotNull private final StringBuilder lineSegmentIds;
   private char pathId1;
   private char pathId2;

   PathCoverageFormatter(@NotNull PrintWriter output)
   {
      this.output = output;
      lineSegmentIds = new StringBuilder(100);
   }

   void writeInformationForEachPath(@NotNull List paths)
   {
      pathId1 = 'A';
      pathId2 = '\0';

      for (Path path : paths) {
         writeCoverageInfoForIndividualPath(path);

         if (pathId2 == '\0' && pathId1 < 'Z') {
            pathId1++;
         }
         else if (pathId2 == '\0') {
            pathId1 = 'A';
            pathId2 = 'A';
         }
         else if (pathId2 < 'Z') {
            pathId2++;
         }
         else {
            pathId1++;
            pathId2 = 'A';
         }
      }
   }

   private void writeCoverageInfoForIndividualPath(@NotNull Path path)
   {
      int executionCount = path.getExecutionCount();
      String lineSegmentIdsForPath = getIdsForLineSegmentsBelongingToThePath(path);

      output.write("        ");
      writePathId();
      output.write(": ");
      output.print(executionCount);
      output.println("");
   }

   @NotNull private String getIdsForLineSegmentsBelongingToThePath(@NotNull Path path)
   {
      lineSegmentIds.setLength(0);

      int previousLine = 0;
      int previousSegment = 0;

      for (Node node : path.getNodes()) {
         int line = node.line;
         int segment = node.getSegment();

         if (line > previousLine) {
            appendSegmentId(line, segment);
            previousLine = line;
         }
         else if (segment > previousSegment) {
            appendSegmentId(line, segment);
         }

         previousSegment = segment;
      }

      return lineSegmentIds.toString();
   }

   private void appendSegmentId(int line, int segment)
   {
      if (lineSegmentIds.length() > 0) {
         lineSegmentIds.append(' ');
      }
      
      lineSegmentIds.append('l').append(line).append('s').append(segment);
   }

   private void writePathId()
   {
      output.write(pathId1);

      if (pathId2 != '\0') {
         output.write(pathId2);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy