assertDiagnosticsIn method

void assertDiagnosticsIn(
  1. List<Diagnostic> diagnostics,
  2. List<ExpectedDiagnostic> expectedDiagnostics
)
inherited

Asserts that the diagnostics in diagnostics match expectedDiagnostics.

Note: Be sure to await any use of this API, to avoid stale analysis results (See DisposedAnalysisContextResult).

Implementation

void assertDiagnosticsIn(
  List<Diagnostic> diagnostics,
  List<ExpectedDiagnostic> expectedDiagnostics,
) {
  // Match actual diagnostics to expected diagnostics.
  var unmatchedActual = diagnostics.toList();
  var unmatchedExpected = expectedDiagnostics.toList();
  var actualIndex = 0;
  while (actualIndex < unmatchedActual.length) {
    var matchFound = false;
    var expectedIndex = 0;
    while (expectedIndex < unmatchedExpected.length) {
      if (unmatchedExpected[expectedIndex].matches(
        unmatchedActual[actualIndex],
      )) {
        matchFound = true;
        unmatchedActual.removeAt(actualIndex);
        unmatchedExpected.removeAt(expectedIndex);
        break;
      }
      expectedIndex++;
    }
    if (!matchFound) {
      actualIndex++;
    }
  }

  // Print the results to the terminal.
  var buffer = StringBuffer();
  if (unmatchedExpected.isNotEmpty) {
    buffer.write(missingExpectedMessage(unmatchedExpected));
  }
  if (unmatchedActual.isNotEmpty) {
    buffer.write(unexpectedMessage(unmatchedActual));
  }
  if (unmatchedExpected.isNotEmpty || unmatchedActual.isNotEmpty) {
    buffer.write(correctionMessage(diagnostics));

    if (dumpAstOnFailures) {
      buffer.writeln();
      buffer.writeln();

      try {
        Spelunker(
          result.unit.toSource(),
          sink: buffer,
          featureSet: result.unit.featureSet,
        ).spelunk();
      } on ArgumentError catch (_) {
        // Perhaps we encountered a parsing error while spelunking.
      }

      buffer.writeln();
    }

    fail(buffer.toString());
  }
}