Skip to content

Commit b338a54

Browse files
committed
Add tests for the ConflictDetection utility class
1 parent 8350042 commit b338a54

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreJUnitMatchers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.sun.tools.javac.code.Type;
2424
import java.util.Optional;
2525
import javax.lang.model.type.TypeKind;
26-
import org.jspecify.nullness.Nullable;
26+
import org.jspecify.annotations.Nullable;
2727

2828
/**
2929
* A collection of JUnit-specific helper methods and {@link Matcher}s.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package tech.picnic.errorprone.bugpatterns.util;
2+
3+
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
4+
5+
import com.google.errorprone.BugPattern;
6+
import com.google.errorprone.CompilationTestHelper;
7+
import com.google.errorprone.VisitorState;
8+
import com.google.errorprone.bugpatterns.BugChecker;
9+
import com.google.errorprone.matchers.Description;
10+
import com.sun.source.tree.MethodTree;
11+
import org.junit.jupiter.api.Test;
12+
13+
final class ConflictDetectionTest {
14+
@Test
15+
void matcher() {
16+
CompilationTestHelper.newInstance(RenameBlockerFlagger.class, getClass())
17+
.addSourceLines(
18+
"/A.java",
19+
"import static staticimport.B.foo3t;",
20+
"",
21+
"class A {",
22+
" private void foo1() {",
23+
" foo3t();",
24+
" }",
25+
"",
26+
" // BUG: Diagnostic contains: [RenameBlockerFlagger] a method named `foo2t` already exists in this",
27+
" // class",
28+
" private void foo2() {}",
29+
"",
30+
" private void foo2t() {}",
31+
"",
32+
" // BUG: Diagnostic contains: [RenameBlockerFlagger] `foo3t` is already statically imported",
33+
" private void foo3() {}",
34+
"",
35+
" // BUG: Diagnostic contains: [RenameBlockerFlagger] `int` is a reserved keyword",
36+
" private void in() {}",
37+
"}")
38+
.addSourceLines(
39+
"/staticimport/B.java",
40+
"package staticimport;",
41+
"",
42+
"public class B {",
43+
" public static void foo3t() {}",
44+
"}")
45+
.doTest();
46+
}
47+
48+
@BugPattern(summary = "Flags blockers for renaming methods", severity = ERROR)
49+
public static final class RenameBlockerFlagger extends BugChecker
50+
implements BugChecker.MethodTreeMatcher {
51+
private static final long serialVersionUID = 1L;
52+
53+
@Override
54+
public Description matchMethod(MethodTree tree, VisitorState state) {
55+
return ConflictDetection.findMethodRenameBlocker(tree.getName() + "t", state)
56+
.map(blocker -> buildDescription(tree).setMessage(blocker).build())
57+
.orElse(Description.NO_MATCH);
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)