Skip to content

Commit

Permalink
Have RefasterTemplateCollection verify template test class names
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Sep 10, 2022
1 parent e34c2ba commit a604b35
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -131,6 +133,8 @@ public static void validate(Class<?> clazz) {

@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
reportIncorrectClassName(tree, state);

List<Description> matches = new ArrayList<>();
delegate.matchCompilationUnit(
tree,
Expand All @@ -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<Integer, String> indexTemplateMatches(
List<Description> matches, EndPosTable endPositions) {
ImmutableRangeMap.Builder<Integer, String> templateMatches = ImmutableRangeMap.builder();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class RefasterTemplateCollectionTest {
classes = {
MatchInWrongMethodTemplates.class,
MethodWithoutPrefixTemplates.class,
MisnamedTestClassTemplates.class,
MissingTestAndWrongTestTemplates.class,
PartialTestMatchTemplates.class,
TemplateWithoutTestTemplates.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
;
Original file line number Diff line number Diff line change
@@ -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. */
;

0 comments on commit a604b35

Please sign in to comment.