Skip to content

Commit cb1c984

Browse files
committed
Add bounds checking with sensible error to UnderscoreFunctionNameSanitizer
1 parent 036025f commit cb1c984

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public String sanitizeFunctionName(String functionName) {
77
StringBuilder sanitized = new StringBuilder();
88

99
String trimmedFunctionName = functionName.trim();
10+
if (trimmedFunctionName.isEmpty()) {
11+
throw new IllegalArgumentException("Cannot have empty function name");
12+
}
1013

1114
sanitized.append(Character.isJavaIdentifierStart(trimmedFunctionName.charAt(0)) ? trimmedFunctionName.charAt(0) : SUBST);
1215
for (int i = 1; i < trimmedFunctionName.length(); i++) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cucumber.runtime.snippets;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.assertEquals;
5+
6+
public class UnderscoreFunctionNameSanitizerTest {
7+
8+
private UnderscoreFunctionNameSanitizer sanitizer = new UnderscoreFunctionNameSanitizer();
9+
10+
@Test
11+
public void testSanitizeFunctionName() {
12+
assertEquals("_test_function_123", sanitizer.sanitizeFunctionName(".test function 123 "));
13+
}
14+
15+
@Test(expected = IllegalArgumentException.class)
16+
public void testSanitizeEmptyFunctionName() {
17+
sanitizer.sanitizeFunctionName("");
18+
}
19+
20+
}

0 commit comments

Comments
 (0)