Apply suggestions

This commit is contained in:
Rick Ossendrijver
2022-03-21 16:53:09 +01:00
parent 29f4a70f61
commit d60598a5d2
17 changed files with 48 additions and 18 deletions

View File

@@ -184,6 +184,11 @@
<artifactId>refaster-resource-compiler</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>refaster-runner</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>refaster-support</artifactId>

View File

@@ -44,7 +44,6 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>refaster-runner</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>

View File

@@ -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.CompilationUnitTree;
import com.sun.source.tree.LineMap;
import com.sun.source.tree.MethodTree;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.tree.EndPosTable;
@@ -125,15 +126,16 @@ public final class RefasterCollectionTestUtil {
VisitorState.createForCustomFindingCollection(new SubContext(state.context), matches::add)
.withPath(state.getPath()));
JCCompilationUnit compilationUnit = (JCCompilationUnit) tree;
ImmutableRangeMap<Integer, String> matchesRangeMap =
buildRangeMapForMatches(matches, ((JCCompilationUnit) tree).endPositions);
buildRangeMapForMatches(matches, compilationUnit.endPositions);
ImmutableSet<String> templatesWithoutMatch = getTemplateNamesWithoutMatch(matches);
if (!templatesWithoutMatch.isEmpty()) {
appendCommentToCompilationUnit(
String.format(
"Did not encounter test in %s for the following template(s)",
ASTHelpers.getFileName(tree)),
"Did not encounter a test in `%s` for the following template(s)",
getNameFromFQCN(compilationUnit.sourcefile.getName().replace(".java", ""))),
templatesWithoutMatch.stream().collect(LIST_COLLECTOR),
state);
}
@@ -197,6 +199,7 @@ public final class RefasterCollectionTestUtil {
String methodName = tree.getName().toString().replace("test", "");
int startPosition = ASTHelpers.getStartPosition(tree);
int endPosition = state.getEndPosition(tree);
LineMap lineMap = state.getPath().getCompilationUnit().getLineMap();
ImmutableRangeMap<Integer, String> matchesInCurrentMethod =
matchesRangeMap.subRangeMap(Range.open(startPosition, endPosition));
@@ -205,10 +208,15 @@ public final class RefasterCollectionTestUtil {
if (!correctTemplatesMatchedInMethod) {
appendCommentToCompilationUnit(
String.format(
"The following matches occurred in method `%s` (position: [%s,%s])",
tree.getName(), startPosition, endPosition),
"The following matches unexpectedly occurred in method `%s`", tree.getName()),
matchesRangeMap.asMapOfRanges().entrySet().stream()
.map(e -> "Template `" + e.getValue() + "` matched on position: " + e.getKey())
.map(
e ->
String.format(
"Template `%s` matches on line %s, while it should match in a method named `test%s`.",
e.getValue(),
lineMap.getLineNumber(e.getKey().lowerEndpoint()),
e.getValue()))
.collect(LIST_COLLECTOR),
state);
}

View File

@@ -1,4 +1,4 @@
/** Utilities that support testing Refaster templates and validate the tests. */
/** Utilities that support testing Refaster templates and allows for validating these tests. */
@com.google.errorprone.annotations.CheckReturnValue
@javax.annotation.ParametersAreNonnullByDefault
package tech.picnic.errorprone.refaster.test;

View File

@@ -3,6 +3,9 @@ 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 match occurring in an unexpected place.
*/
final class MatchInWrongMethodTemplates {
private MatchInWrongMethodTemplates() {}

View File

@@ -3,6 +3,7 @@ package tech.picnic.errorprone.refaster.test;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
/** Refaster template with a number as suffix to validate that it is reported correctly. */
final class MethodNameWithNumberTemplates {
private MethodNameWithNumberTemplates() {}

View File

@@ -3,6 +3,10 @@ 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 that a missing test and misplaced tests are both
* reported.
*/
final class MissingTestAndWrongTestTemplates {
private MissingTestAndWrongTestTemplates() {}

View File

@@ -3,6 +3,7 @@ package tech.picnic.errorprone.refaster.test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
/** Validate error reporting of the Refaster template collections. */
final class RefasterCollectionTestUtilTest {
@ParameterizedTest
@ValueSource(

View File

@@ -3,6 +3,7 @@ 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 the reporting of missing test methods. */
final class TemplateWithoutTestTemplates {
private TemplateWithoutTestTemplates() {}

View File

@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refaster.test;
/** Code to test the Refaster templates from `MatchInWrongMethodTemplates`. */
final class MatchInWrongMethodTemplatesTest implements RefasterTemplateTestCase {
boolean testWrongName() {
"foo".equals("");

View File

@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refaster.test;
/** Code to test the Refaster templates from `MatchInWrongMethodTemplates`. */
final class MatchInWrongMethodTemplatesTest implements RefasterTemplateTestCase {
boolean testWrongName() {
"foo".isEmpty();
@@ -7,8 +8,8 @@ final class MatchInWrongMethodTemplatesTest implements RefasterTemplateTestCase
return "baz".isEmpty();
}
}
/* The following matches occurred in method `testWrongName` (position: [131,233]):
- Template `StringIsEmpty` matched on position: [161..177)
- Template `StringIsEmpty` matched on position: [183..199)
- Template `StringIsEmpty` matched on position: [212..228)
/* The following matches unexpectedly occurred in method `testWrongName`:
- Template `StringIsEmpty` matches on line 6, while it should match in a method named `testStringIsEmpty`.
- Template `StringIsEmpty` matches on line 7, while it should match in a method named `testStringIsEmpty`.
- Template `StringIsEmpty` matches on line 8, while it should match in a method named `testStringIsEmpty`.
*/

View File

@@ -3,6 +3,7 @@ package tech.picnic.errorprone.refaster.test;
import com.google.common.collect.ImmutableSet;
import java.util.Collections;
/** Code to test the Refaster templates from `MethodNameWithNumberTemplates`. */
final class MethodNameWithNumberTemplatesTest implements RefasterTemplateTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {

View File

@@ -3,6 +3,7 @@ package tech.picnic.errorprone.refaster.test;
import com.google.common.collect.ImmutableSet;
import java.util.Collections;
/** Code to test the Refaster templates from `MethodNameWithNumberTemplates`. */
final class MethodNameWithNumberTemplatesTest implements RefasterTemplateTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {

View File

@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refaster.test;
/** Code to test the Refaster templates from `MissingTestAndWrongTestTemplates`. */
final class MissingTestAndWrongTestTemplatesTest implements RefasterTemplateTestCase {
boolean testWrongName() {
"foo".equals("");

View File

@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refaster.test;
/** Code to test the Refaster templates from `MissingTestAndWrongTestTemplates`. */
final class MissingTestAndWrongTestTemplatesTest implements RefasterTemplateTestCase {
boolean testWrongName() {
"foo".isEmpty();
@@ -7,11 +8,11 @@ final class MissingTestAndWrongTestTemplatesTest implements RefasterTemplateTest
return "baz".isEmpty();
}
}
/* Did not encounter test in /tech.picnic.errorprone.refaster.test.MissingTestAndWrongTestTemplatesTestInput.java for the following template(s):
/* Did not encounter a test in `MissingTestAndWrongTestTemplatesTestInput` for the following template(s):
- TemplateWithoutTest
*/
/* The following matches occurred in method `testWrongName` (position: [136,238]):
- Template `StringIsEmpty` matched on position: [166..182)
- Template `StringIsEmpty` matched on position: [188..204)
- Template `StringIsEmpty` matched on position: [217..233)
/* The following matches unexpectedly occurred in method `testWrongName`:
- Template `StringIsEmpty` matches on line 6, while it should match in a method named `testStringIsEmpty`.
- Template `StringIsEmpty` matches on line 7, while it should match in a method named `testStringIsEmpty`.
- Template `StringIsEmpty` matches on line 8, while it should match in a method named `testStringIsEmpty`.
*/

View File

@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refaster.test;
/** Code to test the Refaster templates from `TemplateWithoutTestTemplates`. */
final class TemplateWithoutTestTemplatesTest implements RefasterTemplateTestCase {
boolean testStringIsEmpty() {
return "foo".equals("");

View File

@@ -1,11 +1,12 @@
package tech.picnic.errorprone.refaster.test;
/** Code to test the Refaster templates from `TemplateWithoutTestTemplates`. */
final class TemplateWithoutTestTemplatesTest implements RefasterTemplateTestCase {
boolean testStringIsEmpty() {
return "foo".isEmpty();
}
}
/* Did not encounter test in /tech.picnic.errorprone.refaster.test.TemplateWithoutTestTemplatesTestInput.java for the following template(s):
/* Did not encounter a test in `TemplateWithoutTestTemplatesTestInput` for the following template(s):
- AnotherTemplateWithoutTest
- TemplateWithoutTest
*/