Analysis API: add test for KtDiagnosticsProvider

This commit is contained in:
Tianyu Geng
2021-11-16 04:26:45 +01:00
committed by TeamCityServer
parent cd3827ca2e
commit f197fc93db
18 changed files with 351 additions and 3 deletions
@@ -0,0 +1,11 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.analysis.api.descriptors.test.components.diagnosticsProvider
import org.jetbrains.kotlin.analysis.api.descriptors.test.KtFe10FrontendApiTestConfiguratorService
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.diagnosticProvider.AbstractCollectDiagnosticsTest
abstract class AbstractKtFe10CollectDiagnosticsTest : AbstractCollectDiagnosticsTest(KtFe10FrontendApiTestConfiguratorService)
@@ -0,0 +1,50 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.analysis.api.descriptors.test.components.diagnosticsProvider;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link GenerateNewCompilerTests.kt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics")
@TestDataPath("$PROJECT_ROOT")
public class KtFe10CollectDiagnosticsTestGenerated extends AbstractKtFe10CollectDiagnosticsTest {
@Test
public void testAllFilesPresentInDiagnostics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@Test
@TestMetadata("declarationErrors.kt")
public void testDeclarationErrors() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/declarationErrors.kt");
}
@Test
@TestMetadata("resolutionErrors.kt")
public void testResolutionErrors() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/resolutionErrors.kt");
}
@Test
@TestMetadata("typeMismatches.kt")
public void testTypeMismatches() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/typeMismatches.kt");
}
@Test
@TestMetadata("unresolved.kt")
public void testUnresolved() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/unresolved.kt");
}
}
@@ -0,0 +1,11 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.analysis.api.fir.components.diagnosticsProvider
import org.jetbrains.kotlin.analysis.api.fir.FirFrontendApiTestConfiguratorService
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.diagnosticProvider.AbstractCollectDiagnosticsTest
abstract class AbstractFirCollectDiagnosticsTest : AbstractCollectDiagnosticsTest(FirFrontendApiTestConfiguratorService)
@@ -0,0 +1,50 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.analysis.api.fir.components.diagnosticsProvider;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link GenerateNewCompilerTests.kt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics")
@TestDataPath("$PROJECT_ROOT")
public class FirCollectDiagnosticsTestGenerated extends AbstractFirCollectDiagnosticsTest {
@Test
public void testAllFilesPresentInDiagnostics() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@Test
@TestMetadata("declarationErrors.kt")
public void testDeclarationErrors() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/declarationErrors.kt");
}
@Test
@TestMetadata("resolutionErrors.kt")
public void testResolutionErrors() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/resolutionErrors.kt");
}
@Test
@TestMetadata("typeMismatches.kt")
public void testTypeMismatches() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/typeMismatches.kt");
}
@Test
@TestMetadata("unresolved.kt")
public void testUnresolved() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/unresolved.kt");
}
}
@@ -0,0 +1,81 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.analysis.api.impl.base.test.components.diagnosticProvider
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.analysis.api.components.KtDiagnosticCheckerFilter
import org.jetbrains.kotlin.analysis.api.diagnostics.KtDiagnosticWithPsi
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.FrontendApiTestConfiguratorService
import org.jetbrains.kotlin.analysis.api.impl.base.test.test.framework.AbstractHLApiSingleFileTest
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils.getLineAndColumnRangeInPsiFile
import org.jetbrains.kotlin.diagnostics.PsiDiagnosticUtils.offsetToLineAndColumn
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtTreeVisitorVoid
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.assertions
import kotlin.test.assertEquals
abstract class AbstractCollectDiagnosticsTest(
configurator: FrontendApiTestConfiguratorService
) : AbstractHLApiSingleFileTest(configurator) {
override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) {
fun TextRange.asLineColumnRange(): String {
return getLineAndColumnRangeInPsiFile(ktFile, this).toString()
}
analyseForTest(ktFile) {
val diagnosticsInFile =
ktFile.collectDiagnosticsForFile(KtDiagnosticCheckerFilter.EXTENDED_AND_COMMON_CHECKERS).map { it.getKey() }.sorted()
val diagnosticsFromElements = buildList {
ktFile.accept(object : KtTreeVisitorVoid() {
override fun visitKtElement(element: KtElement) {
for (diagnostic in element.getDiagnostics(KtDiagnosticCheckerFilter.EXTENDED_AND_COMMON_CHECKERS)) {
add(element to diagnostic.getKey())
}
super.visitKtElement(element)
}
})
}.sortedBy { (_, diagnostic) -> diagnostic }
val actual = buildString {
fun DiagnosticKey.print(indent: Int) {
val indentString = " ".repeat(indent)
append(indentString + factoryName)
appendLine("$indentString text ranges: $textRanges")
appendLine("$indentString PSI: ${psi::class.simpleName} at ${psi.textRange.asLineColumnRange()}")
}
appendLine("Diagnostics from elements:")
for ((element, diagnostic) in diagnosticsFromElements) {
appendLine(" for PSI element of type ${element::class.simpleName} at ${element.textRange.asLineColumnRange()}")
diagnostic.print(4)
}
}
testServices.assertions.assertEqualsToTestDataFileSibling(actual)
assertEquals(
diagnosticsInFile,
diagnosticsFromElements.map { (_, v) -> v },
"diagnostics collected from file should be the same as those collected from individual PSI elements."
)
}
}
private data class DiagnosticKey(val factoryName: String?, val psi: PsiElement, val textRanges: Collection<TextRange>) :
Comparable<DiagnosticKey> {
override fun toString(): String {
val document = psi.containingFile.viewProvider.document
return "$factoryName on ${psi::class.simpleName} at ${offsetToLineAndColumn(document, psi.startOffset)})"
}
override fun compareTo(other: DiagnosticKey): Int = this.toString().compareTo(other.toString())
}
private fun KtDiagnosticWithPsi<*>.getKey() = DiagnosticKey(factoryName, psi, textRanges)
}
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.analysis.api.components
import org.jetbrains.kotlin.analysis.api.diagnostics.KtDiagnostic
import org.jetbrains.kotlin.analysis.api.diagnostics.KtDiagnosticWithPsi
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
@@ -16,7 +15,7 @@ public abstract class KtDiagnosticProvider : KtAnalysisSessionComponent() {
}
public interface KtDiagnosticProviderMixIn : KtAnalysisSessionMixIn {
public fun KtElement.getDiagnostics(filter: KtDiagnosticCheckerFilter): Collection<KtDiagnostic> =
public fun KtElement.getDiagnostics(filter: KtDiagnosticCheckerFilter): Collection<KtDiagnosticWithPsi<*>> =
analysisSession.diagnosticProvider.getDiagnosticsForElement(this, filter)
public fun KtFile.collectDiagnosticsForFile(filter: KtDiagnosticCheckerFilter): Collection<KtDiagnosticWithPsi<*>> =
@@ -0,0 +1,19 @@
Diagnostics from elements:
for PSI element of type KtClass at (6,1-11)
ABSTRACT_CLASS_MEMBER_NOT_IMPLEMENTED text ranges: [(62,69)]
PSI: KtClass at (6,1-11)
for PSI element of type KtNamedFunction at (4,3-21)
ABSTRACT_FUNCTION_IN_NON_ABSTRACT_CLASS text ranges: [(41,49)]
PSI: KtNamedFunction at (4,3-21)
for PSI element of type KtTypeReference at (6,10-11)
FINAL_SUPERTYPE text ranges: [(71,72)]
PSI: KtTypeReference at (6,10-11)
for PSI element of type KtProperty at (1,1-11)
MUST_BE_INITIALIZED text ranges: [(0,10)]
PSI: KtProperty at (1,1-11)
for PSI element of type KtNamedFunction at (2,1-18)
NON_MEMBER_FUNCTION_NO_BODY text ranges: [(11,28)]
PSI: KtNamedFunction at (2,1-18)
for PSI element of type KtSuperTypeEntry at (6,10-11)
SUPERTYPE_NOT_INITIALIZED text ranges: [(71,72)]
PSI: KtSuperTypeEntry at (6,10-11)
@@ -0,0 +1,6 @@
val i: Int
fun foo(): String
class A {
abstract fun foo()
}
class B: A
@@ -0,0 +1,19 @@
Diagnostics from elements:
for PSI element of type KtClass at (6,1-11)
ABSTRACT_CLASS_MEMBER_NOT_IMPLEMENTED text ranges: [(62,69)]
PSI: KtClass at (6,1-11)
for PSI element of type KtNamedFunction at (4,3-21)
ABSTRACT_FUNCTION_IN_NON_ABSTRACT_CLASS text ranges: [(41,49)]
PSI: KtNamedFunction at (4,3-21)
for PSI element of type KtTypeReference at (6,10-11)
FINAL_SUPERTYPE text ranges: [(71,72)]
PSI: KtTypeReference at (6,10-11)
for PSI element of type KtProperty at (1,1-11)
MUST_BE_INITIALIZED text ranges: [(0,10)]
PSI: KtProperty at (1,1-11)
for PSI element of type KtNamedFunction at (2,1-18)
NON_MEMBER_FUNCTION_NO_BODY text ranges: [(11,28)]
PSI: KtNamedFunction at (2,1-18)
for PSI element of type KtTypeReference at (6,10-11)
SUPERTYPE_NOT_INITIALIZED text ranges: [(71,72)]
PSI: KtTypeReference at (6,10-11)
@@ -0,0 +1,19 @@
Diagnostics from elements:
for PSI element of type KtNameReferenceExpression at (6,5-8)
NONE_APPLICABLE text ranges: [(89,92)]
PSI: KtNameReferenceExpression at (6,5-8)
for PSI element of type KtNameReferenceExpression at (7,5-8)
NONE_APPLICABLE text ranges: [(100,103)]
PSI: KtNameReferenceExpression at (7,5-8)
for PSI element of type KtNameReferenceExpression at (8,5-8)
NONE_APPLICABLE text ranges: [(115,118)]
PSI: KtNameReferenceExpression at (8,5-8)
for PSI element of type KtConstantExpression at (5,9-10)
TOO_MANY_ARGUMENTS text ranges: [(82,83)]
PSI: KtConstantExpression at (5,9-10)
for PSI element of type KtParameter at (2,9-18)
UNUSED_PARAMETER text ranges: [(21,22)]
PSI: KtParameter at (2,9-18)
for PSI element of type KtParameter at (3,9-22)
UNUSED_PARAMETER text ranges: [(43,44)]
PSI: KtParameter at (3,9-22)
@@ -0,0 +1,9 @@
fun foo() {}
fun bar(s: String) {}
fun bar(f: () -> Unit) {}
fun test() {
foo(1)
bar(2)
bar("", 1)
bar()
}
@@ -0,0 +1,13 @@
Diagnostics from elements:
for PSI element of type KtNameReferenceExpression at (6,5-8)
NONE_APPLICABLE text ranges: [(89,92)]
PSI: KtNameReferenceExpression at (6,5-8)
for PSI element of type KtNameReferenceExpression at (7,5-8)
NONE_APPLICABLE text ranges: [(100,103)]
PSI: KtNameReferenceExpression at (7,5-8)
for PSI element of type KtNameReferenceExpression at (8,5-8)
NONE_APPLICABLE text ranges: [(115,118)]
PSI: KtNameReferenceExpression at (8,5-8)
for PSI element of type KtConstantExpression at (5,9-10)
TOO_MANY_ARGUMENTS text ranges: [(82,83)]
PSI: KtConstantExpression at (5,9-10)
@@ -0,0 +1,13 @@
Diagnostics from elements:
for PSI element of type KtConstantExpression at (5,12-13)
CONSTANT_EXPECTED_TYPE_MISMATCH text ranges: [(68,69)]
PSI: KtConstantExpression at (5,12-13)
for PSI element of type KtNameReferenceExpression at (4,9-10)
TYPE_MISMATCH text ranges: [(54,55)]
PSI: KtNameReferenceExpression at (4,9-10)
for PSI element of type KtStringTemplateExpression at (1,14-16)
TYPE_MISMATCH text ranges: [(13,15)]
PSI: KtStringTemplateExpression at (1,14-16)
for PSI element of type KtParameter at (3,9-18)
UNUSED_PARAMETER text ranges: [(25,26)]
PSI: KtParameter at (3,9-18)
@@ -0,0 +1,6 @@
val i: Int = ""
fun foo(s: String): String {
foo(i)
return 1
}
@@ -0,0 +1,10 @@
Diagnostics from elements:
for PSI element of type KtNameReferenceExpression at (4,9-10)
ARGUMENT_TYPE_MISMATCH text ranges: [(54,55)]
PSI: KtNameReferenceExpression at (4,9-10)
for PSI element of type KtProperty at (1,1-16)
INITIALIZER_TYPE_MISMATCH text ranges: [(13,15)]
PSI: KtProperty at (1,1-16)
for PSI element of type KtConstantExpression at (5,12-13)
RETURN_TYPE_MISMATCH text ranges: [(68,69)]
PSI: KtConstantExpression at (5,12-13)
@@ -0,0 +1,6 @@
fun test() {
foo(1)
bar(abc)
a.b.c.d
"" - ""
}
@@ -0,0 +1,16 @@
Diagnostics from elements:
for PSI element of type KtNameReferenceExpression at (2,5-8)
UNRESOLVED_REFERENCE text ranges: [(17,20)]
PSI: KtNameReferenceExpression at (2,5-8)
for PSI element of type KtNameReferenceExpression at (3,5-8)
UNRESOLVED_REFERENCE text ranges: [(28,31)]
PSI: KtNameReferenceExpression at (3,5-8)
for PSI element of type KtNameReferenceExpression at (3,9-12)
UNRESOLVED_REFERENCE text ranges: [(32,35)]
PSI: KtNameReferenceExpression at (3,9-12)
for PSI element of type KtNameReferenceExpression at (4,5-6)
UNRESOLVED_REFERENCE text ranges: [(41,42)]
PSI: KtNameReferenceExpression at (4,5-6)
for PSI element of type KtOperationReferenceExpression at (5,8-9)
UNRESOLVED_REFERENCE text ranges: [(56,57)]
PSI: KtOperationReferenceExpression at (5,8-9)
@@ -9,8 +9,10 @@ import org.jetbrains.kotlin.analysis.api.descriptors.test.annotations.AbstractAn
import org.jetbrains.kotlin.analysis.api.descriptors.test.annotations.AbstractAnalysisApiFe10AnnotationsOnTypesTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.callResolver.AbstractKtFe10ResolveCallTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.compileTimeConstantProvider.AbstractKtFe10CompileTimeConstantEvaluatorTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.diagnosticsProvider.AbstractKtFe10CollectDiagnosticsTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.expressionInfoProvider.AbstractKtFe10ReturnTargetSymbolTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.expressionInfoProvider.AbstractKtFe10WhenMissingCasesTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.expressionTypeProvider.AbstractKtFe10DeclarationReturnTypeTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.expressionTypeProvider.AbstractKtFe10ExpectedExpressionTypeTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.expressionTypeProvider.AbstractKtFe10HLExpressionTypeTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.smartCastProvider.AbstractKtFe10HLSmartCastInfoTest
@@ -18,7 +20,6 @@ import org.jetbrains.kotlin.analysis.api.descriptors.test.components.symbolDecla
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.symbolDeclarationRenderer.AbstractKtFe10RendererTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.typeCreator.AbstractKtFe10TypeParameterTypeTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.typeProvider.AbstractKtFe10HasCommonSubtypeTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.expressionTypeProvider.AbstractKtFe10DeclarationReturnTypeTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.scopes.AbstractKtFe10SubstitutionOverridesUnwrappingTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.symbols.AbstractKtFe10SymbolByFqNameTest
import org.jetbrains.kotlin.analysis.api.descriptors.test.symbols.AbstractKtFe10SymbolByPsiTest
@@ -29,6 +30,7 @@ import org.jetbrains.kotlin.analysis.api.fir.annotations.AbstractAnalysisApiFirA
import org.jetbrains.kotlin.analysis.api.fir.annotations.AbstractAnalysisApiFirAnnotationsOnTypesTest
import org.jetbrains.kotlin.analysis.api.fir.components.callResolver.AbstractFirResolveCallTest
import org.jetbrains.kotlin.analysis.api.fir.components.compileTimeConstantProvider.AbstractFirCompileTimeConstantEvaluatorTest
import org.jetbrains.kotlin.analysis.api.fir.components.diagnosticsProvider.AbstractFirCollectDiagnosticsTest
import org.jetbrains.kotlin.analysis.api.fir.components.expressionInfoProvider.AbstractFirReturnTargetSymbolTest
import org.jetbrains.kotlin.analysis.api.fir.components.expressionInfoProvider.AbstractFirWhenMissingCasesTest
import org.jetbrains.kotlin.analysis.api.fir.components.expressionTypeProvider.AbstractFirDeclarationReturnTypeTest
@@ -197,6 +199,14 @@ private fun TestGroupSuite.generateAnalysisApiComponentsTests() {
}
}
component("diagnosticsProvider") {
test(
fir = AbstractFirCollectDiagnosticsTest::class, fe10 = AbstractKtFe10CollectDiagnosticsTest::class
) {
model("diagnostics")
}
}
component("importOptimizer") {
test(
fir = AbstractHLImportOptimizerTest::class,