Minor: find tested element with <caret> in ResolveElementCacheTest.kt

This commit is contained in:
Nikolay Krasko
2018-07-06 18:19:25 +03:00
parent 66e68fbb53
commit 90f1829fb8
2 changed files with 28 additions and 24 deletions
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.test.util
import com.intellij.psi.* import com.intellij.psi.*
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
import com.intellij.testFramework.fixtures.CodeInsightTestFixture
import com.intellij.util.SmartFMap import com.intellij.util.SmartFMap
import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtPackageDirective import org.jetbrains.kotlin.psi.KtPackageDirective
@@ -59,4 +60,9 @@ fun PsiFile.findElementsByCommentPrefix(prefix: String): Map<PsiElement, String>
fun findLastModifiedFile(dir: File, skipFile: (File) -> Boolean): File { fun findLastModifiedFile(dir: File, skipFile: (File) -> Boolean): File {
return dir.walk().filterNot(skipFile).maxBy { it.lastModified() }!! return dir.walk().filterNot(skipFile).maxBy { it.lastModified() }!!
} }
val CodeInsightTestFixture.elementByOffset: PsiElement
get() {
return file.findElementAt(editor.caretModel.offset) ?: error("Can't find element at offset. Probably <caret> is missing.")
}
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.idea package org.jetbrains.kotlin.idea
import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.util.parentOfType
import junit.framework.TestCase import junit.framework.TestCase
import org.intellij.lang.annotations.Language import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor 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.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.test.util.elementByOffset
import org.jetbrains.kotlin.types.typeUtil.containsError import org.jetbrains.kotlin.types.typeUtil.containsError
class ResolveElementCacheTest : KotlinLightCodeInsightFixtureTestCase() { class ResolveElementCacheTest : KotlinLightCodeInsightFixtureTestCase() {
@@ -384,11 +386,11 @@ class C(param1: String = "", param2: Int = 0) {
} }
fun testPrimaryConstructorParameterFullAnalysis() { fun testPrimaryConstructorParameterFullAnalysis() {
val file = myFixture.configureByText("Test.kt", """ myFixture.configureByText("Test.kt", """
class My(param: Int = 0) class My(param: Int = <caret>0)
""") as KtFile """) as KtFile
val defaultValue = ((file.declarations[0]) as KtClass).getPrimaryConstructor()!!.valueParameters[0].defaultValue!! val defaultValue = myFixture.elementByOffset.parentOfType<KtExpression>()!!
// Kept to preserve correct behaviour of analyzeFully() on class internal elements // Kept to preserve correct behaviour of analyzeFully() on class internal elements
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
@@ -396,11 +398,13 @@ class C(param1: String = "", param2: Int = 0) {
} }
fun testPrimaryConstructorAnnotationFullAnalysis() { fun testPrimaryConstructorAnnotationFullAnalysis() {
val file = myFixture.configureByText("Test.kt", """ myFixture.configureByText(
class My @Deprecated("xyz") protected constructor(param: Int) "Test.kt", """
""") as KtFile class My @Deprecated("<caret>xyz") protected constructor(param: Int)
"""
) as KtFile
val annotationArguments = ((file.declarations[0]) as KtClass).getPrimaryConstructor()!!.annotationEntries[0].valueArgumentList!! val annotationArguments = myFixture.elementByOffset.parentOfType<KtValueArgumentList>()!!
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
annotationArguments.analyzeWithAllCompilerChecks() annotationArguments.analyzeWithAllCompilerChecks()
@@ -409,14 +413,13 @@ class C(param1: String = "", param2: Int = 0) {
fun testFunctionParameterAnnotation() { fun testFunctionParameterAnnotation() {
val file = myFixture.configureByText("Test.kt", """ val file = myFixture.configureByText("Test.kt", """
annotation class Ann annotation class Ann
fun foo(@Ann p: Int) { fun foo(@<caret>Ann p: Int) {
bar() bar()
} }
""") as KtFile """) as KtFile
val function = (file.declarations[1]) as KtFunction val function = (file.declarations[1]) as KtFunction
val annotationEntry = function.valueParameters[0].annotationEntries[0] val typeRef = myFixture.elementByOffset.parentOfType<KtTypeReference>()!!
val typeRef = annotationEntry.typeReference!!
val bindingContext = typeRef.analyze(BodyResolveMode.PARTIAL) val bindingContext = typeRef.analyze(BodyResolveMode.PARTIAL)
@@ -429,14 +432,12 @@ class C(param1: String = "", param2: Int = 0) {
} }
fun testPrimaryConstructorParameterAnnotation() { fun testPrimaryConstructorParameterAnnotation() {
val file = myFixture.configureByText("Test.kt", """ myFixture.configureByText("Test.kt", """
annotation class Ann annotation class Ann
class X(@set:Ann var p: Int) class X(@set:<caret>Ann var p: Int)
""") as KtFile """) as KtFile
val constructor = ((file.declarations[1]) as KtClass).getPrimaryConstructor()!! val typeRef = myFixture.elementByOffset.parentOfType<KtTypeReference>()!!
val annotationEntry = constructor.valueParameters[0].annotationEntries[0]
val typeRef = annotationEntry.typeReference!!
val bindingContext = typeRef.analyze(BodyResolveMode.PARTIAL) val bindingContext = typeRef.analyze(BodyResolveMode.PARTIAL)
@@ -449,15 +450,14 @@ class C(param1: String = "", param2: Int = 0) {
val file = myFixture.configureByText("Test.kt", """ val file = myFixture.configureByText("Test.kt", """
annotation class Ann annotation class Ann
class X { class X {
constructor(@Ann p: Int) { constructor(@<caret>Ann p: Int) {
foo() foo()
} }
} }
""") as KtFile """) as KtFile
val constructor = ((file.declarations[1]) as KtClass).getSecondaryConstructors()[0] val constructor = ((file.declarations[1]) as KtClass).getSecondaryConstructors()[0]
val annotationEntry = constructor.valueParameters[0].annotationEntries[0] val typeRef = myFixture.elementByOffset.parentOfType<KtTypeReference>()!!
val typeRef = annotationEntry.typeReference!!
val bindingContext = typeRef.analyze(BodyResolveMode.PARTIAL) val bindingContext = typeRef.analyze(BodyResolveMode.PARTIAL)
@@ -518,10 +518,10 @@ class C(param1: String = "", param2: Int = 0) {
} }
fun testResolveDefaultValueInPrimaryConstructor() { fun testResolveDefaultValueInPrimaryConstructor() {
val file = myFixture.configureByText("Test.kt", """ myFixture.configureByText("Test.kt", """
class ClassA<N> ( class ClassA<N> (
messenger: ClassB<N> = object : ClassB<N> { messenger: ClassB<N> = object : ClassB<N> {
override fun methodOne(param: List<N>) { override fun methodOne<caret>(param: List<N>) {
} }
} }
) )
@@ -531,9 +531,7 @@ class C(param1: String = "", param2: Int = 0) {
} }
""") as KtFile """) as KtFile
val classA = file.declarations[0] as KtClass val methodOne = myFixture.elementByOffset.parentOfType<KtFunction>()!!
val defaultValue = classA.primaryConstructor!!.valueParameters[0].defaultValue as KtObjectLiteralExpression
val methodOne = defaultValue.objectDeclaration.declarations[0] as KtFunction
val bindingContext = methodOne.analyze(BodyResolveMode.FULL) val bindingContext = methodOne.analyze(BodyResolveMode.FULL)