Skip to content

Commit ec46132

Browse files
authored
[Core] Fix incomplete id for scenarios under rules in json output (#3026)
When rules were added, the TestSourcesModel was not updated to account for these. As a result, the id for a given scenario was incomplete. Also improved performance a tiny bit by reusing a pre-build regular expression.
1 parent 68e9256 commit ec46132

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
### Fixed
1717
- [JUnit Platform Engine] Don't use Java 9+ APIs ([#3025](https://github.com/cucumber/cucumber-jvm/pull/3025) M.P. Korstanje)
1818
- [JUnit Platform Engine] Implement toString on custom DiscoverySelectors
19+
[Core] Fix incomplete id for scenarios under rules in json output ([#3026](https://github.com/cucumber/cucumber-jvm/pull/3026) M.P. Korstanje)
1920

2021
## [7.25.0] - 2025-07-10
2122
### Changed

cucumber-core/src/main/java/io/cucumber/core/plugin/TestSourcesModel.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.cucumber.messages.types.Feature;
88
import io.cucumber.messages.types.FeatureChild;
99
import io.cucumber.messages.types.GherkinDocument;
10+
import io.cucumber.messages.types.Rule;
1011
import io.cucumber.messages.types.RuleChild;
1112
import io.cucumber.messages.types.Scenario;
1213
import io.cucumber.messages.types.Source;
@@ -21,6 +22,7 @@
2122
import java.util.HashMap;
2223
import java.util.Map;
2324
import java.util.Optional;
25+
import java.util.regex.Pattern;
2426
import java.util.stream.Stream;
2527

2628
final class TestSourcesModel {
@@ -43,6 +45,9 @@ static boolean isBackgroundStep(AstNode astNode) {
4345

4446
static String calculateId(AstNode astNode) {
4547
Object node = astNode.node;
48+
if (node instanceof Rule) {
49+
return calculateId(astNode.parent) + ";" + convertToId(((Rule) node).getName());
50+
}
4651
if (node instanceof Scenario) {
4752
return calculateId(astNode.parent) + ";" + convertToId(((Scenario) node).getName());
4853
}
@@ -61,8 +66,10 @@ static String calculateId(AstNode astNode) {
6166
return "";
6267
}
6368

69+
private static final Pattern replacementPattern = Pattern.compile("[\\s'_,!]");
70+
6471
static String convertToId(String name) {
65-
return name.replaceAll("[\\s'_,!]", "-").toLowerCase();
72+
return replacementPattern.matcher(name).replaceAll("-").toLowerCase();
6673
}
6774

6875
static URI relativize(URI uri) {

cucumber-core/src/test/java/io/cucumber/core/plugin/JsonFormatterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ void should_format_scenario_with_a_rule() throws JSONException {
305305
" \"line\": 4,\n" +
306306
" \"name\": \"Monkey eats bananas\",\n" +
307307
" \"description\": \"\",\n" +
308-
" \"id\": \";monkey-eats-bananas\",\n" +
308+
" \"id\": \"banana-party;this-is-all-monkey-business;monkey-eats-bananas\",\n" +
309309
" \"type\": \"scenario\",\n" +
310310
" \"keyword\": \"Scenario\",\n" +
311311
" \"steps\": [\n" +
@@ -405,7 +405,7 @@ void should_format_scenario_with_a_rule_and_background() throws JSONException {
405405
" \"line\": 11,\n" +
406406
" \"name\": \"Monkey eats bananas\",\n" +
407407
" \"description\": \"\",\n" +
408-
" \"id\": \";monkey-eats-bananas\",\n" +
408+
" \"id\": \"banana-party;this-is-all-monkey-business;monkey-eats-bananas\",\n" +
409409
" \"type\": \"scenario\",\n" +
410410
" \"keyword\": \"Scenario\",\n" +
411411
" \"steps\": [\n" +

0 commit comments

Comments
 (0)