diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java index bccc378342e..0685d74bc61 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java @@ -19,7 +19,7 @@ private MoreMatchers() {} * Determines whether an expression has a meta annotation of the given class name. This includes * annotations inherited from superclasses due to {@link java.lang.annotation.Inherited}. * - * @param The type of the expression tree. + * @param The type of the expression tree. // XXX: Not expression per se. * @param annotationClass The binary class name of the annotation (e.g. " * org.jspecify.nullness.Nullable", or "some.package.OuterClassName$InnerClassName") * @return A {@link Matcher} that matches expressions with the specified meta annotation. diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreJUnitMatchersTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreJUnitMatchersTest.java index 1c1d38bfa87..9b56cfaa046 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreJUnitMatchersTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreJUnitMatchersTest.java @@ -60,9 +60,9 @@ void matches() { " }", "", " @ParameterizedTest", - " @MethodSource", + " @MethodSource(\"booleanArgs\")", " // BUG: Diagnostic contains: TEST_METHOD, HAS_METHOD_SOURCE", - " void testBar() {}", + " void testBar(boolean b) {}", "", " @RepeatedTest(2)", " // BUG: Diagnostic contains: TEST_METHOD", diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchersTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchersTest.java new file mode 100644 index 00000000000..fc295a941e6 --- /dev/null +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchersTest.java @@ -0,0 +1,64 @@ +package tech.picnic.errorprone.bugpatterns.util; + +import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; + +import com.google.errorprone.BugPattern; +import com.google.errorprone.CompilationTestHelper; +import com.google.errorprone.VisitorState; +import com.google.errorprone.bugpatterns.BugChecker; +import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher; +import com.google.errorprone.matchers.Description; +import com.google.errorprone.matchers.Matcher; +import com.sun.source.tree.AnnotationTree; +import org.junit.jupiter.api.Test; + +final class MoreMatchersTest { + @Test + void matcher() { + CompilationTestHelper.newInstance(TestMatcher.class, getClass()) + .addSourceLines( + "/A.java", + "import org.junit.jupiter.api.RepeatedTest;", + "import org.junit.jupiter.api.Test;", + "import org.junit.jupiter.api.AfterAll;", + "import org.junit.jupiter.params.ParameterizedTest;", + "import org.junit.jupiter.api.TestTemplate;", + "", + "class A {", + " private void negative1() {}", + "", + " @Test", + " void negative2() {}", + "", + "", + " @TestTemplate", + " void negative3() {}", + "", + " @AfterAll", + " void negative4() {}", + "", + " // BUG: Diagnostic contains:", + " @ParameterizedTest", + " void testBar() {}", + "", + " // BUG: Diagnostic contains:", + " @RepeatedTest(2)", + " void testBaz() {}", + "}") + .doTest(); + } + + /** A {@link BugChecker} that delegates to `MoreMatchers#hasMetaAnnotation`. */ + @BugPattern(summary = "Interacts with `MoreMatchers` for testing purposes", severity = ERROR) + public static final class TestMatcher extends BugChecker implements AnnotationTreeMatcher { + private static final long serialVersionUID = 1L; + + private static final Matcher DELEGATE = + MoreMatchers.hasMetaAnnotation("org.junit.jupiter.api.TestTemplate"); + + @Override + public Description matchAnnotation(AnnotationTree tree, VisitorState state) { + return DELEGATE.matches(tree, state) ? buildDescription(tree).build() : Description.NO_MATCH; + } + } +}