-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
BugCheckerRules
Refaster rule collection (#526)
- Loading branch information
Bastien Diederichs
authored
Mar 23, 2023
1 parent
04368e9
commit 8f1d1df
Showing
8 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BugCheckerRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...rib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}"); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |