Skip to content

[Core] Use all generated snippets #1443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2018
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
6 changes: 2 additions & 4 deletions core/src/main/java/cucumber/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ private void addTestStepsForPickleSteps(List<PickleStepTestStep> testSteps, Pick
if (match == null) {
List<String> snippets = new ArrayList<>();
for (Backend backend : backends) {
String snippet = backend.getSnippet(step, "**KEYWORD**", runtimeOptions.getSnippetType().getFunctionNameGenerator());
if (snippet != null) {
snippets.add(snippet);
}
List<String> snippet = backend.getSnippet(step, "**KEYWORD**", runtimeOptions.getSnippetType().getFunctionNameGenerator());
snippets.addAll(snippet);
}
if (!snippets.isEmpty()) {
bus.send(new SnippetsSuggestedEvent(bus.getTime(), pickleEvent.uri, step.getLocations(), snippets));
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/cucumber/runtime/Backend.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public interface Backend {
*/
void disposeWorld();

String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator);
List<String> getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator);
}
34 changes: 20 additions & 14 deletions core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import io.cucumber.cucumberexpressions.ParameterTypeRegistry;

import java.lang.reflect.Type;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static java.text.MessageFormat.format;

public class SnippetGenerator {
@SuppressWarnings("RegExpRedundantEscape") // Android can't parse unescaped braces.
private static final ArgumentPattern[] DEFAULT_ARGUMENT_PATTERNS = new ArgumentPattern[]{
Expand All @@ -33,19 +35,23 @@ public SnippetGenerator(Snippet snippet, ParameterTypeRegistry parameterTypeRegi
this.generator = new CucumberExpressionGenerator(parameterTypeRegistry);
}

public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
List<GeneratedExpression> expressions = generator.generateExpressions(step.getText());
GeneratedExpression expression = expressions.get(0);

return MessageFormat.format(
snippet.template(),
keyword,
snippet.escapePattern(expression.getSource()),
functionName(expression.getSource(), functionNameGenerator),
snippet.arguments(arguments(step, expression.getParameterNames(), expression.getParameterTypes())),
REGEXP_HINT,
!step.getArgument().isEmpty() && step.getArgument().get(0) instanceof PickleTable ? snippet.tableHint() : ""
);
public List<String> getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
List<GeneratedExpression> generatedExpressions = generator.generateExpressions(step.getText());
List<String> snippets = new ArrayList<>(generatedExpressions.size());

for (GeneratedExpression expression : generatedExpressions) {
snippets.add(format(
snippet.template(),
keyword,
snippet.escapePattern(expression.getSource()),
functionName(expression.getSource(), functionNameGenerator),
snippet.arguments(arguments(step, expression.getParameterNames(), expression.getParameterTypes())),
REGEXP_HINT,
!step.getArgument().isEmpty() && step.getArgument().get(0) instanceof PickleTable ? snippet.tableHint() : ""
));
}

return snippets;
}

private String functionName(String sentence, FunctionNameGenerator functionNameGenerator) {
Expand Down
7 changes: 5 additions & 2 deletions core/src/test/java/cucumber/runner/TestBackendSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static java.util.Collections.emptyList;

public abstract class TestBackendSupplier implements Backend, BackendSupplier {

Expand All @@ -21,8 +24,8 @@ public void disposeWorld() {
}

@Override
public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return null;
public List<String> getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return emptyList();
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions core/src/test/java/cucumber/runner/TestRunnerSupplier.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cucumber.runner;

import cucumber.runner.EventBus;
import cucumber.runtime.Backend;
import cucumber.runtime.Glue;
import cucumber.runtime.RuntimeOptions;
Expand All @@ -10,6 +9,8 @@
import java.util.Collections;
import java.util.List;

import static java.util.Collections.emptyList;

public class TestRunnerSupplier implements Backend, RunnerSupplier {

private final EventBus bus;
Expand All @@ -36,8 +37,8 @@ public void disposeWorld() {
}

@Override
public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return null;
public List<String> getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return emptyList();
}

@Override
Expand Down
7 changes: 5 additions & 2 deletions core/src/test/java/cucumber/runtime/StubBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import gherkin.pickles.PickleStep;
import io.cucumber.stepexpression.TypeRegistry;

import java.util.Collections;
import java.util.List;

import static java.util.Collections.emptyList;

public class StubBackend implements Backend {

@SuppressWarnings("unused") // reflection to create backend
Expand All @@ -30,7 +33,7 @@ public void disposeWorld() {
}

@Override
public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return null;
public List<String> getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -30,8 +31,8 @@
import static cucumber.runner.TestHelper.createWriteHookAction;
import static cucumber.runner.TestHelper.result;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyListOf;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs;
Expand Down Expand Up @@ -1148,8 +1149,8 @@ public void loadGlue(cucumber.runtime.Glue glue, List<String> gluePaths) {
}

@Override
public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return "TEST SNIPPET";
public List<String> getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return singletonList("TEST SNIPPET");
}
};
final EventBus bus = new TimeServiceEventBus(new TimeServiceStub(1234));
Expand Down Expand Up @@ -1184,8 +1185,8 @@ public void loadGlue(cucumber.runtime.Glue glue, List<String> gluePaths) {
}

@Override
public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return "TEST SNIPPET";
public List<String> getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return singletonList("TEST SNIPPET");
}
};
final EventBus bus = new TimeServiceEventBus(new TimeServiceStub(1234));
Expand Down
2 changes: 1 addition & 1 deletion java/src/main/java/cucumber/runtime/java/JavaBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void disposeWorld() {
}

@Override
public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
public List<String> getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return snippetGenerator.getSnippet(step, keyword, functionNameGenerator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.Test;

import java.util.Collections;
import java.util.List;
import java.util.Locale;

import static org.junit.Assert.assertEquals;
Expand All @@ -28,6 +29,7 @@ public void generatesPlainSnippet() {

private String snippetFor(String name) {
PickleStep step = new PickleStep(name, Collections.<Argument>emptyList(), Collections.<PickleLocation>emptyList());
return new SnippetGenerator(new Java8Snippet(), new ParameterTypeRegistry(Locale.ENGLISH)).getSnippet(step, GIVEN_KEYWORD, null);
List<String> snippet = new SnippetGenerator(new Java8Snippet(), new ParameterTypeRegistry(Locale.ENGLISH)).getSnippet(step, GIVEN_KEYWORD, null);
return StringJoiner.join("\n", snippet);
}
}
39 changes: 30 additions & 9 deletions java/src/test/java/cucumber/runtime/java/JavaSnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import io.cucumber.cucumberexpressions.CaptureGroupTransformer;
import io.cucumber.cucumberexpressions.ParameterType;
import io.cucumber.cucumberexpressions.ParameterTypeRegistry;
import io.cucumber.cucumberexpressions.Transformer;
import io.cucumber.cucumberexpressions.TypeReference;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -209,6 +208,12 @@ public String transform(String... strings) {
"public void i_have_a(String docString, String docString1) {\n" +
" // Write code here that turns the phrase above into concrete actions\n" +
" throw new cucumber.api.PendingException();\n" +
"}\n" +
"\n" +
"@Given(\"I have a {string}:\")\n" +
"public void i_have_a(String string, String docString) {\n" +
" // Write code here that turns the phrase above into concrete actions\n" +
" throw new cucumber.api.PendingException();\n" +
"}\n";
assertEquals(expected, snippetForDocString("I have a \"Documentation String\":", new PickleString(null, "hello"), customParameterType));
}
Expand Down Expand Up @@ -268,8 +273,18 @@ public String transform(String... strings) {
" // Double, Byte, Short, Long, BigInteger or BigDecimal.\n" +
" //\n" +
" // For other transformations you can register a DataTableType.\n" +
// " //\n" +
// " // See: TODO URL\n" +
" throw new cucumber.api.PendingException();\n" +
"}\n" +
"\n" +
"@Given(\"I have in table {string}:\")\n" +
"public void i_have_in_table(String string, DataTable dataTable) {\n" +
" // Write code here that turns the phrase above into concrete actions\n" +
" // For automatic transformation, change DataTable to one of\n" +
" // E, List<E>, List<List<E>>, List<Map<K,V>>, Map<K,V> or\n" +
" // Map<K, List<V>>. E,K,V must be a String, Integer, Float,\n" +
" // Double, Byte, Short, Long, BigInteger or BigDecimal.\n" +
" //\n" +
" // For other transformations you can register a DataTableType.\n" +
" throw new cucumber.api.PendingException();\n" +
"}\n";
PickleTable dataTable = new PickleTable(asList(new PickleRow(asList(new PickleCell(null, "col1")))));
Expand All @@ -290,42 +305,48 @@ public void generateSnippetWithOutlineParam() {

private String snippetFor(String name) {
PickleStep step = new PickleStep(name, Collections.<Argument>emptyList(), Collections.<PickleLocation>emptyList());
return new SnippetGenerator(new JavaSnippet(), new ParameterTypeRegistry(Locale.ENGLISH)).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
List<String> snippet = new SnippetGenerator(new JavaSnippet(), new ParameterTypeRegistry(Locale.ENGLISH)).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
return StringJoiner.join("\n", snippet);
}


private String snippetFor(String name, ParameterType<?> parameterType) {
PickleStep step = new PickleStep(name, Collections.<Argument>emptyList(), Collections.<PickleLocation>emptyList());
ParameterTypeRegistry parameterTypeRegistry = new ParameterTypeRegistry(Locale.ENGLISH);
parameterTypeRegistry.defineParameterType(parameterType);
return new SnippetGenerator(new JavaSnippet(), parameterTypeRegistry).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
List<String> snippet = new SnippetGenerator(new JavaSnippet(), parameterTypeRegistry).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
return StringJoiner.join("\n", snippet);
}

private String snippetForDocString(String name, PickleString docString) {
PickleStep step = new PickleStep(name, asList((Argument) docString), Collections.<PickleLocation>emptyList());
return new SnippetGenerator(new JavaSnippet(), new ParameterTypeRegistry(Locale.ENGLISH)).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
List<String> snippet = new SnippetGenerator(new JavaSnippet(), new ParameterTypeRegistry(Locale.ENGLISH)).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
return StringJoiner.join("\n", snippet);
}


private String snippetForDocString(String name, PickleString docString, ParameterType<String> parameterType) {
PickleStep step = new PickleStep(name, asList((Argument) docString), Collections.<PickleLocation>emptyList());
ParameterTypeRegistry parameterTypeRegistry = new ParameterTypeRegistry(Locale.ENGLISH);
parameterTypeRegistry.defineParameterType(parameterType);
return new SnippetGenerator(new JavaSnippet(), parameterTypeRegistry).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
List<String> snippet = new SnippetGenerator(new JavaSnippet(), parameterTypeRegistry).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
return StringJoiner.join("\n", snippet);
}


private String snippetForDataTable(String name, PickleTable dataTable) {
PickleStep step = new PickleStep(name, asList((Argument) dataTable), Collections.<PickleLocation>emptyList());
return new SnippetGenerator(new JavaSnippet(), new ParameterTypeRegistry(Locale.ENGLISH)).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
List<String> snippet = new SnippetGenerator(new JavaSnippet(), new ParameterTypeRegistry(Locale.ENGLISH)).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
return StringJoiner.join("\n", snippet);
}


private String snippetForDataTable(String name, PickleTable dataTable, ParameterType<String> parameterType) {
PickleStep step = new PickleStep(name, asList((Argument) dataTable), Collections.<PickleLocation>emptyList());
ParameterTypeRegistry parameterTypeRegistry = new ParameterTypeRegistry(Locale.ENGLISH);
parameterTypeRegistry.defineParameterType(parameterType);
return new SnippetGenerator(new JavaSnippet(), parameterTypeRegistry).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
List<String> snippet = new SnippetGenerator(new JavaSnippet(), parameterTypeRegistry).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
return StringJoiner.join("\n", snippet);
}

private static class Size {
Expand Down
18 changes: 18 additions & 0 deletions java/src/test/java/cucumber/runtime/java/StringJoiner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cucumber.runtime.java;

public class StringJoiner {
public static String join(String delimiter, Iterable<String> strings){
//TODO: Java8 replace with StringJoiner.
StringBuilder builder = new StringBuilder();
boolean first = true;
for (String string : strings) {
if (first) {
first = false;
} else {
builder.append(delimiter);
}
builder.append(string);
}
return builder.toString();
}
}
6 changes: 4 additions & 2 deletions junit/src/test/java/cucumber/runtime/stub/StubBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.Collections;
import java.util.List;

import static java.util.Collections.singletonList;

/**
* We need an implementation of Backend to prevent Runtime from blowing up.
*/
Expand Down Expand Up @@ -82,7 +84,7 @@ public void disposeWorld() {
}

@Override
public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return "STUB SNIPPET";
public List<String> getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return singletonList("STUB SNIPPET");
}
}
7 changes: 5 additions & 2 deletions testng/src/test/java/cucumber/runtime/stub/StubBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import cucumber.runtime.snippets.FunctionNameGenerator;
import gherkin.pickles.PickleStep;

import java.util.Collections;
import java.util.List;

import static java.util.Collections.singletonList;

/**
* We need an implementation of Backend to prevent Runtime from blowing up.
*/
Expand All @@ -31,7 +34,7 @@ public void disposeWorld() {
}

@Override
public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return "STUB SNIPPET";
public List<String> getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return singletonList("STUB SNIPPET");
}
}