Refactor CheckerTestUtil

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