Skip to content

Commit

Permalink
Introduce BugCheckerRules Refaster rule collection (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien Diederichs authored Mar 23, 2023
1 parent 04368e9 commit 8f1d1df
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 4 deletions.
2 changes: 1 addition & 1 deletion error-prone-contrib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<dependency>
<groupId>${groupId.error-prone}</groupId>
<artifactId>error_prone_test_helpers</artifactId>
<scope>test</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tech.picnic.errorprone.refasterrules;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChooser;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;

/** Refaster rules related to {@link com.google.errorprone.bugpatterns.BugChecker} classes. */
@OnlineDocumentation
final class BugCheckerRules {
private BugCheckerRules() {}

/**
* Avoid calling {@link BugCheckerRefactoringTestHelper#setFixChooser(FixChooser)} or {@link
* BugCheckerRefactoringTestHelper#setImportOrder(String)} with their respective default values.
*/
static final class BugCheckerRefactoringTestHelperIdentity {
@BeforeTemplate
BugCheckerRefactoringTestHelper before(BugCheckerRefactoringTestHelper helper) {
return Refaster.anyOf(
helper.setFixChooser(FixChoosers.FIRST), helper.setImportOrder("static-first"));
}

@AfterTemplate
BugCheckerRefactoringTestHelper after(BugCheckerRefactoringTestHelper helper) {
return helper;
}
}

/**
* Prefer {@link BugCheckerRefactoringTestHelper.ExpectOutput#expectUnchanged()} over repeating
* the input.
*/
// XXX: This rule assumes that the full source code is specified as a single string, e.g. using a
// text block. Support for multi-line source code input would require a `BugChecker`
// implementation instead.
static final class BugCheckerRefactoringTestHelperAddInputLinesExpectUnchanged {
@BeforeTemplate
BugCheckerRefactoringTestHelper before(
BugCheckerRefactoringTestHelper helper, String path, String source) {
return helper.addInputLines(path, source).addOutputLines(path, source);
}

@AfterTemplate
BugCheckerRefactoringTestHelper after(
BugCheckerRefactoringTestHelper helper, String path, String source) {
return helper.addInputLines(path, source).expectUnchanged();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ void identification() {
@Test
void replacementFirstSuggestedFix() {
BugCheckerRefactoringTestHelper.newInstance(FluxFlatMapUsage.class, getClass())
.setFixChooser(FixChoosers.FIRST)
.addInputLines(
"A.java",
"import reactor.core.publisher.Flux;",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ void identification() {
@Test
void replacementFirstSuggestedFix() {
BugCheckerRefactoringTestHelper.newInstance(IdentityConversion.class, getClass())
.setFixChooser(FixChoosers.FIRST)
.addInputLines(
"A.java",
"import static com.google.errorprone.matchers.Matchers.staticMethod;",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ void identification() {
@Test
void replacementFirstSuggestedFix() {
BugCheckerRefactoringTestHelper.newInstance(StringCaseLocaleUsage.class, getClass())
.setFixChooser(FixChoosers.FIRST)
.addInputLines(
"A.java",
"class A {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class RefasterRulesTest {
AssertJThrowingCallableRules.class,
AssortedRules.class,
BigDecimalRules.class,
BugCheckerRules.class,
CollectionRules.class,
ComparatorRules.class,
DoubleStreamRules.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tech.picnic.errorprone.refasterrules;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
import com.google.errorprone.bugpatterns.BugChecker;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class BugCheckerRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(FixChoosers.class);
}

ImmutableSet<BugCheckerRefactoringTestHelper> testBugCheckerRefactoringTestHelperIdentity() {
return ImmutableSet.of(
BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass())
.setFixChooser(FixChoosers.FIRST),
BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass())
.setImportOrder("static-first"));
}

BugCheckerRefactoringTestHelper
testBugCheckerRefactoringTestHelperAddInputLinesExpectUnchanged() {
return BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass())
.addInputLines("A.java", "class A {}")
.addOutputLines("A.java", "class A {}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tech.picnic.errorprone.refasterrules;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
import com.google.errorprone.bugpatterns.BugChecker;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class BugCheckerRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(FixChoosers.class);
}

ImmutableSet<BugCheckerRefactoringTestHelper> testBugCheckerRefactoringTestHelperIdentity() {
return ImmutableSet.of(
BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass()),
BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass()));
}

BugCheckerRefactoringTestHelper
testBugCheckerRefactoringTestHelperAddInputLinesExpectUnchanged() {
return BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass())
.addInputLines("A.java", "class A {}")
.expectUnchanged();
}
}

0 comments on commit 8f1d1df

Please sign in to comment.