From 7daf12fa6d1f88fb6db66359cec316b8782dc949 Mon Sep 17 00:00:00 2001 From: Dmitry Savvinov Date: Tue, 23 Apr 2019 20:30:03 +0300 Subject: [PATCH] [Testing] Remove duplicate logic, clean-up CheckerTestUtil --- .../checkers/diagnostics/TextDiagnostic.kt | 9 ++-- .../kotlin/checkers/utils/CheckerTestUtil.kt | 41 ++++++------------- .../checkers/utils/diagnosticRangesUtils.kt | 16 ++++++++ .../kotlin/checkers/BaseDiagnosticsTest.kt | 2 +- .../diagnostics/notLinked/dfa/pos/3.kt | 4 +- .../diagnostics/notLinked/dfa/pos/4.kt | 2 +- 6 files changed, 37 insertions(+), 37 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/kotlin/checkers/utils/diagnosticRangesUtils.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/checkers/diagnostics/TextDiagnostic.kt b/compiler/frontend/src/org/jetbrains/kotlin/checkers/diagnostics/TextDiagnostic.kt index efc62cba4db..bc03781848c 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/checkers/diagnostics/TextDiagnostic.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/checkers/diagnostics/TextDiagnostic.kt @@ -67,9 +67,9 @@ class TextDiagnostic( return result } - fun asString(): String { + fun asString(withNewInference: Boolean = true, renderParameters: Boolean = true): String { val result = StringBuilder() - if (inferenceCompatibility.abbreviation != null) { + if (withNewInference && inferenceCompatibility.abbreviation != null) { result.append(inferenceCompatibility.abbreviation) result.append(";") } @@ -78,9 +78,10 @@ class TextDiagnostic( result.append(":") } result.append(name) - if (parameters != null) { + + if (renderParameters && parameters != null) { result.append("(") - result.append(StringUtil.join(parameters, { "\"$it\"" }, ", ")) + result.append(StringUtil.join(parameters, { "\"${it.replace('\n', ' ')}\"" }, ", ")) result.append(")") } return result.toString() diff --git a/compiler/frontend/src/org/jetbrains/kotlin/checkers/utils/CheckerTestUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/checkers/utils/CheckerTestUtil.kt index 82fb570c7aa..027462ded08 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/checkers/utils/CheckerTestUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/checkers/utils/CheckerTestUtil.kt @@ -44,7 +44,7 @@ object CheckerTestUtil { private const val IGNORE_DIAGNOSTIC_PARAMETER = "IGNORE" private const val INDIVIDUAL_DIAGNOSTIC = """(\w+;)?(\w+:)?(\w+)(?:\(((?:".*?")(?:,\s*".*?")*)\))?""" - private val rangeStartOrEndPattern = Pattern.compile("()|()") + internal val rangeStartOrEndPattern = Pattern.compile("()|()") val individualDiagnosticPattern: Pattern = Pattern.compile(INDIVIDUAL_DIAGNOSTIC) fun getDiagnosticsIncludingSyntaxErrors( @@ -328,6 +328,7 @@ object CheckerTestUtil { return false if (expected.parameters == null) return true + if (actual.parameters == null || expected.parameters.size != actual.parameters.size) return false @@ -420,7 +421,7 @@ object CheckerTestUtil { psiFile, diagnostics, emptyMap(), - com.intellij.util.Function { it.text }, + { it.text }, emptyList(), false, false @@ -430,12 +431,12 @@ object CheckerTestUtil { psiFile: PsiFile, diagnostics: Collection, diagnosticToExpectedDiagnostic: Map, - getFileText: com.intellij.util.Function, + getFileText: (PsiFile) -> String, uncheckedDiagnostics: Collection, withNewInferenceDirective: Boolean, renderDiagnosticMessages: Boolean ): StringBuffer { - val text = getFileText.`fun`(psiFile) + val text = getFileText(psiFile) val result = StringBuffer() val diagnosticsFiltered = diagnostics.filter { actualDiagnostic -> psiFile == actualDiagnostic.file } if (diagnosticsFiltered.isEmpty() && uncheckedDiagnostics.isEmpty()) { @@ -514,33 +515,15 @@ object CheckerTestUtil { for (diagnostic in diagnostics) { val expectedDiagnostic = diagnosticToExpectedDiagnostic[diagnostic] - if (expectedDiagnostic != null) { - val actualTextDiagnostic = TextDiagnostic.asTextDiagnostic(diagnostic) + val actualTextDiagnostic = TextDiagnostic.asTextDiagnostic(diagnostic) + + if (expectedDiagnostic != null || !hasExplicitDefinitionOnlyOption(diagnostic)) { + val shouldRenderParameters = + renderDiagnosticMessages || expectedDiagnostic?.parameters != null + diagnosticsAsText.add( - if (compareTextDiagnostic(expectedDiagnostic, actualTextDiagnostic)) - expectedDiagnostic.asString() else actualTextDiagnostic.asString() + actualTextDiagnostic.asString(withNewInferenceDirective, shouldRenderParameters) ) - } else if (!hasExplicitDefinitionOnlyOption(diagnostic)) { - val diagnosticText = StringBuilder() - if (withNewInferenceDirective && diagnostic.inferenceCompatibility.abbreviation != null) { - diagnosticText.append(diagnostic.inferenceCompatibility.abbreviation) - diagnosticText.append(";") - } - if (diagnostic.platform != null) { - diagnosticText.append(diagnostic.platform) - diagnosticText.append(":") - } - diagnosticText.append(diagnostic.name) - if (renderDiagnosticMessages) { - val textDiagnostic = TextDiagnostic.asTextDiagnostic(diagnostic) - if (textDiagnostic.parameters != null) { - diagnosticText - .append("(") - .append(textDiagnostic.parameters.joinToString(", ")) - .append(")") - } - } - diagnosticsAsText.add(diagnosticText.toString()) } } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/checkers/utils/diagnosticRangesUtils.kt b/compiler/frontend/src/org/jetbrains/kotlin/checkers/utils/diagnosticRangesUtils.kt new file mode 100644 index 00000000000..46104e185d7 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/checkers/utils/diagnosticRangesUtils.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.checkers.utils + +import java.io.File + +fun clearFileFromDiagnosticMarkup(file: File) { + val text = file.readText() + val cleanText = clearTextFromDiagnosticMarkup(text) + file.writeText(cleanText) +} + +fun clearTextFromDiagnosticMarkup(text: String): String = CheckerTestUtil.rangeStartOrEndPattern.matcher(text).replaceAll("") \ No newline at end of file diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.kt index 38031c01f1a..67bd33d775e 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.kt @@ -330,7 +330,7 @@ abstract class BaseDiagnosticsTest : KotlinMultiFileTestWithJava file.text }, + { file -> file.text }, uncheckedDiagnostics, withNewInferenceDirective, renderDiagnosticMessages diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/3.kt b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/3.kt index 0be8a3e5dad..cfc22405015 100644 --- a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/3.kt +++ b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/3.kt @@ -226,8 +226,8 @@ fun case_19(b: Boolean) { } } else null - if (a != null && a.B19 != null && a.B19.C19 != null && a.B19.C19.D19 != null && a.B19.C19.D19.x == null) { - a.B19.C19.D19.x + if (a != null && a.B19 != null && a.B19.C19 != null && a.B19.C19.D19 != null && a.B19.C19.D19.x == null) { + a.B19.C19.D19.x } } diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/4.kt b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/4.kt index 3f8acb3438b..2ad2be14cdc 100644 --- a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/4.kt +++ b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/4.kt @@ -128,7 +128,7 @@ fun case_12(x: TypealiasString, y: TypealiasString) = if (x != null) { 1 - } else x + } else x // TESTCASE NUMBER: 14 class A14 {