Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify real expression in U{FreeIdent,Repeated}Test #32

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After merging the PR, we can rebase it and also drop the name field here and below.

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
Original file line number Diff line number Diff line change
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