Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- [Core] Emit Suggestion message ([#3073](https://github.com/cucumber/cucumber-jvm/pull/3073) M.P. Korstanje)

### Fixed
- [Core] Emit StepMatchArgumentsList for ambiguous steps ([#3066](https://github.com/cucumber/cucumber-jvm/pull/3066) M.P. Korstanje)

Expand Down
11 changes: 11 additions & 0 deletions cucumber-core/src/main/java/io/cucumber/core/backend/Snippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@
import java.lang.reflect.Type;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Optional;

@API(status = API.Status.STABLE)
public interface Snippet {

/**
* The language of the generated snippet.
*
* @see io.cucumber.messages.types.Snippet#getLanguage()
* @return the language of the generated snippet.
*/
default Optional<String> language() {
return Optional.empty();
}

/**
* @return a {@link java.text.MessageFormat} template used to generate a
* snippet. The template can access the following variables:
Expand Down
55 changes: 35 additions & 20 deletions cucumber-core/src/main/java/io/cucumber/core/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import io.cucumber.core.logging.LoggerFactory;
import io.cucumber.core.snippets.SnippetGenerator;
import io.cucumber.core.stepexpression.StepTypeRegistry;
import io.cucumber.messages.types.Envelope;
import io.cucumber.messages.types.Snippet;
import io.cucumber.plugin.event.HookType;
import io.cucumber.plugin.event.Location;
import io.cucumber.plugin.event.SnippetsSuggestedEvent;
import io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion;

Expand All @@ -26,11 +27,11 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static io.cucumber.core.exception.ExceptionUtils.throwAsUncheckedException;
import static io.cucumber.core.runner.StackManipulation.removeFrameworkFrames;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;

public final class Runner {

Expand Down Expand Up @@ -128,7 +129,7 @@ private List<SnippetGenerator> createSnippetGeneratorsForPickle(StepTypeRegistry
.map(Backend::getSnippet)
.filter(Objects::nonNull)
.map(s -> new SnippetGenerator(s, stepTypeRegistry.parameterTypeRegistry()))
.collect(Collectors.toList());
.collect(toList());
}

private void buildBackendWorlds() {
Expand Down Expand Up @@ -193,16 +194,28 @@ private PickleStepDefinitionMatch matchStepToStepDefinition(Pickle pickle, Step
}

private void emitSnippetSuggestedEvent(Pickle pickle, Step step) {
List<String> snippets = generateSnippetsForStep(step);
List<Snippet> snippets = generateSnippetsForStep(step);
if (snippets.isEmpty()) {
return;
}
Suggestion suggestion = new Suggestion(step.getText(), snippets);
Location scenarioLocation = pickle.getLocation();
Location stepLocation = step.getLocation();
SnippetsSuggestedEvent event = new SnippetsSuggestedEvent(bus.getInstant(), pickle.getUri(), scenarioLocation,
stepLocation, suggestion);
bus.send(event);

bus.send(new SnippetsSuggestedEvent(
bus.getInstant(),
pickle.getUri(),
pickle.getLocation(),
step.getLocation(),
new Suggestion(
step.getText(),
snippets.stream()
.map(Snippet::getCode)
.collect(toList()))));

bus.send(
Envelope.of(
new io.cucumber.messages.types.Suggestion(
bus.generateId().toString(),
step.getId(),
snippets)));
}

private List<HookTestStep> createAfterStepHooks(List<String> tags) {
Expand All @@ -219,16 +232,18 @@ private List<HookTestStep> createTestStepsForHooks(
return hooks.stream()
.filter(hook -> hook.matches(tags))
.map(hook -> new HookTestStep(bus.generateId(), hookType, new HookDefinitionMatch(hook)))
.collect(Collectors.toList());
}

private List<String> generateSnippetsForStep(Step step) {
List<String> snippets = new ArrayList<>();
for (SnippetGenerator snippetGenerator : snippetGenerators) {
List<String> snippet = snippetGenerator.getSnippet(step, runnerOptions.getSnippetType());
snippets.addAll(snippet);
}
return snippets;
.collect(toList());
}

private List<Snippet> generateSnippetsForStep(Step step) {
return snippetGenerators.stream()
.flatMap(generator -> {
String language = generator.getLanguage().orElse("unknown");
return generator.getSnippet(step, runnerOptions.getSnippetType())
.stream()
.map(code -> new Snippet(language, code));
})
.collect(toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -37,6 +38,10 @@ public SnippetGenerator(Snippet snippet, ParameterTypeRegistry parameterTypeRegi
this.generator = new CucumberExpressionGenerator(parameterTypeRegistry);
}

public Optional<String> getLanguage() {
return snippet.language();
}

public List<String> getSnippet(Step step, SnippetType snippetType) {
List<GeneratedExpression> generatedExpressions = generator.generateExpressions(step.getText());
IdentifierGenerator functionNameGenerator = new IdentifierGenerator(snippetType.joiner());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
import java.lang.reflect.Type;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Optional;

public class TestSnippet implements Snippet {

@Override
public Optional<String> language() {
return Optional.of("test");
}

private int i;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

import java.lang.reflect.Type;
import java.util.Map;
import java.util.Optional;

import static java.util.stream.Collectors.joining;

abstract class AbstractJavaSnippet implements Snippet {

@Override
public Optional<String> language() {
return Optional.of("java");
}

@Override
public final String tableHint() {
return "" +
Expand Down
Loading