Skip to content

Commit

Permalink
Assert that BugCheckerRefactoringTestHelper outputs compile
Browse files Browse the repository at this point in the history
RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=135979952
  • Loading branch information
cushon authored and nick-someone committed Oct 13, 2016
1 parent b8a5a3c commit f24abe8
Showing 1 changed file with 20 additions and 27 deletions.
Expand Up @@ -21,7 +21,6 @@
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
import static org.junit.Assert.fail;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.io.CharStreams;
Expand Down Expand Up @@ -150,9 +149,18 @@ private BugCheckerRefactoringTestHelper addInputAndOutput(

private void runTestOnPair(JavaFileObject input, JavaFileObject output, TestMode testMode)
throws IOException {
Context context = new Context();
JCCompilationUnit tree = doCompile(input, sources.keySet(), context);
JavaFileObject transformed = applyDiff(input, context, tree);
testMode.verifyMatch(transformed, output);
doCompile(output, sources.values(), new Context());
}

private JCCompilationUnit doCompile(
final JavaFileObject input, Iterable<JavaFileObject> files, Context context)
throws IOException {
JavacTool tool = JavacTool.create();
DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<>();
Context context = new Context();
context.put(ErrorProneOptions.class, ErrorProneOptions.empty());
JavacTaskImpl task =
(JavacTaskImpl)
Expand All @@ -162,23 +170,22 @@ private void runTestOnPair(JavaFileObject input, JavaFileObject output, TestMode
diagnosticsCollector,
options,
/*classes=*/ null,
ImmutableList.copyOf(sources.keySet()),
files,
context);
JCCompilationUnit tree = parseAndAnalyze(task, input);
Iterable<? extends CompilationUnitTree> trees = task.parse();
task.analyze();
JCCompilationUnit tree =
Iterables.getOnlyElement(
Iterables.filter(
Iterables.filter(trees, JCCompilationUnit.class),
compilationUnit -> compilationUnit.getSourceFile() == input));
Iterable<Diagnostic<? extends JavaFileObject>> errorDiagnostics =
Iterables.filter(
diagnosticsCollector.getDiagnostics(),
new Predicate<Diagnostic<? extends JavaFileObject>>() {
@Override
public boolean apply(Diagnostic<? extends JavaFileObject> input) {
return input.getKind() == Diagnostic.Kind.ERROR;
}
});
diagnosticsCollector.getDiagnostics(), d -> d.getKind() == Diagnostic.Kind.ERROR);
if (!Iterables.isEmpty(errorDiagnostics)) {
fail("compilation failed unexpectedly: " + errorDiagnostics);
}
JavaFileObject transformed = applyDiff(input, context, tree);
testMode.verifyMatch(transformed, output);
return tree;
}

private JavaFileObject applyDiff(
Expand Down Expand Up @@ -209,20 +216,6 @@ public void onDescribed(Description description) {
return transformed;
}

private JCCompilationUnit parseAndAnalyze(JavacTaskImpl task, final JavaFileObject input) {
Iterable<? extends CompilationUnitTree> trees = task.parse();
task.analyze();
return Iterables.getOnlyElement(
Iterables.filter(
Iterables.filter(trees, JCCompilationUnit.class),
new Predicate<JCCompilationUnit>() {
@Override
public boolean apply(JCCompilationUnit compilation) {
return compilation.getSourceFile() == input;
}
}));
}

private ErrorProneScannerTransformer transformer(BugChecker bugChecker) {
ErrorProneScanner scanner = new ErrorProneScanner(bugChecker);
return ErrorProneScannerTransformer.create(scanner);
Expand Down

0 comments on commit f24abe8

Please sign in to comment.