Last active
January 3, 2019 08:33
-
-
Save syhily/2cbbb109a37ff6892fe9338917d167b6 to your computer and use it in GitHub Desktop.
A JUnit 5 display name generator for JUnit 5.4 or above.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.DisplayNameGenerator; | |
import java.lang.reflect.Method; | |
import static java.lang.Character.*; | |
/** | |
* Change the camelcase or underscore method name into a readable display name | |
* | |
* @author せいうはん (Employee ID: 17092068) | |
* @version 3.0.0, 2018-12-24 17:32 | |
* @since 3.0.0, 2018-12-24 17:32 | |
*/ | |
public class CamelCastDisplayNameGenerator implements DisplayNameGenerator { | |
private static final DisplayNameGenerator internal = new DisplayNameGenerator.Standard(); | |
@Override | |
public String generateDisplayNameForClass(Class<?> testClass) { | |
return internal.generateDisplayNameForClass(testClass); | |
} | |
@Override | |
public String generateDisplayNameForNestedClass(Class<?> nestedClass) { | |
return internal.generateDisplayNameForNestedClass(nestedClass); | |
} | |
@Override | |
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) { | |
String methodName = testMethod == null ? "" : removeExtraClassTag(testMethod.getName()); | |
return camelToText(methodName).replaceAll("_", " "); | |
} | |
/** | |
* This method was copy from https://github.com/krasa/StringManipulation | |
*/ | |
@SuppressWarnings("squid:S3776") | |
private static String camelToText(String text) { | |
StringBuilder builder = new StringBuilder(); | |
char lastChar = ' '; | |
for (char c : text.toCharArray()) { | |
char nc = c; | |
if (isUpperCase(nc) && !isUpperCase(lastChar)) { | |
if (lastChar != ' ' && isLetterOrDigit(lastChar)) { | |
builder.append(" "); | |
} | |
nc = Character.toLowerCase(c); | |
} else if (isDigit(lastChar) && !isDigit(c)) { | |
if (lastChar != ' ') { | |
builder.append(" "); | |
} | |
nc = Character.toLowerCase(c); | |
} | |
if (lastChar != ' ' || c != ' ') { | |
builder.append(nc); | |
} | |
lastChar = c; | |
} | |
return builder.toString(); | |
} | |
/** | |
* Kotlin based test sometime would have some extra module name be appended to the test method name. | |
* We just need drop them for clean display name. | |
*/ | |
private String removeExtraClassTag(String name) { | |
String nonNullName = name == null ? "" : name; | |
if (nonNullName.contains("$")) { | |
return nonNullName.substring(0, nonNullName.indexOf('$')); | |
} else { | |
return nonNullName; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.DisplayNameGeneration; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.TestInfo; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
/** | |
* @author せいうはん (Employee ID: 17092068) | |
*/ | |
@SuppressWarnings({"squid:S1192", "squid:S00100"}) | |
@DisplayNameGeneration(CamelCastDisplayNameGenerator.class) | |
class CamelCastDisplayNameGeneratorTest { | |
@Test | |
void camelCaseCouldBeConvertToA_MoreBetterDisplayName(TestInfo testInfo) { | |
assertEquals( | |
"camel case could be convert to a more better display name", | |
testInfo.getDisplayName() | |
); | |
} | |
@Test | |
void underscore_could_be_transform_to_display_name(TestInfo testInfo) { | |
assertEquals( | |
"underscore could be transform to display name", | |
testInfo.getDisplayName() | |
); | |
} | |
@Test | |
void anything_after_dollar_in_method_name_would_be_removed$thisWouldBeRemove(TestInfo testInfo) { | |
assertEquals( | |
"anything after dollar in method name would be removed", | |
testInfo.getDisplayName() | |
); | |
} | |
@Test | |
void number1ShouldAlsoBeSupported2(TestInfo testInfo) { | |
assertEquals( | |
"number1 should also be supported2", | |
testInfo.getDisplayName() | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment