Skip to content

Commit

Permalink
[Groovy] Udated groovy for the use of Gherkin v4.0.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
brasmusson committed Dec 18, 2016
1 parent c9af7f5 commit 46a35ba
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 37 deletions.
16 changes: 12 additions & 4 deletions groovy/pom.xml
Expand Up @@ -107,17 +107,25 @@


<groovy><![CDATA[ <groovy><![CDATA[
import groovy.text.SimpleTemplateEngine import groovy.text.SimpleTemplateEngine
import gherkin.I18n import gherkin.GherkinDialect
import gherkin.GherkinDialectProvider
def engine = new SimpleTemplateEngine() def engine = new SimpleTemplateEngine()
def templateSource = new File(project.baseDir, "src${File.separator}main${File.separator}code_generator${File.separator}I18n.groovy.txt").getText() def templateSource = new File(project.baseDir, "src${File.separator}main${File.separator}code_generator${File.separator}I18n.groovy.txt").getText()
I18n.all.each { i18n -> def unsupported = ["EM"] // The generated files for Emoij do not compile.
def binding = ["i18n":i18n] def dialectProvider = new GherkinDialectProvider()
GherkinDialectProvider.DIALECTS.keySet().each { language ->
def dialect = dialectProvider.getDialect(language, null)
def normalized_language = dialect.language.replaceAll("[\\s-]", "_").toUpperCase()
if (!unsupported.contains(normalized_language)) {
def binding = ["i18n":dialect, "normalized_language":normalized_language]
template = engine.createTemplate(templateSource).make(binding) template = engine.createTemplate(templateSource).make(binding)
def file = new File(project.baseDir, "target${File.separator}generated-sources${File.separator}i18n${File.separator}java${File.separator}cucumber${File.separator}api${File.separator}groovy${File.separator}${i18n.underscoredIsoCode.toUpperCase()}.java") def file = new File(project.baseDir, "target${File.separator}generated-sources${File.separator}i18n${File.separator}java${File.separator}cucumber${File.separator}api${File.separator}groovy${File.separator}${normalized_language}.java")
file.parentFile.mkdirs() file.parentFile.mkdirs()
file.write(template.toString(), "UTF-8") file.write(template.toString(), "UTF-8")
}
} }
]]></groovy> ]]></groovy>


Expand Down
8 changes: 4 additions & 4 deletions groovy/src/main/code_generator/I18n.groovy.txt
Expand Up @@ -5,13 +5,13 @@ import cucumber.runtime.groovy.*;


import java.util.regex.Pattern; import java.util.regex.Pattern;


public class ${i18n.underscoredIsoCode.toUpperCase()} { public class ${normalized_language} {
<% i18n.codeKeywords.each { kw -> %> <% i18n.stepKeywords.findAll { !it.contains('*') && !it.matches("^\\d.*") }.unique().each { kw -> %>
public static void ${kw}(Pattern regexp, Closure body) throws Throwable { public static void ${kw.replaceAll("[\\s',!]", "")}(Pattern regexp, Closure body) throws Throwable {
GroovyBackend.getInstance().addStepDefinition(regexp, 0, body); GroovyBackend.getInstance().addStepDefinition(regexp, 0, body);
} }


public static void ${java.text.Normalizer.normalize(kw, java.text.Normalizer.Form.NFC)}(Pattern regexp, long timeoutMillis, Closure body) throws Throwable { public static void ${java.text.Normalizer.normalize(kw.replaceAll("[\\s',!]", ""), java.text.Normalizer.Form.NFC)}(Pattern regexp, long timeoutMillis, Closure body) throws Throwable {
GroovyBackend.getInstance().addStepDefinition(regexp, timeoutMillis, body); GroovyBackend.getInstance().addStepDefinition(regexp, timeoutMillis, body);
} }
<% } %> <% } %>
Expand Down
2 changes: 1 addition & 1 deletion groovy/src/main/java/cucumber/api/groovy/Hooks.java
@@ -1,8 +1,8 @@
package cucumber.api.groovy; package cucumber.api.groovy;


import cucumber.runtime.CucumberException; import cucumber.runtime.CucumberException;
import cucumber.runtime.TagExpression;
import cucumber.runtime.groovy.GroovyBackend; import cucumber.runtime.groovy.GroovyBackend;
import gherkin.TagExpression;
import groovy.lang.Closure; import groovy.lang.Closure;


import java.util.ArrayList; import java.util.ArrayList;
Expand Down
Expand Up @@ -5,13 +5,13 @@
import cucumber.runtime.CucumberException; import cucumber.runtime.CucumberException;
import cucumber.runtime.Glue; import cucumber.runtime.Glue;
import cucumber.runtime.UnreportedStepExecutor; import cucumber.runtime.UnreportedStepExecutor;
import cucumber.runtime.TagExpression;
import cucumber.runtime.io.Resource; import cucumber.runtime.io.Resource;
import cucumber.runtime.io.ResourceLoader; import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder; import cucumber.runtime.io.ResourceLoaderClassFinder;
import cucumber.runtime.snippets.FunctionNameGenerator; import cucumber.runtime.snippets.FunctionNameGenerator;
import cucumber.runtime.snippets.SnippetGenerator; import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.TagExpression; import gherkin.pickles.PickleStep;
import gherkin.formatter.model.Step;
import groovy.lang.Binding; import groovy.lang.Binding;
import groovy.lang.Closure; import groovy.lang.Closure;
import groovy.lang.GroovyShell; import groovy.lang.GroovyShell;
Expand Down Expand Up @@ -128,8 +128,8 @@ public void disposeWorld() {
} }


@Override @Override
public String getSnippet(Step step, FunctionNameGenerator functionNameGenerator) { public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return snippetGenerator.getSnippet(step, null); return snippetGenerator.getSnippet(step, keyword, null);
} }


public void addStepDefinition(Pattern regexp, long timeoutMillis, Closure body) { public void addStepDefinition(Pattern regexp, long timeoutMillis, Closure body) {
Expand Down
Expand Up @@ -3,8 +3,8 @@
import cucumber.api.Scenario; import cucumber.api.Scenario;
import cucumber.runtime.HookDefinition; import cucumber.runtime.HookDefinition;
import cucumber.runtime.Timeout; import cucumber.runtime.Timeout;
import gherkin.TagExpression; import cucumber.runtime.TagExpression;
import gherkin.formatter.model.Tag; import gherkin.pickles.PickleTag;
import groovy.lang.Closure; import groovy.lang.Closure;


import java.util.Collection; import java.util.Collection;
Expand Down Expand Up @@ -50,7 +50,7 @@ public Object call() throws Throwable {
} }


@Override @Override
public boolean matches(Collection<Tag> tags) { public boolean matches(Collection<PickleTag> tags) {
return tagExpression.evaluate(tags); return tagExpression.evaluate(tags);
} }


Expand Down
@@ -1,12 +1,11 @@
package cucumber.runtime.groovy; package cucumber.runtime.groovy;


import cucumber.runtime.Argument;
import cucumber.runtime.JdkPatternArgumentMatcher; import cucumber.runtime.JdkPatternArgumentMatcher;
import cucumber.runtime.ParameterInfo; import cucumber.runtime.ParameterInfo;
import cucumber.runtime.StepDefinition; import cucumber.runtime.StepDefinition;
import cucumber.runtime.Timeout; import cucumber.runtime.Timeout;
import gherkin.I18n; import gherkin.pickles.PickleStep;
import gherkin.formatter.Argument;
import gherkin.formatter.model.Step;
import groovy.lang.Closure; import groovy.lang.Closure;
import org.codehaus.groovy.runtime.StackTraceUtils; import org.codehaus.groovy.runtime.StackTraceUtils;


Expand Down Expand Up @@ -36,8 +35,8 @@ public GroovyStepDefinition(Pattern pattern, long timeoutMillis, Closure body, S
this.parameterInfos = getParameterInfos(); this.parameterInfos = getParameterInfos();
} }


public List<Argument> matchedArguments(Step step) { public List<Argument> matchedArguments(PickleStep step) {
return argumentMatcher.argumentsFrom(step.getName()); return argumentMatcher.argumentsFrom(step.getText());
} }


public String getLocation(boolean detail) { public String getLocation(boolean detail) {
Expand All @@ -59,7 +58,7 @@ private List<ParameterInfo> getParameterInfos() {
return ParameterInfo.fromTypes(parameterTypes); return ParameterInfo.fromTypes(parameterTypes);
} }


public void execute(I18n i18n, final Object[] args) throws Throwable { public void execute(String language, final Object[] args) throws Throwable {
try { try {
Timeout.timeout(new Timeout.Callback<Object>() { Timeout.timeout(new Timeout.Callback<Object>() {
@Override @Override
Expand Down
34 changes: 19 additions & 15 deletions groovy/src/test/java/cucumber/runtime/groovy/GroovySnippetTest.java
@@ -1,10 +1,13 @@
package cucumber.runtime.groovy; package cucumber.runtime.groovy;


import cucumber.runtime.snippets.SnippetGenerator; import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.formatter.model.Comment; import gherkin.pickles.Argument;
import gherkin.formatter.model.DataTableRow; import gherkin.pickles.PickleCell;
import gherkin.formatter.model.DocString; import gherkin.pickles.PickleLocation;
import gherkin.formatter.model.Step; import gherkin.pickles.PickleRow;
import gherkin.pickles.PickleStep;
import gherkin.pickles.PickleString;
import gherkin.pickles.PickleTable;
import org.junit.Test; import org.junit.Test;


import java.util.Collections; import java.util.Collections;
Expand All @@ -15,7 +18,8 @@


public class GroovySnippetTest { public class GroovySnippetTest {


private static final List<Comment> NO_COMMENTS = Collections.emptyList(); private static final List<Argument> NO_ARGUMENTS = Collections.emptyList();
private static final List<PickleLocation> NO_LOCATIONS = Collections.emptyList();


@Test @Test
public void generatesPlainSnippet() { public void generatesPlainSnippet() {
Expand Down Expand Up @@ -96,7 +100,7 @@ public void generatesSnippetWithDocString() {
" // Write code here that turns the phrase above into concrete actions\n" + " // Write code here that turns the phrase above into concrete actions\n" +
" throw new PendingException()\n" + " throw new PendingException()\n" +
"}\n"; "}\n";
assertEquals(expected, snippetForDocString("I have:", new DocString("text/plain", "hello", 1))); assertEquals(expected, snippetForDocString("I have:", new PickleString(null, "hello")));
} }


@Test @Test
Expand All @@ -106,7 +110,7 @@ public void generatesSnippetWithDataTable() {
" // Write code here that turns the phrase above into concrete actions\n" + " // Write code here that turns the phrase above into concrete actions\n" +
" throw new PendingException()\n" + " throw new PendingException()\n" +
"}\n"; "}\n";
List<DataTableRow> dataTable = asList(new DataTableRow(NO_COMMENTS, asList("col1"), 1)); PickleTable dataTable = new PickleTable(asList(new PickleRow(asList(new PickleCell(null, "col1")))));
assertEquals(expected, snippetForDataTable("I have:", dataTable)); assertEquals(expected, snippetForDataTable("I have:", dataTable));
} }


Expand All @@ -122,17 +126,17 @@ public void generateSnippetWithEscapedEscapeCharacter() {
} }


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


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


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

0 comments on commit 46a35ba

Please sign in to comment.