Skip to content

Commit

Permalink
Add test to verify Service implementation and fix the logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurens-W committed Dec 2, 2024
1 parent b622803 commit 4f80878
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@ public String standardizeMethodName(String oldMethodName) {
int nameLength = oldMethodName.length();
for (int i = 0; i < nameLength; i++) {
char c = oldMethodName.charAt(i);

if (i == 0) {
// the java specification requires identifiers to start with [a-zA-Z$_]
if (c != '$' && c != '_') {
result.append(Character.toLowerCase(c));
}
} else {
if (!Character.isLetterOrDigit(c)) {
while (i < nameLength && (!Character.isLetterOrDigit(c) || c > 'z')) {
c = oldMethodName.charAt(i++);
while ((!Character.isLetterOrDigit(c) || c > 'z')) {
i++;
if (i < nameLength) {
c = oldMethodName.charAt(i);
} else {
break;
}
}
if (i < nameLength) {
result.append(Character.toUpperCase(c));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.openrewrite.java.service;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.assertj.core.api.Assertions.assertThat;

class JavaNamingServiceTest {

@ParameterizedTest
@CsvSource(textBlock = """
foo_bar,fooBar
foo$bar,fooBar
foo_bar$,fooBar
foo$bar$,fooBar
""")
void changeMethodName(String before, String after) {
String actual = new JavaNamingService().standardizeMethodName(before);
assertThat(actual).isEqualTo(after);
}

}

0 comments on commit 4f80878

Please sign in to comment.