Skip to content

Commit

Permalink
Simplify replaceUnderscore name generator
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-dejongh committed Nov 20, 2024
1 parent 7cf92fb commit c03f4b3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 9 deletions.
7 changes: 6 additions & 1 deletion commons-testing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@
<artifactId>archunit-junit5-engine</artifactId>
<version>${archunit.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<!-- BEGIN TEST SCOPE -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,10 @@ public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod
return this.replaceCapitals(super.generateDisplayNameForMethod(testClass, testMethod));
}

private String replaceCapitals(String name) {
name = name.replaceAll("([A-Z])", " $1").toLowerCase();
String replaceCapitals(String name) {
name = name.replaceAll("([A-Z])", " $1");
name = name.replaceAll("(\\d+)", " $1");
name = name.replace("given", "GIVEN");
name = name.replace("when", "WHEN");
name = name.replace("then", "THEN");
name = name.replace("and", "AND");
return name;
return name.toLowerCase();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*-
* #%L
* commons-testing
* %%
* Copyright (C) 2020 - 2024 SD Development
* %%
* Licensed under the EUPL, Version 1.1 or – as soon they will be
* approved by the European Commission - subsequent versions of the
* EUPL (the "Licence");
*
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl5
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
* #L%
*/

package be.sddevelopment.commons.testing.naming;

import static org.junit.jupiter.params.provider.Arguments.of;

import java.util.stream.Stream;
import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

@DisplayName("Replace Underscored Camel Casing TestCase naming")
class ReplaceUnderscoredCamelCasingTest implements WithAssertions {

private final ReplaceUnderscoredCamelCasing toTest = new ReplaceUnderscoredCamelCasing();

@ParameterizedTest
@MethodSource("methodNamesForValidation")
void replacesUnderscoresWithSpaces(String input, String expected) {
assertThat(input).isNotBlank();

var result = toTest.replaceCapitals(input);

assertThat(result).isEqualTo(expected);
}

public static Stream<Arguments> methodNamesForValidation() {
return Stream.of(
of("canHandleEmptyDataSets", "can handle empty data sets"),
of("parsingRequiresAtLeastAHeaderLine", "parsing requires at least a header line"),
of("whenAnExceptionIsSuppressedUsingDefaultSuppressionThenChainInterpretsItAsEmptyResult", "when an exception is suppressed using default suppression then chain interprets it as empty result"),
of("insultsYouGivenFileWithDataAndYourMum", "insults you given file with data and your mum")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static String throwExceptionIfNotBLank(String url) throws MalformedURLException
}

@Test
void whenAnExceptionIsSuppressed_usingDefaultSupress_thenChainInterpretsItAsEmptyResult() {
void whenAnExceptionIsSuppressed_usingDefaultSuppression_thenChainInterpretsItAsEmptyResult() {
Optional<String> result = Optional
.of(Strings.NON_EMPTY_STRING)
.map(ignore(TestMethods::throwExceptionIfNotBLank));
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
<artifactId>archunit-junit5-engine</artifactId>
<version>${archunit.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<!-- END UTIL SCOPE -->
</dependencies>
</dependencyManagement>
Expand Down

0 comments on commit c03f4b3

Please sign in to comment.