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-2015 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 javax.annotation.*;

import mockit.coverage.paths.*;

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

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

   void writeInformationForEachPath(@Nonnull 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(@Nonnull Path path)
   {
      int executionCount = path.getExecutionCount();
      String lineSegmentIdsForPath = getIdsForLineSegmentsBelongingToThePath(path);

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

   @Nonnull
   private String getIdsForLineSegmentsBelongingToThePath(@Nonnull 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