Skip to content

Commit 383922e

Browse files
Improve CamelCaseFunctionNameSanitizer
Function names are now correctly formatted if they contain acronyms. The name sanitizer also will not change the case of words that are already pascal or camel case in the input.
1 parent a5b30d4 commit 383922e

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

core/src/main/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizer.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public String sanitizeFunctionName(String functionName) {
1717
sanitized.append(words[0].toLowerCase());
1818

1919
for (int i = 1; i < words.length; i++) {
20-
sanitized.append(capitalize(words[i].toLowerCase()));
20+
sanitized.append(sanitizeWord(words[i]));
2121
}
2222

2323
return sanitized.toString();
@@ -26,4 +26,28 @@ public String sanitizeFunctionName(String functionName) {
2626
private String capitalize(String line) {
2727
return line.length() > 0 ? Character.toUpperCase(line.charAt(0)) + line.substring(1) : "";
2828
}
29+
30+
private boolean isUpperCaseAcronym(String word) {
31+
if (word == null || word.length() < 2) {
32+
return false;
33+
}
34+
35+
for (char c : word.toCharArray()) {
36+
if (Character.isLowerCase(c)) {
37+
return false;
38+
}
39+
}
40+
41+
return true;
42+
}
43+
44+
private String sanitizeWord(String word) {
45+
if (word == null) {
46+
return "";
47+
} else if (word.length() == 2 && isUpperCaseAcronym(word)) {
48+
return word;
49+
} else {
50+
return capitalize(isUpperCaseAcronym(word) ? word.toLowerCase() : word);
51+
}
52+
}
2953
}

core/src/test/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizerTest.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,31 @@ public void testSanitizeFunctionName() {
1414
String functionName = "I am a function name";
1515
String expected = "iAmAFunctionName";
1616
String actual = generator.sanitizeFunctionName(functionName);
17-
1817
assertEquals(expected, actual);
1918

2019
functionName = "Function name with multiple spaces";
2120
expected = "functionNameWithMultipleSpaces";
2221
actual = generator.sanitizeFunctionName(functionName);
22+
assertEquals(expected, actual);
23+
24+
functionName = "Function name with pascalCase word";
25+
expected = "functionNameWithPascalCaseWord";
26+
actual = generator.sanitizeFunctionName(functionName);
27+
assertEquals(expected, actual);
28+
29+
functionName = "Function name with CamelCase word";
30+
expected = "functionNameWithCamelCaseWord";
31+
actual = generator.sanitizeFunctionName(functionName);
32+
assertEquals(expected, actual);
33+
34+
functionName = "Function name with multi char acronym HTTP Server";
35+
expected = "functionNameWithMultiCharAcronymHttpServer";
36+
actual = generator.sanitizeFunctionName(functionName);
37+
assertEquals(expected, actual);
2338

39+
functionName = "Function name with two char acronym US";
40+
expected = "functionNameWithTwoCharAcronymUS";
41+
actual = generator.sanitizeFunctionName(functionName);
2442
assertEquals(expected, actual);
2543
}
2644
}

0 commit comments

Comments
 (0)