Have RefasterTemplateCollection verify template test class names (#233)

This commit is contained in:
Stephan Schroevers
2022-09-29 11:53:22 +02:00
committed by GitHub
parent 5b079eef84
commit 2ba7bf9f46
7 changed files with 76 additions and 3 deletions

View File

@@ -7,7 +7,7 @@ import com.google.common.collect.Iterables;
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);

View File

@@ -7,7 +7,7 @@ import com.google.common.collect.Iterables;
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);

View File

@@ -29,6 +29,7 @@ import com.google.errorprone.fixes.Replacement;
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<String> 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 final class RefasterTemplateCollection extends BugChecker
@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
reportIncorrectClassName(tree, state);
List<Description> matches = new ArrayList<>();
delegate.matchCompilationUnit(
tree,
@@ -147,6 +151,29 @@ public final class RefasterTemplateCollection extends BugChecker
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 token. */\n")));
}
}
}
private static ImmutableRangeMap<Integer, String> indexTemplateMatches(
List<Description> matches, EndPosTable endPositions) {
ImmutableRangeMap.Builder<Integer, String> templateMatches = ImmutableRangeMap.builder();

View File

@@ -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();
}
}
}

View File

@@ -19,6 +19,7 @@ final class RefasterTemplateCollectionTest {
classes = {
MatchInWrongMethodTemplates.class,
MethodWithoutPrefixTemplates.class,
MisnamedTestClassTemplates.class,
MissingTestAndWrongTestTemplates.class,
PartialTestMatchTemplates.class,
TemplateWithoutTestTemplates.class,

View File

@@ -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.
;

View File

@@ -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 token. */
;