Skip to content

Commit 12e0223

Browse files
committed
Scenario Outlines: Replace tokens in the names of the generated "Example Scenario"s.
1 parent 08a6541 commit 12e0223

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

core/src/main/java/cucumber/runtime/model/CucumberScenarioOutline.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public void run(Formatter formatter, Reporter reporter, Runtime runtime) {
4848
}
4949

5050
CucumberScenario createExampleScenario(ExamplesTableRow header, ExamplesTableRow example, List<Tag> examplesTags) {
51-
Scenario exampleScenario = new Scenario(example.getComments(), examplesTags, getGherkinModel().getKeyword(), getGherkinModel().getName(), "", example.getLine(), example.getId());
51+
// Make sure we replace the tokens in the name of the scenario
52+
String exampleScenarioName = replaceTokens(new HashSet<Integer>(), header.getCells(), example.getCells(), getGherkinModel().getName());
53+
54+
Scenario exampleScenario = new Scenario(example.getComments(), examplesTags, getGherkinModel().getKeyword(), exampleScenarioName, "", example.getLine(), example.getId());
5255
CucumberScenario cucumberScenario = new CucumberScenario(cucumberFeature, cucumberBackground, exampleScenario, example);
5356
for (Step step : getSteps()) {
5457
cucumberScenario.step(createExampleStep(step, header, example));

core/src/test/java/cucumber/runtime/model/CucumberScenarioOutlineTest.java

+41-1
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
import gherkin.formatter.model.DataTableRow;
55
import gherkin.formatter.model.DocString;
66
import gherkin.formatter.model.ExamplesTableRow;
7+
import gherkin.formatter.model.ScenarioOutline;
78
import gherkin.formatter.model.Step;
9+
import gherkin.formatter.model.Tag;
810
import org.junit.Test;
911

1012
import java.util.ArrayList;
13+
import java.util.Collections;
1114
import java.util.List;
1215

1316
import static java.util.Arrays.asList;
1417
import static org.junit.Assert.assertEquals;
1518

1619
public class CucumberScenarioOutlineTest {
1720
private static final List<Comment> C = new ArrayList<Comment>();
21+
private static final List<Tag> T = Collections.<Tag>emptyList();
1822

1923
@Test
2024
public void replaces_tokens_in_step_names() {
@@ -38,5 +42,41 @@ public void replaces_tokens_in_data_tables() {
3842

3943
Step exampleStep = CucumberScenarioOutline.createExampleStep(outlineStep, new ExamplesTableRow(C, asList("n"), 1, ""), new ExamplesTableRow(C, asList("10"), 1, ""));
4044
assertEquals(asList("I", "have 10 cukes"), exampleStep.getRows().get(0).getCells());
41-
}
45+
}
46+
47+
/***
48+
* From a scenario outline, we create one or more "Example Scenario"s. This is composed
49+
* of each step from the outline, with the tokens replaced with the pertient values
50+
* for the current example row. <p />
51+
*
52+
* Each "Example Scenario" has a name. This was previously just a copy of the outline's
53+
* name. However, we'd like to be able to support token replacement in the scenario too,
54+
* for example:
55+
*
56+
* <pre>
57+
* Scenario Outline: Time offset check for <LOCATION_NAME>
58+
* Given my local country is <LOCATION_NAME>
59+
* When I compare the time difference to GMT
60+
* Then the time offset should be <OFFSET>
61+
*
62+
* Examples:
63+
* | LOCATION_NAME | OFFSET |
64+
* | London | 1 |
65+
* | San Fran | 8 |
66+
* </pre>
67+
*
68+
* Will create a scenario named "Time offset check for London" for the first row in the
69+
* examples table.
70+
*/
71+
@Test
72+
public void replaces_tokens_in_scenario_names() {
73+
// Create Gherkin the outline itself ...
74+
ScenarioOutline outline = new ScenarioOutline(C, T,"Scenario Outline", "Time offset check for <LOCATION_NAME>", "", new Integer(1), "");
75+
76+
// ... then the Cukes implementation
77+
CucumberScenarioOutline cukeOutline = new CucumberScenarioOutline(null, null, outline);
78+
CucumberScenario exampleScenario = cukeOutline.createExampleScenario(new ExamplesTableRow(C, asList("LOCATION_NAME"), 1, ""), new ExamplesTableRow(C, asList("London"), 1, ""), T);
79+
80+
assertEquals("Time offset check for London", exampleScenario.getGherkinModel().getName());
81+
}
4282
}

0 commit comments

Comments
 (0)