Skip to content

Commit

Permalink
Merge pull request #45 from daczczcz1/master
Browse files Browse the repository at this point in the history
Added method to substitute placeholders in ScenarioOutline name
  • Loading branch information
Benjamin Bischoff authored Feb 9, 2018
2 parents f5e0a00 + 70863cc commit 37a6ad2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/main/java/com/trivago/rta/gherkin/GherkinDocumentParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Singleton
public class GherkinDocumentParser {
Expand Down Expand Up @@ -186,7 +188,7 @@ private List<SingleScenario> getSingleScenariosFromOutline(
new SingleScenario(
featureName,
featureDescription,
scenarioName,
substituteScenarioNameExamplePlaceholders(scenarioName, exampleMap, i),
scenarioDescription,
featureTags,
backgroundSteps
Expand Down Expand Up @@ -353,5 +355,31 @@ private boolean scenarioShouldBeIncluded(
return result;
}
}

/**
* Replaces the example value placeholders in ScenarioOutline name by the actual example table values.
*
* @param scenarioOutlineName The ScenarioOutline generic name.
* @param exampleMap The generated example map from an example table.
* @param rowIndex The row index of the example table to consider.
* @return a {@link String} name with placeholders substituted for actual values from example table.
*/

private String substituteScenarioNameExamplePlaceholders(
final String scenarioOutlineName,
final Map<String, List<String>> exampleMap,
final int rowIndex) {
String result = scenarioOutlineName;
String placeholderPattern = "<.+?>";
Pattern p = Pattern.compile(placeholderPattern);
Matcher m = p.matcher(scenarioOutlineName);

while (m.find()) {
String currentPlaceholder = m.group(0);
result = result.replace(currentPlaceholder, exampleMap.get(currentPlaceholder).get(rowIndex));
}

return result;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,32 @@ public void validFeatureWithScenarioOutlineTest() throws Exception {
assertThat(scenario.getSteps().get(1).getName(), is("Then I see the value 'two'"));
}

@Test
public void validScenarioNamesWithScenarioOutlineTest() throws Exception {
String featureContent = "Feature: test feature 3\n" +
"\n" +
" Scenario Outline: This is a scenario outline, key = <key>, value = <value>\n" +
" This is a step\n" +
" How about another step\n" +
"\n" +
" Examples:\n" +
" | key | value |\n" +
" | 1 | one |\n" +
" | 2 | two |";

List<SingleScenario> singleScenariosFromFeature = gherkinDocumentParser.getSingleScenariosFromFeature(featureContent, null, null, null);
assertThat(singleScenariosFromFeature.size(), is(2));

SingleScenario scenario = singleScenariosFromFeature.get(0);

assertThat(scenario.getScenarioName(), is("This is a scenario outline, key = 1, value = one"));

scenario = singleScenariosFromFeature.get(1);

assertThat(scenario.getScenarioName(), is("This is a scenario outline, key = 2, value = two"));
}


@Test
public void replaceDataTableExamplePlaceholderTest() throws Exception {
String featureContent = "Feature: test feature 3\n" +
Expand Down

0 comments on commit 37a6ad2

Please sign in to comment.