Skip to content

Commit

Permalink
Use long naming strategy for pickles
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Dec 5, 2022
1 parent d52c96d commit 44fd048
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
54 changes: 44 additions & 10 deletions java/src/main/java/io/cucumber/junitxmlformatter/XmlReportData.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package io.cucumber.junitxmlformatter;

import io.cucumber.messages.types.Envelope;
import io.cucumber.messages.types.Examples;
import io.cucumber.messages.types.Feature;
import io.cucumber.messages.types.GherkinDocument;
import io.cucumber.messages.types.Pickle;
import io.cucumber.messages.types.PickleStep;
import io.cucumber.messages.types.Rule;
import io.cucumber.messages.types.Scenario;
import io.cucumber.messages.types.TableRow;
import io.cucumber.messages.types.TestCase;
import io.cucumber.messages.types.TestCaseFinished;
import io.cucumber.messages.types.TestCaseStarted;
Expand All @@ -15,7 +20,13 @@

import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.Comparator;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Function;
Expand Down Expand Up @@ -48,6 +59,7 @@ class XmlReportData {
private final Map<String, String> pickleIdToScenarioAstNodeId = new ConcurrentHashMap<>();
private final Map<String, String> scenarioAstNodeIdToFeatureName = new ConcurrentHashMap<>();
private final Map<String, String> stepAstNodeIdToStepKeyWord = new ConcurrentHashMap<>();
private final Map<String, String> pickleAstNodeIdToLongName = new ConcurrentHashMap<>();

void collect(Envelope envelope) {
envelope.getTestRunStarted().ifPresent(event -> this.testRunStarted = timestampToJavaInstant(event.getTimestamp()));
Expand Down Expand Up @@ -91,23 +103,42 @@ private void source(GherkinDocument event) {
featureChild.getRule().ifPresent(rule -> {
rule.getChildren().forEach(ruleChild -> {
ruleChild.getScenario().ifPresent(scenario -> {
scenarioAstNodeIdToFeatureName.put(scenario.getId(), feature.getName());
scenario.getSteps().forEach(step -> {
stepAstNodeIdToStepKeyWord.put(step.getId(), step.getKeyword());
});
scenario(feature, rule, scenario);
});
});
});
featureChild.getScenario().ifPresent(scenario -> {
scenarioAstNodeIdToFeatureName.put(scenario.getId(), feature.getName());
scenario.getSteps().forEach(step -> {
stepAstNodeIdToStepKeyWord.put(step.getId(), step.getKeyword());
});
scenario(feature, null, scenario);
});
});
});
}

private void scenario(Feature feature, Rule rule, Scenario scenario) {
scenarioAstNodeIdToFeatureName.put(scenario.getId(), feature.getName());
scenario.getSteps().forEach(step -> {
stepAstNodeIdToStepKeyWord.put(step.getId(), step.getKeyword());
});

String rulePrefix = rule == null ? "" : rule.getName() + " - ";
pickleAstNodeIdToLongName.put(scenario.getId(), rulePrefix + scenario.getName());

List<Examples> examples = scenario.getExamples();
for (int examplesIndex = 0; examplesIndex < examples.size(); examplesIndex++) {
Examples currentExamples = examples.get(examplesIndex);
List<TableRow> tableRows = currentExamples.getTableBody();
for (int exampleIndex = 0; exampleIndex < tableRows.size(); exampleIndex++) {
TableRow currentExample = tableRows.get(exampleIndex);
StringBuilder suffix = new StringBuilder(" - ");
if (!currentExamples.getName().isEmpty()) {
suffix.append(currentExamples.getName()).append(" - ");
}
suffix.append("Example #").append(examplesIndex + 1).append(".").append(exampleIndex + 1);
pickleAstNodeIdToLongName.put(currentExample.getId(), rulePrefix + scenario.getName() + suffix);
}
}
}

private void pickle(Pickle event) {
pickleIdToPickle.put(event.getId(), event);
// @formatter:off
Expand Down Expand Up @@ -153,7 +184,10 @@ int getTestCaseCount() {
String getPickleName(String testCaseStartedId) {
String testCaseId = testCaseStartedIdToTestCaseId.get(testCaseStartedId);
String pickleId = testCaseIdToTestCase.get(testCaseId).getPickleId();
return pickleIdToPickle.get(pickleId).getName();
Pickle pickle = pickleIdToPickle.get(pickleId);
List<String> astNodeIds = pickle.getAstNodeIds();
String pickleAstNodeId = astNodeIds.get(astNodeIds.size() - 1);
return pickleAstNodeIdToLongName.getOrDefault(pickleAstNodeId, pickle.getName());
}

public String getFeatureName(String testCaseStartedId) {
Expand Down
2 changes: 1 addition & 1 deletion testdata/attachments.feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ When an array with 10 bytes is attached as "text/plain".....................pass
When a JPEG image is attached...............................................passed
]]></system-out>
</testcase>
<testcase classname="Attachments" name="Attaching images in examples" time="0.005"><system-out><![CDATA[
<testcase classname="Attachments" name="Attaching images in examples - Example #1.1" time="0.005"><system-out><![CDATA[
When the cucumber.png png is attached.......................................passed
]]></system-out>
</testcase>
Expand Down
12 changes: 6 additions & 6 deletions testdata/examples-tables.feature.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="Cucumber" time="0.049" tests="6" skipped="0" failures="4" errors="0">
<testcase classname="Examples Tables" name="eating cucumbers" time="0.007"><system-out><![CDATA[
<testcase classname="Examples Tables" name="eating cucumbers - These are passing - Example #1.1" time="0.007"><system-out><![CDATA[
Given there are 12 cucumbers................................................passed
When I eat 5 cucumbers......................................................passed
Then I should have 7 cucumbers..............................................passed
]]></system-out>
</testcase>
<testcase classname="Examples Tables" name="eating cucumbers" time="0.007"><system-out><![CDATA[
<testcase classname="Examples Tables" name="eating cucumbers - These are passing - Example #1.2" time="0.007"><system-out><![CDATA[
Given there are 20 cucumbers................................................passed
When I eat 5 cucumbers......................................................passed
Then I should have 15 cucumbers.............................................passed
]]></system-out>
</testcase>
<testcase classname="Examples Tables" name="eating cucumbers" time="0.007">
<testcase classname="Examples Tables" name="eating cucumbers - These are failing - Example #2.1" time="0.007">
<failure>
<![CDATA[Expected values to be strictly equal:
Expand All @@ -27,7 +27,7 @@ When I eat 20 cucumbers.....................................................pass
Then I should have 0 cucumbers..............................................failed
]]></system-out>
</testcase>
<testcase classname="Examples Tables" name="eating cucumbers" time="0.007">
<testcase classname="Examples Tables" name="eating cucumbers - These are failing - Example #2.2" time="0.007">
<failure>
<![CDATA[Expected values to be strictly equal:
Expand All @@ -42,14 +42,14 @@ When I eat 1 cucumbers......................................................pass
Then I should have 0 cucumbers..............................................failed
]]></system-out>
</testcase>
<testcase classname="Examples Tables" name="eating cucumbers" time="0.007">
<testcase classname="Examples Tables" name="eating cucumbers - These are undefined because the value is not an {int} - Example #3.1" time="0.007">
<failure/><system-out><![CDATA[
Given there are 12 cucumbers................................................passed
When I eat banana cucumbers.................................................undefined
Then I should have 12 cucumbers.............................................skipped
]]></system-out>
</testcase>
<testcase classname="Examples Tables" name="eating cucumbers" time="0.007">
<testcase classname="Examples Tables" name="eating cucumbers - These are undefined because the value is not an {int} - Example #3.2" time="0.007">
<failure/><system-out><![CDATA[
Given there are 0 cucumbers.................................................passed
When I eat 1 cucumbers......................................................passed
Expand Down
4 changes: 2 additions & 2 deletions testdata/markdown.feature.md.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="Cucumber" time="0.021" tests="2" skipped="0" failures="1" errors="0">
<testcase classname="Cheese" name="Ylajali!" time="0.009">
<testcase classname="Cheese" name="Nom nom nom - Ylajali! - because we need more tables - Example #1.1" time="0.009">
<failure>
<![CDATA[You asked me to fail
samples/markdown/markdown.feature.md:24
Expand All @@ -13,7 +13,7 @@ When we use a data table and attach something and then fail.................fail
Then this might or might not run............................................skipped
]]></system-out>
</testcase>
<testcase classname="Cheese" name="Ylajali!" time="0.009"><system-out><![CDATA[
<testcase classname="Cheese" name="Nom nom nom - Ylajali! - because we need more tables - Example #1.2" time="0.009"><system-out><![CDATA[
Given some TypeScript code:.................................................passed
And some classic Gherkin:...................................................passed
When we use a data table and attach something and then pass.................passed
Expand Down
6 changes: 3 additions & 3 deletions testdata/rules.feature.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="Cucumber" time="0.029" tests="3" skipped="0" failures="0" errors="0">
<testcase classname="Rules" name="no change" time="0.007"><system-out><![CDATA[
<testcase classname="Rules" name="a sale cannot happen if change cannot be returned - no change" time="0.007"><system-out><![CDATA[
Given there are 5 0.20 coins inside.........................................passed
When the customer tries to buy a 0.85 chocolate with a 1 coin...............passed
Then the sale should not happen.............................................passed
]]></system-out>
</testcase>
<testcase classname="Rules" name="exact change" time="0.009"><system-out><![CDATA[
<testcase classname="Rules" name="a sale cannot happen if change cannot be returned - exact change" time="0.009"><system-out><![CDATA[
Given there are 5 0.20 coins inside.........................................passed
And there are 3 chocolates inside...........................................passed
When the customer tries to buy a 0.80 chocolate with a 1 coin...............passed
Then the customer's change should be 1 0.20 coin............................passed
]]></system-out>
</testcase>
<testcase classname="Rules" name="no chocolates left" time="0.009"><system-out><![CDATA[
<testcase classname="Rules" name="a sale cannot happen if we're out of stock - no chocolates left" time="0.009"><system-out><![CDATA[
Given there are no chocolates inside........................................passed
But there are 10 0.5 coins inside...........................................passed
When the customer tries to buy a 0.85 chocolate with a 1 coin...............passed
Expand Down

0 comments on commit 44fd048

Please sign in to comment.