-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support DocString and DataTable in generated snippets. Closes #227
- Loading branch information
1 parent
2f823cf
commit 3c13008
Showing
5 changed files
with
191 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
groovy/src/test/groovy/cucumber/runtime/groovy/GroovySnippetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters