From 9df103e0959c4f84604112eefe16fc004942e486 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 7 Jun 2022 09:26:57 +0200 Subject: [PATCH 1/3] Unify real expression in `U{FreeIdent,Repeated}Test` tests --- .../errorprone/refaster/UFreeIdentTest.java | 42 ++++++++++++++++--- .../errorprone/refaster/URepeatedTest.java | 42 ++++++++++++++++--- 2 files changed, 74 insertions(+), 10 deletions(-) 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 7954f6888ab0..2bce30d1a6c6 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.ExpressionStatementTreeMatcher; +import com.google.errorprone.matchers.Description; +import com.sun.source.tree.ExpressionStatementTree; +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(DummyChecker.class, getClass()) + .addSourceLines( + "A.java", + "class A {", + " public void bar() {", + " int x = 0;", + " \"abcdefg\".charAt(x + 1);", + " }", + "}") + .doTest(); + } + + @BugPattern( + summary = "Verify that unifying the expression results in the correct binding", + explanation = "For test purposes only", + severity = SUGGESTION) + public static class DummyChecker extends BugChecker implements ExpressionStatementTreeMatcher { + @Override + public Description matchExpressionStatement(ExpressionStatementTree tree, VisitorState state) { + Unifier unifier = new Unifier(new Context()); + UFreeIdent ident = UFreeIdent.create("foo"); + + assertThat(ident.unify(tree.getExpression(), unifier)).isNotNull(); + assertThat(unifier.getBindings()) + .containsExactly(new UFreeIdent.Key("foo"), tree.getExpression()); + + 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 3e54de571797..67924b468bea 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.ExpressionStatementTreeMatcher; +import com.google.errorprone.matchers.Description; +import com.sun.source.tree.ExpressionStatementTree; +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(DummyChecker.class, getClass()) + .addSourceLines( + "A.java", + "class A {", + " public void bar() {", + " int x = 0;", + " \"abcdefg\".charAt(x + 1);", + " }", + "}") + .doTest(); + } + + @BugPattern( + summary = "Verify that unifying the expression results in the correct binding", + explanation = "For test purposes only", + severity = SUGGESTION) + public static class DummyChecker extends BugChecker implements ExpressionStatementTreeMatcher { + @Override + public Description matchExpressionStatement(ExpressionStatementTree tree, VisitorState state) { + Unifier unifier = new Unifier(new Context()); + URepeated ident = URepeated.create("foo", UFreeIdent.create("foo")); + + assertThat(ident.unify(tree.getExpression(), unifier)).isNotNull(); + assertThat(unifier.getBindings()) + .containsExactly(new UFreeIdent.Key("foo"), tree.getExpression()); + + return Description.NO_MATCH; + } } @Test From 20508e29d00bbad0b922ece0bcb075453eccfbf9 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Thu, 9 Jun 2022 09:51:47 +0200 Subject: [PATCH 2/3] Rename and improve `BugChecker`s used for testing --- .../java/com/google/errorprone/refaster/UFreeIdentTest.java | 6 ++++-- .../java/com/google/errorprone/refaster/URepeatedTest.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) 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 2bce30d1a6c6..ad153d89c7e1 100644 --- a/core/src/test/java/com/google/errorprone/refaster/UFreeIdentTest.java +++ b/core/src/test/java/com/google/errorprone/refaster/UFreeIdentTest.java @@ -49,7 +49,7 @@ public void inlinesExpression() { @Test public void binds() { - CompilationTestHelper.newInstance(DummyChecker.class, getClass()) + CompilationTestHelper.newInstance(UnificationChecker.class, getClass()) .addSourceLines( "A.java", "class A {", @@ -62,10 +62,12 @@ public void binds() { } @BugPattern( + name = "UnificationChecker", summary = "Verify that unifying the expression results in the correct binding", explanation = "For test purposes only", severity = SUGGESTION) - public static class DummyChecker extends BugChecker implements ExpressionStatementTreeMatcher { + public static class UnificationChecker extends BugChecker + implements ExpressionStatementTreeMatcher { @Override public Description matchExpressionStatement(ExpressionStatementTree tree, VisitorState state) { Unifier unifier = new Unifier(new Context()); 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 67924b468bea..9e09e2540654 100644 --- a/core/src/test/java/com/google/errorprone/refaster/URepeatedTest.java +++ b/core/src/test/java/com/google/errorprone/refaster/URepeatedTest.java @@ -39,7 +39,7 @@ public class URepeatedTest extends AbstractUTreeTest { @Test public void unifies() { - CompilationTestHelper.newInstance(DummyChecker.class, getClass()) + CompilationTestHelper.newInstance(UnificationChecker.class, getClass()) .addSourceLines( "A.java", "class A {", @@ -52,10 +52,12 @@ public void unifies() { } @BugPattern( + name = "UnificationChecker", summary = "Verify that unifying the expression results in the correct binding", explanation = "For test purposes only", severity = SUGGESTION) - public static class DummyChecker extends BugChecker implements ExpressionStatementTreeMatcher { + public static class UnificationChecker extends BugChecker + implements ExpressionStatementTreeMatcher { @Override public Description matchExpressionStatement(ExpressionStatementTree tree, VisitorState state) { Unifier unifier = new Unifier(new Context()); From f99e3f5e234d129bcac7944981ec55895494ac4a Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Mon, 4 Jul 2022 06:21:30 +0200 Subject: [PATCH 3/3] Suggestions --- .../google/errorprone/refaster/UFreeIdentTest.java | 14 ++++++-------- .../google/errorprone/refaster/URepeatedTest.java | 14 ++++++-------- 2 files changed, 12 insertions(+), 16 deletions(-) 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 ad153d89c7e1..1e0840459b88 100644 --- a/core/src/test/java/com/google/errorprone/refaster/UFreeIdentTest.java +++ b/core/src/test/java/com/google/errorprone/refaster/UFreeIdentTest.java @@ -25,9 +25,9 @@ import com.google.errorprone.CompilationTestHelper; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker; -import com.google.errorprone.bugpatterns.BugChecker.ExpressionStatementTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; import com.google.errorprone.matchers.Description; -import com.sun.source.tree.ExpressionStatementTree; +import com.sun.source.tree.MethodInvocationTree; import com.sun.tools.javac.util.Context; import org.junit.Test; import org.junit.runner.RunWith; @@ -66,16 +66,14 @@ public void binds() { 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 ExpressionStatementTreeMatcher { + public static class UnificationChecker extends BugChecker implements MethodInvocationTreeMatcher { @Override - public Description matchExpressionStatement(ExpressionStatementTree tree, VisitorState state) { + public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { Unifier unifier = new Unifier(new Context()); UFreeIdent ident = UFreeIdent.create("foo"); - assertThat(ident.unify(tree.getExpression(), unifier)).isNotNull(); - assertThat(unifier.getBindings()) - .containsExactly(new UFreeIdent.Key("foo"), tree.getExpression()); + assertThat(ident.unify(tree, unifier)).isNotNull(); + assertThat(unifier.getBindings()).containsExactly(new UFreeIdent.Key("foo"), tree); return Description.NO_MATCH; } 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 9e09e2540654..c73a87d0a973 100644 --- a/core/src/test/java/com/google/errorprone/refaster/URepeatedTest.java +++ b/core/src/test/java/com/google/errorprone/refaster/URepeatedTest.java @@ -25,9 +25,9 @@ import com.google.errorprone.CompilationTestHelper; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker; -import com.google.errorprone.bugpatterns.BugChecker.ExpressionStatementTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; import com.google.errorprone.matchers.Description; -import com.sun.source.tree.ExpressionStatementTree; +import com.sun.source.tree.MethodInvocationTree; import com.sun.tools.javac.util.Context; import org.junit.Test; import org.junit.runner.RunWith; @@ -56,16 +56,14 @@ public void unifies() { 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 ExpressionStatementTreeMatcher { + public static class UnificationChecker extends BugChecker implements MethodInvocationTreeMatcher { @Override - public Description matchExpressionStatement(ExpressionStatementTree tree, VisitorState state) { + 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.getExpression(), unifier)).isNotNull(); - assertThat(unifier.getBindings()) - .containsExactly(new UFreeIdent.Key("foo"), tree.getExpression()); + assertThat(ident.unify(tree, unifier)).isNotNull(); + assertThat(unifier.getBindings()).containsExactly(new UFreeIdent.Key("foo"), tree); return Description.NO_MATCH; }