From 6cfc42666c3303acea5a9b444713f8ce4173f2f8 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 20 Dec 2016 15:32:52 +0300 Subject: [PATCH] Refactor CheckerTestUtil Simplify code somewhat, prepare for a larger refactoring --- .../kotlin/checkers/CheckerTestUtil.java | 70 +++++++++++-------- .../kotlin/checkers/BaseDiagnosticsTest.kt | 3 +- .../kotlin/checkers/CheckerTestUtilTest.java | 5 +- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/checkers/CheckerTestUtil.java b/compiler/frontend/src/org/jetbrains/kotlin/checkers/CheckerTestUtil.java index c27a62f59dc..89fccd0f117 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/checkers/CheckerTestUtil.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/checkers/CheckerTestUtil.java @@ -30,6 +30,8 @@ import com.intellij.util.Function; import com.intellij.util.SmartList; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.Stack; +import kotlin.collections.CollectionsKt; +import kotlin.jvm.functions.Function1; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; @@ -187,12 +189,13 @@ public class CheckerTestUtil { void unexpectedDiagnostic(TextDiagnostic diagnostic, int actualStart, int actualEnd); } - public static void diagnosticsDiff( - Map diagnosticToExpectedDiagnostic, + public static Map diagnosticsDiff( List expected, Collection actual, DiagnosticDiffCallbacks callbacks ) { + Map diagnosticToExpectedDiagnostic = new HashMap(); + assertSameFile(actual); Iterator expectedDiagnostics = expected.iterator(); @@ -217,7 +220,7 @@ public class CheckerTestUtil { currentExpected = safeAdvance(expectedDiagnostics); } else if (expectedStart > actualStart) { - unexpectedDiagnostics(currentActual.getDiagnostics(), callbacks); + unexpectedDiagnostics(currentActual, callbacks); currentActual = safeAdvance(actualDiagnostics); } else if (expectedEnd > actualEnd) { @@ -227,7 +230,7 @@ public class CheckerTestUtil { } else if (expectedEnd < actualEnd) { assert expectedStart == actualStart; - unexpectedDiagnostics(currentActual.getDiagnostics(), callbacks); + unexpectedDiagnostics(currentActual, callbacks); currentActual = safeAdvance(actualDiagnostics); } else { @@ -241,10 +244,12 @@ public class CheckerTestUtil { //noinspection ConstantConditions assert (currentActual != null); - unexpectedDiagnostics(currentActual.getDiagnostics(), callbacks); + unexpectedDiagnostics(currentActual, callbacks); currentActual = safeAdvance(actualDiagnostics); } } + + return diagnosticToExpectedDiagnostic; } private static void compareDiagnostics( @@ -263,22 +268,30 @@ public class CheckerTestUtil { Map actualDiagnostics = currentActual.getTextDiagnosticsMap(); List 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); + for (final TextDiagnostic expectedDiagnostic : expectedDiagnostics) { + Map.Entry actualDiagnosticEntry = CollectionsKt.firstOrNull( + actualDiagnostics.entrySet(), new Function1, Boolean>() { + @Override + public Boolean invoke(Map.Entry entry) { + return expectedDiagnostic.getName().equals(entry.getValue().getName()); + } } + ); - actualDiagnostics.remove(actualDiagnostic); - diagnosticToInput.put(actualDiagnostic, expectedDiagnostic); - diagnosticFound = true; - break; + if (actualDiagnosticEntry != null) { + Diagnostic actualDiagnostic = actualDiagnosticEntry.getKey(); + TextDiagnostic actualTextDiagnostic = actualDiagnosticEntry.getValue(); + + if (!compareTextDiagnostic(expectedDiagnostic, actualTextDiagnostic)) { + callbacks.wrongParametersDiagnostic(expectedDiagnostic, actualTextDiagnostic, expectedStart, expectedEnd); } + + actualDiagnostics.remove(actualDiagnostic); + diagnosticToInput.put(actualDiagnostic, expectedDiagnostic); + } + else { + callbacks.missingDiagnostic(expectedDiagnostic, expectedStart, expectedEnd); } - if (!diagnosticFound) callbacks.missingDiagnostic(expectedDiagnostic, expectedStart, expectedEnd); } for (TextDiagnostic unexpectedDiagnostic : actualDiagnostics.values()) { @@ -311,13 +324,9 @@ public class CheckerTestUtil { } } - private static void unexpectedDiagnostics(List actual, DiagnosticDiffCallbacks callbacks) { - for (Diagnostic diagnostic : actual) { - List textRanges = diagnostic.getTextRanges(); - for (TextRange textRange : textRanges) { - callbacks.unexpectedDiagnostic(TextDiagnostic.asTextDiagnostic(diagnostic), textRange.getStartOffset(), - textRange.getEndOffset()); - } + private static void unexpectedDiagnostics(DiagnosticDescriptor descriptor, DiagnosticDiffCallbacks callbacks) { + for (Diagnostic diagnostic : descriptor.diagnostics) { + callbacks.unexpectedDiagnostic(TextDiagnostic.asTextDiagnostic(diagnostic), descriptor.start, descriptor.end); } } @@ -445,8 +454,8 @@ public class CheckerTestUtil { result.append(" iterator = currentDescriptor.diagnostics.iterator(); iterator.hasNext(); ) { Diagnostic diagnostic = iterator.next(); - if (diagnosticToExpectedDiagnostic.containsKey(diagnostic)) { - TextDiagnostic expectedDiagnostic = diagnosticToExpectedDiagnostic.get(diagnostic); + TextDiagnostic expectedDiagnostic = diagnosticToExpectedDiagnostic.get(diagnostic); + if (expectedDiagnostic != null) { TextDiagnostic actualTextDiagnostic = TextDiagnostic.asTextDiagnostic(diagnostic); if (compareTextDiagnostic(expectedDiagnostic, actualTextDiagnostic)) { result.append(expectedDiagnostic.asString()); @@ -620,10 +629,6 @@ public class CheckerTestUtil { return end; } - public List getDiagnostics() { - return diagnostics; - } - public TextRange getTextRange() { return new TextRange(start, end); } @@ -726,6 +731,11 @@ public class CheckerTestUtil { } }, "; ") + ')'; } + + @Override + public String toString() { + return asString(); + } } public static class DiagnosedRange { diff --git a/compiler/tests-common/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.kt b/compiler/tests-common/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.kt index 51919a48620..38049100090 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.kt +++ b/compiler/tests-common/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.kt @@ -213,8 +213,7 @@ abstract class BaseDiagnosticsTest : KotlinMultiFileTestWithJava() - CheckerTestUtil.diagnosticsDiff(diagnosticToExpectedDiagnostic, diagnosedRanges, diagnostics, object : CheckerTestUtil.DiagnosticDiffCallbacks { + val diagnosticToExpectedDiagnostic = CheckerTestUtil.diagnosticsDiff(diagnosedRanges, diagnostics, object : CheckerTestUtil.DiagnosticDiffCallbacks { override fun missingDiagnostic(diagnostic: CheckerTestUtil.TextDiagnostic, expectedStart: Int, expectedEnd: Int) { val message = "Missing " + diagnostic.name + DiagnosticUtils.atLocation(ktFile, TextRange(expectedStart, expectedEnd)) System.err.println(message) diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/CheckerTestUtilTest.java b/compiler/tests/org/jetbrains/kotlin/checkers/CheckerTestUtilTest.java index 15884085d40..6cd03b4c637 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/CheckerTestUtilTest.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/CheckerTestUtilTest.java @@ -19,7 +19,6 @@ package org.jetbrains.kotlin.checkers; import com.google.common.collect.Lists; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiFile; -import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.checkers.CheckerTestUtil.DiagnosedRange; import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment; @@ -162,9 +161,7 @@ public class CheckerTestUtilTest extends KotlinTestWithEnvironment { List expectedMessages = Lists.newArrayList(expected); final List actualMessages = Lists.newArrayList(); - CheckerTestUtil.diagnosticsDiff(ContainerUtil.newHashMap(), - diagnosedRanges, diagnostics, new CheckerTestUtil.DiagnosticDiffCallbacks() { - + CheckerTestUtil.diagnosticsDiff(diagnosedRanges, diagnostics, new CheckerTestUtil.DiagnosticDiffCallbacks() { @Override public void missingDiagnostic(CheckerTestUtil.TextDiagnostic diagnostic, int expectedStart, int expectedEnd) { actualMessages.add(missing(diagnostic.getName(), expectedStart, expectedEnd));