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 58aad0b commit d78f43e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 65 deletions.
8 changes: 4 additions & 4 deletions java/src/main/java/io/cucumber/query/Lineage.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import static java.util.Objects.requireNonNull;

/**
* A structure containing all ancestors of a given element.
* A structure containing all ancestors of a given
* {@linkplain GherkinDocument GherkinDocument element} or
* {@link io.cucumber.messages.types.Pickle}.
* <p>
* This works without any ordering because Gherkins document
* structure is simple enough to hard code.
*
* @see LineageCollector
* @see LineageReducer
*/
class Lineage {

Expand Down
10 changes: 3 additions & 7 deletions java/src/main/java/io/cucumber/query/LineageCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
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.
* Collect the {@link Lineage} of a
* {@linkplain io.cucumber.messages.types.GherkinDocument GherkinDocument element}
* or {@link Pickle} and reduce it to a single result.
*
* @param <T> the type reduced to.
*/
Expand Down
12 changes: 9 additions & 3 deletions java/src/main/java/io/cucumber/query/LineageReducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
import static java.util.Objects.requireNonNull;

/**
* S
* Visit the {@link Lineage} of a {@linkplain io.cucumber.messages.types.GherkinDocument GherkinDocument element}
* or {@link Pickle} and reduce it.
* <p>
* Because we are using messages we can not express the hierarchy of elements in
* a {@link io.cucumber.messages.types.GherkinDocument} programmatically as a
* tree of nodes. But we can still express the operations that would be typically
* done this way as an operation on the lineage of those messages.
*
* @param <T>
* @param <T> the type reduced to.
*/
interface LineageReducer<T> {

static <T> LineageReducer<T> descending(Supplier<? extends LineageCollector<T>> collector){
static <T> LineageReducer<T> descending(Supplier<? extends LineageCollector<T>> collector) {
return new LineageReducerDescending<>(collector);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static io.cucumber.query.NamingStrategy.FeatureName.INCLUDE;
import static io.cucumber.query.NamingStrategy.Strategy.SHORT;

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

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

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

private NamingReducer(Strategy strategy, FeatureName featureName, ExampleName exampleName) {
private NamingCollector(Strategy strategy, FeatureName featureName, ExampleName exampleName) {
this.strategy = strategy;
this.featureName = featureName;
this.exampleName = exampleName;
Expand Down
7 changes: 3 additions & 4 deletions java/src/main/java/io/cucumber/query/NamingStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import io.cucumber.messages.types.GherkinDocument;
import io.cucumber.messages.types.Pickle;

import java.util.function.Supplier;

import static io.cucumber.query.LineageReducer.descending;
import static io.cucumber.query.NamingCollector.of;
import static io.cucumber.query.NamingStrategy.ExampleName.NUMBER_AND_PICKLE_IF_PARAMETERIZED;
import static io.cucumber.query.NamingStrategy.FeatureName.INCLUDE;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -134,8 +134,7 @@ public Builder featureName(FeatureName featureName) {
}

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

Expand Down
71 changes: 28 additions & 43 deletions java/src/main/java/io/cucumber/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,117 +147,102 @@ public Optional<TestStepResult> findMostSevereTestStepResulBy(TestCaseStarted te
public String findNameOf(GherkinDocument element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
return findLineageBy(element)
.map(namingStrategy::reduce)
return reduceLinageOf(element, namingStrategy)
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

public String findNameOf(Feature element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
return findLineageBy(element)
.map(namingStrategy::reduce)
return reduceLinageOf(element, namingStrategy)
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

public String findNameOf(Rule element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
return findLineageBy(element)
.map(namingStrategy::reduce)
return reduceLinageOf(element, namingStrategy)
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

public String findNameOf(Scenario element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
return findLineageBy(element)
.map(namingStrategy::reduce)
return reduceLinageOf(element, namingStrategy)
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

public String findNameOf(Examples element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
return findLineageBy(element)
.map(namingStrategy::reduce)
return reduceLinageOf(element, namingStrategy)
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

public String findNameOf(TableRow element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
return findLineageBy(element)
.map(namingStrategy::reduce)
return reduceLinageOf(element, namingStrategy)
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

public Optional<String> findNameOf(Pickle element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
return findLineageBy(element)
.map(lineage -> namingStrategy.reduce(lineage, element));
return reduceLinageOf(element, namingStrategy);
}

private static Supplier<IllegalArgumentException> createElementWasNotPartOfThisQueryObject() {
return () -> new IllegalArgumentException("Element was not part of this query object");
}

<T> Optional<T> reduceLinageOf(GherkinDocument element, Supplier<LineageCollector<T>> reducerSupplier) {
<T> Optional<T> reduceLinageOf(GherkinDocument element, LineageReducer<T> reducer) {
requireNonNull(element);
requireNonNull(reducerSupplier);
LineageReducerDescending<T> strategy = new LineageReducerDescending<>(reducerSupplier);
requireNonNull(reducer);
return findLineageBy(element)
.map(strategy::reduce);
.map(reducer::reduce);
}

<T> Optional<T> reduceLinageOf(Feature element, Supplier<LineageCollector<T>> reducerSupplier) {
<T> Optional<T> reduceLinageOf(Feature element, LineageReducer<T> reducer) {
requireNonNull(element);
requireNonNull(reducerSupplier);
LineageReducerDescending<T> strategy = new LineageReducerDescending<>(reducerSupplier);
requireNonNull(reducer);
return findLineageBy(element)
.map(strategy::reduce);
.map(reducer::reduce);
}


<T> Optional<T> reduceLinageOf(Rule element, Supplier<LineageCollector<T>> reducerSupplier) {
<T> Optional<T> reduceLinageOf(Rule element, LineageReducer<T> reducer) {
requireNonNull(element);
requireNonNull(reducerSupplier);
LineageReducerDescending<T> strategy = new LineageReducerDescending<>(reducerSupplier);
requireNonNull(reducer);
return findLineageBy(element)
.map(strategy::reduce);
.map(reducer::reduce);
}

<T> Optional<T> reduceLinageOf(Scenario element, Supplier<LineageCollector<T>> reducerSupplier) {
<T> Optional<T> reduceLinageOf(Scenario element, LineageReducer<T> reducer) {
requireNonNull(element);
requireNonNull(reducerSupplier);
LineageReducerDescending<T> strategy = new LineageReducerDescending<>(reducerSupplier);
requireNonNull(reducer);
return findLineageBy(element)
.map(strategy::reduce);
.map(reducer::reduce);
}

<T> Optional<T> reduceLinageOf(Examples element, Supplier<LineageCollector<T>> reducerSupplier) {
<T> Optional<T> reduceLinageOf(Examples element, LineageReducer<T> reducer) {
requireNonNull(element);
requireNonNull(reducerSupplier);
LineageReducerDescending<T> strategy = new LineageReducerDescending<>(reducerSupplier);
requireNonNull(reducer);
return findLineageBy(element)
.map(strategy::reduce);
.map(reducer::reduce);
}

<T> Optional<T> reduceLinageOf(TableRow element, Supplier<LineageCollector<T>> reducerSupplier) {
<T> Optional<T> reduceLinageOf(TableRow element, LineageReducer<T> reducer) {
requireNonNull(element);
requireNonNull(reducerSupplier);
LineageReducerDescending<T> strategy = new LineageReducerDescending<>(reducerSupplier);
requireNonNull(reducer);
return findLineageBy(element)
.map(strategy::reduce);
.map(reducer::reduce);
}

<T> Optional<T> reduceLinageOf(Pickle element, Supplier<LineageCollector<T>> reducerSupplier) {
<T> Optional<T> reduceLinageOf(Pickle element, LineageReducer<T> reducer) {
requireNonNull(element);
requireNonNull(reducerSupplier);
LineageReducerDescending<T> strategy = new LineageReducerDescending<>(reducerSupplier);
requireNonNull(reducer);
return findLineageBy(element)
.map(lineage -> strategy.reduce(lineage, element));
.map(lineage -> reducer.reduce(lineage, element));
}

public Optional<Pickle> findPickleBy(TestCaseStarted testCaseStarted) {
Expand Down

0 comments on commit d78f43e

Please sign in to comment.