Skip to content

Commit 151890d

Browse files
committed
Add test for overloading expect MemberDescriptors discrimination
Issue #KT-38298
1 parent 90e888a commit 151890d

File tree

7 files changed

+221
-0
lines changed

7 files changed

+221
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.gradle
7+
8+
import com.intellij.openapi.vfs.VirtualFile
9+
import com.intellij.psi.*
10+
import com.intellij.psi.util.PsiTreeUtil
11+
import com.intellij.testFramework.runInEdtAndGet
12+
import org.jetbrains.kotlin.idea.codeInsight.gradle.MultiplePluginVersionGradleImportingTestCase
13+
import org.jetbrains.kotlin.idea.core.util.toPsiFile
14+
import org.jetbrains.kotlin.idea.util.application.runReadAction
15+
import org.jetbrains.kotlin.psi.KtTreeVisitorVoid
16+
import org.jetbrains.kotlin.psi.psiUtil.startOffset
17+
import org.jetbrains.plugins.gradle.tooling.annotation.PluginTargetVersions
18+
import org.junit.Test
19+
20+
// TODO: run this test on the Gradle plugin from the current build
21+
// 1. specify appropriate 'pluginVersion' in [PluginTargetVersions]
22+
// 2. avoid using fixed Gradle plugin version in testdata
23+
class ImportAndCheckNavigation : MultiplePluginVersionGradleImportingTestCase() {
24+
25+
@Test
26+
@PluginTargetVersions(gradleVersion = "5.0+", pluginVersion = "1.3.50+")
27+
fun testNavigationToCommonizedLibrary() {
28+
val files = configureAndImportProject()
29+
30+
files.forEach { vFile ->
31+
val referencesToTest = vFile.collectReferencesToTest()
32+
referencesToTest.forEach { (psiReference, expectedElementText) ->
33+
runReadAction {
34+
val referencedElement = psiReference.resolve()
35+
assertNotNull(
36+
"PSI reference \"${psiReference.canonicalText}\" in ${vFile.relPath} can't be resolved",
37+
referencedElement
38+
)
39+
40+
val referencedElementText = referencedElement!!.text
41+
assertTrue(
42+
"Resolved reference \"${psiReference.canonicalText}\" from ${vFile.relPath} does not " +
43+
"contain \"$expectedElementText\", instead it contains \"$referencedElementText\"",
44+
expectedElementText in referencedElementText
45+
)
46+
}
47+
}
48+
}
49+
}
50+
51+
override fun testDataDirName() = "importAndCheckNavigation"
52+
53+
private fun configureAndImportProject(): List<VirtualFile> {
54+
val files = configureByFiles()
55+
importProject()
56+
return files
57+
}
58+
59+
private fun VirtualFile.collectReferencesToTest(): Map<PsiReference, String> {
60+
if (extension != "kt") return emptyMap()
61+
62+
val referencesToTest = mutableMapOf<PsiReference, String>()
63+
64+
runInEdtAndGet {
65+
val psiFile = toPsiFile(project)
66+
assertNotNull(
67+
"Can't get PSI file for $relPath",
68+
psiFile
69+
)
70+
71+
psiFile!!.accept(object : KtTreeVisitorVoid() {
72+
override fun visitComment(comment: PsiComment) {
73+
val commentText = comment.text ?: return
74+
75+
val unwrappedCommentText = if (commentText.startsWith("/*")) {
76+
commentText.removePrefix("/*").removeSuffix("*/")
77+
} else {
78+
commentText.removePrefix("//")
79+
}.trim(Char::isWhitespace)
80+
81+
if (unwrappedCommentText.startsWith("NAVIGATION-TARGET:")) {
82+
val expectedElementText = unwrappedCommentText.substringAfter("NAVIGATION-TARGET:").trimStart(Char::isWhitespace)
83+
assertTrue(
84+
"Empty expected element text in $relPath in comment \"$commentText\" at offset ${comment.startOffset}",
85+
expectedElementText.isNotEmpty()
86+
)
87+
88+
val nextElement = PsiTreeUtil.skipSiblingsForward(
89+
comment,
90+
PsiWhiteSpace::class.java, PsiComment::class.java
91+
)
92+
assertNotNull(
93+
"Next element not found in $relPath after comment \"$commentText\" at offset ${comment.startOffset}",
94+
nextElement
95+
)
96+
97+
val reference = nextElement!!.findReferenceAt(0)
98+
assertNotNull(
99+
"Can find PSI reference for \"${nextElement.text}\" in $relPath at offset ${nextElement.startOffset}",
100+
reference
101+
)
102+
103+
referencesToTest[reference!!] = expectedElementText
104+
}
105+
}
106+
})
107+
108+
}
109+
110+
return referencesToTest
111+
}
112+
113+
private val VirtualFile.relPath: String
114+
get() = canonicalPath?.removePrefix(projectPath)?.trimStart('/', '\\') ?: name
115+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
buildscript {
2+
repositories {
3+
maven("https://dl.bintray.com/kotlin/kotlin-dev") // TODO: use the Gradle plugin from the current build
4+
mavenCentral()
5+
}
6+
7+
dependencies {
8+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0-dev-7568") // TODO: use the Gradle plugin from the current build
9+
}
10+
}
11+
12+
plugins {
13+
kotlin("multiplatform")
14+
}
15+
16+
repositories {
17+
maven("https://dl.bintray.com/kotlin/kotlin-dev") // TODO: use the Gradle plugin from the current build
18+
mavenCentral()
19+
}
20+
21+
kotlin {
22+
val commonMain by sourceSets.getting {
23+
dependencies {
24+
implementation(kotlin("stdlib-common"))
25+
implementation(kotlin("stdlib"))
26+
}
27+
}
28+
29+
val commonTest by sourceSets.getting {
30+
dependencies {
31+
implementation(kotlin("test-common"))
32+
implementation(kotlin("test-annotations-common"))
33+
}
34+
}
35+
36+
// there is no Linux HMPP shortcut preset, so need to configure targets and common source sets manually
37+
val linuxMain by sourceSets.creating { dependsOn(commonMain) }
38+
val linuxTest by sourceSets.creating { dependsOn(commonTest) }
39+
40+
linuxX64 {
41+
compilations["main"].defaultSourceSet.dependsOn(linuxMain)
42+
compilations["test"].defaultSourceSet.dependsOn(linuxTest)
43+
}
44+
45+
linuxArm64 {
46+
compilations["main"].defaultSourceSet.dependsOn(linuxMain)
47+
compilations["test"].defaultSourceSet.dependsOn(linuxTest)
48+
}
49+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
kotlin.mpp.enableGranularSourceSetsMetadata=true
2+
kotlin.native.enableDependencyPropagation=false
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
pluginManagement {
2+
resolutionStrategy {
3+
eachPlugin {
4+
if (requested.id.name == "multiplatform") {
5+
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0-dev-7568") // TODO: use the Gradle plugin from the current build
6+
}
7+
}
8+
}
9+
10+
repositories {
11+
maven("https://dl.bintray.com/kotlin/kotlin-dev") // TODO: use the Gradle plugin from the current build
12+
gradlePluginPortal()
13+
}
14+
}
15+
16+
rootProject.name = "test"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package arm64
2+
3+
import kotlinx.cinterop.CPointer
4+
import platform.posix.FILE
5+
import platform.posix.fopen
6+
import platform.posix.fprintf
7+
import platform.zlib.uInt
8+
9+
fun test() {
10+
val file: CPointer< /* NAVIGATION-TARGET:typealias FILE = */ FILE> = /* NAVIGATION-TARGET:external fun fopen */ fopen("file.txt", "r") ?: return
11+
fun f1(): /* NAVIGATION-TARGET:typealias uInt = */ uInt = TODO()
12+
/* NAVIGATION-TARGET:external fun fprintf */ fprintf(null, "")
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package common
2+
3+
import kotlinx.cinterop.CPointer
4+
import platform.posix.FILE
5+
import platform.posix.fopen
6+
import platform.posix.fprintf
7+
import platform.zlib.uInt
8+
9+
fun test() {
10+
val file: CPointer< /* NAVIGATION-TARGET:expect class FILE */ FILE> = /* NAVIGATION-TARGET:external expect fun fopen */ fopen("file.txt", "r") ?: return
11+
fun f1(): /* NAVIGATION-TARGET:expect class uInt */ uInt = TODO()
12+
/* NAVIGATION-TARGET:external expect fun fprintf */ fprintf(null, "")
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package x64
2+
3+
import kotlinx.cinterop.CPointer
4+
import platform.posix.FILE
5+
import platform.posix.fopen
6+
import platform.posix.fprintf
7+
import platform.zlib.uInt
8+
9+
fun test() {
10+
val file: CPointer< /* NAVIGATION-TARGET:typealias FILE = */ FILE> = /* NAVIGATION-TARGET:external fun fopen */ fopen("file.txt", "r") ?: return
11+
fun f1(): /* NAVIGATION-TARGET:typealias uInt = */ uInt = TODO()
12+
/* NAVIGATION-TARGET:external fun fprintf */ fprintf(null, "")
13+
}

0 commit comments

Comments
 (0)