From f78ea754318d66764f8d8e383c69a39a4e74a292 Mon Sep 17 00:00:00 2001 From: Dmitry Savvinov Date: Mon, 20 Jan 2020 16:35:39 +0300 Subject: [PATCH] Fix running tests from expect class by using FakeLightClasses Unfortunately, IJ API requires us to return instances of PsiClass/PsiMethod in order to proceed with run configuration creation. In fact, those instances will be queried only by trivial requests, like containingFile or name. Therefore, full-blown LightClasses are not needed and even harmful, because for some declarations we can not build proper light classes (like for expect declarations, for example) Previously it mostly worked because most of declarations in common indeed have corresponding Light-instances (because common source-set can be seen from Java through JVM-part of a project). ^KT-34503 Fixed --- .../org/jetbrains/kotlin/idea/run/kotlinTestClassUtils.kt | 8 ++++---- .../src/commonTest/kotlin/sample/SampleTests.kt | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/run/kotlinTestClassUtils.kt b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/run/kotlinTestClassUtils.kt index b330d8228e3..efcd73e1124 100644 --- a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/run/kotlinTestClassUtils.kt +++ b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/run/kotlinTestClassUtils.kt @@ -9,6 +9,8 @@ import com.intellij.execution.Location import com.intellij.psi.PsiClass import com.intellij.psi.PsiMethod import org.jetbrains.kotlin.asJava.toLightClass +import org.jetbrains.kotlin.idea.caches.lightClasses.KtFakeLightClass +import org.jetbrains.kotlin.idea.caches.lightClasses.KtFakeLightMethod import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.psi.KtNamedFunction @@ -17,13 +19,11 @@ import org.jetbrains.kotlin.psi.psiUtil.getParentOfType internal fun getTestMethodForKotlinTest(location: Location<*>): PsiMethod? { val leaf = location.psiElement val function = leaf?.getParentOfType(false) ?: return null - val owner = function.getParentOfType(true) as? KtClass ?: return null - val delegate = owner.toLightClass() ?: return null - return delegate.methods.firstOrNull { it.navigationElement == function } ?: return null + return KtFakeLightMethod.get(function) } internal fun getTestClassForKotlinTest(location: Location<*>): PsiClass? { val leaf = location.psiElement val owner = leaf?.getParentOfType(true) as? KtClass ?: return null - return owner.toLightClass() + return KtFakeLightClass(owner) } \ No newline at end of file diff --git a/idea/testData/gradle/testRunConfigurations/expectClassWithTests/src/commonTest/kotlin/sample/SampleTests.kt b/idea/testData/gradle/testRunConfigurations/expectClassWithTests/src/commonTest/kotlin/sample/SampleTests.kt index 5ac18915618..78be806f6dd 100644 --- a/idea/testData/gradle/testRunConfigurations/expectClassWithTests/src/commonTest/kotlin/sample/SampleTests.kt +++ b/idea/testData/gradle/testRunConfigurations/expectClassWithTests/src/commonTest/kotlin/sample/SampleTests.kt @@ -3,7 +3,7 @@ package sample import kotlin.test.Test import kotlin.test.assertTrue -expect class SampleTests { +expect class SampleTests { @Test - fun testMe() + fun testMe() } \ No newline at end of file