1515 */
1616package io .qameta .allure .cucumber7jvm .testsourcemodel ;
1717
18- import io .cucumber .gherkin .Gherkin ;
18+ import io .cucumber .gherkin .GherkinParser ;
1919import io .cucumber .messages .types .Background ;
2020import io .cucumber .messages .types .Envelope ;
2121import io .cucumber .messages .types .Examples ;
2424import io .cucumber .messages .types .GherkinDocument ;
2525import io .cucumber .messages .types .RuleChild ;
2626import io .cucumber .messages .types .Scenario ;
27+ import io .cucumber .messages .types .Source ;
28+ import io .cucumber .messages .types .SourceMediaType ;
2729import io .cucumber .messages .types .Step ;
2830import io .cucumber .messages .types .TableRow ;
2931import io .cucumber .plugin .event .TestSourceRead ;
3032
3133import java .net .URI ;
3234import java .util .HashMap ;
33- import java .util .List ;
3435import java .util .Map ;
35- import java .util .Objects ;
36- import java .util .UUID ;
37-
38- import static io .cucumber .gherkin .Gherkin .makeSourceEnvelope ;
39- import static java .util .Collections .singletonList ;
40- import static java .util .stream .Collectors .toList ;
36+ import java .util .Optional ;
37+ import java .util .stream .Stream ;
4138
4239final class TestSourcesModel {
4340 private final Map <URI , TestSourceRead > pathToReadEventMap = new HashMap <>();
@@ -61,7 +58,7 @@ public Feature getFeature(final URI path) {
6158 parseGherkinSource (path );
6259 }
6360 if (pathToAstMap .containsKey (path )) {
64- return pathToAstMap .get (path ).getFeature ();
61+ return pathToAstMap .get (path ).getFeature (). orElse ( null ) ;
6562 }
6663 return null ;
6764 }
@@ -72,26 +69,27 @@ private void parseGherkinSource(final URI path) {
7269 }
7370 final String source = pathToReadEventMap .get (path ).getSource ();
7471
75- final List < Envelope > sources = singletonList (
76- makeSourceEnvelope ( source , path . toString ()) );
72+ final GherkinParser parser = GherkinParser . builder ()
73+ . build ( );
7774
78- final List <Envelope > envelopes = Gherkin .fromSources (
79- sources ,
80- true ,
81- true ,
82- true ,
83- () -> String .valueOf (UUID .randomUUID ())).collect (toList ());
75+ final Stream <Envelope > envelopes = parser .parse (
76+ Envelope .of (new Source (path .toString (), source , SourceMediaType .TEXT_X_CUCUMBER_GHERKIN_PLAIN )));
8477
85- final GherkinDocument gherkinDocument = envelopes .stream ()
78+ // TODO: What about empty gherkin docs?
79+ final GherkinDocument gherkinDocument = envelopes
8680 .map (Envelope ::getGherkinDocument )
87- .filter (Objects ::nonNull )
81+ .filter (Optional ::isPresent )
82+ .map (Optional ::get )
8883 .findFirst ()
8984 .orElse (null );
9085
9186 pathToAstMap .put (path , gherkinDocument );
9287 final Map <Long , AstNode > nodeMap = new HashMap <>();
93- final AstNode currentParent = createAstNode (Objects .requireNonNull (gherkinDocument ).getFeature (), null );
94- for (FeatureChild child : gherkinDocument .getFeature ().getChildren ()) {
88+
89+ // TODO: What about gherkin docs with no features?
90+ final Feature feature = gherkinDocument .getFeature ().get ();
91+ final AstNode currentParent = new AstNode (feature , null );
92+ for (FeatureChild child : feature .getChildren ()) {
9593 processFeatureDefinition (nodeMap , child , currentParent );
9694 }
9795 pathToNodeMap .put (path , nodeMap );
@@ -100,17 +98,13 @@ private void parseGherkinSource(final URI path) {
10098
10199 private void processFeatureDefinition (
102100 final Map <Long , AstNode > nodeMap , final FeatureChild child , final AstNode currentParent ) {
103- if (child .getBackground () != null ) {
104- processBackgroundDefinition (nodeMap , child .getBackground (), currentParent );
105- } else if (child .getScenario () != null ) {
106- processScenarioDefinition (nodeMap , child .getScenario (), currentParent );
107- } else if (child .getRule () != null ) {
108- final AstNode childNode = createAstNode (child .getRule (), currentParent );
109- nodeMap .put (child .getRule ().getLocation ().getLine (), childNode );
110- for (RuleChild ruleChild : child .getRule ().getChildren ()) {
111- processRuleDefinition (nodeMap , ruleChild , childNode );
112- }
113- }
101+ child .getBackground ().ifPresent (background -> processBackgroundDefinition (nodeMap , background , currentParent ));
102+ child .getScenario ().ifPresent (scenario -> processScenarioDefinition (nodeMap , scenario , currentParent ));
103+ child .getRule ().ifPresent (rule -> {
104+ final AstNode childNode = new AstNode (rule , currentParent );
105+ nodeMap .put (rule .getLocation ().getLine (), childNode );
106+ rule .getChildren ().forEach (ruleChild -> processRuleDefinition (nodeMap , ruleChild , childNode ));
107+ });
114108 }
115109
116110 private void processBackgroundDefinition (
@@ -137,19 +131,17 @@ private void processScenarioDefinition(
137131
138132 private void processRuleDefinition (
139133 final Map <Long , AstNode > nodeMap , final RuleChild child , final AstNode currentParent ) {
140- if (child .getBackground () != null ) {
141- processBackgroundDefinition (nodeMap , child .getBackground (), currentParent );
142- } else if (child .getScenario () != null ) {
143- processScenarioDefinition (nodeMap , child .getScenario (), currentParent );
144- }
134+ child .getBackground ().ifPresent (background -> processBackgroundDefinition (nodeMap , background , currentParent ));
135+ child .getScenario ().ifPresent (scenario -> processScenarioDefinition (nodeMap , scenario , currentParent ));
145136 }
146137
147138 private void processScenarioOutlineExamples (
148139 final Map <Long , AstNode > nodeMap , final Scenario scenarioOutline , final AstNode parent
149140 ) {
150141 for (Examples examples : scenarioOutline .getExamples ()) {
151142 final AstNode examplesNode = createAstNode (examples , parent );
152- final TableRow headerRow = examples .getTableHeader ();
143+ // TODO: Can tables without headers even exist?
144+ final TableRow headerRow = examples .getTableHeader ().get ();
153145 final AstNode headerNode = createAstNode (headerRow , examplesNode );
154146 nodeMap .put (headerRow .getLocation ().getLine (), headerNode );
155147 for (int i = 0 ; i < examples .getTableBody ().size (); ++i ) {
0 commit comments