Skip to content

Commit

Permalink
Apply feedback and update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed Aug 4, 2022
1 parent 5ce5a50 commit 5b49655
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import tech.picnic.errorprone.refaster.test.RefasterTemplateCollectionValidator;
import tech.picnic.errorprone.refaster.test.RefasterTemplateCollection;

final class RefasterTemplatesTest {
/** The names of all Refaster template groups defined in this module. */
Expand Down Expand Up @@ -76,6 +76,6 @@ private static Stream<Arguments> validateTemplateCollectionTestCases() {
@MethodSource("validateTemplateCollectionTestCases")
@ParameterizedTest
void validateTemplateCollection(Class<?> clazz) {
RefasterTemplateCollectionValidator.validate(clazz);
RefasterTemplateCollection.validate(clazz);
}
}
40 changes: 20 additions & 20 deletions refaster-test-support/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ match the associated output file.

This extension ensures that each Refaster template is tested, making sure that
it matches and transforms code as intended. If a Refaster template is not
covered by a test, if it influences unrelated test code code, or if the
associated test doesn't follow certain established standards, then this
irregularity will be reported, and the associated template collection test will
fail. This way developers receive guidance on how to write Refaster template
tests and assurance that every template is properly tested.
covered by a test, if it influences unrelated test code, or if the associated
test doesn't follow certain established standards, then this irregularity will
be reported, and the associated template collection test will fail. This way
developers receive guidance on how to write Refaster template tests and
assurance that every template is properly tested.

## How to test a collection of Refaster templates

In a nutshell, to test a Refaster template collection class using
`RefasterTemplateCollectionValidator`, one should create suitably named input
and output files. The collection's Refaster templates are applied to the input
file and must exactly match the contents of the provided output file.
A class that contains one or more Refaster templates is called a Refaster
template collection. In a nutshell, to test a Refaster template collection
class using `RefasterTemplateCollection`, one should create suitably named
input and output files. The collection's Refaster templates are applied to the
input file and must exactly match the contents of the provided output file.

To test Refaster templates, one can create a (parameterized) test for every
class containing the Refaster templates to invoke the
`RefasterTemplateCollectionValidator`. A class that contains one or more
Refaster templates is called a Refaster template collection. This test utility
applies the Refaster templates in the collection to a provided input file, and
expects the result to exactly match the contents of a provided output file.
class that contains Refaster templates and invoke
`RefasterTemplateCollection#validate`. This test utility applies the Refaster
templates in the collection to a provided input file, and expects the result
to exactly match the contents of a provided output file.

To adopt this setup, the following requirements have to be met:

- Create a class with a (parameterized) test method that calls the
`RefasterTemplateCollectionValidator#validate` and passes the collection(s)
to validate. The Refaster template collection must match the naming
convention `<TemplateCollectionName>Templates.java`.
- Create a class with a (parameterized) test method that invokes
`RefasterTemplateCollection#validate` and passes the collection(s) to
validate. The Refaster template collection must match the naming convention
`<TemplateCollectionName>Templates.java`.
- An input file matching the naming convention
`<TemplateCollectionName>TemplatesTestInput.java` is added for every template
collection.
- An output file matching the naming convention
`<TemplateCollectionName>TemplatesTestOutput.java` file is added for every
template collection.
- For each Refaster template in the collection, the input and output file must
- For every Refaster template in the collection, the input and output file must
contain a method. The name of the method is equal to the name of the Refaster
template prefixed with `test` (e.g. `test<RefasterTemplateClassName>`).
- The method contains at least one expression that matches the
Expand All @@ -68,7 +68,7 @@ src/
tech.picnic.errorprone.refastertemplates
└── RefasterCollectionTest.java
-- This test class invokes
-- `RefasterTemplateCollectionValidator#validate`.
-- `RefasterTemplateCollection#validate`.
resources/
tech.picnic.errorprone.refastertemplates
└── ExampleTemplatesTestInput.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@
* exactly one distinct method, as indicated by each method's name.
*/
@BugPattern(
name = "RefasterTemplateCollectionValidator",
name = "RefasterTemplateCollection",
summary = "Exercises a Refaster template collection",
severity = ERROR)
public final class RefasterTemplateCollectionValidator extends BugChecker
public final class RefasterTemplateCollection extends BugChecker
implements CompilationUnitTreeMatcher {
private static final long serialVersionUID = 1L;
private static final String TEMPLATE_COLLECTION_FLAG =
"RefasterTemplateCollectionValidator:TemplateCollection";
"RefasterTemplateCollection:TemplateCollection";
private static final String TEST_METHOD_NAME_PREFIX = "test";

private final ImmutableSortedSet<String> templatesUnderTest;
private final RefasterCheck delegate;

/**
* Instantiates a {@link RefasterTemplateCollectionValidator} instance.
* Instantiates a {@link RefasterTemplateCollection} instance.
*
* @param flags Any provided command line flags.
*/
public RefasterTemplateCollectionValidator(ErrorProneFlags flags) {
public RefasterTemplateCollection(ErrorProneFlags flags) {
String templateCollectionUnderTest = getTemplateCollectionUnderTest(flags);
delegate = createRefasterCheck(templateCollectionUnderTest);
templatesUnderTest = getTemplatesUnderTest(templateCollectionUnderTest);
Expand Down Expand Up @@ -117,7 +117,7 @@ private static ImmutableSortedSet<String> getTemplatesUnderTest(
public static void validate(Class<?> clazz) {
String className = clazz.getSimpleName();

BugCheckerRefactoringTestHelper.newInstance(RefasterTemplateCollectionValidator.class, clazz)
BugCheckerRefactoringTestHelper.newInstance(RefasterTemplateCollection.class, clazz)
.setArgs(ImmutableList.of("-XepOpt:" + TEMPLATE_COLLECTION_FLAG + '=' + className))
.addInput(className + "TestInput.java")
.addOutput(className + "TestOutput.java")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import org.junit.jupiter.params.provider.ValueSource;

/**
* Validates {@link RefasterTemplateCollectionValidator} error reporting.
* Validates {@link RefasterTemplateCollection} error reporting.
*
* <p>The goal of the Refaster template collections under test is to verify the reporting of
* violations by {@link RefasterTemplateCollectionValidator} using the associated {@code
* TestInput.java} and {@code TestOutput.java} files. Normally, {@link
* RefasterTemplateCollectionValidator} will raise error messages to be rendered in the console or
* IDE. However, to verify that these error messages are as intended, the {@code *TestOutput.java}
* files in this package contain error reporting that is normally not present.
* violations by {@link RefasterTemplateCollection} using the associated {@code TestInput.java} and
* {@code TestOutput.java} files. Normally, {@link RefasterTemplateCollection} will raise error
* messages to be rendered in the console or IDE. However, to verify that these error messages are
* as intended, the {@code *TestOutput.java} files in this package contain error reporting that is
* normally not present.
*/
final class RefasterTemplateCollectionValidatorTest {
final class RefasterTemplateCollectionTest {
@ParameterizedTest
@ValueSource(
classes = {
Expand All @@ -25,6 +25,6 @@ final class RefasterTemplateCollectionValidatorTest {
ValidTemplates.class
})
void verifyRefasterTemplateCollections(Class<?> clazz) {
RefasterTemplateCollectionValidator.validate(clazz);
RefasterTemplateCollection.validate(clazz);
}
}

0 comments on commit 5b49655

Please sign in to comment.