Class DiagnosticTestHelper

java.lang.Object
com.google.errorprone.DiagnosticTestHelper

public class DiagnosticTestHelper extends Object
Utility class for tests which need to assert on the diagnostics produced during compilation.
Author:
alexeagle@google.com (Alex Eagle)
  • Field Details

    • collector

      public final com.google.errorprone.DiagnosticTestHelper.ClearableDiagnosticCollector<JavaFileObject> collector
    • DIAGNOSTIC_CONTAINING

      public static final com.google.common.truth.Correspondence<Diagnostic<? extends JavaFileObject>, String> DIAGNOSTIC_CONTAINING
  • Constructor Details

    • DiagnosticTestHelper

      public DiagnosticTestHelper()
      Construct a DiagnosticTestHelper not associated with a specific check.
    • DiagnosticTestHelper

      public DiagnosticTestHelper(String checkName)
      Construct a DiagnosticTestHelper for a check with the given name.
  • Method Details

    • getDiagnostics

      public List<Diagnostic<? extends JavaFileObject>> getDiagnostics()
    • clearDiagnostics

      public void clearDiagnostics()
    • describe

      public String describe()
    • diagnosticOnLine

      public static org.hamcrest.Matcher<Diagnostic<? extends JavaFileObject>> diagnosticOnLine(URI fileUri, long line)
    • diagnosticOnLine

      public static org.hamcrest.Matcher<Diagnostic<? extends JavaFileObject>> diagnosticOnLine(URI fileUri, long line, Predicate<? super String> matcher)
    • expectErrorMessage

      public void expectErrorMessage(String key, Predicate<? super String> matcher)
      Expects an error message matching matcher at the line below a comment matching the key. For example, given the source
        // BUG: Diagnostic matches: X
        a = b + c;
      
      ... you can use expectErrorMessage("X", Predicates.containsPattern("Can't add b to c"));

      Error message keys that don't match any diagnostics will cause test to fail.

    • assertHasDiagnosticOnAllMatchingLines

      public void assertHasDiagnosticOnAllMatchingLines(JavaFileObject source, com.google.errorprone.DiagnosticTestHelper.LookForCheckNameInDiagnostic lookForCheckNameInDiagnostic) throws IOException
      Asserts that the diagnostics contain a diagnostic on each line of the source file that matches our bug marker pattern. Parses the bug marker pattern for the specific string to look for in the diagnostic. The failure message names every line that fails, each with the first expectation it fails.
      Parameters:
      source - File in which to find matching lines
      Throws:
      IOException
    • getUnusedLookupKeys

      public Set<String> getUnusedLookupKeys()
      Returns the lookup keys that weren't used.