Skip to content

Commit

Permalink
Add tests for the ConflictDetection utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-picnic committed Dec 19, 2022
1 parent 8350042 commit cf726c2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.sun.tools.javac.code.Type;
import java.util.Optional;
import javax.lang.model.type.TypeKind;
import org.jspecify.nullness.Nullable;
import org.jspecify.annotations.Nullable;

/**
* A collection of JUnit-specific helper methods and {@link Matcher}s.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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.matchers.Description;
import com.sun.source.tree.MethodTree;
import org.junit.jupiter.api.Test;

final class ConflictDetectionTest {
@Test
void matcher() {
CompilationTestHelper.newInstance(RenameBlockerFlagger.class, getClass())
.addSourceLines(
"/A.java",
"class A {",
" private void foo1() {}",
"",
" // BUG: Diagnostic contains: [RenameBlockerFlagger] a method named `foo2t` already exists in this",
" // class",
" private void foo2() {}",
"",
" private void foo2t() {}",
"",
" // BUG: Diagnostic contains: [RenameBlockerFlagger] `foo3t` is already statically imported",
" private void foo3() {}",
"",
" // BUG: Diagnostic contains: [RenameBlockerFlagger] `int` is a reserved keyword",
" private void in() {}",
"}")
.addSourceLines(
"/staticimport/B.java",
"package staticimport;",
"",
"public class B {",
" public static void foo3t() {}",
"}")
.doTest();
}

@BugPattern(summary = "Flags blockers for renaming methods", severity = ERROR)
public static final class RenameBlockerFlagger extends BugChecker
implements BugChecker.MethodTreeMatcher {
private static final long serialVersionUID = 1L;

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
return ConflictDetection.findMethodRenameBlocker(tree.getName() + "t", state)
.map(blocker -> buildDescription(tree).setMessage(blocker).build())
.orElse(Description.NO_MATCH);
}
}
}

0 comments on commit cf726c2

Please sign in to comment.