diff --git a/core/src/test/java/com/google/errorprone/refaster/UFreeIdentTest.java b/core/src/test/java/com/google/errorprone/refaster/UFreeIdentTest.java index 7954f6888ab..1e0840459b8 100644 --- a/core/src/test/java/com/google/errorprone/refaster/UFreeIdentTest.java +++ b/core/src/test/java/com/google/errorprone/refaster/UFreeIdentTest.java @@ -17,10 +17,18 @@ package com.google.errorprone.refaster; import static com.google.common.truth.Truth.assertThat; +import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; import com.google.common.testing.EqualsTester; import com.google.common.testing.SerializableTester; -import com.sun.tools.javac.tree.JCTree.JCExpression; +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.MethodInvocationTreeMatcher; +import com.google.errorprone.matchers.Description; +import com.sun.source.tree.MethodInvocationTree; +import com.sun.tools.javac.util.Context; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -41,10 +49,34 @@ public void inlinesExpression() { @Test public void binds() { - JCExpression expr = parseExpression("\"abcdefg\".charAt(x + 1)"); - UFreeIdent ident = UFreeIdent.create("foo"); - assertThat(ident.unify(expr, unifier)).isNotNull(); - assertThat(unifier.getBindings()).containsExactly(new UFreeIdent.Key("foo"), expr); + CompilationTestHelper.newInstance(UnificationChecker.class, getClass()) + .addSourceLines( + "A.java", + "class A {", + " public void bar() {", + " int x = 0;", + " \"abcdefg\".charAt(x + 1);", + " }", + "}") + .doTest(); + } + + @BugPattern( + name = "UnificationChecker", + summary = "Verify that unifying the expression results in the correct binding", + explanation = "For test purposes only", + severity = SUGGESTION) + public static class UnificationChecker extends BugChecker implements MethodInvocationTreeMatcher { + @Override + public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { + Unifier unifier = new Unifier(new Context()); + UFreeIdent ident = UFreeIdent.create("foo"); + + assertThat(ident.unify(tree, unifier)).isNotNull(); + assertThat(unifier.getBindings()).containsExactly(new UFreeIdent.Key("foo"), tree); + + return Description.NO_MATCH; + } } @Test diff --git a/core/src/test/java/com/google/errorprone/refaster/URepeatedTest.java b/core/src/test/java/com/google/errorprone/refaster/URepeatedTest.java index 3e54de57179..c73a87d0a97 100644 --- a/core/src/test/java/com/google/errorprone/refaster/URepeatedTest.java +++ b/core/src/test/java/com/google/errorprone/refaster/URepeatedTest.java @@ -17,10 +17,18 @@ package com.google.errorprone.refaster; import static com.google.common.truth.Truth.assertThat; +import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; import com.google.common.testing.EqualsTester; import com.google.common.testing.SerializableTester; -import com.sun.tools.javac.tree.JCTree.JCExpression; +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.MethodInvocationTreeMatcher; +import com.google.errorprone.matchers.Description; +import com.sun.source.tree.MethodInvocationTree; +import com.sun.tools.javac.util.Context; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -31,10 +39,34 @@ public class URepeatedTest extends AbstractUTreeTest { @Test public void unifies() { - JCExpression expr = parseExpression("\"abcdefg\".charAt(x + 1)"); - URepeated ident = URepeated.create("foo", UFreeIdent.create("foo")); - assertThat(ident.unify(expr, unifier)).isNotNull(); - assertThat(unifier.getBindings()).containsExactly(new UFreeIdent.Key("foo"), expr); + CompilationTestHelper.newInstance(UnificationChecker.class, getClass()) + .addSourceLines( + "A.java", + "class A {", + " public void bar() {", + " int x = 0;", + " \"abcdefg\".charAt(x + 1);", + " }", + "}") + .doTest(); + } + + @BugPattern( + name = "UnificationChecker", + summary = "Verify that unifying the expression results in the correct binding", + explanation = "For test purposes only", + severity = SUGGESTION) + public static class UnificationChecker extends BugChecker implements MethodInvocationTreeMatcher { + @Override + public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { + Unifier unifier = new Unifier(new Context()); + URepeated ident = URepeated.create("foo", UFreeIdent.create("foo")); + + assertThat(ident.unify(tree, unifier)).isNotNull(); + assertThat(unifier.getBindings()).containsExactly(new UFreeIdent.Key("foo"), tree); + + return Description.NO_MATCH; + } } @Test