Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Jun 21, 2024
1 parent b53ba40 commit 58aad0b
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 112 deletions.
7 changes: 7 additions & 0 deletions java/src/main/java/io/cucumber/query/Lineage.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

Expand All @@ -17,6 +18,8 @@
* <p>
* This works without any ordering because Gherkins document
* structure is simple enough to hard code.
*
* @see LineageCollector
*/
class Lineage {

Expand Down Expand Up @@ -96,6 +99,10 @@ Optional<Integer> exampleIndex() {
return Optional.ofNullable(exampleIndex);
}

<T> LineageReducer reduce(Supplier<LineageCollector<T>> collector) {
return new LineageReducerDescending(collector);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
49 changes: 49 additions & 0 deletions java/src/main/java/io/cucumber/query/LineageCollector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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;

/**
* Visit the members of {@link Lineage} of a gherkin document element or
* {@link Pickle} and collect these into a single result.
* <p>
* Because we are using messages we can not express the hierarchy of elements in
* a {@link GherkinDocument} programmatically as a tree of nodes. But we can
* still express the operations that would be typically done with a tree as
* operations on that elements lineage.
*
* @param <T> the type reduced to.
*/
interface LineageCollector<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();
}
37 changes: 12 additions & 25 deletions java/src/main/java/io/cucumber/query/LineageReducer.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
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) {
import java.util.function.Supplier;

}

default void add(Scenario scenario) {
import static java.util.Objects.requireNonNull;

}
/**
* S
*
* @param <T>
*/
interface LineageReducer<T> {

default void add(Examples examples, int index) {
static <T> LineageReducer<T> descending(Supplier<? extends LineageCollector<T>> collector){
return new LineageReducerDescending<>(collector);
}

default void add(TableRow example, int index) {
}
T reduce(Lineage lineage);

default void add(Pickle pickle) {
}
T reduce(Lineage lineage, Pickle pickle);

T finish();
}
45 changes: 45 additions & 0 deletions java/src/main/java/io/cucumber/query/LineageReducerDescending.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.cucumber.query;

import io.cucumber.messages.types.Pickle;

import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

/**
* Reduces the lineage of a Gherkin document element in descending order.
*
* @param <T> type to which the lineage is reduced.
*/
class LineageReducerDescending<T> implements LineageReducer<T> {

private final Supplier<? extends LineageCollector<T>> reducerSupplier;

LineageReducerDescending(Supplier<? extends LineageCollector<T>> reducerSupplier) {
this.reducerSupplier = requireNonNull(reducerSupplier);
}

@Override
public T reduce(Lineage lineage) {
LineageCollector<T> reducer = reducerSupplier.get();
reduceAddLineage(reducer, lineage);
return reducer.finish();
}

@Override
public T reduce(Lineage lineage, Pickle pickle) {
LineageCollector<T> reducer = reducerSupplier.get();
reduceAddLineage(reducer, lineage);
reducer.add(pickle);
return reducer.finish();
}

private static <T> void reduceAddLineage(LineageCollector<T> reducer, Lineage lineage) {
reducer.add(lineage.document());
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)));
}
}
51 changes: 0 additions & 51 deletions java/src/main/java/io/cucumber/query/LineageReducerStrategy.java

This file was deleted.

30 changes: 20 additions & 10 deletions java/src/main/java/io/cucumber/query/Lineages.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,72 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;

class Lineages {

static Map<Object, Lineage> of(GherkinDocument document) {
Map<Object, Lineage> elements = new HashMap<>();
/**
* Create map of a {@link GherkinDocument} element to its {@link Lineage} in that document.
* <p>
* @param document to create the lineage of
* @return a map of the document elements to their lineage.
*/
static Map<String, Lineage> of(GherkinDocument document) {
Map<String, Lineage> elements = new HashMap<>();
Lineage lineage = new Lineage(document);
elements.put(document, lineage);
String uri = document.getUri()
.orElseThrow(() -> new IllegalArgumentException("document.uri must not be null"));
elements.put(uri, lineage);
document.getFeature().ifPresent(ofFeature(lineage, elements));
return elements;
}

private static Consumer<Feature> ofFeature(Lineage parent, Map<Object, Lineage> elements) {
private static Consumer<Feature> ofFeature(Lineage parent, Map<String, 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) {
private static Consumer<FeatureChild> ofFeatureChild(Lineage parent, Map<String, 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) {
private static Consumer<Rule> ofRule(Lineage parent, Map<String, 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) {
private static Consumer<RuleChild> ofRuleChild(Lineage parent, Map<String, Lineage> elements) {
return ruleChild -> ruleChild.getScenario().ifPresent(ofScenario(parent, elements));
}

private static Consumer<Scenario> ofScenario(Lineage parent, Map<Object, Lineage> elements) {
private static Consumer<Scenario> ofScenario(Lineage parent, Map<String, 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) {
private static BiConsumer<Examples, Integer> ofExamples(Lineage parent, Map<String, 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) {
private static BiConsumer<TableRow, Integer> ofExample(Lineage parent, Map<String, Lineage> elements) {
return (example, exampleIndex) -> {
Lineage lineage = new Lineage(parent, example, exampleIndex);
elements.put(example.getId(), lineage);
Expand Down
9 changes: 7 additions & 2 deletions java/src/main/java/io/cucumber/query/NamingReducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static io.cucumber.query.NamingStrategy.FeatureName.INCLUDE;
import static io.cucumber.query.NamingStrategy.Strategy.SHORT;

class NamingReducer implements LineageReducer<String> {
class NamingReducer implements LineageCollector<String> {

private final Deque<String> parts = new ArrayDeque<>();
private final CharSequence delimiter = " - ";
Expand All @@ -29,7 +30,11 @@ class NamingReducer implements LineageReducer<String> {
private boolean isExample;
private int examplesIndex;

NamingReducer(Strategy strategy, FeatureName featureName, ExampleName exampleName) {
static Supplier<NamingReducer> of(Strategy strategy, FeatureName featureName, ExampleName exampleName) {
return () -> new NamingReducer(strategy, featureName, exampleName);
}

private NamingReducer(Strategy strategy, FeatureName featureName, ExampleName exampleName) {
this.strategy = strategy;
this.featureName = featureName;
this.exampleName = exampleName;
Expand Down
11 changes: 5 additions & 6 deletions java/src/main/java/io/cucumber/query/NamingStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* <li>Eating 1 cucumbers
* </ul>
*/
public abstract class NamingStrategy implements LineageReducerStrategy<String> {
public abstract class NamingStrategy implements LineageReducer<String> {

public enum Strategy {
/**
Expand Down Expand Up @@ -134,16 +134,15 @@ public Builder featureName(FeatureName featureName) {
}

public NamingStrategy build() {
Supplier<LineageReducer<String>> reducer = () -> new NamingReducer(strategy, featureName, exampleName);
LineageReducerStrategy<String> strategy = new Descending<>(reducer);
return new Adaptor(strategy);
LineageReducer<String> reducer = LineageReducer.descending(NamingReducer.of(strategy, featureName, exampleName));
return new Adaptor(reducer);
}
}

private static class Adaptor extends NamingStrategy {
private final LineageReducerStrategy<String> delegate;
private final LineageReducer<String> delegate;

Adaptor(LineageReducerStrategy<String> delegate) {
Adaptor(LineageReducer<String> delegate) {
this.delegate = delegate;
}

Expand Down
Loading

0 comments on commit 58aad0b

Please sign in to comment.