From a5b30d4484b707fd3671eac544cd25a593940b00 Mon Sep 17 00:00:00 2001 From: Frieder Bluemle Date: Wed, 18 Sep 2013 13:12:02 -0700 Subject: [PATCH 1/2] Fix exception in snippet generator This fixes a StringIndexOutOfBoundsException that would occur in CamelCaseFunctionNameSanitizer for function names with multiple spaces, that is for step definitions with more than one capture group. --- .../runtime/snippets/CamelCaseFunctionNameSanitizer.java | 2 +- .../snippets/CamelCaseFunctionNameSanitizerTest.java | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizer.java b/core/src/main/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizer.java index 988e000c17..9a779f91fe 100644 --- a/core/src/main/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizer.java +++ b/core/src/main/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizer.java @@ -24,6 +24,6 @@ public String sanitizeFunctionName(String functionName) { } private String capitalize(String line) { - return Character.toUpperCase(line.charAt(0)) + line.substring(1); + return line.length() > 0 ? Character.toUpperCase(line.charAt(0)) + line.substring(1) : ""; } } diff --git a/core/src/test/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizerTest.java b/core/src/test/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizerTest.java index 406a6594f4..c627be482f 100644 --- a/core/src/test/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizerTest.java +++ b/core/src/test/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizerTest.java @@ -16,5 +16,11 @@ public void testSanitizeFunctionName() { String actual = generator.sanitizeFunctionName(functionName); assertEquals(expected, actual); + + functionName = "Function name with multiple spaces"; + expected = "functionNameWithMultipleSpaces"; + actual = generator.sanitizeFunctionName(functionName); + + assertEquals(expected, actual); } } From 383922efcc934001b7ee6529f2ba43de26d8d094 Mon Sep 17 00:00:00 2001 From: Frieder Bluemle Date: Wed, 18 Sep 2013 15:04:12 -0700 Subject: [PATCH 2/2] 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. --- .../CamelCaseFunctionNameSanitizer.java | 26 ++++++++++++++++++- .../CamelCaseFunctionNameSanitizerTest.java | 20 +++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizer.java b/core/src/main/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizer.java index 9a779f91fe..a03f327442 100644 --- a/core/src/main/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizer.java +++ b/core/src/main/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizer.java @@ -17,7 +17,7 @@ public String sanitizeFunctionName(String functionName) { sanitized.append(words[0].toLowerCase()); for (int i = 1; i < words.length; i++) { - sanitized.append(capitalize(words[i].toLowerCase())); + sanitized.append(sanitizeWord(words[i])); } return sanitized.toString(); @@ -26,4 +26,28 @@ public String sanitizeFunctionName(String functionName) { private String capitalize(String line) { return line.length() > 0 ? Character.toUpperCase(line.charAt(0)) + line.substring(1) : ""; } + + private boolean isUpperCaseAcronym(String word) { + if (word == null || word.length() < 2) { + return false; + } + + for (char c : word.toCharArray()) { + if (Character.isLowerCase(c)) { + return false; + } + } + + return true; + } + + private String sanitizeWord(String word) { + if (word == null) { + return ""; + } else if (word.length() == 2 && isUpperCaseAcronym(word)) { + return word; + } else { + return capitalize(isUpperCaseAcronym(word) ? word.toLowerCase() : word); + } + } } diff --git a/core/src/test/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizerTest.java b/core/src/test/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizerTest.java index c627be482f..39f00177c3 100644 --- a/core/src/test/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizerTest.java +++ b/core/src/test/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizerTest.java @@ -14,13 +14,31 @@ public void testSanitizeFunctionName() { String functionName = "I am a function name"; String expected = "iAmAFunctionName"; String actual = generator.sanitizeFunctionName(functionName); - assertEquals(expected, actual); functionName = "Function name with multiple spaces"; expected = "functionNameWithMultipleSpaces"; actual = generator.sanitizeFunctionName(functionName); + assertEquals(expected, actual); + + functionName = "Function name with pascalCase word"; + expected = "functionNameWithPascalCaseWord"; + actual = generator.sanitizeFunctionName(functionName); + assertEquals(expected, actual); + + functionName = "Function name with CamelCase word"; + expected = "functionNameWithCamelCaseWord"; + actual = generator.sanitizeFunctionName(functionName); + assertEquals(expected, actual); + + functionName = "Function name with multi char acronym HTTP Server"; + expected = "functionNameWithMultiCharAcronymHttpServer"; + actual = generator.sanitizeFunctionName(functionName); + assertEquals(expected, actual); + functionName = "Function name with two char acronym US"; + expected = "functionNameWithTwoCharAcronymUS"; + actual = generator.sanitizeFunctionName(functionName); assertEquals(expected, actual); } }