Beispiel #1
0
  public static String parseDiagnosedRanges(String text, List<DiagnosedRange> result) {
    Matcher matcher = RANGE_START_OR_END_PATTERN.matcher(text);

    Stack<DiagnosedRange> opened = new Stack<DiagnosedRange>();

    int offsetCompensation = 0;

    while (matcher.find()) {
      int effectiveOffset = matcher.start() - offsetCompensation;
      String matchedText = matcher.group();
      if ("<!>".equals(matchedText)) {
        opened.pop().setEnd(effectiveOffset);
      } else {
        Matcher diagnosticTypeMatcher = INDIVIDUAL_DIAGNOSTIC_PATTERN.matcher(matchedText);
        DiagnosedRange range = new DiagnosedRange(effectiveOffset);
        while (diagnosticTypeMatcher.find()) {
          range.addDiagnostic(diagnosticTypeMatcher.group());
        }
        opened.push(range);
        result.add(range);
      }
      offsetCompensation += matchedText.length();
    }

    assert opened.isEmpty() : "Stack is not empty";

    matcher.reset();
    return matcher.replaceAll("");
  }
Beispiel #2
0
  public static void diagnosticsDiff(
      Map<Diagnostic, TextDiagnostic> diagnosticToExpectedDiagnostic,
      List<DiagnosedRange> expected,
      Collection<Diagnostic> actual,
      DiagnosticDiffCallbacks callbacks) {
    assertSameFile(actual);

    Iterator<DiagnosedRange> expectedDiagnostics = expected.iterator();
    List<DiagnosticDescriptor> sortedDiagnosticDescriptors = getSortedDiagnosticDescriptors(actual);
    Iterator<DiagnosticDescriptor> actualDiagnostics = sortedDiagnosticDescriptors.iterator();

    DiagnosedRange currentExpected = safeAdvance(expectedDiagnostics);
    DiagnosticDescriptor currentActual = safeAdvance(actualDiagnostics);
    while (currentExpected != null || currentActual != null) {
      if (currentExpected != null) {
        if (currentActual == null) {
          missingDiagnostics(callbacks, currentExpected);
          currentExpected = safeAdvance(expectedDiagnostics);
        } else {
          int expectedStart = currentExpected.getStart();
          int actualStart = currentActual.getStart();
          int expectedEnd = currentExpected.getEnd();
          int actualEnd = currentActual.getEnd();
          if (expectedStart < actualStart) {
            missingDiagnostics(callbacks, currentExpected);
            currentExpected = safeAdvance(expectedDiagnostics);
          } else if (expectedStart > actualStart) {
            unexpectedDiagnostics(currentActual.getDiagnostics(), callbacks);
            currentActual = safeAdvance(actualDiagnostics);
          } else if (expectedEnd > actualEnd) {
            assert expectedStart == actualStart;
            missingDiagnostics(callbacks, currentExpected);
            currentExpected = safeAdvance(expectedDiagnostics);
          } else if (expectedEnd < actualEnd) {
            assert expectedStart == actualStart;
            unexpectedDiagnostics(currentActual.getDiagnostics(), callbacks);
            currentActual = safeAdvance(actualDiagnostics);
          } else {
            compareDiagnostics(
                callbacks, currentExpected, currentActual, diagnosticToExpectedDiagnostic);
            currentExpected = safeAdvance(expectedDiagnostics);
            currentActual = safeAdvance(actualDiagnostics);
          }
        }
      } else {
        //noinspection ConstantConditions
        assert (currentActual != null);

        unexpectedDiagnostics(currentActual.getDiagnostics(), callbacks);
        currentActual = safeAdvance(actualDiagnostics);
      }
    }
  }
Beispiel #3
0
  private static void compareDiagnostics(
      @NotNull DiagnosticDiffCallbacks callbacks,
      @NotNull DiagnosedRange currentExpected,
      @NotNull DiagnosticDescriptor currentActual,
      @NotNull Map<Diagnostic, TextDiagnostic> diagnosticToInput) {
    int expectedStart = currentExpected.getStart();
    int expectedEnd = currentExpected.getEnd();

    int actualStart = currentActual.getStart();
    int actualEnd = currentActual.getEnd();
    assert expectedStart == actualStart && expectedEnd == actualEnd;

    Map<Diagnostic, TextDiagnostic> actualDiagnostics = currentActual.getTextDiagnosticsMap();
    List<TextDiagnostic> expectedDiagnostics = currentExpected.getDiagnostics();

    for (TextDiagnostic expectedDiagnostic : expectedDiagnostics) {
      boolean diagnosticFound = false;
      for (Diagnostic actualDiagnostic : actualDiagnostics.keySet()) {
        TextDiagnostic actualTextDiagnostic = actualDiagnostics.get(actualDiagnostic);
        if (expectedDiagnostic.getName().equals(actualTextDiagnostic.getName())) {
          if (!compareTextDiagnostic(expectedDiagnostic, actualTextDiagnostic)) {
            callbacks.wrongParametersDiagnostic(
                expectedDiagnostic, actualTextDiagnostic, expectedStart, expectedEnd);
          }

          actualDiagnostics.remove(actualDiagnostic);
          diagnosticToInput.put(actualDiagnostic, expectedDiagnostic);
          diagnosticFound = true;
          break;
        }
      }
      if (!diagnosticFound)
        callbacks.missingDiagnostic(expectedDiagnostic, expectedStart, expectedEnd);
    }

    for (TextDiagnostic unexpectedDiagnostic : actualDiagnostics.values()) {
      callbacks.unexpectedDiagnostic(unexpectedDiagnostic, actualStart, actualEnd);
    }
  }
Beispiel #4
0
 private static void missingDiagnostics(
     DiagnosticDiffCallbacks callbacks, DiagnosedRange currentExpected) {
   for (TextDiagnostic diagnostic : currentExpected.getDiagnostics()) {
     callbacks.missingDiagnostic(diagnostic, currentExpected.getStart(), currentExpected.getEnd());
   }
 }