Skip to content
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 @@ -228,6 +228,8 @@ void f(String hello) {
""")
// TODO: b/472686687 - remove allowBreakingChanges when the bug is fixed
.allowBreakingChanges()
// TODO: b/472686687 - remove allowFormattingErrors when the bug is fixed
.allowFormattingErrors()
// TODO: b/472686687 - remove TEXT_MATCH when the bug is fixed
.doTest(TEXT_MATCH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,46 @@ public class BugCheckerRefactoringTestHelper {
public enum TestMode {
TEXT_MATCH {
@Override
void verifyMatch(JavaFileObject refactoredSource, JavaFileObject expectedSource)
void verifyMatch(
JavaFileObject refactoredSource,
JavaFileObject expectedSource,
boolean allowFormattingErrors)
throws IOException {
assertThat(maybeFormat(refactoredSource.getCharContent(false).toString()))
.isEqualTo(maybeFormat(expectedSource.getCharContent(false).toString()));
assertThat(
maybeFormat(
refactoredSource.getCharContent(false).toString(), allowFormattingErrors))
.isEqualTo(
maybeFormat(
expectedSource.getCharContent(false).toString(), allowFormattingErrors));
}

private String maybeFormat(String input) {
private String maybeFormat(String input, boolean allowFormattingErrors) {
try {
Formatter formatter = new Formatter();
return StringWrapper.wrap(formatter.formatSource(input), formatter);
} catch (FormatterException e) {
return input;
if (allowFormattingErrors) {
return input;
}
throw new AssertionError(
"Failed to format source, and allowFormattingErrors is false", e);
}
}
},
AST_MATCH {
@Override
void verifyMatch(JavaFileObject refactoredSource, JavaFileObject expectedSource) {
void verifyMatch(
JavaFileObject refactoredSource,
JavaFileObject expectedSource,
boolean allowFormattingErrors) {
assertAbout(javaSource()).that(refactoredSource).parsesAs(expectedSource);
}
};

abstract void verifyMatch(JavaFileObject refactoredSource, JavaFileObject expectedSource)
abstract void verifyMatch(
JavaFileObject refactoredSource,
JavaFileObject expectedSource,
boolean allowFormattingErrors)
throws IOException;
}

Expand Down Expand Up @@ -156,6 +173,7 @@ public Fix choose(List<Fix> fixes) {
private FixChooser fixChooser = FixChoosers.FIRST;
private ImmutableList<String> options = ImmutableList.of();
private boolean allowBreakingChanges = false;
private boolean allowFormattingErrors = false;
private String importOrder = "static-first";

private boolean run = false;
Expand Down Expand Up @@ -241,6 +259,13 @@ public BugCheckerRefactoringTestHelper allowBreakingChanges() {
return this;
}

/** If set, formatting errors in the output are allowed. Off by default. */
@CanIgnoreReturnValue
public BugCheckerRefactoringTestHelper allowFormattingErrors() {
allowFormattingErrors = true;
return this;
}

@CanIgnoreReturnValue
public BugCheckerRefactoringTestHelper setImportOrder(String importOrder) {
this.importOrder = importOrder;
Expand Down Expand Up @@ -283,7 +308,7 @@ private void runTestOnPair(JavaFileObject input, JavaFileObject output, TestMode
JCCompilationUnit tree = doCompile(input, sources.keySet(), context);
JavaFileObject transformed = applyDiff(input, context, tree);
closeCompiler(context);
testMode.verifyMatch(transformed, output);
testMode.verifyMatch(transformed, output, allowFormattingErrors);
if (!allowBreakingChanges) {
Context anotherContext = new Context();
doCompile(output, sources.values(), anotherContext);
Expand Down
Loading