KTIJ-26423 [AA] Extend KtImportOptimizer testdata with imports usage information

`KtImportOptimizerResult.unusedImports` should be removed soon, and
part of the testdata for the import optimizer service should go
to `intellij` repository
This commit is contained in:
Roman Golyshev
2023-10-24 17:06:26 +02:00
committed by teamcity
parent f90831398c
commit 421793673b
55 changed files with 501 additions and 4 deletions
@@ -14,12 +14,14 @@ import org.jetbrains.kotlin.test.services.assertions
abstract class AbstractAnalysisApiImportOptimizerTest : AbstractAnalysisApiBasedSingleModuleTest(){
override fun doTestByFileStructure(ktFiles: List<KtFile>, module: TestModule, testServices: TestServices) {
val mainKtFile = ktFiles.singleOrNull() ?: ktFiles.first { it.name == "main.kt" }
val unusedImports = analyseForTest(mainKtFile) {
val results = analyseImports(mainKtFile)
@Suppress("DEPRECATION")
results.unusedImports
val importsAnalysis = analyseForTest(mainKtFile) {
analyseImports(mainKtFile)
}
@Suppress("DEPRECATION")
val unusedImports = importsAnalysis.unusedImports
val unusedImportPaths = unusedImports
.map { it.importPath ?: error("Import $it should have an import path, instead was ${it.text}") }
.sortedBy { it.toString() } // for stable results
@@ -28,6 +30,27 @@ abstract class AbstractAnalysisApiImportOptimizerTest : AbstractAnalysisApiBased
unusedImportPaths.forEach(::appendLine)
}
val importAnalysisRendered = buildString {
val sortedUsedDeclarations = importsAnalysis.usedDeclarations
.toSortedMap(compareBy { importPath -> importPath.toString() })
.mapValues { (_, importedNames) -> importedNames.sorted() }
appendLine("USED DECLARATIONS:")
for ((path, elements) in sortedUsedDeclarations) {
appendLine()
appendLine("Declaration: $path")
appendLine("By names: $elements")
}
appendLine()
val sortedUnresolvedNames = importsAnalysis.unresolvedNames.sorted()
appendLine("UNRESOLVED NAMES:")
sortedUnresolvedNames.forEach(::appendLine)
}
testServices.assertions.assertEqualsToTestDataFileSibling(actualUnusedImports, extension = ".imports")
testServices.assertions.assertEqualsToTestDataFileSibling(importAnalysisRendered, extension = ".importsAnalysis")
}
}
@@ -0,0 +1,3 @@
USED DECLARATIONS:
UNRESOLVED NAMES:
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: dependency.function
By names: [function]
UNRESOLVED NAMES:
@@ -0,0 +1,4 @@
USED DECLARATIONS:
UNRESOLVED NAMES:
readText
@@ -0,0 +1,4 @@
USED DECLARATIONS:
UNRESOLVED NAMES:
readText
@@ -0,0 +1,4 @@
USED DECLARATIONS:
UNRESOLVED NAMES:
prop
@@ -0,0 +1,4 @@
USED DECLARATIONS:
UNRESOLVED NAMES:
X
@@ -0,0 +1,7 @@
USED DECLARATIONS:
Declaration: kotlin.collections.List
By names: [List]
UNRESOLVED NAMES:
X
@@ -0,0 +1,5 @@
USED DECLARATIONS:
UNRESOLVED NAMES:
C
X
@@ -0,0 +1,4 @@
USED DECLARATIONS:
UNRESOLVED NAMES:
prp
@@ -0,0 +1,4 @@
USED DECLARATIONS:
UNRESOLVED NAMES:
D
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: dependency.Bar
By names: [Bar]
UNRESOLVED NAMES:
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: dependency.Bar
By names: [Bar]
UNRESOLVED NAMES:
@@ -0,0 +1,12 @@
USED DECLARATIONS:
Declaration: <local>.b
By names: [b]
Declaration: dependency.Bar
By names: [Bar]
Declaration: dependency.fooExt
By names: [fooExt]
UNRESOLVED NAMES:
@@ -0,0 +1,12 @@
USED DECLARATIONS:
Declaration: <local>.b
By names: [b]
Declaration: dependency.fooExtProp
By names: [fooExtProp]
Declaration: kotlin.Any
By names: [Any]
UNRESOLVED NAMES:
@@ -0,0 +1,12 @@
USED DECLARATIONS:
Declaration: <local>.b
By names: [b]
Declaration: dependency.Bar
By names: [Bar]
Declaration: dependency.invoke
By names: [invoke]
UNRESOLVED NAMES:
@@ -0,0 +1,7 @@
USED DECLARATIONS:
Declaration: dependency.Bar
By names: [Bar]
UNRESOLVED NAMES:
WithGeneric
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: dependency.Bar
By names: [Bar]
UNRESOLVED NAMES:
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: dependency.Bar.Companion.minutes
By names: [minutes]
UNRESOLVED NAMES:
@@ -0,0 +1,5 @@
USED DECLARATIONS:
UNRESOLVED NAMES:
SamePackage
samePackage
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: dependency.Bar
By names: [Bar]
UNRESOLVED NAMES:
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: kotlin.with
By names: [with]
UNRESOLVED NAMES:
@@ -0,0 +1,15 @@
USED DECLARATIONS:
Declaration: <local>.<destruct>
By names: [<destruct>]
Declaration: <local>.target
By names: [target]
Declaration: dependency.component1
By names: [component1]
Declaration: dependency.component2
By names: [component2]
UNRESOLVED NAMES:
@@ -0,0 +1,18 @@
USED DECLARATIONS:
Declaration: kotlin.Int
By names: [Int]
Declaration: kotlin.String
By names: [String]
Declaration: kotlin.with
By names: [with]
Declaration: test.Base
By names: [Base]
Declaration: test.Foo
By names: [Foo]
UNRESOLVED NAMES:
@@ -0,0 +1,3 @@
USED DECLARATIONS:
UNRESOLVED NAMES:
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: test.MyClass
By names: [MyClass]
UNRESOLVED NAMES:
@@ -0,0 +1,9 @@
USED DECLARATIONS:
Declaration: <local>.<destruct>
By names: [<destruct>]
Declaration: dependency.foo
By names: [foo]
UNRESOLVED NAMES:
@@ -0,0 +1,9 @@
USED DECLARATIONS:
Declaration: <local>.bar
By names: [bar]
Declaration: kotlin.with
By names: [with]
UNRESOLVED NAMES:
@@ -0,0 +1,9 @@
USED DECLARATIONS:
Declaration: test.SamePackage
By names: [SamePackage]
Declaration: test.samePackage
By names: [samePackage]
UNRESOLVED NAMES:
@@ -0,0 +1,9 @@
USED DECLARATIONS:
Declaration: <local>.foo
By names: [foo]
Declaration: kotlin.Int
By names: [Int]
UNRESOLVED NAMES:
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: dependency.MyObject.invoke
By names: [invoke]
UNRESOLVED NAMES:
@@ -0,0 +1,12 @@
USED DECLARATIONS:
Declaration: KotlinChild
By names: [KotlinChild]
Declaration: dependency.Base
By names: [Base]
Declaration: dependency.Child
By names: [Child]
UNRESOLVED NAMES:
@@ -0,0 +1,3 @@
USED DECLARATIONS:
UNRESOLVED NAMES:
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: dependency.MyException
By names: [MyException]
UNRESOLVED NAMES:
@@ -0,0 +1,15 @@
USED DECLARATIONS:
Declaration: <local>.<destruct>
By names: [<destruct>]
Declaration: <local>.holder
By names: [holder]
Declaration: dependency.Holder
By names: [Holder]
Declaration: dependency.MyClass
By names: [MyClass]
UNRESOLVED NAMES:
@@ -0,0 +1,9 @@
USED DECLARATIONS:
Declaration: test.SamePackage
By names: [Aliased, SamePackage]
Declaration: test.samePackage
By names: [aliased, samePackage]
UNRESOLVED NAMES:
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: dependency.foo
By names: [aliasedFoo]
UNRESOLVED NAMES:
@@ -0,0 +1,9 @@
USED DECLARATIONS:
Declaration: test.SamePackage
By names: [Aliased]
Declaration: test.samePackage
By names: [aliased]
UNRESOLVED NAMES:
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: dependency.Bar
By names: [BarAlias]
UNRESOLVED NAMES:
@@ -0,0 +1,9 @@
USED DECLARATIONS:
Declaration: dependency.Bar.extFun
By names: [extFun]
Declaration: kotlin.with
By names: [with]
UNRESOLVED NAMES:
@@ -0,0 +1,15 @@
USED DECLARATIONS:
Declaration: <local>.<destruct>
By names: [<destruct>]
Declaration: <local>.target
By names: [target]
Declaration: dependency.component1
By names: [component1]
Declaration: dependency.component2
By names: [component2]
UNRESOLVED NAMES:
@@ -0,0 +1,15 @@
USED DECLARATIONS:
Declaration: dependency.Bar
By names: [Bar]
Declaration: dependency.extCallable
By names: [extCallable]
Declaration: dependency.extFun
By names: [extFun]
Declaration: dependency.extVal
By names: [extVal]
UNRESOLVED NAMES:
@@ -0,0 +1,6 @@
USED DECLARATIONS:
Declaration: dependency.foo
By names: [foo]
UNRESOLVED NAMES:
@@ -0,0 +1,30 @@
USED DECLARATIONS:
Declaration: kotlin.Int
By names: [Int]
Declaration: kotlin.String
By names: [String]
Declaration: test.Base
By names: [Base]
Declaration: test.Foo.callableReference
By names: [callableReference]
Declaration: test.Foo.extFuncFromBase
By names: [extFuncFromBase]
Declaration: test.Foo.extPropFromBase
By names: [extPropFromBase]
Declaration: test.Foo.funcFromBase
By names: [funcFromBase]
Declaration: test.Foo.invoke
By names: [invoke]
Declaration: test.Foo.propFromBase
By names: [propFromBase]
UNRESOLVED NAMES:
@@ -0,0 +1,9 @@
USED DECLARATIONS:
Declaration: dependency.Bar
By names: [Bar]
Declaration: test.MyClass
By names: [MyClass]
UNRESOLVED NAMES:
@@ -0,0 +1,12 @@
USED DECLARATIONS:
Declaration: dependency.Bar.callable
By names: [callable]
Declaration: dependency.Bar.function
By names: [function]
Declaration: dependency.Bar.property
By names: [property]
UNRESOLVED NAMES:
@@ -0,0 +1,18 @@
USED DECLARATIONS:
Declaration: <local>.m
By names: [m]
Declaration: <local>.str
By names: [str]
Declaration: dependency.invoke
By names: [str]
Declaration: kotlin.String
By names: [String]
Declaration: test.My
By names: [My]
UNRESOLVED NAMES:
@@ -0,0 +1,18 @@
USED DECLARATIONS:
Declaration: <local>.m
By names: [m]
Declaration: <local>.str
By names: [str]
Declaration: dependency.invoke
By names: [invoke]
Declaration: kotlin.String
By names: [String]
Declaration: test.My
By names: [My]
UNRESOLVED NAMES:
@@ -0,0 +1,12 @@
USED DECLARATIONS:
Declaration: <local>.foo
By names: [foo]
Declaration: dependency.invoke
By names: [invoke]
Declaration: kotlin.Int
By names: [Int]
UNRESOLVED NAMES:
@@ -0,0 +1,9 @@
USED DECLARATIONS:
Declaration: dependency.MyObject
By names: [MyObject]
Declaration: dependency.MyObject.invoke
By names: [invoke]
UNRESOLVED NAMES:
@@ -0,0 +1,15 @@
USED DECLARATIONS:
Declaration: KotlinChild.nonStaticFieldFromBase
By names: [nonStaticFieldFromBase]
Declaration: dependency.Base
By names: [Base]
Declaration: dependency.Child.STATIC_CONSTANT_FROM_BASE
By names: [STATIC_CONSTANT_FROM_BASE]
Declaration: dependency.Child.staticFunFromBase
By names: [staticFunFromBase]
UNRESOLVED NAMES:
@@ -0,0 +1,9 @@
USED DECLARATIONS:
Declaration: dependency.Bar
By names: [Bar]
Declaration: test.WithGeneric
By names: [WithGeneric]
UNRESOLVED NAMES:
@@ -0,0 +1,9 @@
USED DECLARATIONS:
Declaration: dependency.Bar
By names: [Bar]
Declaration: kotlin.Array
By names: [Bar]
UNRESOLVED NAMES: