diff --git a/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/PatternFilter.java b/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/PatternFilter.java index 0bc8fecd..c32abb65 100644 --- a/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/PatternFilter.java +++ b/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/PatternFilter.java @@ -2,6 +2,8 @@ import java.util.function.Predicate; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.junit.platform.engine.FilterResult; import org.junit.platform.engine.TestDescriptor; import org.junit.platform.engine.TestSource; @@ -59,10 +61,19 @@ public FilterResult apply(TestDescriptor object) { return FilterResult.excluded("Did not match " + rawPattern); } + /** + * Converts comma-separated selections in patterns like: + * + * + */ private static String convertCommaSeparatedSelections(String pattern) { var selections = pattern.split(","); if (selections.length == 1) { - return pattern; + return ensureExactMethodName(pattern); } var precedingClassSelection = selections[0]; var precedingHashIndex = precedingClassSelection.indexOf('#'); @@ -76,6 +87,13 @@ private static String convertCommaSeparatedSelections(String pattern) { selections[i] = precedingClassSelection.substring(0, precedingHashIndex + 1) + selection; } } - return String.join("|", selections); + return Stream.of(selections) + .map(PatternFilter::ensureExactMethodName) + .collect(Collectors.joining("|")); + } + + /** Appends '$' to patterns like "class#method" or "#method", unless already done. */ + private static String ensureExactMethodName(String pattern) { + return pattern.matches(".*#.*[^$]$") ? pattern + '$' : pattern; } } diff --git a/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/FilteringTest.java b/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/FilteringTest.java index 06300dfb..e9307160 100644 --- a/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/FilteringTest.java +++ b/java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/FilteringTest.java @@ -134,7 +134,7 @@ public void shouldIncludeATestMethodIfTheFilterIsJustTheClassName() { assertTrue(siblingTestResult.included()); FilterResult nestedTestResult = filter.apply(nestedTestMethodTestDescriptor); - assertFalse(nestedTestResult.included(), "nested class should not be matched"); + assertFalse(nestedTestResult.included(), "method in nested class should not be matched"); } @Test @@ -143,10 +143,10 @@ public void shouldIncludeANestedTestMethodIfTheFilterIsJustTheNestedClassName() new PatternFilter(JUnit5StyleTest.NestedTest.class.getName().replace("$", "\\$") + "#"); FilterResult testResult = filter.apply(testMethodTestDescriptor); - assertFalse(testResult.included(), "enclosing class should not be matched"); + assertFalse(testResult.included(), "method in enclosing class should not be matched"); FilterResult siblingTestResult = filter.apply(siblingTestMethodTestDescriptor); - assertFalse(siblingTestResult.included(), "enclosing class should not be matched"); + assertFalse(siblingTestResult.included(), "method in enclosing class should not be matched"); FilterResult nestedTestResult = filter.apply(nestedTestMethodTestDescriptor); assertTrue(nestedTestResult.included()); @@ -186,11 +186,25 @@ public void shouldNotIncludeATestMethodIfTheFilterDoesNotMatchTheMethodName() { } @Test - public void shouldIncludeATestMethodIfTheFilterMatchesTheMethodName() { + public void shouldIncludeATestMethodIfTheFilterMatchesTheExactShortMethodName() { + PatternFilter filter = new PatternFilter("#alwaysPasses"); + + FilterResult testResult = filter.apply(testMethodTestDescriptor); + assertTrue(testResult.included()); + + FilterResult siblingTestResult = filter.apply(siblingTestMethodTestDescriptor); + assertFalse(siblingTestResult.included(), "longer method name should not be matched"); + + FilterResult nestedTestResult = filter.apply(nestedTestMethodTestDescriptor); + assertFalse(nestedTestResult.included(), "longer method name should not be matched"); + } + + @Test + public void shouldIncludeATestMethodIfTheFilterMatchesTheExactLongMethodName() { PatternFilter filter = new PatternFilter("#alwaysPassesToo"); FilterResult testResult = filter.apply(testMethodTestDescriptor); - assertFalse(testResult.included(), "different method name should not be matched"); + assertFalse(testResult.included(), "shorter method name should not be matched"); FilterResult siblingTestResult = filter.apply(siblingTestMethodTestDescriptor); assertTrue(siblingTestResult.included());