Skip to content

Commit

Permalink
Introduce MoreMatcherTest and additional cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed Nov 12, 2022
1 parent 90e7dc5 commit e495239
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> The type of the expression tree.
* @param <T> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AnnotationTree> 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;
}
}
}

0 comments on commit e495239

Please sign in to comment.