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 7374a993eb7..2ef85111047 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 598a38168f4..5c46789db94 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 63c4b9e1040..cacfc3e4b75 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 00000000000..decb664984e --- /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 360a17902c1..f6182b46b9f 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 00000000000..3560524e0b9 --- /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 00000000000..981e4a4024c --- /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. */ +;