|
12 | 12 | import io.cucumber.messages.types.TestStepFinished; |
13 | 13 | import io.cucumber.messages.types.TestStepResult; |
14 | 14 | import io.cucumber.messages.types.TestStepResultStatus; |
| 15 | +import io.cucumber.query.Lineage; |
15 | 16 | import io.cucumber.query.NamingStrategy; |
16 | 17 | import io.cucumber.query.Query; |
17 | 18 |
|
18 | 19 | import java.time.Duration; |
19 | 20 | import java.time.Instant; |
20 | 21 | import java.time.format.DateTimeFormatter; |
21 | 22 | import java.util.AbstractMap.SimpleEntry; |
| 23 | +import java.util.LinkedHashMap; |
22 | 24 | import java.util.List; |
23 | 25 | import java.util.Locale; |
24 | 26 | import java.util.Map; |
|
27 | 29 | import java.util.Set; |
28 | 30 |
|
29 | 31 | import static io.cucumber.messages.types.TestStepResultStatus.PASSED; |
| 32 | +import static java.util.Comparator.comparing; |
| 33 | +import static java.util.Comparator.naturalOrder; |
| 34 | +import static java.util.Comparator.nullsFirst; |
| 35 | +import static java.util.stream.Collectors.collectingAndThen; |
| 36 | +import static java.util.stream.Collectors.groupingBy; |
30 | 37 | import static java.util.stream.Collectors.toList; |
31 | 38 |
|
32 | 39 | class XmlReportData { |
33 | 40 |
|
| 41 | + private static final io.cucumber.messages.types.Duration ZERO_DURATION = |
| 42 | + new io.cucumber.messages.types.Duration(0L, 0L); |
| 43 | + // By definition, but see https://github.com/cucumber/gherkin/issues/11 |
| 44 | + private static final TestStepResult SCENARIO_WITH_NO_STEPS = new TestStepResult(ZERO_DURATION, null, PASSED, null); |
34 | 45 | final Query query = new Query(); |
35 | | - |
36 | 46 | private final NamingStrategy namingStrategy; |
37 | 47 |
|
38 | 48 | XmlReportData(NamingStrategy namingStrategy) { |
@@ -66,8 +76,9 @@ int getTestCaseCount() { |
66 | 76 | String getPickleName(TestCaseStarted testCaseStarted) { |
67 | 77 | Pickle pickle = query.findPickleBy(testCaseStarted) |
68 | 78 | .orElseThrow(() -> new IllegalStateException("No pickle for " + testCaseStarted.getId())); |
69 | | - |
70 | | - return query.findNameOf(pickle, namingStrategy); |
| 79 | + return query.findLineageBy(pickle) |
| 80 | + .map(lineage -> namingStrategy.reduce(lineage, pickle)) |
| 81 | + .orElseGet(pickle::getName); |
71 | 82 | } |
72 | 83 |
|
73 | 84 | List<Entry<String, String>> getStepsAndResult(TestCaseStarted testCaseStarted) { |
@@ -107,14 +118,40 @@ private String renderTestStepText(TestStep testStep) { |
107 | 118 | } |
108 | 119 |
|
109 | 120 | Set<Entry<Optional<Feature>, List<TestCaseStarted>>> getAllTestCaseStartedGroupedByFeature() { |
110 | | - return query.findAllTestCaseStartedGroupedByFeature().entrySet(); |
| 121 | + return query.findAllTestCaseStarted() |
| 122 | + .stream() |
| 123 | + .map(testCaseStarted -> { |
| 124 | + Optional<Lineage> astNodes = query.findLineageBy(testCaseStarted); |
| 125 | + return new SimpleEntry<>(astNodes, testCaseStarted); |
| 126 | + }) |
| 127 | + // Sort entries by gherkin document URI for consistent ordering |
| 128 | + .sorted(comparing( |
| 129 | + entry -> entry.getKey() |
| 130 | + .flatMap(nodes -> nodes.document().getUri()) |
| 131 | + .orElse(null), |
| 132 | + nullsFirst(naturalOrder()) |
| 133 | + )) |
| 134 | + .map(entry -> { |
| 135 | + // Unpack the now sorted entries |
| 136 | + Optional<Feature> feature = entry.getKey().flatMap(Lineage::feature); |
| 137 | + TestCaseStarted testcaseStarted = entry.getValue(); |
| 138 | + return new SimpleEntry<>(feature, testcaseStarted); |
| 139 | + }) |
| 140 | + // Group into a linked hashmap to preserve order |
| 141 | + .collect(groupingBy( |
| 142 | + SimpleEntry::getKey, |
| 143 | + LinkedHashMap::new, |
| 144 | + collectingAndThen( |
| 145 | + toList(), |
| 146 | + entries -> entries.stream() |
| 147 | + .map(SimpleEntry::getValue) |
| 148 | + .collect(toList()) |
| 149 | + ) |
| 150 | + ) |
| 151 | + ) |
| 152 | + .entrySet(); |
111 | 153 | } |
112 | 154 |
|
113 | | - private static final io.cucumber.messages.types.Duration ZERO_DURATION = |
114 | | - new io.cucumber.messages.types.Duration(0L, 0L); |
115 | | - // By definition, but see https://github.com/cucumber/gherkin/issues/11 |
116 | | - private static final TestStepResult SCENARIO_WITH_NO_STEPS = new TestStepResult(ZERO_DURATION, null, PASSED, null); |
117 | | - |
118 | 155 | TestStepResult getTestCaseStatus(TestCaseStarted testCaseStarted) { |
119 | 156 | return query.findMostSevereTestStepResultBy(testCaseStarted) |
120 | 157 | .orElse(SCENARIO_WITH_NO_STEPS); |
|
0 commit comments