[FIR] Verify diagnostic messages don't reference non-existent parameters
This commit is contained in:
committed by
Space Team
parent
c956b4aeae
commit
a818c543a9
+26
-12
@@ -5,29 +5,43 @@
|
||||
|
||||
package org.jetbrains.kotlin.test.utils
|
||||
|
||||
import org.jetbrains.kotlin.diagnostics.AbstractKtDiagnosticFactory
|
||||
import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactoryForDeprecation
|
||||
import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactoryToRendererMap
|
||||
import org.jetbrains.kotlin.diagnostics.*
|
||||
import org.junit.Assert
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.full.memberProperties
|
||||
|
||||
fun KtDiagnosticFactoryToRendererMap.checkMissingMessages(objectWithErrors: Any) {
|
||||
for (property in objectWithErrors.javaClass.kotlin.memberProperties) {
|
||||
fun KtDiagnosticFactoryToRendererMap.verifyMessages(objectWithErrors: Any) {
|
||||
for (property in objectWithErrors::class.memberProperties) {
|
||||
when (val factory = property.getter.call(objectWithErrors)) {
|
||||
is AbstractKtDiagnosticFactory -> {
|
||||
checkMissingMessagesForFactory(factory, property)
|
||||
verifyMessageForFactory(factory, property)
|
||||
}
|
||||
is KtDiagnosticFactoryForDeprecation<*> -> {
|
||||
checkMissingMessagesForFactory(factory.warningFactory, property)
|
||||
checkMissingMessagesForFactory(factory.errorFactory, property)
|
||||
verifyMessageForFactory(factory.warningFactory, property)
|
||||
verifyMessageForFactory(factory.errorFactory, property)
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun KtDiagnosticFactoryToRendererMap.checkMissingMessagesForFactory(factory: AbstractKtDiagnosticFactory, property: KProperty<*>) {
|
||||
if (!containsKey(factory)) {
|
||||
throw IllegalStateException("No default diagnostic renderer is provided for ${property.name}")
|
||||
private val messageParameterRegex = """\{\d.*?}""".toRegex()
|
||||
|
||||
fun KtDiagnosticFactoryToRendererMap.verifyMessageForFactory(factory: AbstractKtDiagnosticFactory, property: KProperty<*>) {
|
||||
Assert.assertTrue("No default diagnostic renderer is provided for ${property.name}", containsKey(factory))
|
||||
|
||||
val renderer = get(factory)!!
|
||||
|
||||
val parameterCount = when (renderer) {
|
||||
is KtDiagnosticWithParameters4Renderer<*, *, *, *> -> 4
|
||||
is KtDiagnosticWithParameters3Renderer<*, *, *> -> 3
|
||||
is KtDiagnosticWithParameters2Renderer<*, *> -> 2
|
||||
is KtDiagnosticWithParameters1Renderer<*> -> 1
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
|
||||
for (parameter in messageParameterRegex.findAll(renderer.message)) {
|
||||
val index = parameter.value.substring(1, 2).toInt()
|
||||
Assert.assertTrue("Message for ${property.name} references wrong parameter {$index}", index < parameterCount)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user