Created
December 9, 2011 09:03
-
-
Save pettermahlen/1450821 to your computer and use it in GitHub Desktop.
Snippet Generator Fix for issue cucumber-jvm 117
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
diff --git a/core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java b/core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java | |
index 2a20435..ce647d8 100644 | |
--- a/core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java | |
+++ b/core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java | |
@@ -87,11 +87,14 @@ public abstract class SnippetGenerator { | |
protected String sanitizeFunctionName(String functionName) { | |
StringBuilder sanitized = new StringBuilder(); | |
- sanitized.append(Character.isJavaIdentifierStart(functionName.charAt(0)) ? functionName.charAt(0) : SUBST); | |
- for (int i = 1; i < functionName.length(); i++) { | |
- if (Character.isJavaIdentifierPart(functionName.charAt(i))) { | |
- sanitized.append(functionName.charAt(i)); | |
- } else if (sanitized.charAt(sanitized.length() - 1) != SUBST && i != functionName.length() - 1) { | |
+ | |
+ String trimmedFunctionName = functionName.trim(); | |
+ | |
+ sanitized.append(Character.isJavaIdentifierStart(trimmedFunctionName.charAt(0)) ? trimmedFunctionName.charAt(0) : SUBST); | |
+ for (int i = 1; i < trimmedFunctionName.length(); i++) { | |
+ if (Character.isJavaIdentifierPart(trimmedFunctionName.charAt(i))) { | |
+ sanitized.append(trimmedFunctionName.charAt(i)); | |
+ } else if (sanitized.charAt(sanitized.length() - 1) != SUBST && i != trimmedFunctionName.length() - 1) { | |
sanitized.append(SUBST); | |
} | |
} | |
@@ -120,14 +123,22 @@ public abstract class SnippetGenerator { | |
} | |
int pos = 0; | |
while (true) { | |
+ int matchedLength = 1; | |
+ | |
for (int i = 0; i < matchers.length; i++) { | |
Matcher m = matchers[i].region(pos, name.length()); | |
if (m.lookingAt()) { | |
Class<?> typeForSignature = argumentPatterns()[i].type(); | |
argTypes.add(typeForSignature); | |
+ | |
+ matchedLength = m.group().length(); | |
+ break; | |
} | |
} | |
- if (pos++ == name.length()) { | |
+ | |
+ pos += matchedLength; | |
+ | |
+ if (pos == name.length()) { | |
break; | |
} | |
} | |
diff --git a/java/src/test/java/cucumber/runtime/java/JavaSnippetGeneratorTest.java b/java/src/test/java/cucumber/runtime/java/JavaSnippetGeneratorTest.java | |
index fd384c6..b78e6ca 100644 | |
--- a/java/src/test/java/cucumber/runtime/java/JavaSnippetGeneratorTest.java | |
+++ b/java/src/test/java/cucumber/runtime/java/JavaSnippetGeneratorTest.java | |
@@ -13,20 +13,20 @@ public class JavaSnippetGeneratorTest { | |
@Test | |
public void generatesPlainSnippet() { | |
String expected = "" + | |
- "@Given(\"^I have (\\\\d+) cukes in my \\\"([^\\\"]*)\\\" belly$\")\n" + | |
- "public void I_have_cukes_in_my_belly(int arg1, String arg2) {\n" + | |
- " // Express the Regexp above with the code you wish you had\n" + | |
- "}\n"; | |
+ "@Given(\"^I have (\\\\d+) cukes in my \\\"([^\\\"]*)\\\" belly$\")\n" + | |
+ "public void I_have_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$\")\n" + | |
- "public void before_after(int arg1) {\n" + | |
- " // Express the Regexp above with the code you wish you had\n" + | |
- "}\n"; | |
+ "@Given(\"^before (\\\\d+) after$\")\n" + | |
+ "public void before_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); | |
} | |
@@ -34,13 +34,24 @@ public class JavaSnippetGeneratorTest { | |
@Test | |
public void generatesCopyPasteReadySnippetWhenStepHasIllegalJavaIdentifierChars() { | |
String expected = "" + | |
- "@Given(\"^I have (\\\\d+) cukes in: my \\\"([^\\\"]*)\\\" red-belly!$\")\n" + | |
- "public void I_have_cukes_in_my_red_belly(int arg1, String arg2) {\n" + | |
- " // Express the Regexp above with the code you wish you had\n" + | |
- "}\n"; | |
+ "@Given(\"^I have (\\\\d+) cukes in: my \\\"([^\\\"]*)\\\" red-belly!$\")\n" + | |
+ "public void I_have_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 \\\"([^\\\"]*)\\\"$\")\n" + | |
+ "public void 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 } ] }\"")); | |
+ } | |
+ | |
private String snippetFor(String name) { | |
Step step = new Step(Collections.<Comment>emptyList(), "Given ", name, 0, null, null); | |
return new JavaSnippetGenerator(step).getSnippet(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment