[Testing] Remove duplicate logic, clean-up CheckerTestUtil
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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("(<!$INDIVIDUAL_DIAGNOSTIC(,\\s*$INDIVIDUAL_DIAGNOSTIC)*!>)|(<!>)")
|
||||
internal val rangeStartOrEndPattern = Pattern.compile("(<!$INDIVIDUAL_DIAGNOSTIC(,\\s*$INDIVIDUAL_DIAGNOSTIC)*!>)|(<!>)")
|
||||
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<ActualDiagnostic>,
|
||||
diagnosticToExpectedDiagnostic: Map<AbstractTestDiagnostic, TextDiagnostic>,
|
||||
getFileText: com.intellij.util.Function<PsiFile, String>,
|
||||
getFileText: (PsiFile) -> String,
|
||||
uncheckedDiagnostics: Collection<PositionalTextDiagnostic>,
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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("")
|
||||
Reference in New Issue
Block a user