From 90f1829fb8f8005e6892e56221840502097d8e26 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 6 Jul 2018 18:19:25 +0300 Subject: [PATCH] Minor: find tested element with in ResolveElementCacheTest.kt --- .../kotlin/test/util/jetTestUtils.kt | 8 +++- .../kotlin/idea/ResolveElementCacheTest.kt | 44 +++++++++---------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/jetTestUtils.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/jetTestUtils.kt index 17e6368c5d0..6f275f846f4 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/jetTestUtils.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/jetTestUtils.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.test.util import com.intellij.psi.* import com.intellij.psi.util.PsiTreeUtil +import com.intellij.testFramework.fixtures.CodeInsightTestFixture import com.intellij.util.SmartFMap import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.psi.KtPackageDirective @@ -59,4 +60,9 @@ fun PsiFile.findElementsByCommentPrefix(prefix: String): Map fun findLastModifiedFile(dir: File, skipFile: (File) -> Boolean): File { return dir.walk().filterNot(skipFile).maxBy { it.lastModified() }!! -} \ No newline at end of file +} + +val CodeInsightTestFixture.elementByOffset: PsiElement + get() { + return file.findElementAt(editor.caretModel.offset) ?: error("Can't find element at offset. Probably is missing.") + } diff --git a/idea/tests/org/jetbrains/kotlin/idea/ResolveElementCacheTest.kt b/idea/tests/org/jetbrains/kotlin/idea/ResolveElementCacheTest.kt index 60670569d08..2112fbcf0e7 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/ResolveElementCacheTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/ResolveElementCacheTest.kt @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.idea import com.intellij.psi.PsiDocumentManager +import com.intellij.psi.util.parentOfType import junit.framework.TestCase import org.intellij.lang.annotations.Language import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor @@ -35,6 +36,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +import org.jetbrains.kotlin.test.util.elementByOffset import org.jetbrains.kotlin.types.typeUtil.containsError class ResolveElementCacheTest : KotlinLightCodeInsightFixtureTestCase() { @@ -384,11 +386,11 @@ class C(param1: String = "", param2: Int = 0) { } fun testPrimaryConstructorParameterFullAnalysis() { - val file = myFixture.configureByText("Test.kt", """ - class My(param: Int = 0) + myFixture.configureByText("Test.kt", """ + class My(param: Int = 0) """) as KtFile - val defaultValue = ((file.declarations[0]) as KtClass).getPrimaryConstructor()!!.valueParameters[0].defaultValue!! + val defaultValue = myFixture.elementByOffset.parentOfType()!! // Kept to preserve correct behaviour of analyzeFully() on class internal elements @Suppress("DEPRECATION") @@ -396,11 +398,13 @@ class C(param1: String = "", param2: Int = 0) { } fun testPrimaryConstructorAnnotationFullAnalysis() { - val file = myFixture.configureByText("Test.kt", """ - class My @Deprecated("xyz") protected constructor(param: Int) - """) as KtFile + myFixture.configureByText( + "Test.kt", """ + class My @Deprecated("xyz") protected constructor(param: Int) + """ + ) as KtFile - val annotationArguments = ((file.declarations[0]) as KtClass).getPrimaryConstructor()!!.annotationEntries[0].valueArgumentList!! + val annotationArguments = myFixture.elementByOffset.parentOfType()!! @Suppress("DEPRECATION") annotationArguments.analyzeWithAllCompilerChecks() @@ -409,14 +413,13 @@ class C(param1: String = "", param2: Int = 0) { fun testFunctionParameterAnnotation() { val file = myFixture.configureByText("Test.kt", """ annotation class Ann - fun foo(@Ann p: Int) { + fun foo(@Ann p: Int) { bar() } """) as KtFile val function = (file.declarations[1]) as KtFunction - val annotationEntry = function.valueParameters[0].annotationEntries[0] - val typeRef = annotationEntry.typeReference!! + val typeRef = myFixture.elementByOffset.parentOfType()!! val bindingContext = typeRef.analyze(BodyResolveMode.PARTIAL) @@ -429,14 +432,12 @@ class C(param1: String = "", param2: Int = 0) { } fun testPrimaryConstructorParameterAnnotation() { - val file = myFixture.configureByText("Test.kt", """ + myFixture.configureByText("Test.kt", """ annotation class Ann - class X(@set:Ann var p: Int) + class X(@set:Ann var p: Int) """) as KtFile - val constructor = ((file.declarations[1]) as KtClass).getPrimaryConstructor()!! - val annotationEntry = constructor.valueParameters[0].annotationEntries[0] - val typeRef = annotationEntry.typeReference!! + val typeRef = myFixture.elementByOffset.parentOfType()!! val bindingContext = typeRef.analyze(BodyResolveMode.PARTIAL) @@ -449,15 +450,14 @@ class C(param1: String = "", param2: Int = 0) { val file = myFixture.configureByText("Test.kt", """ annotation class Ann class X { - constructor(@Ann p: Int) { + constructor(@Ann p: Int) { foo() } } """) as KtFile val constructor = ((file.declarations[1]) as KtClass).getSecondaryConstructors()[0] - val annotationEntry = constructor.valueParameters[0].annotationEntries[0] - val typeRef = annotationEntry.typeReference!! + val typeRef = myFixture.elementByOffset.parentOfType()!! val bindingContext = typeRef.analyze(BodyResolveMode.PARTIAL) @@ -518,10 +518,10 @@ class C(param1: String = "", param2: Int = 0) { } fun testResolveDefaultValueInPrimaryConstructor() { - val file = myFixture.configureByText("Test.kt", """ + myFixture.configureByText("Test.kt", """ class ClassA ( messenger: ClassB = object : ClassB { - override fun methodOne(param: List) { + override fun methodOne(param: List) { } } ) @@ -531,9 +531,7 @@ class C(param1: String = "", param2: Int = 0) { } """) as KtFile - val classA = file.declarations[0] as KtClass - val defaultValue = classA.primaryConstructor!!.valueParameters[0].defaultValue as KtObjectLiteralExpression - val methodOne = defaultValue.objectDeclaration.declarations[0] as KtFunction + val methodOne = myFixture.elementByOffset.parentOfType()!! val bindingContext = methodOne.analyze(BodyResolveMode.FULL)