Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camel case Java snippet generation #302

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clojure/src/main/clj/cucumber/runtime/clj.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
(arguments [_ argumentTypes]
(str/replace (SnippetGenerator/untypedArguments argumentTypes)
"," ""))
(sanitizeFunctionName [_ functionName] nil)
(namedGroupStart [_] nil)
(namedGroupEnd [_] nil)
(tableHint [_] nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public String template() {
" (throw (cucumber.runtime.PendingException.)))\n";
}

@Override
public String sanitizeFunctionName(String functionName) {
return null;
}

@Override
public String tableHint() {
return null;
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/cucumber/runtime/snippets/Snippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public interface Snippet {
*/
String template();

/**
* @return a name suitable for defining a function in the selected programming language (for languages that don't define a function, e.g. JavaScript, this can be null))
*/
String sanitizeFunctionName(String functionName);

/**
* @return a hint about alternative ways to declare a table argument
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public final class SnippetGenerator {
};

private static final String REGEXP_HINT = "Express the Regexp above with the code you wish you had";
private static final Character SUBST = '_';

private final Snippet snippet;

Expand Down Expand Up @@ -67,25 +66,10 @@ private String functionName(String name) {
for (ArgumentPattern argumentPattern : argumentPatterns()) {
functionName = argumentPattern.replaceMatchesWithSpace(functionName);
}
functionName = sanitizeFunctionName(functionName);
functionName = snippet.sanitizeFunctionName(functionName);
return functionName;
}

protected String sanitizeFunctionName(String functionName) {
StringBuilder sanitized = new StringBuilder();

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);
}
}
return sanitized.toString();
}

private String withNamedGroups(String snippetPattern) {
Matcher m = GROUP_PATTERN.matcher(snippetPattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public String template() {
return "{0} {1}";
}

@Override
public String sanitizeFunctionName(String functionName) {
return null;
}

@Override
public String tableHint() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public String template() {
"'}'\n";
}

@Override
public String sanitizeFunctionName(String functionName) {
return null;
}

@Override
public String tableHint() {
return null;
Expand Down
5 changes: 5 additions & 0 deletions ioke/src/main/java/cucumber/runtime/ioke/IokeSnippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public String template() {
")\n";
}

@Override
public String sanitizeFunctionName(String functionName) {
return null;
}

@Override
public String tableHint() {
return null;
Expand Down
24 changes: 24 additions & 0 deletions java/src/main/java/cucumber/runtime/java/JavaSnippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

public class JavaSnippet implements Snippet {

private static final Character SAFE_START_CHAR = '_';

@Override
public String arguments(List<Class<?>> argumentTypes) {
StringBuilder sb = new StringBuilder();
Expand All @@ -28,6 +30,28 @@ public String template() {
"'}'\n";
}

@Override
public String sanitizeFunctionName(String functionName) {
StringBuilder sanitized = new StringBuilder();

String trimmedFunctionName = functionName.trim();

Character startChar = trimmedFunctionName.charAt(0);
sanitized.append(Character.isJavaIdentifierStart(startChar) ? startChar.toString().toLowerCase() : SAFE_START_CHAR);

boolean previousCharEndsWord = false;
for (int i = 1; i < trimmedFunctionName.length(); i++) {
Character nextChar = trimmedFunctionName.charAt(i);
if (Character.isJavaIdentifierPart(nextChar)) {
sanitized.append(previousCharEndsWord ? nextChar.toString().toUpperCase() : nextChar.toString().toLowerCase());
previousCharEndsWord = false;
} else {
previousCharEndsWord = true;
}
}
return sanitized.toString();
}

@Override
public String tableHint() {
return " // For automatic conversion, change DataTable to List<YourType>\n";
Expand Down
20 changes: 10 additions & 10 deletions java/src/test/java/cucumber/runtime/java/JavaSnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class JavaSnippetTest {
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" +
"public void iHaveCukesInMyBelly(int arg1, String arg2) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
" throw new PendingException();\n" +
"}\n";
Expand All @@ -33,7 +33,7 @@ public void generatesPlainSnippet() {
public void generatesCopyPasteReadyStepSnippetForNumberParameters() throws Exception {
String expected = "" +
"@Given(\"^before (\\\\d+) after$\")\n" +
"public void before_after(int arg1) {\n" +
"public void beforeAfter(int arg1) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
" throw new PendingException();\n" +
"}\n";
Expand All @@ -45,7 +45,7 @@ public void generatesCopyPasteReadyStepSnippetForNumberParameters() throws Excep
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" +
"public void iHaveCukesInMyRedBelly(int arg1, String arg2) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
" throw new PendingException();\n" +
"}\n";
Expand All @@ -57,7 +57,7 @@ public void generatesCopyPasteReadySnippetWhenStepHasIllegalJavaIdentifierChars(
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" +
"public void theDiSystemReceivesAMessageSaying(String arg1) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
" throw new PendingException();\n" +
"}\n";
Expand All @@ -68,7 +68,7 @@ public void generatesCopyPasteReadySnippetWhenStepHasIntegersInsideStringParamet
public void generatesSnippetWithEscapedDollarSigns() {
String expected = "" +
"@Given(\"^I have \\\\$(\\\\d+)$\")\n" +
"public void I_have_$(int arg1) {\n" +
"public void iHave$(int arg1) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
" throw new PendingException();\n" +
"}\n";
Expand All @@ -79,7 +79,7 @@ public void generatesSnippetWithEscapedDollarSigns() {
public void generatesSnippetWithEscapedParentheses() {
String expected = "" +
"@Given(\"^I have (\\\\d+) cukes \\\\(maybe more\\\\)$\")\n" +
"public void I_have_cukes_maybe_more(int arg1) {\n" +
"public void iHaveCukesMaybeMore(int arg1) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
" throw new PendingException();\n" +
"}\n";
Expand All @@ -90,7 +90,7 @@ public void generatesSnippetWithEscapedParentheses() {
public void generatesSnippetWithEscapedBrackets() {
String expected = "" +
"@Given(\"^I have (\\\\d+) cukes \\\\[maybe more\\\\]$\")\n" +
"public void I_have_cukes_maybe_more(int arg1) {\n" +
"public void iHaveCukesMaybeMore(int arg1) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
" throw new PendingException();\n" +
"}\n";
Expand All @@ -101,7 +101,7 @@ public void generatesSnippetWithEscapedBrackets() {
public void generatesSnippetWithDocString() {
String expected = "" +
"@Given(\"^I have:$\")\n" +
"public void I_have(String arg1) {\n" +
"public void iHave(String arg1) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
" throw new PendingException();\n" +
"}\n";
Expand All @@ -113,7 +113,7 @@ public void generatesSnippetWithDocString() {
public void recognisesWordWithNumbers() {
String expected = "" +
"@Given(\"^Then it responds ([^\\\"]*)$\")\n" +
"public void Then_it_responds_UTF(int arg1) {\n" +
"public void thenItRespondsUTF(int arg1) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
"}\n";
assertEquals(expected, snippetFor("Then it responds UTF-8"));
Expand All @@ -123,7 +123,7 @@ public void recognisesWordWithNumbers() {
public void generatesSnippetWithDataTable() {
String expected = "" +
"@Given(\"^I have:$\")\n" +
"public void I_have(DataTable arg1) {\n" +
"public void iHave(DataTable arg1) {\n" +
" // Express the Regexp above with the code you wish you had\n" +
" // For automatic conversion, change DataTable to List<YourType>\n" +
" throw new PendingException();\n" +
Expand Down
5 changes: 5 additions & 0 deletions jruby/src/main/java/cucumber/runtime/jruby/JRubySnippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public String template() {
"end\n";
}

@Override
public String sanitizeFunctionName(String functionName) {
return null;
}

@Override
public String tableHint() {
return null;
Expand Down
19 changes: 19 additions & 0 deletions jython/src/main/java/cucumber/runtime/jython/JythonSnippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public class JythonSnippet implements Snippet {

private static final Character SAFE_START_CHAR = '_';

@Override
public String template() {
return "@{0}(''{1}'')\n" +
Expand All @@ -17,6 +19,23 @@ public String template() {
"";
}

@Override
public String sanitizeFunctionName(String functionName) {
StringBuilder sanitized = new StringBuilder();

String trimmedFunctionName = functionName.trim();

sanitized.append(Character.isJavaIdentifierStart(trimmedFunctionName.charAt(0)) ? trimmedFunctionName.charAt(0) : SAFE_START_CHAR);
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) != SAFE_START_CHAR && i != trimmedFunctionName.length() - 1) {
sanitized.append(SAFE_START_CHAR);
}
}
return sanitized.toString();
}

@Override
public String tableHint() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public String template() {
"'}');\n";
}

@Override
public String sanitizeFunctionName(String functionName) {
return null;
}

@Override
public String tableHint() {
return null;
Expand Down
2 changes: 2 additions & 0 deletions scala/src/main/scala/cucumber/runtime/ScalaSnippet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ScalaSnippetGenerator extends Snippet {
" throw new PendingException()\n" +
"'}'"

def sanitizeFunctionName(functionName: String) = null

def tableHint() = null

def arguments(argumentTypes: List[Class[_]]) = {
Expand Down