|
| 1 | +/* |
| 2 | + * Copyright 2016-2023 the original author or authors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | + * accompanies this distribution and is available at |
| 7 | + * |
| 8 | + * http://www.eclipse.org/legal/epl-v20.html |
| 9 | + */ |
| 10 | + |
| 11 | +package org.junitpioneer.jupiter.displaynamegenerator; |
| 12 | + |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.DisplayNameGenerator; |
| 18 | + |
| 19 | +/** |
| 20 | + * <p>A class extending {@linkplain DisplayNameGenerator.Standard }.</p> |
| 21 | + * |
| 22 | + * <p>This extension handles method names with CamelCase, underscore and numbers.</p> |
| 23 | + * |
| 24 | + * <p>The aim is to simplify unit test display names. Instead of using this method annotation {@linkplain org.junit.jupiter.api.DisplayName }, |
| 25 | + * we can just use this class annotation {@linkplain org.junit.jupiter.api.DisplayNameGeneration } and use that method annotation if needed. |
| 26 | + * </p> |
| 27 | + * |
| 28 | + * <p>This generator follows 3 rules:</p> |
| 29 | + * |
| 30 | + * <ul> |
| 31 | + * <li>Each uppercase letter is turned into its lowercase value prepended by space.</li> |
| 32 | + * <li>Each underscore is turned into space. Words bounded by underscores or just starting with underscore are not transformed. Usually these words represent classes, variables...</li> |
| 33 | + * <li>Each number is prepended by space.</li> |
| 34 | + * </ul> |
| 35 | + * <p> |
| 36 | + * Usage example: |
| 37 | + * |
| 38 | + * <pre> |
| 39 | + * |
| 40 | + * {@code @DisplayNameGeneration(ReplaceCamelCaseAndUnderscoreAndNumber.class)} |
| 41 | + * class ExampleTest {} |
| 42 | + * </pre> |
| 43 | + * |
| 44 | + * @since 2.3.0 |
| 45 | + * @see org.junit.jupiter.api.DisplayNameGenerator.Standard |
| 46 | + * @see <a href="https://junit-pioneer.org/docs/replace-camelcase-and-underscore-and-number">Usage example of ReplaceCamelCaseAndUnderscoreAndNumber</a> |
| 47 | + */ |
| 48 | +public class ReplaceCamelCaseAndUnderscoreAndNumber extends DisplayNameGenerator.Standard { |
| 49 | + |
| 50 | + public static final DisplayNameGenerator INSTANCE = new ReplaceCamelCaseAndUnderscoreAndNumber(); |
| 51 | + |
| 52 | + private ReplaceCamelCaseAndUnderscoreAndNumber() { |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) { |
| 57 | + if (hasParameters(testMethod)) { |
| 58 | + return replaceCamelCaseAndUnderscoreAndNumber(testMethod.getName()) + " " |
| 59 | + + DisplayNameGenerator.parameterTypesAsString(testMethod); |
| 60 | + } |
| 61 | + return replaceCamelCaseAndUnderscoreAndNumber(testMethod.getName()); |
| 62 | + } |
| 63 | + |
| 64 | + private String replaceCamelCaseAndUnderscoreAndNumber(String input) { |
| 65 | + // Remove leading underscore(s) |
| 66 | + var sanitized = input.replaceAll("^_+(.*)$", "$1"); |
| 67 | + List<String> list = new ArrayList<>(); |
| 68 | + String[] split = sanitized.split("_"); |
| 69 | + for (int i = 0; i < split.length; i++) { |
| 70 | + if (i % 2 == 1) { |
| 71 | + // If parity is odd, we are between two underscores, no formatting necessary |
| 72 | + list.add(split[i]); |
| 73 | + } else { |
| 74 | + list.add(formatCamelCase(split[i])); |
| 75 | + } |
| 76 | + } |
| 77 | + // Some cases can lead to double spaces - i.e.: underscore (closing) followed by capital letter |
| 78 | + var joined = String.join(" ", list).replaceAll("\\s{2}", " "); |
| 79 | + // Capitalize the first letter |
| 80 | + return joined.substring(0, 1).toUpperCase() + joined.substring(1); |
| 81 | + } |
| 82 | + |
| 83 | + private String formatCamelCase(String in) { |
| 84 | + return in.replaceAll("(\\d+)", " $1").replaceAll("([A-Z]+)", " $1").toLowerCase(); |
| 85 | + } |
| 86 | + |
| 87 | + private boolean hasParameters(Method method) { |
| 88 | + return method.getParameterCount() > 0; |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments