diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/report/asciidoc/AsciiDocReportGenerator.java b/jgiven-core/src/main/java/com/tngtech/jgiven/report/asciidoc/AsciiDocReportGenerator.java index bc0c8a61d4..a49c86d3a8 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/report/asciidoc/AsciiDocReportGenerator.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/report/asciidoc/AsciiDocReportGenerator.java @@ -23,6 +23,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,6 +59,7 @@ public class AsciiDocReportGenerator extends AbstractReportGenerator { private File targetDir; private File featuresDir; + private File tagsDir; @Override @@ -77,6 +81,14 @@ public void generate() { writeIndexFileForPendingScenarios(); + final HierarchyCalculator hierarchyCalculator = new HierarchyCalculator(allTags, taggedScenarioFeatures); + + final Map>> groupedTags = hierarchyCalculator.computeGroupedTag(); + + groupedTags.forEach(this::writeIndexFileForTaggedScenarios); + + writeIndexFileForAllTags(groupedTags); + writeTotalStatisticsFile(); writeIndexFileForFullReport(config.getTitle()); @@ -150,6 +162,76 @@ private void writeIndexFileForPendingScenarios() { writeAsciiDocBlocksToFile(targetDir, "pendingScenarios", asciiDocBlocks); } + private void writeIndexFileForTaggedScenarios(final String tagType, final Map> taggedScenarios) { + final Optional firstTag = taggedScenarios.keySet().stream() + .findFirst() + .map(allTags::get); + + if (firstTag.isEmpty()) { + return; + } + + final int numTaggedScenarios = taggedScenarios.keySet().stream().mapToInt(taggedScenarioCounts::get).sum(); + + final List asciiDocBlocks = taggedScenarios.keySet().size() == 1 + ? singleValuedTag(taggedScenarios, firstTag.get(), numTaggedScenarios) + : multiValuedTag(taggedScenarios, firstTag.get(), numTaggedScenarios); + + writeAsciiDocBlocksToFile(tagsDir, tagType, asciiDocBlocks); + } + + private List multiValuedTag(final Map> taggedScenarios, final Tag tag, final int numTaggedScenarios) { + final AsciiDocSnippetGenerator snippetGenerator = new AsciiDocSnippetGenerator( + tag.getName(), "tagged scenarios", numTaggedScenarios); + + final List asciiDocBlocks = snippetGenerator.generateIntroSnippet(tag.getDescription()); + taggedScenarios.forEach((tagId, features) -> { + final List snippet = snippetGenerator.generateTagSnippet( + allTags.get(tagId), taggedScenarioCounts.get(tagId), features, 0); + asciiDocBlocks.addAll(snippet); + }); + return asciiDocBlocks; + } + + private List singleValuedTag(final Map> taggedScenarios, final Tag tag, final int numTaggedScenarios) { + final AsciiDocSnippetGenerator snippetGenerator = new AsciiDocSnippetGenerator( + tag.toString(), "tagged scenarios", numTaggedScenarios); + + final List asciiDocBlocks = snippetGenerator.generateIntroSnippet(tag.getDescription()); + taggedScenarios.forEach((tagId, features) -> { + final Tag valueTag = allTags.get(tagId); + final String tagName = TagMapper.toAsciiDocTagName(valueTag); + asciiDocBlocks.add("=== Scenarios"); + final List snippet = snippetGenerator.generateIndexSnippet("../" + FEATURE_PATH, features, tagName, 0); + asciiDocBlocks.addAll(snippet); + }); + return asciiDocBlocks; + } + + + private void writeIndexFileForAllTags(final Map>> strings) { + + final List tagFiles = strings.entrySet().stream() + .sorted((o1, o2) -> { + final Tag tag1 = allTags.get(o1.getValue().keySet().stream().findFirst().orElse("")); + final Tag tag2 = allTags.get(o2.getValue().keySet().stream().findFirst().orElse("")); + return Objects.compare(tag1, tag2, Comparator.comparing(Tag::getName)); + + }) + .map(entry -> entry.getKey().replace(' ', '_')) + .collect(Collectors.toList()); + final Integer total = taggedScenarioCounts.values().stream().reduce(Integer::sum).orElse(999); + final AsciiDocSnippetGenerator snippetGenerator = new AsciiDocSnippetGenerator( + "Tags", "all tags are beautiful", total + ); + + final List asciiDocBlocks = snippetGenerator.generateIntroSnippet(""); + asciiDocBlocks.addAll(snippetGenerator.generateIndexSnippet("tags", tagFiles, "", 1)); + writeAsciiDocBlocksToFile(targetDir, "allTags", + asciiDocBlocks); + + } + private void writeTotalStatisticsFile() { final ListMultimap featureStatistics = completeReportModel.getAllReportModels() .stream() @@ -180,7 +262,6 @@ private void writeIndexFileForFullReport(final String reportTitle) { } private boolean prepareDirectories(final File targetDir) { - File tagsDir; this.targetDir = targetDir; if (this.targetDir == null) { log.error("Target directory was not configured"); diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/report/asciidoc/HierarchyCalculator.java b/jgiven-core/src/main/java/com/tngtech/jgiven/report/asciidoc/HierarchyCalculator.java new file mode 100644 index 0000000000..f3bd986423 --- /dev/null +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/report/asciidoc/HierarchyCalculator.java @@ -0,0 +1,29 @@ +package com.tngtech.jgiven.report.asciidoc; + +import com.tngtech.jgiven.report.model.Tag; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class HierarchyCalculator { + private final Map> taggedScenarioFiles; + private final Map allTags; + + public HierarchyCalculator(final Map allTags, final Map> taggedScenarioFiles) { + this.taggedScenarioFiles = taggedScenarioFiles; + this.allTags = allTags; + } + + Map>> computeGroupedTag() { + return taggedScenarioFiles.entrySet().stream() + .filter(entry -> allTags.get(entry.getKey()).getShownInNavigation()) + .collect(Collectors.groupingBy(this::fullType, Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + } + + private String fullType(final Map.Entry> entry) { + final Tag tag = allTags.get(entry.getKey()); + + return tag.getFullType(); + + } +} diff --git a/jgiven-core/src/main/resources/com/tngtech/jgiven/report/asciidoc/index.asciidoc b/jgiven-core/src/main/resources/com/tngtech/jgiven/report/asciidoc/index.asciidoc index 75ec86dcd6..54a5b77992 100644 --- a/jgiven-core/src/main/resources/com/tngtech/jgiven/report/asciidoc/index.asciidoc +++ b/jgiven-core/src/main/resources/com/tngtech/jgiven/report/asciidoc/index.asciidoc @@ -10,3 +10,5 @@ include::allScenarios.asciidoc[] include::failedScenarios.asciidoc[] include::pendingScenarios.asciidoc[] + +include::allTags.asciidoc[]