[tests] Refactor AbstractReferenceResolveTest. Part 1

Instead of relying on UNRESOLVED_REFERENCE directive for the whole file,
just render the unresolved reference right in the .txt testdata

This will allow to introduce multi-caret resolve testdata more naturally

Required for KT-65152 and KT-62695
This commit is contained in:
Roman Golyshev
2024-01-18 13:54:31 +01:00
committed by Space Team
parent c958aef679
commit 304112cd8a
54 changed files with 61 additions and 65 deletions
@@ -58,15 +58,10 @@ abstract class AbstractReferenceResolveTest : AbstractAnalysisApiBasedTest() {
val resolvedTo = analyzeReferenceElement(ktReferences.first().element, mainModule) { val resolvedTo = analyzeReferenceElement(ktReferences.first().element, mainModule) {
val symbols = ktReferences.flatMap { it.resolveToSymbols() } val symbols = ktReferences.flatMap { it.resolveToSymbols() }
checkReferenceResultForValidity(ktReferences, mainModule, testServices, symbols)
val renderPsiClassName = Directives.RENDER_PSI_CLASS_NAME in mainModule.directives val renderPsiClassName = Directives.RENDER_PSI_CLASS_NAME in mainModule.directives
renderResolvedTo(symbols, renderPsiClassName, renderingOptions) { getAdditionalSymbolInfo(it) } renderResolvedTo(symbols, renderPsiClassName, renderingOptions) { getAdditionalSymbolInfo(it) }
} }
if (Directives.UNRESOLVED_REFERENCE in mainModule.directives) {
return
}
val actual = "Resolved to:\n$resolvedTo" val actual = "Resolved to:\n$resolvedTo"
testServices.assertions.assertEqualsToTestDataFileSibling(actual) testServices.assertions.assertEqualsToTestDataFileSibling(actual)
} }
@@ -80,28 +75,7 @@ abstract class AbstractReferenceResolveTest : AbstractAnalysisApiBasedTest() {
private fun findReferencesAtCaret(mainKtFile: KtFile, caretPosition: Int): List<KtReference> = private fun findReferencesAtCaret(mainKtFile: KtFile, caretPosition: Int): List<KtReference> =
mainKtFile.findReferenceAt(caretPosition)?.unwrapMultiReferences().orEmpty().filterIsInstance<KtReference>() mainKtFile.findReferenceAt(caretPosition)?.unwrapMultiReferences().orEmpty().filterIsInstance<KtReference>()
private fun KtAnalysisSession.checkReferenceResultForValidity(
references: List<KtReference>,
module: TestModule,
testServices: TestServices,
resolvedTo: List<KtSymbol>,
) {
if (Directives.UNRESOLVED_REFERENCE in module.directives) {
testServices.assertions.assertTrue(resolvedTo.isEmpty()) {
"Reference should be unresolved, but was resolved to ${renderResolvedTo(resolvedTo)}"
}
} else {
if (resolvedTo.isEmpty()) {
testServices.assertions.fail { "Unresolved reference ${references.first().element.text}" }
}
}
}
private object Directives : SimpleDirectivesContainer() { private object Directives : SimpleDirectivesContainer() {
val UNRESOLVED_REFERENCE by directive(
"Reference should be unresolved",
)
val RENDER_PSI_CLASS_NAME by directive( val RENDER_PSI_CLASS_NAME by directive(
"Render also PSI class name for resolved reference" "Render also PSI class name for resolved reference"
) )
@@ -15,16 +15,24 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
object TestReferenceResolveResultRenderer { object TestReferenceResolveResultRenderer {
private const val UNRESOLVED_REFERENCE_RESULT = "Nothing (Unresolved reference)"
/**
* Empty [symbols] list equals to unresolved reference.
*/
fun KtAnalysisSession.renderResolvedTo( fun KtAnalysisSession.renderResolvedTo(
symbols: List<KtSymbol>, symbols: List<KtSymbol>,
renderPsiClassName: Boolean = false, renderPsiClassName: Boolean = false,
renderer: KtDeclarationRenderer = KtDeclarationRendererForDebug.WITH_QUALIFIED_NAMES, renderer: KtDeclarationRenderer = KtDeclarationRendererForDebug.WITH_QUALIFIED_NAMES,
additionalInfo: KtAnalysisSession.(KtSymbol) -> String? = { null } additionalInfo: KtAnalysisSession.(KtSymbol) -> String? = { null }
) = ): String {
symbols.map { renderResolveResult(it, renderPsiClassName, renderer, additionalInfo) } if (symbols.isEmpty()) return UNRESOLVED_REFERENCE_RESULT
return symbols.map { renderResolveResult(it, renderPsiClassName, renderer, additionalInfo) }
.sorted() .sorted()
.withIndex() .withIndex()
.joinToString(separator = "\n") { "${it.index}: ${it.value}" } .joinToString(separator = "\n") { "${it.index}: ${it.value}" }
}
private fun KtAnalysisSession.renderResolveResult( private fun KtAnalysisSession.renderResolveResult(
symbol: KtSymbol, symbol: KtSymbol,
@@ -1,4 +1,3 @@
// UNRESOLVED_REFERENCE
package test package test
interface Foo interface Foo
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,4 +1,3 @@
// UNRESOLVED_REFERENCE
package test package test
class B(val n: Int) { class B(val n: Int) {
@@ -1,2 +1,2 @@
Resolved to: Resolved to:
0: (in test.B) operator fun set(i: kotlin.Int, a: test.B) Nothing (Unresolved reference)
@@ -1,5 +1,3 @@
// UNRESOLVED_REFERENCE
val foo: Int <caret>by Bar() val foo: Int <caret>by Bar()
class Bar class Bar
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,4 +1,3 @@
// UNRESOLVED_REFERENCE
fun test() { fun test() {
for (x <caret>in Y()) {} for (x <caret>in Y()) {}
} }
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,5 +1,3 @@
// UNRESOLVED_REFERENCE
open class Base { open class Base {
companion object { companion object {
fun foo() {} fun foo() {}
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,5 +1,3 @@
// UNRESOLVED_REFERENCE
open class Base { open class Base {
companion object { companion object {
fun foo() {} fun foo() {}
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,4 +1,3 @@
// UNRESOLVED_REFERENCE
// FILE: main.kt // FILE: main.kt
import dependency.JavaBase import dependency.JavaBase
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,4 +1,3 @@
// UNRESOLVED_REFERENCE
// FILE: main.kt // FILE: main.kt
import dependency.JavaBase import dependency.JavaBase
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,5 +1,3 @@
// UNRESOLVED_REFERENCE
/** /**
* [th<caret>is] * [th<caret>is]
*/ */
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,5 +1,3 @@
// UNRESOLVED_REFERENCE
/** /**
* [th<caret>is] * [th<caret>is]
*/ */
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,5 +1,3 @@
// UNRESOLVED_REFERENCE
/** /**
* [th<caret>is] * [th<caret>is]
*/ */
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,6 +1,5 @@
// FE1.0 can resolve to `kotlin` package when it's not allowed // FE1.0 can resolve to `kotlin` package when it's not allowed
// IGNORE_FE10 // IGNORE_FE10
// UNRESOLVED_REFERENCE
// COMPILATION_ERRORS // COMPILATION_ERRORS
// FILE: a.kt // FILE: a.kt
package kotlin.pckg package kotlin.pckg
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,4 +1,3 @@
// NO_RUNTIME // NO_RUNTIME
// UNRESOLVED_REFERENCE
// IGNORE_FE10 // IGNORE_FE10
val (<caret>x) val (<caret>x)
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,4 +1,3 @@
// UNRESOLVED_REFERENCE
fun bar(block: () -> Unit) {} fun bar(block: () -> Unit) {}
fun foo() { fun foo() {
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,4 +1,3 @@
// UNRESOLVED_REFERENCE
class Foo { class Foo {
fun foo() { fun foo() {
class Local { class Local {
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,5 +1,3 @@
// UNRESOLVED_REFERENCE
@Deprecated("don't use i", level = DeprecationLevel.HIDDEN) @Deprecated("don't use i", level = DeprecationLevel.HIDDEN)
val i: Int = 1 val i: Int = 1
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,5 +1,3 @@
// UNRESOLVED_REFERENCE
// FILE: main.kt // FILE: main.kt
import java.lang.* // will not import Fake import java.lang.* // will not import Fake
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,4 +1,3 @@
// UNRESOLVED_REFERENCE
// FILE: main.kt // FILE: main.kt
fun foo() { fun foo() {
<caret>Fake() // not imported within "java.lang.*" default import <caret>Fake() // not imported within "java.lang.*" default import
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,4 +1,3 @@
// UNRESOLVED_REFERENCE
// FILE: main.kt // FILE: main.kt
package nonRoot package nonRoot
import java.lang.* import java.lang.*
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,4 +1,3 @@
// UNRESOLVED_REFERENCE
// FILE: main.kt // FILE: main.kt
package nonRoot package nonRoot
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,5 +1,4 @@
// Should not fall on temp references in invalid code // Should not fall on temp references in invalid code
// UNRESOLVED_REFERENCE
object Testing { object Testing {
companion object { companion object {
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,4 +1,3 @@
// UNRESOLVED_REFERENCE
// IGNORE_FE10 // IGNORE_FE10
fun <T : Any, Z> createTuple(a: T, b: Z&Any): Pair<T, Z&Any> { fun <T : Any, Z> createTuple(a: T, b: Z&Any): Pair<T, Z&Any> {
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,5 +1,3 @@
// UNRESOLVED_REFERENCE
// MODULE: extendedModule // MODULE: extendedModule
// WITH_RESOLVE_EXTENSION // WITH_RESOLVE_EXTENSION
// RESOLVE_EXTENSION_PACKAGE: generated // RESOLVE_EXTENSION_PACKAGE: generated
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,5 +1,3 @@
// UNRESOLVED_REFERENCE
// MODULE: extendedModule // MODULE: extendedModule
// WITH_RESOLVE_EXTENSION // WITH_RESOLVE_EXTENSION
// RESOLVE_EXTENSION_PACKAGE: generated // RESOLVE_EXTENSION_PACKAGE: generated
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,7 +1,6 @@
// WITH_RESOLVE_EXTENSION // WITH_RESOLVE_EXTENSION
// RESOLVE_EXTENSION_PACKAGE: generated // RESOLVE_EXTENSION_PACKAGE: generated
// RESOLVE_EXTENSION_SHADOWED: \.hidden\.kt$ // RESOLVE_EXTENSION_SHADOWED: \.hidden\.kt$
// UNRESOLVED_REFERENCE
// FILE: declarations.hidden.kt // FILE: declarations.hidden.kt
package foo package foo
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)
@@ -1,7 +1,6 @@
// WITH_RESOLVE_EXTENSION // WITH_RESOLVE_EXTENSION
// RESOLVE_EXTENSION_PACKAGE: generated // RESOLVE_EXTENSION_PACKAGE: generated
// RESOLVE_EXTENSION_SHADOWED: \.hidden\.[a-z]+$ // RESOLVE_EXTENSION_SHADOWED: \.hidden\.[a-z]+$
// UNRESOLVED_REFERENCE
// FILE: TestClass.hidden.java // FILE: TestClass.hidden.java
package foo; package foo;
@@ -0,0 +1,2 @@
Resolved to:
Nothing (Unresolved reference)