Skip to content

Commit

Permalink
Support DocString and DataTable in generated snippets. Closes #227
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Mar 20, 2012
1 parent 2f823cf commit 3c13008
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 4 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [Git master](https://github.com/cucumber/cucumber-jvm/compare/v1.0.0.RC21...master)

* [Core] Support DocString and DataTable in generated snippets ([#227](https://github.com/cucumber/cucumber-jvm/issues/227) Aslak Hellesøy)
* [Core] Fix broken --tags option (and get rid of JCommander for CLI parsing). ([#266](https://github.com/cucumber/cucumber-jvm/issues/266) Aslak Hellesøy)
* [Clojure] Make Clojure DSL syntax cleaner ([#244](https://github.com/cucumber/cucumber-jvm/issues/244) [#267](https://github.com/cucumber/cucumber-jvm/pull/267) rplevy-draker)
* [Clojure] Native Clojure backend ([#138](https://github.com/cucumber/cucumber-jvm/pull/138) [#265](https://github.com/cucumber/cucumber-jvm/pull/265) Kevin Downey, Nils Wloka)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,37 @@

import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.formatter.model.Comment;
import gherkin.formatter.model.DataTableRow;
import gherkin.formatter.model.Step;
import org.junit.Test;

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

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;

public class ClojureSnippetTest {
private static final List<Comment> NO_COMMENTS = Collections.emptyList();

@Test
public void generatesPlainSnippet() {
Step step = new Step(Collections.<Comment>emptyList(), "Given ", "I have 4 cukes in my \"big\" belly", 0, null, null);
Step step = new Step(NO_COMMENTS, "Given ", "I have 4 cukes in my \"big\" belly", 0, null, null);
String snippet = new SnippetGenerator(new ClojureSnippet()).getSnippet(step);
String expected = "" +
"(Given #\"^I have (\\d+) cukes in my \"([^\"]*)\" belly$\" [arg1, arg2]\n" +
" (comment Express the Regexp above with the code you wish you had ))\n";
assertEquals(expected, snippet);
}

@Test
public void generatesSnippetWithDataTable() {
List<DataTableRow> dataTable = asList(new DataTableRow(NO_COMMENTS, asList("col1"), 1));
Step step = new Step(NO_COMMENTS, "Given ", "I have:", 0, dataTable, null);
String snippet = new SnippetGenerator(new ClojureSnippet()).getSnippet(step);
String expected = "" +
"(Given #\"^I have:$\" [arg1]\n" +
" (comment Express the Regexp above with the code you wish you had ))\n";
assertEquals(expected, snippet);
}
}
19 changes: 17 additions & 2 deletions core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cucumber.runtime.snippets;

import cucumber.table.DataTable;
import gherkin.I18n;
import gherkin.formatter.model.Step;

Expand Down Expand Up @@ -45,7 +46,14 @@ public SnippetGenerator(Snippet snippet) {
}

public String getSnippet(Step step) {
return MessageFormat.format(snippet.template(), I18n.codeKeywordFor(step.getKeyword()), snippet.escapePattern(patternFor(step.getName())), functionName(step.getName()), snippet.arguments(argumentTypes(step.getName())), HINT);
return MessageFormat.format(
snippet.template(),
I18n.codeKeywordFor(step.getKeyword()),
snippet.escapePattern(patternFor(step.getName())),
functionName(step.getName()),
snippet.arguments(argumentTypes(step)),
HINT
);
}

protected String patternFor(String stepName) {
Expand Down Expand Up @@ -104,7 +112,8 @@ private String withNamedGroups(String snippetPattern) {
}


private List<Class<?>> argumentTypes(String name) {
private List<Class<?>> argumentTypes(Step step) {
String name = step.getName();
List<Class<?>> argTypes = new ArrayList<Class<?>>();
Matcher[] matchers = new Matcher[argumentPatterns().length];
for (int i = 0; i < argumentPatterns().length; i++) {
Expand All @@ -131,6 +140,12 @@ private List<Class<?>> argumentTypes(String name) {
break;
}
}
if(step.getDocString() != null) {
argTypes.add(String.class);
}
if(step.getRows() != null) {
argTypes.add(DataTable.class);
}
return argTypes;
}

Expand Down
118 changes: 118 additions & 0 deletions groovy/src/test/groovy/cucumber/runtime/groovy/GroovySnippetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package cucumber.runtime.groovy;

import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.formatter.model.Comment;
import gherkin.formatter.model.DataTableRow;
import gherkin.formatter.model.DocString;
import gherkin.formatter.model.Step;
import org.junit.Test;

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

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;

public class GroovySnippetTest {

private static final List<Comment> NO_COMMENTS = Collections.emptyList();

@Test
public void generatesPlainSnippet() {
String expected = "" +
"Given(~\"^I have (\\d+) cukes in my \\\"([^\\\"]*)\\\" belly$\") { int arg1, String arg2 ->\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
assertEquals(expected, snippetFor("I have 4 cukes in my \"big\" belly"));
}

@Test
public void generatesCopyPasteReadyStepSnippetForNumberParameters() throws Exception {
String expected = "" +
"Given(~\"^before (\\d+) after$\") { int arg1 ->\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
String snippet = snippetFor("before 5 after");
assertEquals(expected, snippet);
}

@Test
public void generatesCopyPasteReadySnippetWhenStepHasIllegalJavaIdentifierChars() {
String expected = "" +
"Given(~\"^I have (\\d+) cukes in: my \\\"([^\\\"]*)\\\" red-belly!$\") { int arg1, String arg2 ->\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
assertEquals(expected, snippetFor("I have 4 cukes in: my \"big\" red-belly!"));
}


@Test
public void generatesCopyPasteReadySnippetWhenStepHasIntegersInsideStringParameter() {
String expected = "" +
"Given(~\"^the DI system receives a message saying \\\"([^\\\"]*)\\\"$\") { String arg1 ->\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
assertEquals(expected, snippetFor("the DI system receives a message saying \"{ dataIngestion: { feeds: [ feed: { merchantId: 666, feedId: 1, feedFileLocation: feed.csv } ] }\""));
}

@Test
public void generatesSnippetWithEscapedDollarSigns() {
String expected = "" +
"Given(~\"^I have \\$(\\d+)$\") { int arg1 ->\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
assertEquals(expected, snippetFor("I have $5"));
}

@Test
public void generatesSnippetWithEscapedParentheses() {
String expected = "" +
"Given(~\"^I have (\\d+) cukes \\(maybe more\\)$\") { int arg1 ->\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
assertEquals(expected, snippetFor("I have 5 cukes (maybe more)"));
}

@Test
public void generatesSnippetWithEscapedBrackets() {
String expected = "" +
"Given(~\"^I have (\\d+) cukes \\[maybe more\\]$\") { int arg1 ->\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
assertEquals(expected, snippetFor("I have 5 cukes [maybe more]"));
}

@Test
public void generatesSnippetWithDocString() {
String expected = "" +
"Given(~\"^I have:$\") { String arg1 ->\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
assertEquals(expected, snippetForDocString("I have:", new DocString("text/plain", "hello", 1)));
}

@Test
public void generatesSnippetWithDataTable() {
String expected = "" +
"Given(~\"^I have:$\") { DataTable arg1 ->\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
List<DataTableRow> dataTable = asList(new DataTableRow(NO_COMMENTS, asList("col1"), 1));
assertEquals(expected, snippetForDataTable("I have:", dataTable));
}

private String snippetFor(String name) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, null, null);
return new SnippetGenerator(new GroovySnippet()).getSnippet(step);
}

private String snippetForDocString(String name, DocString docString) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, null, docString);
return new SnippetGenerator(new GroovySnippet()).getSnippet(step);
}

private String snippetForDataTable(String name, List<DataTableRow> dataTable) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, dataTable, null);
return new SnippetGenerator(new GroovySnippet()).getSnippet(step);
}
}
39 changes: 38 additions & 1 deletion java/src/test/java/cucumber/runtime/java/JavaSnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.formatter.model.Comment;
import gherkin.formatter.model.DataTableRow;
import gherkin.formatter.model.DocString;
import gherkin.formatter.model.Step;
import org.junit.Test;

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

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;

public class JavaSnippetTest {

private static final List<Comment> NO_COMMENTS = Collections.emptyList();

@Test
public void generatesPlainSnippet() {
String expected = "" +
Expand Down Expand Up @@ -83,8 +89,39 @@ public void generatesSnippetWithEscapedBrackets() {
assertEquals(expected, snippetFor("I have 5 cukes [maybe more]"));
}

@Test
public void generatesSnippetWithDocString() {
String expected = "" +
"@Given(\"^I have:$\")\n" +
"public void I_have(String arg1) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
assertEquals(expected, snippetForDocString("I have:", new DocString("text/plain", "hello", 1)));
}

@Test
public void generatesSnippetWithDataTable() {
String expected = "" +
"@Given(\"^I have:$\")\n" +
"public void I_have(DataTable arg1) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
List<DataTableRow> dataTable = asList(new DataTableRow(NO_COMMENTS, asList("col1"), 1));
assertEquals(expected, snippetForDataTable("I have:", dataTable));
}

private String snippetFor(String name) {
Step step = new Step(Collections.<Comment>emptyList(), "Given ", name, 0, null, null);
Step step = new Step(NO_COMMENTS, "Given ", name, 0, null, null);
return new SnippetGenerator(new JavaSnippet()).getSnippet(step);
}

private String snippetForDocString(String name, DocString docString) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, null, docString);
return new SnippetGenerator(new JavaSnippet()).getSnippet(step);
}

private String snippetForDataTable(String name, List<DataTableRow> dataTable) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, dataTable, null);
return new SnippetGenerator(new JavaSnippet()).getSnippet(step);
}
}

0 comments on commit 3c13008

Please sign in to comment.