Search in sources :

Example 1 with LookForCheckNameInDiagnostic

use of com.google.errorprone.DiagnosticTestHelper.LookForCheckNameInDiagnostic in project error-prone by google.

the class CompilationTestHelper method doTest.

/**
   * Performs a compilation and checks that the diagnostics and result match the expectations.
   */
// TODO(eaftan): any way to ensure that this is actually called?
public void doTest() {
    Preconditions.checkState(!sources.isEmpty(), "No source files to compile");
    List<String> allArgs = buildArguments(args);
    Result result = compile(sources, allArgs.toArray(new String[allArgs.size()]));
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticHelper.getDiagnostics()) {
        if (diagnostic.getCode().contains("error.prone.crash")) {
            fail(diagnostic.getMessage(Locale.ENGLISH));
        }
    }
    if (expectNoDiagnostics) {
        List<Diagnostic<? extends JavaFileObject>> diagnostics = diagnosticHelper.getDiagnostics();
        assertWithMessage(String.format("Expected no diagnostics produced, but found %d: %s", diagnostics.size(), diagnostics)).that(diagnostics.size()).isEqualTo(0);
    } else {
        for (JavaFileObject source : sources) {
            try {
                diagnosticHelper.assertHasDiagnosticOnAllMatchingLines(source, lookForCheckNameInDiagnostic);
            } catch (IOException e) {
                throw new IOError(e);
            }
        }
        assertTrue("Unused error keys: " + diagnosticHelper.getUnusedLookupKeys(), diagnosticHelper.getUnusedLookupKeys().isEmpty());
    }
    if (expectedResult.isPresent()) {
        assertWithMessage(String.format("Expected compilation result %s, but was %s", expectedResult.get(), result)).that(result).isEqualTo(expectedResult.get());
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) IOError(java.io.IOError) LookForCheckNameInDiagnostic(com.google.errorprone.DiagnosticTestHelper.LookForCheckNameInDiagnostic) Diagnostic(javax.tools.Diagnostic) IOException(java.io.IOException) Result(com.sun.tools.javac.main.Main.Result)

Aggregations

LookForCheckNameInDiagnostic (com.google.errorprone.DiagnosticTestHelper.LookForCheckNameInDiagnostic)1 Result (com.sun.tools.javac.main.Main.Result)1 IOError (java.io.IOError)1 IOException (java.io.IOException)1 Diagnostic (javax.tools.Diagnostic)1 JavaFileObject (javax.tools.JavaFileObject)1