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

nl.hsac.fitnesse.slimcoverage.SlimCoverageReportWriter Maven / Gradle / Ivy

There is a newer version: 1.32.11
Show newest version
package nl.hsac.fitnesse.slimcoverage;

import java.util.Collection;
import java.util.Map;
import java.util.function.Consumer;

public class SlimCoverageReportWriter {
    private static final String COPY_TO_CLIPBOARD_JS =
            "function elementContentsToClipboard(el) {" +
                    "var selected = document.getSelection().rangeCount > 0? document.getSelection().getRangeAt(0):false;" +
                    "var body = document.body, range, sel;" +
                    "if (document.createRange && window.getSelection) {" +
                    "range = document.createRange();" +
                    "sel = window.getSelection();" +
                    "sel.removeAllRanges();" +
                    "try {" +
                    "range.selectNodeContents(el);" +
                    "sel.addRange(range);" +
                    "} catch (e) {" +
                    "range.selectNode(el);" +
                    "sel.addRange(range);" +
                    "}" +
                    "document.execCommand('copy');" +
                    "} else if (body.createTextRange) {" +
                    "range = body.createTextRange();" +
                    "range.moveToElementText(el);" +
                    "range.select();" +
                    "range.execCommand('copy');" +
                    "}" +
                    "document.getSelection().removeAllRanges();" +
                    "if (selected) document.getSelection().addRange(selected);" +
                    "}";

    private final SlimScenarioUsage usage;
    private final Consumer outputWriter;

    public SlimCoverageReportWriter(SlimScenarioUsage usage, Consumer outputWriter) {
        this.usage = usage;
        this.outputWriter = outputWriter;
    }

    public void reportScenarioUsage() {
        Map totalUsage = usage.getScenarioUsage().getUsage();
        if (totalUsage.isEmpty()) {
            write("No scenarios in run");
        } else {
            Collection unused = usage.getUnusedScenarios();
            if (!unused.isEmpty()) {
                reportScenarioUsageHeader("Unused scenarios:");
                write("
    "); for (String scenarioName : unused) { write("
  • " + scenarioName + "
  • "); } write("
"); reportScenarioUsageNewline(); } writeJavaScript(); reportScenarioUsageHeader("Total usage count per scenario:"); writeCopyButton("totalUsageCountPerScenario"); write(""); write(""); outputTableRows(totalUsage); write("
ScenarioCount
"); reportScenarioUsageNewline(); reportScenarioUsageHeader("Scenarios grouped by usage scope:"); Map> scenariosBySmallestScope = usage.getScenariosBySmallestScope(); outputNestedList(scenariosBySmallestScope); reportScenarioUsageNewline(); reportScenarioUsageHeader("Usage count per scenario per page:"); writeCopyButton("usageCountPerScenarioPerPage"); write(""); write(""); for (SlimScenarioUsagePer usagePerPage : usage.getUsage()) { String pageName = usagePerPage.getGroupName(); String pageLink = String.format("%1$s", pageName); outputTableRows(usagePerPage.getUsage(), pageLink); } write("
PageScenarioCount
"); Map> overriddenPerPage = usage.getOverriddenScenariosPerPage(); if (!overriddenPerPage.isEmpty()) { reportScenarioUsageNewline(); reportScenarioUsageHeader("Overridden scenario(s) per page:"); outputNestedList(overriddenPerPage); } } } protected void writeJavaScript() { StringBuilder builder = new StringBuilder(); builder.append(""); write(builder.toString()); } protected void writeCopyButton(String idToCopy) { StringBuilder output = new StringBuilder(); output.append(""); write(output.toString()); } protected void outputTableRows(Map tableRows, String... prefixes) { for (Map.Entry usagePerScenario : tableRows.entrySet()) { write(""); write(""); for (String prefix : prefixes) { write(prefix + ""); } write(usagePerScenario.getKey() + "" + usagePerScenario.getValue()); write(""); write(""); } } protected void outputNestedList(Map> nestedList) { write("
    "); for (Map.Entry> item : nestedList.entrySet()) { String itemName = item.getKey(); write("
  • "); write(itemName); write("
      "); for (String nestedName : item.getValue()) { write("
    • " + nestedName + "
    • "); } write("
    "); write("
  • "); } write("
"); } protected void reportScenarioUsageHeader(String header) { write("

" + header + "

"); } protected void reportScenarioUsageNewline() { write("
"); } protected void write(String output) { outputWriter.accept(output); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy