diff --git a/clojure/src/main/java/cucumber/runtime/clojure/ClojureBackend.java b/clojure/src/main/java/cucumber/runtime/clojure/ClojureBackend.java index dfa00bd761..7ce6df1767 100644 --- a/clojure/src/main/java/cucumber/runtime/clojure/ClojureBackend.java +++ b/clojure/src/main/java/cucumber/runtime/clojure/ClojureBackend.java @@ -1,13 +1,14 @@ package cucumber.runtime.clojure; +import clojure.lang.Compiler; import clojure.lang.IFn; import clojure.lang.RT; -import clojure.lang.Compiler; import cucumber.io.Resource; import cucumber.io.ResourceLoader; import cucumber.runtime.Backend; import cucumber.runtime.CucumberException; import cucumber.runtime.World; +import cucumber.runtime.snippets.SnippetGenerator; import gherkin.formatter.model.Step; import java.io.IOException; @@ -17,6 +18,7 @@ public class ClojureBackend implements Backend { private static ClojureBackend instance; + private final SnippetGenerator snippetGenerator = new SnippetGenerator(new ClojureSnippet()); private final ResourceLoader resourceLoader; private World world; @@ -60,7 +62,7 @@ public void disposeWorld() { @Override public String getSnippet(Step step) { - return new ClojureSnippetGenerator(step).getSnippet(); + return snippetGenerator.getSnippet(step); } private StackTraceElement stepDefLocation(String interpreterClassName, String interpreterMethodName) { diff --git a/clojure/src/main/java/cucumber/runtime/clojure/ClojureSnippet.java b/clojure/src/main/java/cucumber/runtime/clojure/ClojureSnippet.java new file mode 100644 index 0000000000..bfb417fcf0 --- /dev/null +++ b/clojure/src/main/java/cucumber/runtime/clojure/ClojureSnippet.java @@ -0,0 +1,37 @@ +package cucumber.runtime.clojure; + +import cucumber.runtime.snippets.Snippet; + +import java.util.List; + +import static cucumber.runtime.snippets.SnippetGenerator.untypedArguments; + +public class ClojureSnippet implements Snippet { + @Override + public String template() { + return "({0} #\"{1}\"\n" + + " (fn [{3}]\n" + + " \" {4}\n" + // TODO: The " should be a ', but that causes a propblem with MessageFormat escaping {4}. Need to read up on MessageFormat docs. + " ))\n"; + } + + @Override + public String arguments(List> argumentTypes) { + return untypedArguments(argumentTypes); + } + + @Override + public String namedGroupStart() { + return null; + } + + @Override + public String namedGroupEnd() { + return null; + } + + @Override + public String escapePattern(String pattern) { + return pattern; + } +} diff --git a/clojure/src/main/java/cucumber/runtime/clojure/ClojureSnippetGenerator.java b/clojure/src/main/java/cucumber/runtime/clojure/ClojureSnippetGenerator.java deleted file mode 100644 index 09cc0e4389..0000000000 --- a/clojure/src/main/java/cucumber/runtime/clojure/ClojureSnippetGenerator.java +++ /dev/null @@ -1,25 +0,0 @@ -package cucumber.runtime.clojure; - -import cucumber.runtime.snippets.SnippetGenerator; -import gherkin.formatter.model.Step; - -import java.util.List; - -public class ClojureSnippetGenerator extends SnippetGenerator { - protected ClojureSnippetGenerator(Step step) { - super(step); - } - - @Override - protected String template() { - return "({0} #\"{1}\"\n" + - " (fn [{3}]\n" + - " \" {4}\n" + // TODO: The " should be a ', but that causes a propblem with MessageFormat escaping {4}. Need to read up on MessageFormat docs. - " ))\n"; - } - - @Override - protected String arguments(List> argumentTypes) { - return untypedArguments(argumentTypes); - } -} diff --git a/core/src/main/java/cucumber/runtime/snippets/Snippet.java b/core/src/main/java/cucumber/runtime/snippets/Snippet.java new file mode 100644 index 0000000000..24b2ae4b29 --- /dev/null +++ b/core/src/main/java/cucumber/runtime/snippets/Snippet.java @@ -0,0 +1,19 @@ +package cucumber.runtime.snippets; + +import java.util.List; + +public interface Snippet { + String template(); + + String arguments(List> argumentTypes); + + /** + * Langauges that don't support named capture groups should return null. + * @return the start of a named group + */ + String namedGroupStart(); + + String namedGroupEnd(); + + String escapePattern(String pattern); +} diff --git a/core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java b/core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java index 5c6939bc70..201abfc850 100644 --- a/core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java +++ b/core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java @@ -21,7 +21,7 @@ *
  • {4} : Hint comment
  • * */ -public abstract class SnippetGenerator { +public final class SnippetGenerator { private static final ArgumentPattern[] DEFAULT_ARGUMENT_PATTERNS = new ArgumentPattern[]{ new ArgumentPattern(Pattern.compile("\"([^\"]*)\""), String.class), new ArgumentPattern(Pattern.compile("(\\d+)"), Integer.TYPE) @@ -30,46 +30,22 @@ public abstract class SnippetGenerator { private static final String HINT = "Express the Regexp above with the code you wish you had"; private static final Character SUBST = '_'; - private final Step step; - private final String namedGroupStart; - private final String namedGroupEnd; - - /** - * Constructor for languages that do not support named capture groups, such as Java. - * - * @param step the step to generate snippet for. - */ - protected SnippetGenerator(Step step) { - this(step, null, null); - } + private final Snippet snippet; - /** - * Constructor for langauges that support named capture groups, such ash Ioke. - * - * @param step the step to generate snippet for. - * @param namedGroupStart beginning of named group, for example "{arg". - * @param namedGroupEnd end of named group, for example "}". - */ - protected SnippetGenerator(Step step, String namedGroupStart, String namedGroupEnd) { - this.step = step; - this.namedGroupStart = namedGroupStart; - this.namedGroupEnd = namedGroupEnd; + public SnippetGenerator(Snippet snippet) { + this.snippet = snippet; } - public String getSnippet() { - return MessageFormat.format(template(), I18n.codeKeywordFor(step.getKeyword()), patternFor(step.getName()), functionName(step.getName()), arguments(argumentTypes(step.getName())), HINT); + 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); } - protected abstract String template(); - - protected abstract String arguments(List> argumentTypes); - protected String patternFor(String stepName) { String pattern = stepName; for (ArgumentPattern argumentPattern : argumentPatterns()) { pattern = argumentPattern.replaceMatchesWithGroups(pattern); } - if (namedGroupStart != null) { + if (snippet.namedGroupStart() != null) { pattern = withNamedGroups(pattern); } @@ -107,7 +83,7 @@ private String withNamedGroups(String snippetPattern) { StringBuffer sb = new StringBuffer(); int n = 1; while (m.find()) { - m.appendReplacement(sb, "(" + namedGroupStart + n++ + namedGroupEnd); + m.appendReplacement(sb, "(" + snippet.namedGroupStart() + n++ + snippet.namedGroupEnd()); } m.appendTail(sb); @@ -149,7 +125,7 @@ protected ArgumentPattern[] argumentPatterns() { return DEFAULT_ARGUMENT_PATTERNS; } - protected String untypedArguments(List> argumentTypes) { + public static String untypedArguments(List> argumentTypes) { StringBuilder sb = new StringBuilder(); for (int n = 0; n < argumentTypes.size(); n++) { if (n > 0) { diff --git a/core/src/test/java/cucumber/runtime/UndefinedStepsTrackerTest.java b/core/src/test/java/cucumber/runtime/UndefinedStepsTrackerTest.java index fe88c357c7..7e99c048df 100644 --- a/core/src/test/java/cucumber/runtime/UndefinedStepsTrackerTest.java +++ b/core/src/test/java/cucumber/runtime/UndefinedStepsTrackerTest.java @@ -1,5 +1,6 @@ package cucumber.runtime; +import cucumber.runtime.snippets.Snippet; import cucumber.runtime.snippets.SnippetGenerator; import gherkin.formatter.model.Step; import org.junit.Test; @@ -62,23 +63,34 @@ public void disposeWorld() { @Override public String getSnippet(Step step) { - return new TestSnippetGenerator(step).getSnippet(); + return new SnippetGenerator(new TestSnippet()).getSnippet(step); } } - private class TestSnippetGenerator extends SnippetGenerator { - protected TestSnippetGenerator(Step step) { - super(step); - } - + private class TestSnippet implements Snippet { @Override - protected String template() { + public String template() { return "{0} {1}"; } @Override - protected String arguments(List> argumentTypes) { + public String arguments(List> argumentTypes) { return argumentTypes.toString(); } + + @Override + public String namedGroupStart() { + return null; + } + + @Override + public String namedGroupEnd() { + return null; + } + + @Override + public String escapePattern(String pattern) { + return pattern; + } } } diff --git a/groovy/src/main/java/cucumber/runtime/groovy/GroovyBackend.java b/groovy/src/main/java/cucumber/runtime/groovy/GroovyBackend.java index 5be31a0268..2528959196 100644 --- a/groovy/src/main/java/cucumber/runtime/groovy/GroovyBackend.java +++ b/groovy/src/main/java/cucumber/runtime/groovy/GroovyBackend.java @@ -5,6 +5,7 @@ import cucumber.runtime.Backend; import cucumber.runtime.CucumberException; import cucumber.runtime.World; +import cucumber.runtime.snippets.SnippetGenerator; import gherkin.TagExpression; import gherkin.formatter.model.Step; import groovy.lang.Binding; @@ -21,6 +22,7 @@ public class GroovyBackend implements Backend { static GroovyBackend instance; + private final SnippetGenerator snippetGenerator = new SnippetGenerator(new GroovySnippet()); private final ResourceLoader resourceLoader; private final GroovyShell shell; private Closure worldClosure; @@ -73,7 +75,7 @@ public void disposeWorld() { @Override public String getSnippet(Step step) { - return new GroovySnippetGenerator(step).getSnippet(); + return snippetGenerator.getSnippet(step); } public void addStepDefinition(Pattern regexp, Closure body) { diff --git a/groovy/src/main/java/cucumber/runtime/groovy/GroovySnippetGenerator.java b/groovy/src/main/java/cucumber/runtime/groovy/GroovySnippet.java similarity index 58% rename from groovy/src/main/java/cucumber/runtime/groovy/GroovySnippetGenerator.java rename to groovy/src/main/java/cucumber/runtime/groovy/GroovySnippet.java index f3d0617fed..036f56f1c3 100644 --- a/groovy/src/main/java/cucumber/runtime/groovy/GroovySnippetGenerator.java +++ b/groovy/src/main/java/cucumber/runtime/groovy/GroovySnippet.java @@ -1,29 +1,19 @@ package cucumber.runtime.groovy; -import cucumber.runtime.snippets.SnippetGenerator; -import gherkin.formatter.model.Step; +import cucumber.runtime.snippets.Snippet; import java.util.List; -public class GroovySnippetGenerator extends SnippetGenerator { - public GroovySnippetGenerator(Step step) { - super(step); - } - +public class GroovySnippet implements Snippet { @Override - protected String patternFor(String stepName) { - return super.patternFor(stepName).replaceAll("\"", "\\\\\""); - } - - @Override - protected String template() { + public String template() { return "{0}(~\"{1}\") '{' {3}->\n" + " // {4}\n" + "'}'\n"; } @Override - protected String arguments(List> argumentTypes) { + public String arguments(List> argumentTypes) { StringBuilder sb = new StringBuilder(); int n = 1; for (Class argType : argumentTypes) { @@ -37,4 +27,19 @@ protected String arguments(List> argumentTypes) { } return sb.toString(); } + + @Override + public String namedGroupStart() { + return null; + } + + @Override + public String namedGroupEnd() { + return null; + } + + @Override + public String escapePattern(String pattern) { + return pattern.replaceAll("\"", "\\\\\""); + } } diff --git a/ioke/src/main/java/cucumber/runtime/ioke/IokeBackend.java b/ioke/src/main/java/cucumber/runtime/ioke/IokeBackend.java index b271326328..ce73bdd060 100644 --- a/ioke/src/main/java/cucumber/runtime/ioke/IokeBackend.java +++ b/ioke/src/main/java/cucumber/runtime/ioke/IokeBackend.java @@ -5,6 +5,7 @@ import cucumber.runtime.Backend; import cucumber.runtime.CucumberException; import cucumber.runtime.World; +import cucumber.runtime.snippets.SnippetGenerator; import cucumber.table.DataTable; import gherkin.formatter.model.Step; import ioke.lang.IokeObject; @@ -17,6 +18,7 @@ import java.util.List; public class IokeBackend implements Backend { + private final SnippetGenerator snippetGenerator = new SnippetGenerator(new IokeSnippet()); private final ResourceLoader resourceLoader; private final Runtime ioke; private final List failureRescues; @@ -65,7 +67,7 @@ public void disposeWorld() { @Override public String getSnippet(Step step) { - return new IokeSnippetGenerator(step).getSnippet(); + return snippetGenerator.getSnippet(step); } public void addStepDefinition(Object iokeStepDefObject) throws Throwable { diff --git a/ioke/src/main/java/cucumber/runtime/ioke/IokeSnippet.java b/ioke/src/main/java/cucumber/runtime/ioke/IokeSnippet.java new file mode 100644 index 0000000000..0ad3610bb0 --- /dev/null +++ b/ioke/src/main/java/cucumber/runtime/ioke/IokeSnippet.java @@ -0,0 +1,35 @@ +package cucumber.runtime.ioke; + +import cucumber.runtime.snippets.Snippet; + +import java.util.List; + +public class IokeSnippet implements Snippet { + + @Override + public String template() { + return "{0}(#/{1}/,\n" + + " # {4}\n" + + ")\n"; + } + + @Override + public String arguments(List> argumentTypes) { + return null; // not used + } + + @Override + public String namedGroupStart() { + return "{arg"; + } + + @Override + public String namedGroupEnd() { + return "}"; + } + + @Override + public String escapePattern(String pattern) { + return pattern; + } +} diff --git a/ioke/src/main/java/cucumber/runtime/ioke/IokeSnippetGenerator.java b/ioke/src/main/java/cucumber/runtime/ioke/IokeSnippetGenerator.java deleted file mode 100644 index 01e35e8479..0000000000 --- a/ioke/src/main/java/cucumber/runtime/ioke/IokeSnippetGenerator.java +++ /dev/null @@ -1,24 +0,0 @@ -package cucumber.runtime.ioke; - -import cucumber.runtime.snippets.SnippetGenerator; -import gherkin.formatter.model.Step; - -import java.util.List; - -public class IokeSnippetGenerator extends SnippetGenerator { - protected IokeSnippetGenerator(Step step) { - super(step, "{arg", "}"); - } - - @Override - protected String template() { - return "{0}(#/{1}/,\n" + - " # {4}\n" + - ")\n"; - } - - @Override - protected String arguments(List> argumentTypes) { - return null; // not used - } -} diff --git a/ioke/src/test/java/cucumber/runtime/ioke/IokeSnippetGeneratorTest.java b/ioke/src/test/java/cucumber/runtime/ioke/IokeSnippetTest.java similarity index 80% rename from ioke/src/test/java/cucumber/runtime/ioke/IokeSnippetGeneratorTest.java rename to ioke/src/test/java/cucumber/runtime/ioke/IokeSnippetTest.java index 9dc69c0899..c91c204ed6 100644 --- a/ioke/src/test/java/cucumber/runtime/ioke/IokeSnippetGeneratorTest.java +++ b/ioke/src/test/java/cucumber/runtime/ioke/IokeSnippetTest.java @@ -1,5 +1,6 @@ package cucumber.runtime.ioke; +import cucumber.runtime.snippets.SnippetGenerator; import gherkin.formatter.model.Comment; import gherkin.formatter.model.Step; import org.junit.Test; @@ -8,11 +9,11 @@ import static org.junit.Assert.assertEquals; -public class IokeSnippetGeneratorTest { +public class IokeSnippetTest { @Test public void generatesPlainSnippet() { Step step = new Step(Collections.emptyList(), "Given ", "I have 4 cukes in my \"big\" belly", 0, null, null); - String snippet = new IokeSnippetGenerator(step).getSnippet(); + String snippet = new SnippetGenerator(new IokeSnippet()).getSnippet(step); String expected = "" + "Given(#/^I have ({arg1}\\d+) cukes in my \"({arg2}[^\"]*)\" belly$/,\n" + " # Express the Regexp above with the code you wish you had\n" + diff --git a/java/src/main/java/cucumber/runtime/java/JavaBackend.java b/java/src/main/java/cucumber/runtime/java/JavaBackend.java index 7a0d170a03..c3590cfed9 100644 --- a/java/src/main/java/cucumber/runtime/java/JavaBackend.java +++ b/java/src/main/java/cucumber/runtime/java/JavaBackend.java @@ -9,6 +9,7 @@ import cucumber.runtime.Backend; import cucumber.runtime.CucumberException; import cucumber.runtime.World; +import cucumber.runtime.snippets.SnippetGenerator; import gherkin.formatter.model.Step; import java.lang.annotation.Annotation; @@ -21,6 +22,7 @@ import java.util.regex.Pattern; public class JavaBackend implements Backend { + private final SnippetGenerator snippetGenerator = new SnippetGenerator(new JavaSnippet()); private final Set stepDefinitionClasses = new HashSet(); private final ObjectFactory objectFactory; private final ClasspathMethodScanner classpathMethodScanner = new ClasspathMethodScanner(); @@ -58,7 +60,7 @@ public void disposeWorld() { @Override public String getSnippet(Step step) { - return new JavaSnippetGenerator(step).getSnippet(); + return snippetGenerator.getSnippet(step); } void addStepDefinition(Annotation annotation, Method method) { diff --git a/java/src/main/java/cucumber/runtime/java/JavaSnippetGenerator.java b/java/src/main/java/cucumber/runtime/java/JavaSnippet.java similarity index 61% rename from java/src/main/java/cucumber/runtime/java/JavaSnippetGenerator.java rename to java/src/main/java/cucumber/runtime/java/JavaSnippet.java index 116b78d660..fbe05d0b2f 100644 --- a/java/src/main/java/cucumber/runtime/java/JavaSnippetGenerator.java +++ b/java/src/main/java/cucumber/runtime/java/JavaSnippet.java @@ -1,24 +1,13 @@ package cucumber.runtime.java; -import cucumber.runtime.snippets.SnippetGenerator; -import gherkin.formatter.model.Step; +import cucumber.runtime.snippets.Snippet; import java.util.List; -public class JavaSnippetGenerator extends SnippetGenerator { - - public JavaSnippetGenerator(Step step) { - super(step); - } - - @Override - protected String patternFor(String stepName) { - String pattern = super.patternFor(stepName); - return pattern.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\""); - } +public class JavaSnippet implements Snippet { @Override - protected String arguments(List> argumentTypes) { + public String arguments(List> argumentTypes) { StringBuilder sb = new StringBuilder(); int n = 1; for (Class argType : argumentTypes) { @@ -31,11 +20,25 @@ protected String arguments(List> argumentTypes) { } @Override - protected String template() { + public String template() { return "@{0}(\"{1}\")\n" + "public void {2}({3}) '{'\n" + " // {4}\n" + "'}'\n"; } + @Override + public String namedGroupStart() { + return null; + } + + @Override + public String namedGroupEnd() { + return null; + } + + @Override + public String escapePattern(String pattern) { + return pattern.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\""); + } } diff --git a/java/src/test/java/cucumber/runtime/java/JavaSnippetGeneratorTest.java b/java/src/test/java/cucumber/runtime/java/JavaSnippetTest.java similarity index 93% rename from java/src/test/java/cucumber/runtime/java/JavaSnippetGeneratorTest.java rename to java/src/test/java/cucumber/runtime/java/JavaSnippetTest.java index 449eb5ad99..c8da8bd378 100644 --- a/java/src/test/java/cucumber/runtime/java/JavaSnippetGeneratorTest.java +++ b/java/src/test/java/cucumber/runtime/java/JavaSnippetTest.java @@ -1,5 +1,6 @@ package cucumber.runtime.java; +import cucumber.runtime.snippets.SnippetGenerator; import gherkin.formatter.model.Comment; import gherkin.formatter.model.Step; import org.junit.Test; @@ -8,7 +9,7 @@ import static org.junit.Assert.assertEquals; -public class JavaSnippetGeneratorTest { +public class JavaSnippetTest { @Test public void generatesPlainSnippet() { @@ -54,6 +55,6 @@ public void generatesCopyPasteReadySnippetWhenStepHasIntegersInsideStringParamet private String snippetFor(String name) { Step step = new Step(Collections.emptyList(), "Given ", name, 0, null, null); - return new JavaSnippetGenerator(step).getSnippet(); + return new SnippetGenerator(new JavaSnippet()).getSnippet(step); } } \ No newline at end of file diff --git a/jruby/src/main/java/cucumber/runtime/jruby/JRubyBackend.java b/jruby/src/main/java/cucumber/runtime/jruby/JRubyBackend.java index 39ad1086c3..5df0cbaa86 100644 --- a/jruby/src/main/java/cucumber/runtime/jruby/JRubyBackend.java +++ b/jruby/src/main/java/cucumber/runtime/jruby/JRubyBackend.java @@ -6,7 +6,7 @@ import cucumber.runtime.CucumberException; import cucumber.runtime.PendingException; import cucumber.runtime.World; -import gherkin.formatter.model.Comment; +import cucumber.runtime.snippets.SnippetGenerator; import gherkin.formatter.model.Step; import org.jruby.CompatVersion; import org.jruby.RubyObject; @@ -16,13 +16,13 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; -import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Properties; public class JRubyBackend implements Backend { private static final String DSL = "/cucumber/runtime/jruby/dsl.rb"; + private final SnippetGenerator snippetGenerator = new SnippetGenerator(new JRubySnippet()); private final ScriptingContainer jruby = new ScriptingContainer(); private World world; private ResourceLoader resourceLoader; @@ -80,7 +80,7 @@ public void disposeWorld() { @Override public String getSnippet(Step step) { - return new JRubySnippetGenerator(step).getSnippet(); + return snippetGenerator.getSnippet(step); } public void pending(String reason) throws PendingException { diff --git a/jruby/src/main/java/cucumber/runtime/jruby/JRubySnippetGenerator.java b/jruby/src/main/java/cucumber/runtime/jruby/JRubySnippet.java similarity index 57% rename from jruby/src/main/java/cucumber/runtime/jruby/JRubySnippetGenerator.java rename to jruby/src/main/java/cucumber/runtime/jruby/JRubySnippet.java index f0fa7a43de..aa74c95f76 100644 --- a/jruby/src/main/java/cucumber/runtime/jruby/JRubySnippetGenerator.java +++ b/jruby/src/main/java/cucumber/runtime/jruby/JRubySnippet.java @@ -1,17 +1,12 @@ package cucumber.runtime.jruby; -import cucumber.runtime.snippets.SnippetGenerator; -import gherkin.formatter.model.Step; +import cucumber.runtime.snippets.Snippet; import java.util.List; -public class JRubySnippetGenerator extends SnippetGenerator { - protected JRubySnippetGenerator(Step step) { - super(step); - } - +public class JRubySnippet implements Snippet { @Override - protected String template() { + public String template() { return "{0} '/'{1}'/' do {3}\n" + " # {4}\n" + " pending\n" + @@ -19,7 +14,7 @@ protected String template() { } @Override - protected String arguments(List> argumentTypes) { + public String arguments(List> argumentTypes) { StringBuilder sb = new StringBuilder(argumentTypes.isEmpty() ? "" : "|"); for (int n = 0; n < argumentTypes.size(); n++) { if (n > 1) { @@ -30,4 +25,20 @@ protected String arguments(List> argumentTypes) { sb.append(argumentTypes.isEmpty() ? "" : "|"); return sb.toString(); } + + @Override + public String namedGroupStart() { + return null; + } + + @Override + public String namedGroupEnd() { + return null; + } + + @Override + public String escapePattern(String pattern) { + return pattern; + } + } diff --git a/jython/src/main/java/cucumber/runtime/jython/JythonBackend.java b/jython/src/main/java/cucumber/runtime/jython/JythonBackend.java index a977752cb2..226cb8c43b 100644 --- a/jython/src/main/java/cucumber/runtime/jython/JythonBackend.java +++ b/jython/src/main/java/cucumber/runtime/jython/JythonBackend.java @@ -5,6 +5,7 @@ import cucumber.runtime.Backend; import cucumber.runtime.CucumberException; import cucumber.runtime.World; +import cucumber.runtime.snippets.SnippetGenerator; import gherkin.formatter.model.Step; import org.python.core.PyInstance; import org.python.core.PyObject; @@ -16,6 +17,7 @@ public class JythonBackend implements Backend { private static final String DSL = "/cucumber/runtime/jython/dsl.py"; + private final SnippetGenerator snippetGenerator = new SnippetGenerator(new JythonSnippet()); private final ResourceLoader resourceLoader; private final PythonInterpreter jython = new PythonInterpreter(); private PyObject pyWorld; @@ -53,7 +55,7 @@ public void disposeWorld() { @Override public String getSnippet(Step step) { - return new JythonSnippetGenerator(step).getSnippet(); + return snippetGenerator.getSnippet(step); } public void registerStepdef(PyInstance stepdef, int arity) { diff --git a/jython/src/main/java/cucumber/runtime/jython/JythonSnippet.java b/jython/src/main/java/cucumber/runtime/jython/JythonSnippet.java new file mode 100644 index 0000000000..e0d4f8aa92 --- /dev/null +++ b/jython/src/main/java/cucumber/runtime/jython/JythonSnippet.java @@ -0,0 +1,39 @@ +package cucumber.runtime.jython; + +import cucumber.runtime.snippets.Snippet; +import cucumber.runtime.snippets.SnippetGenerator; +import gherkin.formatter.model.Step; + +import java.util.List; + +import static cucumber.runtime.snippets.SnippetGenerator.untypedArguments; + +public class JythonSnippet implements Snippet { + + @Override + public String template() { + return "@{0}(''{1}'')\n" + + "def {2}({3}):\n" + + " # {4}\n"; + } + + @Override + public String arguments(List> argumentTypes) { + return untypedArguments(argumentTypes); + } + + @Override + public String namedGroupStart() { + return null; + } + + @Override + public String namedGroupEnd() { + return null; + } + + @Override + public String escapePattern(String pattern) { + return pattern; + } +} diff --git a/jython/src/main/java/cucumber/runtime/jython/JythonSnippetGenerator.java b/jython/src/main/java/cucumber/runtime/jython/JythonSnippetGenerator.java deleted file mode 100644 index 879b2822ef..0000000000 --- a/jython/src/main/java/cucumber/runtime/jython/JythonSnippetGenerator.java +++ /dev/null @@ -1,24 +0,0 @@ -package cucumber.runtime.jython; - -import cucumber.runtime.snippets.SnippetGenerator; -import gherkin.formatter.model.Step; - -import java.util.List; - -public class JythonSnippetGenerator extends SnippetGenerator { - protected JythonSnippetGenerator(Step step) { - super(step); - } - - @Override - protected String template() { - return "@{0}(''{1}'')\n" + - "def {2}({3}):\n" + - " # {4}\n"; - } - - @Override - protected String arguments(List> argumentTypes) { - return untypedArguments(argumentTypes); - } -} diff --git a/rhino/src/main/java/cucumber/runtime/javascript/JavaScriptSnippet.java b/rhino/src/main/java/cucumber/runtime/javascript/JavaScriptSnippet.java new file mode 100644 index 0000000000..0e1ef2aa5c --- /dev/null +++ b/rhino/src/main/java/cucumber/runtime/javascript/JavaScriptSnippet.java @@ -0,0 +1,36 @@ +package cucumber.runtime.javascript; + +import cucumber.runtime.snippets.Snippet; + +import java.util.List; + +import static cucumber.runtime.snippets.SnippetGenerator.untypedArguments; + +public class JavaScriptSnippet implements Snippet { + @Override + public String template() { + return "{0}(/{1}/, function({3}) '{'\n" + + " // {4}\n" + + "'}');\n"; + } + + @Override + public String arguments(List> argumentTypes) { + return untypedArguments(argumentTypes); + } + + @Override + public String namedGroupStart() { + return null; + } + + @Override + public String namedGroupEnd() { + return null; + } + + @Override + public String escapePattern(String pattern) { + return pattern; + } +} diff --git a/rhino/src/main/java/cucumber/runtime/javascript/JavaScriptSnippetGenerator.java b/rhino/src/main/java/cucumber/runtime/javascript/JavaScriptSnippetGenerator.java deleted file mode 100644 index 2100d04929..0000000000 --- a/rhino/src/main/java/cucumber/runtime/javascript/JavaScriptSnippetGenerator.java +++ /dev/null @@ -1,24 +0,0 @@ -package cucumber.runtime.javascript; - -import cucumber.runtime.snippets.SnippetGenerator; -import gherkin.formatter.model.Step; - -import java.util.List; - -public class JavaScriptSnippetGenerator extends SnippetGenerator { - public JavaScriptSnippetGenerator(Step step) { - super(step); - } - - @Override - protected String template() { - return "{0}(/{1}/, function({3}) '{'\n" + - " // {4}\n" + - "'}');\n"; - } - - @Override - protected String arguments(List> argumentTypes) { - return untypedArguments(argumentTypes); - } -} diff --git a/rhino/src/main/java/cucumber/runtime/rhino/RhinoBackend.java b/rhino/src/main/java/cucumber/runtime/rhino/RhinoBackend.java index 9ea00e59b2..a9d1c1191b 100644 --- a/rhino/src/main/java/cucumber/runtime/rhino/RhinoBackend.java +++ b/rhino/src/main/java/cucumber/runtime/rhino/RhinoBackend.java @@ -5,7 +5,8 @@ import cucumber.runtime.Backend; import cucumber.runtime.CucumberException; import cucumber.runtime.World; -import cucumber.runtime.javascript.JavaScriptSnippetGenerator; +import cucumber.runtime.javascript.JavaScriptSnippet; +import cucumber.runtime.snippets.SnippetGenerator; import gherkin.formatter.model.Step; import org.mozilla.javascript.Context; import org.mozilla.javascript.NativeFunction; @@ -20,6 +21,7 @@ public class RhinoBackend implements Backend { private static final String JS_DSL = "/cucumber/runtime/rhino/dsl.js"; + private final SnippetGenerator snippetGenerator = new SnippetGenerator(new JavaScriptSnippet()); private final ResourceLoader resourceLoader; private final Context cx; private final Scriptable scope; @@ -58,7 +60,7 @@ public void disposeWorld() { @Override public String getSnippet(Step step) { - return new JavaScriptSnippetGenerator(step).getSnippet(); + return snippetGenerator.getSnippet(step); } private StackTraceElement stepDefLocation(String extension) { diff --git a/rhino/src/test/java/cucumber/runtime/javascript/JavaScriptSnippetGeneratorTest.java b/rhino/src/test/java/cucumber/runtime/javascript/JavaScriptSnippetTest.java similarity index 81% rename from rhino/src/test/java/cucumber/runtime/javascript/JavaScriptSnippetGeneratorTest.java rename to rhino/src/test/java/cucumber/runtime/javascript/JavaScriptSnippetTest.java index 4a1a73e573..3a8a4cdb37 100644 --- a/rhino/src/test/java/cucumber/runtime/javascript/JavaScriptSnippetGeneratorTest.java +++ b/rhino/src/test/java/cucumber/runtime/javascript/JavaScriptSnippetTest.java @@ -1,5 +1,6 @@ package cucumber.runtime.javascript; +import cucumber.runtime.snippets.SnippetGenerator; import gherkin.formatter.model.Comment; import gherkin.formatter.model.Step; import org.junit.Test; @@ -8,7 +9,7 @@ import static org.junit.Assert.assertEquals; -public class JavaScriptSnippetGeneratorTest { +public class JavaScriptSnippetTest { @Test public void generatesPlainSnippet() { @@ -21,6 +22,6 @@ public void generatesPlainSnippet() { private String snippetFor(String name) { Step step = new Step(Collections.emptyList(), "Given ", name, 0, null, null); - return new JavaScriptSnippetGenerator(step).getSnippet(); + return new SnippetGenerator(new JavaScriptSnippet()).getSnippet(step); } } \ No newline at end of file diff --git a/scala/src/main/scala/cucumber/runtime/ScalaBackend.scala b/scala/src/main/scala/cucumber/runtime/ScalaBackend.scala index fb9237c778..58eb08099d 100644 --- a/scala/src/main/scala/cucumber/runtime/ScalaBackend.scala +++ b/scala/src/main/scala/cucumber/runtime/ScalaBackend.scala @@ -4,13 +4,14 @@ package runtime import _root_.java.util.{List => JList} import gherkin.formatter.model.Step +import snippets.SnippetGenerator import io.ResourceLoader import io.ClasspathResourceLoader import scala.collection.JavaConversions._ class ScalaBackend(ignore:ResourceLoader) extends Backend { - + private var snippetGenerator = new SnippetGenerator(new ScalaSnippetGenerator()) private var instances:Seq[ScalaDsl] = Nil def getStepDefinitions = instances.flatMap(_.stepDefinitions) @@ -23,7 +24,7 @@ class ScalaBackend(ignore:ResourceLoader) extends Backend { instances = Nil } - def getSnippet(step: Step) = new ScalaSnippetGenerator(step).getSnippet + def getSnippet(step: Step) = snippetGenerator.getSnippet(step) def buildWorld(gluePaths: JList[String], world: World) { instances = gluePaths flatMap { new ClasspathResourceLoader().instantiateSubclasses(classOf[ScalaDsl], _, Array(), Array()) } diff --git a/scala/src/main/scala/cucumber/runtime/ScalaSnippetGenerator.scala b/scala/src/main/scala/cucumber/runtime/ScalaSnippet.scala similarity index 69% rename from scala/src/main/scala/cucumber/runtime/ScalaSnippetGenerator.scala rename to scala/src/main/scala/cucumber/runtime/ScalaSnippet.scala index 5066ccd781..eac07908ec 100644 --- a/scala/src/main/scala/cucumber/runtime/ScalaSnippetGenerator.scala +++ b/scala/src/main/scala/cucumber/runtime/ScalaSnippet.scala @@ -1,18 +1,18 @@ package cucumber.runtime -import cucumber.runtime.snippets.SnippetGenerator +import snippets.Snippet import gherkin.formatter.model.Step import collection.JavaConverters._ import _root_.java.util.List -class ScalaSnippetGenerator(step: Step) extends SnippetGenerator(step: Step) { +class ScalaSnippetGenerator extends Snippet { - protected def template() = + def template() = "{0}(\"\"\"{1}\"\"\")'{' ({3}) =>\n" + " //// {4}\n" + "'}'" - protected def arguments(argumentTypes: List[Class[_]]) = { + def arguments(argumentTypes: List[Class[_]]) = { val indexed = argumentTypes.asScala.zipWithIndex def name(clazz: Class[_]) = @@ -29,4 +29,10 @@ class ScalaSnippetGenerator(step: Step) extends SnippetGenerator(step: Step) { named.mkString(", ") } + def namedGroupStart() = null + + def namedGroupEnd() = null + + def escapePattern(pattern:String) = pattern + }