Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion java/src/main/java/io/cucumber/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.Supplier;
Expand Down Expand Up @@ -129,6 +129,10 @@ public List<TestCaseStarted> findAllTestCaseStarted() {
.collect(toList());
}

/**
* @deprecated {@link #findLineageBy} is public, this method can be inlined.
*/
@Deprecated
public Map<Optional<Feature>, List<TestCaseStarted>> findAllTestCaseStartedGroupedByFeature() {
return findAllTestCaseStarted()
.stream()
Expand Down Expand Up @@ -170,6 +174,10 @@ public List<Attachment> findAttachmentsBy(TestStepFinished testStepFinished) {
.collect(toList());
}

/**
* @deprecated {@link #findLineageBy} is public, this method can be inlined.
*/
@Deprecated
public Optional<Feature> findFeatureBy(TestCaseStarted testCaseStarted) {
return findLineageBy(testCaseStarted).flatMap(Lineage::feature);
}
Expand All @@ -192,6 +200,10 @@ public Optional<TestStepResult> findMostSevereTestStepResultBy(TestCaseStarted t
.max(testStepResultComparator);
}

/**
* @deprecated {@link #findLineageBy} is public, this method can be inlined.
*/
@Deprecated
public String findNameOf(GherkinDocument element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
Expand All @@ -200,6 +212,10 @@ public String findNameOf(GherkinDocument element, NamingStrategy namingStrategy)
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

/**
* @deprecated {@link #findLineageBy} is public, this method can be inlined.
*/
@Deprecated
public String findNameOf(Feature element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
Expand All @@ -208,6 +224,10 @@ public String findNameOf(Feature element, NamingStrategy namingStrategy) {
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

/**
* @deprecated {@link #findLineageBy} is public, this method can be inlined.
*/
@Deprecated
public String findNameOf(Rule element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
Expand All @@ -216,6 +236,10 @@ public String findNameOf(Rule element, NamingStrategy namingStrategy) {
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

/**
* @deprecated {@link #findLineageBy} is public, this method can be inlined.
*/
@Deprecated
public String findNameOf(Scenario element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
Expand All @@ -224,6 +248,10 @@ public String findNameOf(Scenario element, NamingStrategy namingStrategy) {
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

/**
* @deprecated {@link #findLineageBy} is public, this method can be inlined.
*/
@Deprecated
public String findNameOf(Examples element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
Expand All @@ -232,6 +260,10 @@ public String findNameOf(Examples element, NamingStrategy namingStrategy) {
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

/**
* @deprecated {@link #findLineageBy} is public, this method can be inlined.
*/
@Deprecated
public String findNameOf(TableRow element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
Expand All @@ -240,6 +272,10 @@ public String findNameOf(TableRow element, NamingStrategy namingStrategy) {
.orElseThrow(createElementWasNotPartOfThisQueryObject());
}

/**
* @deprecated {@link #findLineageBy} is public, this method can be inlined.
*/
@Deprecated
public String findNameOf(Pickle element, NamingStrategy namingStrategy) {
requireNonNull(element);
requireNonNull(namingStrategy);
Expand Down
128 changes: 128 additions & 0 deletions java/src/test/java/io/cucumber/query/NamingStrategyAcceptanceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package io.cucumber.query;

import io.cucumber.messages.NdjsonToMessageIterable;
import io.cucumber.messages.types.Envelope;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static io.cucumber.query.Jackson.OBJECT_MAPPER;
import static io.cucumber.query.NamingStrategy.ExampleName.NUMBER_AND_PICKLE_IF_PARAMETERIZED;
import static io.cucumber.query.NamingStrategy.ExampleName.PICKLE;
import static io.cucumber.query.NamingStrategy.FeatureName.EXCLUDE;
import static io.cucumber.query.NamingStrategy.Strategy.LONG;
import static io.cucumber.query.NamingStrategy.Strategy.SHORT;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.newOutputStream;
import static java.nio.file.Files.readAllBytes;
import static org.assertj.core.api.Assertions.assertThat;

public class NamingStrategyAcceptanceTest {
private static final NdjsonToMessageIterable.Deserializer deserializer = (json) -> OBJECT_MAPPER.readValue(json, Envelope.class);

static List<TestCase> acceptance() {
Map<String, NamingStrategy> strategies = new LinkedHashMap<>();
strategies.put("long", NamingStrategy.strategy(LONG).build());
strategies.put("long-exclude-feature-name", NamingStrategy.strategy(LONG).featureName(EXCLUDE).build());
strategies.put("long-with-pickle-name", NamingStrategy.strategy(LONG).exampleName(PICKLE).build());
strategies.put("long-with-pickle-name-if-parameterized", NamingStrategy.strategy(LONG).exampleName(NUMBER_AND_PICKLE_IF_PARAMETERIZED).build());
strategies.put("short", NamingStrategy.strategy(SHORT).build());

List<Path> sources = Arrays.asList(
Paths.get("../testdata/minimal.feature.ndjson"),
Paths.get("../testdata/rules.feature.ndjson"),
Paths.get("../testdata/examples-tables.feature.ndjson")
);

List<TestCase> testCases = new ArrayList<>();
sources.forEach(path ->
strategies.forEach((strategyName, strategy) ->
testCases.add(new TestCase(path, strategyName, strategy))));

return testCases;
}

private static String writeResults(TestCase testCase, NamingStrategy strategy) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
writeResults(strategy, testCase, out);
return new String(out.toByteArray(), UTF_8);
}

private static void writeResults(NamingStrategy strategy, TestCase testCase, OutputStream out) throws IOException {
try (InputStream in = Files.newInputStream(testCase.source)) {
try (NdjsonToMessageIterable envelopes = new NdjsonToMessageIterable(in, deserializer)) {
try (PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)))) {
Query query = new Query();
for (Envelope envelope : envelopes) {
query.update(envelope);
}
query.findAllPickles().forEach(pickle -> {
query.findLineageBy(pickle)
.map(lineage -> strategy.reduce(lineage, pickle))
.ifPresent(writer::println);
});
}

}
}
}

@ParameterizedTest
@MethodSource("acceptance")
void test(TestCase testCase) throws IOException {
String actual = writeResults(testCase, testCase.strategy);
String expected = new String(readAllBytes(testCase.expected), UTF_8);
assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("acceptance")
@Disabled
void updateExpectedQueryResultFiles(TestCase testCase) throws IOException {
try (OutputStream out = newOutputStream(testCase.expected)) {
writeResults(testCase.strategy, testCase, out);
}
}

static class TestCase {
private final Path source;
private final NamingStrategy strategy;
private final Path expected;

private final String name;
private final String strategyName;

TestCase(Path source, String strategyName, NamingStrategy strategy) {
this.source = source;
this.strategy = strategy;
this.strategyName = strategyName;
String fileName = source.getFileName().toString();
this.name = fileName.substring(0, fileName.lastIndexOf(".ndjson"));
this.expected = source.getParent().resolve(name + ".naming-strategy." + strategyName + ".txt");
}

@Override
public String toString() {
return name + " -> " + strategyName;
}

}

}
41 changes: 0 additions & 41 deletions java/src/test/java/io/cucumber/query/QueryAcceptanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,6 @@ private static Map<String, Object> createQueryResults(Query query) {
results.put("findAllPickleSteps", query.findAllPickleSteps().size());
results.put("findAllTestCaseStarted", query.findAllTestCaseStarted().size());
results.put("findAllTestSteps", query.findAllTestSteps().size());
results.put("findAllTestCaseStartedGroupedByFeature", query.findAllTestCaseStartedGroupedByFeature()
.entrySet()
.stream()
.map(entry -> Arrays.asList(entry.getKey().map(Feature::getName), entry.getValue().stream()
.map(TestCaseStarted::getId)
.collect(toList()))));
results.put("findAttachmentsBy", query.findAllTestCaseStarted().stream()
.map(query::findTestStepFinishedAndTestStepBy)
.flatMap(Collection::stream)
Expand All @@ -129,10 +123,6 @@ private static Map<String, Object> createQueryResults(Query query) {
attachment.getContentEncoding()
))
.collect(toList()));
results.put("findFeatureBy", query.findAllTestCaseStarted().stream()
.map(query::findFeatureBy)
.map(feature -> feature.map(Feature::getName))
.collect(toList()));
results.put("findHookBy", query.findAllTestSteps().stream()
.map(query::findHookBy)
.map(hook -> hook.map(Hook::getId))
Expand All @@ -148,25 +138,6 @@ private static Map<String, Object> createQueryResults(Query query) {
.map(testStepResult -> testStepResult.map(TestStepResult::getStatus))
.collect(toList()));

Map<String, Object> names = new LinkedHashMap<>();
names.put("long", query.findAllPickles().stream()
.map(pickle -> query.findNameOf(pickle, NamingStrategy.strategy(LONG).build()))
.collect(toList()));
names.put("excludeFeatureName", query.findAllPickles().stream()
.map(pickle -> query.findNameOf(pickle, NamingStrategy.strategy(LONG).featureName(EXCLUDE).build()))
.collect(toList()));
names.put("longPickleName", query.findAllPickles().stream()
.map(pickle -> query.findNameOf(pickle, NamingStrategy.strategy(LONG).exampleName(PICKLE).build()))
.collect(toList()));
names.put("short", query.findAllPickles().stream()
.map(pickle -> query.findNameOf(pickle, NamingStrategy.strategy(SHORT).build()))
.collect(toList()));
names.put("shortPickleName", query.findAllPickles().stream()
.map(pickle -> query.findNameOf(pickle, NamingStrategy.strategy(SHORT).exampleName(PICKLE).build()))
.collect(toList()));

results.put("findNameOf", names);

results.put("findPickleBy", query.findAllTestCaseStarted().stream()
.map(query::findPickleBy)
.map(pickle -> pickle.map(Pickle::getName))
Expand Down Expand Up @@ -249,18 +220,6 @@ public String toString() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestCase testCase = (TestCase) o;
return source.equals(testCase.source);
}

@Override
public int hashCode() {
return Objects.hash(source);
}
}

}
Loading
Loading