Add test for overloading expect MemberDescriptors discrimination

Issue #KT-38298
This commit is contained in:
Dmitriy Dolovov
2020-04-23 14:56:05 +07:00
parent 90e888a1b6
commit 151890dde5
7 changed files with 221 additions and 0 deletions
@@ -0,0 +1,115 @@
/*
* Copyright 2010-2020 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.gradle
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.*
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.testFramework.runInEdtAndGet
import org.jetbrains.kotlin.idea.codeInsight.gradle.MultiplePluginVersionGradleImportingTestCase
import org.jetbrains.kotlin.idea.core.util.toPsiFile
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.psi.KtTreeVisitorVoid
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.plugins.gradle.tooling.annotation.PluginTargetVersions
import org.junit.Test
// TODO: run this test on the Gradle plugin from the current build
// 1. specify appropriate 'pluginVersion' in [PluginTargetVersions]
// 2. avoid using fixed Gradle plugin version in testdata
class ImportAndCheckNavigation : MultiplePluginVersionGradleImportingTestCase() {
@Test
@PluginTargetVersions(gradleVersion = "5.0+", pluginVersion = "1.3.50+")
fun testNavigationToCommonizedLibrary() {
val files = configureAndImportProject()
files.forEach { vFile ->
val referencesToTest = vFile.collectReferencesToTest()
referencesToTest.forEach { (psiReference, expectedElementText) ->
runReadAction {
val referencedElement = psiReference.resolve()
assertNotNull(
"PSI reference \"${psiReference.canonicalText}\" in ${vFile.relPath} can't be resolved",
referencedElement
)
val referencedElementText = referencedElement!!.text
assertTrue(
"Resolved reference \"${psiReference.canonicalText}\" from ${vFile.relPath} does not " +
"contain \"$expectedElementText\", instead it contains \"$referencedElementText\"",
expectedElementText in referencedElementText
)
}
}
}
}
override fun testDataDirName() = "importAndCheckNavigation"
private fun configureAndImportProject(): List<VirtualFile> {
val files = configureByFiles()
importProject()
return files
}
private fun VirtualFile.collectReferencesToTest(): Map<PsiReference, String> {
if (extension != "kt") return emptyMap()
val referencesToTest = mutableMapOf<PsiReference, String>()
runInEdtAndGet {
val psiFile = toPsiFile(project)
assertNotNull(
"Can't get PSI file for $relPath",
psiFile
)
psiFile!!.accept(object : KtTreeVisitorVoid() {
override fun visitComment(comment: PsiComment) {
val commentText = comment.text ?: return
val unwrappedCommentText = if (commentText.startsWith("/*")) {
commentText.removePrefix("/*").removeSuffix("*/")
} else {
commentText.removePrefix("//")
}.trim(Char::isWhitespace)
if (unwrappedCommentText.startsWith("NAVIGATION-TARGET:")) {
val expectedElementText = unwrappedCommentText.substringAfter("NAVIGATION-TARGET:").trimStart(Char::isWhitespace)
assertTrue(
"Empty expected element text in $relPath in comment \"$commentText\" at offset ${comment.startOffset}",
expectedElementText.isNotEmpty()
)
val nextElement = PsiTreeUtil.skipSiblingsForward(
comment,
PsiWhiteSpace::class.java, PsiComment::class.java
)
assertNotNull(
"Next element not found in $relPath after comment \"$commentText\" at offset ${comment.startOffset}",
nextElement
)
val reference = nextElement!!.findReferenceAt(0)
assertNotNull(
"Can find PSI reference for \"${nextElement.text}\" in $relPath at offset ${nextElement.startOffset}",
reference
)
referencesToTest[reference!!] = expectedElementText
}
}
})
}
return referencesToTest
}
private val VirtualFile.relPath: String
get() = canonicalPath?.removePrefix(projectPath)?.trimStart('/', '\\') ?: name
}