From a65cade714e8e2ee3a92383803f12ea807571182 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 10 Sep 2022 19:49:13 +0200 Subject: [PATCH 1/3] Have `RefasterTemplateCollection` verify template test class names --- .../AssertJEnumerableTemplatesTestInput.java | 2 +- .../AssertJEnumerableTemplatesTestOutput.java | 2 +- .../test/RefasterTemplateCollection.java | 30 ++++++++++++++++++- .../test/MisnamedTestClassTemplates.java | 21 +++++++++++++ .../test/RefasterTemplateCollectionTest.java | 1 + .../MisnamedTestClassTemplatesTestInput.java | 11 +++++++ .../MisnamedTestClassTemplatesTestOutput.java | 13 ++++++++ 7 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplates.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestInput.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestInput.java index 7374a993eb..2ef8511104 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestInput.java @@ -7,7 +7,7 @@ import org.assertj.core.api.EnumerableAssert; import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; -final class AssertJEnumableTemplatesTest implements RefasterTemplateTestCase { +final class AssertJEnumerableTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(Iterables.class); diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestOutput.java index 598a38168f..5c46789db9 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestOutput.java @@ -7,7 +7,7 @@ import org.assertj.core.api.EnumerableAssert; import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; -final class AssertJEnumableTemplatesTest implements RefasterTemplateTestCase { +final class AssertJEnumerableTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(Iterables.class); diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java index 63c4b9e104..cacfc3e4b7 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java @@ -29,6 +29,7 @@ import com.google.errorprone.fixes.SuggestedFix; import com.google.errorprone.matchers.Description; import com.google.errorprone.util.ASTHelpers; +import com.sun.source.tree.ClassTree; import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.LineMap; import com.sun.source.tree.MethodTree; @@ -68,6 +69,7 @@ public final class RefasterTemplateCollection extends BugChecker "RefasterTemplateCollection:TemplateCollection"; private static final String TEST_METHOD_NAME_PREFIX = "test"; + private final String templateCollectionUnderTest; private final ImmutableSortedSet templatesUnderTest; private final Refaster delegate; @@ -77,7 +79,7 @@ public final class RefasterTemplateCollection extends BugChecker * @param flags Any provided command line flags. */ public RefasterTemplateCollection(ErrorProneFlags flags) { - String templateCollectionUnderTest = getTemplateCollectionUnderTest(flags); + templateCollectionUnderTest = getTemplateCollectionUnderTest(flags); delegate = createRefasterChecker(templateCollectionUnderTest); templatesUnderTest = getTemplatesUnderTest(templateCollectionUnderTest); } @@ -131,6 +133,8 @@ public static void validate(Class clazz) { @Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { + reportIncorrectClassName(tree, state); + List matches = new ArrayList<>(); delegate.matchCompilationUnit( tree, @@ -147,6 +151,30 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s return Description.NO_MATCH; } + private void reportIncorrectClassName(CompilationUnitTree tree, VisitorState state) { + String expectedClassName = templateCollectionUnderTest + "Test"; + + for (Tree typeDeclaration : tree.getTypeDecls()) { + if (typeDeclaration instanceof ClassTree) { + if (!((ClassTree) typeDeclaration).getSimpleName().contentEquals(expectedClassName)) { + state.reportMatch( + describeMatch( + typeDeclaration, + SuggestedFix.prefixWith( + typeDeclaration, + String.format( + "/* ERROR: Class should be named `%s`. */\n", expectedClassName)))); + } + } else { + state.reportMatch( + describeMatch( + typeDeclaration, + SuggestedFix.prefixWith( + typeDeclaration, "/* ERROR: Unexpected declaration. */\n"))); + } + } + } + private static ImmutableRangeMap indexTemplateMatches( List matches, EndPosTable endPositions) { ImmutableRangeMap.Builder templateMatches = ImmutableRangeMap.builder(); diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplates.java new file mode 100644 index 0000000000..decb664984 --- /dev/null +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplates.java @@ -0,0 +1,21 @@ +package tech.picnic.errorprone.refaster.test; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; + +/** Refaster template collection to validate reporting of a misnamed test class. */ +final class MisnamedTestClassTemplates { + private MisnamedTestClassTemplates() {} + + static final class StringIsEmpty { + @BeforeTemplate + boolean before(String string) { + return string.equals(""); + } + + @AfterTemplate + boolean after(String string) { + return string.isEmpty(); + } + } +} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionTest.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionTest.java index 360a17902c..f6182b46b9 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionTest.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionTest.java @@ -19,6 +19,7 @@ final class RefasterTemplateCollectionTest { classes = { MatchInWrongMethodTemplates.class, MethodWithoutPrefixTemplates.class, + MisnamedTestClassTemplates.class, MissingTestAndWrongTestTemplates.class, PartialTestMatchTemplates.class, TemplateWithoutTestTemplates.class, diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestInput.java new file mode 100644 index 0000000000..3560524e0b --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestInput.java @@ -0,0 +1,11 @@ +package tech.picnic.errorprone.refaster.test; + +/** Code to test the Refaster templates from {@link MisnamedTestClassTemplates}. */ +final class IncorrectNameTemplatesTest implements RefasterTemplateTestCase { + boolean testStringIsEmpty() { + return "foo".equals(""); + } +} + +// This is a comment to appease CheckStyle. +; diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java new file mode 100644 index 0000000000..981e4a4024 --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java @@ -0,0 +1,13 @@ +package tech.picnic.errorprone.refaster.test; + +/** Code to test the Refaster templates from {@link MisnamedTestClassTemplates}. */ +/* ERROR: Class should be named `MisnamedTestClassTemplatesTest`. */ +final class IncorrectNameTemplatesTest implements RefasterTemplateTestCase { + boolean testStringIsEmpty() { + return "foo".isEmpty(); + } +} + +// This is a comment to appease CheckStyle. +/* ERROR: Unexpected declaration. */ +; From 0a23f9f7bcb2b4ccf1f90b56a6e65e728d4be980 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 28 Sep 2022 20:41:50 +0200 Subject: [PATCH 2/3] `s/CheckStyle/Checkstyle` --- .../refaster/test/MisnamedTestClassTemplatesTestInput.java | 2 +- .../refaster/test/MisnamedTestClassTemplatesTestOutput.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestInput.java index 3560524e0b..a89fb8b7fb 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestInput.java @@ -7,5 +7,5 @@ boolean testStringIsEmpty() { } } -// This is a comment to appease CheckStyle. +// This is a comment to appease Checkstyle. ; diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java index 981e4a4024..a5741167de 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java @@ -8,6 +8,6 @@ boolean testStringIsEmpty() { } } -// This is a comment to appease CheckStyle. +// This is a comment to appease Checkstyle. /* ERROR: Unexpected declaration. */ ; From c1e8bb6a391e1e9d4ea42ee2cbe1599aaaa34e86 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Thu, 29 Sep 2022 08:25:13 +0200 Subject: [PATCH 3/3] `s/Unexpected declaration/Unexpected token/` --- .../errorprone/refaster/test/RefasterTemplateCollection.java | 3 +-- .../refaster/test/MisnamedTestClassTemplatesTestOutput.java | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java index cacfc3e4b7..e3d6f3567e 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java @@ -169,8 +169,7 @@ private void reportIncorrectClassName(CompilationUnitTree tree, VisitorState sta state.reportMatch( describeMatch( typeDeclaration, - SuggestedFix.prefixWith( - typeDeclaration, "/* ERROR: Unexpected declaration. */\n"))); + SuggestedFix.prefixWith(typeDeclaration, "/* ERROR: Unexpected token. */\n"))); } } } diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java index a5741167de..12e3ba161d 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MisnamedTestClassTemplatesTestOutput.java @@ -9,5 +9,5 @@ boolean testStringIsEmpty() { } // This is a comment to appease Checkstyle. -/* ERROR: Unexpected declaration. */ +/* ERROR: Unexpected token. */ ;