|
| 1 | +package io.cucumber.core.gherkin.messages; |
| 2 | + |
| 3 | +import io.cucumber.messages.Messages.GherkinDocument; |
| 4 | +import io.cucumber.messages.Messages.GherkinDocument.Feature.FeatureChild; |
| 5 | +import io.cucumber.messages.Messages.GherkinDocument.Feature.FeatureChild.RuleChild; |
| 6 | +import io.cucumber.messages.Messages.GherkinDocument.Feature.Scenario; |
| 7 | +import io.cucumber.messages.Messages.GherkinDocument.Feature.Scenario.Examples; |
| 8 | +import io.cucumber.messages.Messages.GherkinDocument.Feature.Step; |
| 9 | +import io.cucumber.messages.Messages.GherkinDocument.Feature.TableRow; |
| 10 | +import io.cucumber.messages.Messages.Location; |
| 11 | + |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import static java.util.Objects.requireNonNull; |
| 17 | + |
| 18 | +final class CucumberQuery { |
| 19 | + private final Map<String, Step> gherkinStepById = new HashMap<>(); |
| 20 | + private final Map<String, Scenario> gherkinScenarioById = new HashMap<>(); |
| 21 | + private final Map<String, Location> locationBySourceId = new HashMap<>(); |
| 22 | + |
| 23 | + void update(GherkinDocument gherkinDocument) { |
| 24 | + for (FeatureChild featureChild : gherkinDocument.getFeature().getChildrenList()) { |
| 25 | + if (featureChild.hasBackground()) { |
| 26 | + this.updateBackground( |
| 27 | + featureChild.getBackground(), |
| 28 | + gherkinDocument.getUri() |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + if (featureChild.hasScenario()) { |
| 33 | + this.updateScenario( |
| 34 | + featureChild.getScenario(), |
| 35 | + gherkinDocument.getUri() |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + if (featureChild.hasRule()) { |
| 40 | + for (RuleChild ruleChild : featureChild.getRule().getChildrenList()) { |
| 41 | + if (ruleChild.hasBackground()) { |
| 42 | + this.updateBackground( |
| 43 | + ruleChild.getBackground(), |
| 44 | + gherkinDocument.getUri() |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + if (ruleChild.hasScenario()) { |
| 49 | + this.updateScenario( |
| 50 | + ruleChild.getScenario(), |
| 51 | + gherkinDocument.getUri() |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private void updateScenario(Scenario scenario, String uri) { |
| 60 | + gherkinScenarioById.put(requireNonNull(scenario.getId()), scenario); |
| 61 | + locationBySourceId.put(requireNonNull(scenario.getId()), scenario.getLocation()); |
| 62 | + updateStep(scenario.getStepsList()); |
| 63 | + |
| 64 | + for (Examples examples: scenario.getExamplesList()) { |
| 65 | + for (TableRow tableRow: examples.getTableBodyList()) { |
| 66 | + this.locationBySourceId.put(requireNonNull(tableRow.getId()), tableRow.getLocation()); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private void updateBackground(GherkinDocument.Feature.Background background, String uri) { |
| 72 | + updateStep(background.getStepsList()); |
| 73 | + } |
| 74 | + |
| 75 | + private void updateStep(List<Step> stepsList) { |
| 76 | + for (Step step : stepsList) { |
| 77 | + locationBySourceId.put(requireNonNull(step.getId()), step.getLocation()); |
| 78 | + gherkinStepById.put(requireNonNull(step.getId()), step); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + Step getGherkinStep(String sourceId) { |
| 83 | + return requireNonNull(gherkinStepById.get(requireNonNull(sourceId))); |
| 84 | + } |
| 85 | + |
| 86 | + Scenario getGherkinScenario(String sourceId) { |
| 87 | + return requireNonNull(gherkinScenarioById.get(requireNonNull(sourceId))); |
| 88 | + } |
| 89 | + |
| 90 | + Location getLocation(String sourceId) { |
| 91 | + Location location = locationBySourceId.get(requireNonNull(sourceId)); |
| 92 | + return requireNonNull(location); |
| 93 | + } |
| 94 | +} |
0 commit comments