-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0906b3
commit d23d5d5
Showing
10 changed files
with
518 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.cucumber.query; | ||
|
||
import io.cucumber.messages.types.Examples; | ||
import io.cucumber.messages.types.Feature; | ||
import io.cucumber.messages.types.GherkinDocument; | ||
import io.cucumber.messages.types.Pickle; | ||
import io.cucumber.messages.types.Rule; | ||
import io.cucumber.messages.types.Scenario; | ||
import io.cucumber.messages.types.TableRow; | ||
|
||
interface LineageReducer<T> { | ||
default void add(GherkinDocument document) { | ||
|
||
} | ||
default void add(Feature feature) { | ||
|
||
} | ||
|
||
default void add(Rule rule) { | ||
|
||
} | ||
|
||
default void add(Scenario scenario) { | ||
|
||
} | ||
|
||
default void add(Examples examples, int index) { | ||
} | ||
|
||
default void add(TableRow example, int index) { | ||
} | ||
|
||
default void add(Pickle pickle) { | ||
} | ||
|
||
T finish(); | ||
} |
51 changes: 51 additions & 0 deletions
51
java/src/main/java/io/cucumber/query/LineageReducerStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.cucumber.query; | ||
|
||
import io.cucumber.messages.types.Pickle; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* TODO: Explain how this solves the lack of an AST that has nodes. | ||
* | ||
* @param <T> | ||
*/ | ||
interface LineageReducerStrategy<T> { | ||
|
||
T reduce(Lineage lineage); | ||
|
||
T reduce(Lineage lineage, Pickle pickle); | ||
|
||
class Descending<T> implements LineageReducerStrategy<T> { | ||
|
||
private final Supplier<LineageReducer<T>> reducerSupplier; | ||
|
||
Descending(Supplier<LineageReducer<T>> reducerSupplier) { | ||
this.reducerSupplier = requireNonNull(reducerSupplier); | ||
} | ||
|
||
@Override | ||
public T reduce(Lineage lineage) { | ||
LineageReducer<T> reducer = reducerSupplier.get(); | ||
reduceAddLineage(reducer, lineage); | ||
return reducer.finish(); | ||
} | ||
|
||
@Override | ||
public T reduce(Lineage lineage, Pickle pickle) { | ||
LineageReducer<T> reducer = reducerSupplier.get(); | ||
reduceAddLineage(reducer, lineage); | ||
reducer.add(pickle); | ||
return reducer.finish(); | ||
} | ||
|
||
private static <T> void reduceAddLineage(LineageReducer<T> reducer, Lineage lineage) { | ||
lineage.feature().ifPresent(reducer::add); | ||
lineage.rule().ifPresent(reducer::add); | ||
lineage.scenario().ifPresent(reducer::add); | ||
lineage.examples().ifPresent(examples -> reducer.add(examples, lineage.examplesIndex().orElse(0))); | ||
lineage.example().ifPresent(example -> reducer.add(example, lineage.exampleIndex().orElse(0))); | ||
} | ||
} | ||
} |
100 changes: 0 additions & 100 deletions
100
java/src/main/java/io/cucumber/query/LineageVisitingStrategy.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package io.cucumber.query; | ||
|
||
import io.cucumber.messages.types.Examples; | ||
import io.cucumber.messages.types.Feature; | ||
import io.cucumber.messages.types.FeatureChild; | ||
import io.cucumber.messages.types.GherkinDocument; | ||
import io.cucumber.messages.types.Rule; | ||
import io.cucumber.messages.types.RuleChild; | ||
import io.cucumber.messages.types.Scenario; | ||
import io.cucumber.messages.types.TableRow; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Consumer; | ||
|
||
class Lineages { | ||
|
||
static Map<Object, Lineage> of(GherkinDocument document) { | ||
Map<Object, Lineage> elements = new HashMap<>(); | ||
Lineage lineage = new Lineage(document); | ||
elements.put(document, lineage); | ||
document.getFeature().ifPresent(ofFeature(lineage, elements)); | ||
return elements; | ||
} | ||
|
||
private static Consumer<Feature> ofFeature(Lineage parent, Map<Object, Lineage> elements) { | ||
return feature -> { | ||
Lineage lineage = new Lineage(parent, feature); | ||
feature.getChildren().forEach(ofFeatureChild(lineage, elements)); | ||
}; | ||
} | ||
|
||
private static Consumer<FeatureChild> ofFeatureChild(Lineage parent, Map<Object, Lineage> elements) { | ||
return featureChild -> { | ||
featureChild.getScenario().ifPresent(ofScenario(parent, elements)); | ||
featureChild.getRule().ifPresent(ofRule(parent, elements)); | ||
}; | ||
} | ||
|
||
private static Consumer<Rule> ofRule(Lineage parent, Map<Object, Lineage> elements) { | ||
return rule -> { | ||
Lineage lineage = new Lineage(parent, rule); | ||
elements.put(rule.getId(), lineage); | ||
rule.getChildren().forEach(ofRuleChild(lineage, elements)); | ||
}; | ||
} | ||
|
||
private static Consumer<RuleChild> ofRuleChild(Lineage parent, Map<Object, Lineage> elements) { | ||
return ruleChild -> ruleChild.getScenario().ifPresent(ofScenario(parent, elements)); | ||
} | ||
|
||
private static Consumer<Scenario> ofScenario(Lineage parent, Map<Object, Lineage> elements) { | ||
return scenario -> { | ||
Lineage lineage = new Lineage(parent, scenario); | ||
elements.put(scenario.getId(), lineage); | ||
forEachIndexed(scenario.getExamples(), ofExamples(lineage, elements)); | ||
}; | ||
} | ||
|
||
private static BiConsumer<Examples, Integer> ofExamples(Lineage parent, Map<Object, Lineage> elements) { | ||
return (examples, examplesIndex) -> { | ||
Lineage lineage = new Lineage(parent, examples, examplesIndex); | ||
elements.put(examples.getId(), lineage); | ||
forEachIndexed(examples.getTableBody(), ofExample(lineage, elements)); | ||
}; | ||
} | ||
|
||
private static BiConsumer<TableRow, Integer> ofExample(Lineage parent, Map<Object, Lineage> elements) { | ||
return (example, exampleIndex) -> { | ||
Lineage lineage = new Lineage(parent, example, exampleIndex); | ||
elements.put(example.getId(), lineage); | ||
}; | ||
} | ||
|
||
private static <T> void forEachIndexed(List<T> items, BiConsumer<T, Integer> consumer) { | ||
for (int i = 0; i < items.size(); i++) { | ||
consumer.accept(items.get(i), i); | ||
} | ||
} | ||
} |
Oops, something went wrong.