Skip to content

Commit

Permalink
Unify real expression in U{FreeIdent,Repeated}Test
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed Jul 4, 2022
2 parents 21bc6e2 + f99e3f5 commit 3b86713
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 3b86713

Please sign in to comment.