Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Camel case Java snippet generation #302

Closed
wants to merge 1 commit into from

3 participants

@rapaul

JavaSnippetTest.java is the end result of this change, iHaveCukesInMyBelly instead of I_have_cukes_in_my_belly.

Pushed the creation of a language specific function name down into the
implementations of Snippet. Most languages don't create functions, so
all languages besides Java & Jython return null for this.

@rapaul rapaul Camel case Java snippet generation
Pushed the creation of a language specific function name down into the
implementations of Snippet. Most languages don't create functions, so
all languages besides Java & Jython return null for this.
9756873
@aslakhellesoy

I personally find it a lot easier to read I_have_cukes_in_my_belly than iHaveCukesInMyBelly and I have adopted this style for my JUnit tests as well.

To me, readability is more important than following a naming convention. I only apply this to test code, which tends to have long sentence-like method names, not production code or APIs.

@rapaul rapaul closed this
@meza

Is there a way to make this configurable? It could still default to the underscore style if you wish.

@aslakhellesoy

Only if someone sends a pull request. It could be toggled with a command line argument: --snippets [camelcase|underscore] where underscore is the default.

I'm fully aware that underscore is non-idiomatic for java, but I prefer it in sentence-like test methods since I find it easier to read.

@meza
@meza

I'm at a point where I need to decide to add the snippet type to the Backend getSnippet interface, or do an ugly if and hack it into the java backend only.
Which one would you prefer?

@meza

Forget it, going with a non ugly, only java solution.

@aslakhellesoy aslakhellesoy referenced this pull request in cucumber/cucumber-ruby
Closed

Generates unconventional methods #646

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 21, 2012
  1. @rapaul

    Camel case Java snippet generation

    rapaul authored
    Pushed the creation of a language specific function name down into the
    implementations of Snippet. Most languages don't create functions, so
    all languages besides Java & Jython return null for this.
This page is out of date. Refresh to see the latest.
View
1  clojure/src/main/clj/cucumber/runtime/clj.clj
@@ -29,6 +29,7 @@
(arguments [_ argumentTypes]
(str/replace (SnippetGenerator/untypedArguments argumentTypes)
"," ""))
+ (sanitizeFunctionName [_ functionName] nil)
(namedGroupStart [_] nil)
(namedGroupEnd [_] nil)
(tableHint [_] nil)
View
5 clojure/src/test/java/cucumber/runtime/clojure/ClojureSnippet.java
@@ -19,6 +19,11 @@ public String template() {
}
@Override
+ public String sanitizeFunctionName(String functionName) {
+ return null;
+ }
+
+ @Override
public String tableHint() {
return null;
}
View
5 core/src/main/java/cucumber/runtime/snippets/Snippet.java
@@ -18,6 +18,11 @@
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
*/
String tableHint();
View
18 core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java
@@ -25,7 +25,6 @@
};
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;
@@ -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);
View
5 core/src/test/java/cucumber/runtime/UndefinedStepsTrackerTest.java
@@ -95,6 +95,11 @@ public String template() {
}
@Override
+ public String sanitizeFunctionName(String functionName) {
+ return null;
+ }
+
+ @Override
public String tableHint() {
return null;
}
View
5 groovy/src/main/java/cucumber/runtime/groovy/GroovySnippet.java
@@ -14,6 +14,11 @@ public String template() {
}
@Override
+ public String sanitizeFunctionName(String functionName) {
+ return null;
+ }
+
+ @Override
public String tableHint() {
return null;
}
View
5 ioke/src/main/java/cucumber/runtime/ioke/IokeSnippet.java
@@ -14,6 +14,11 @@ public String template() {
}
@Override
+ public String sanitizeFunctionName(String functionName) {
+ return null;
+ }
+
+ @Override
public String tableHint() {
return null;
}
View
24 java/src/main/java/cucumber/runtime/java/JavaSnippet.java
@@ -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();
@@ -29,6 +31,28 @@ public String template() {
}
@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";
}
View
20 java/src/test/java/cucumber/runtime/java/JavaSnippetTest.java
@@ -22,7 +22,7 @@
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";
@@ -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";
@@ -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";
@@ -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";
@@ -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";
@@ -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";
@@ -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";
@@ -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";
@@ -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"));
@@ -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" +
View
5 jruby/src/main/java/cucumber/runtime/jruby/JRubySnippet.java
@@ -14,6 +14,11 @@ public String template() {
}
@Override
+ public String sanitizeFunctionName(String functionName) {
+ return null;
+ }
+
+ @Override
public String tableHint() {
return null;
}
View
19 jython/src/main/java/cucumber/runtime/jython/JythonSnippet.java
@@ -8,6 +8,8 @@
public class JythonSnippet implements Snippet {
+ private static final Character SAFE_START_CHAR = '_';
+
@Override
public String template() {
return "@{0}(''{1}'')\n" +
@@ -18,6 +20,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;
}
View
5 rhino/src/main/java/cucumber/runtime/javascript/JavaScriptSnippet.java
@@ -16,6 +16,11 @@ public String template() {
}
@Override
+ public String sanitizeFunctionName(String functionName) {
+ return null;
+ }
+
+ @Override
public String tableHint() {
return null;
}
View
2  scala/src/main/scala/cucumber/runtime/ScalaSnippet.scala
@@ -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[_]]) = {
Something went wrong with that request. Please try again.