Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have RefasterTemplateCollection verify template test class names #233

Merged
merged 3 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,29 @@ 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 token. */\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 token. */
;