diff --git a/plugins/uast-kotlin/tests/AbstractKotlinResolveEverythingTest.kt b/plugins/uast-kotlin/tests/AbstractKotlinResolveEverythingTest.kt new file mode 100644 index 00000000000..693fbde83c9 --- /dev/null +++ b/plugins/uast-kotlin/tests/AbstractKotlinResolveEverythingTest.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. 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.uast.test.kotlin + +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.psi.KtBlockExpression +import org.jetbrains.uast.UFile +import org.jetbrains.uast.UReferenceExpression +import org.jetbrains.uast.test.common.kotlin.IndentedPrintingVisitor +import org.jetbrains.uast.test.common.kotlin.visitUFileAndGetResult +import org.jetbrains.uast.test.env.kotlin.assertEqualsToFile +import org.jetbrains.uast.toUElementOfType +import java.io.File + + +abstract class AbstractKotlinResolveEverythingTest : AbstractKotlinUastTest() { + + private fun getTestFile(testName: String, ext: String) = + File(File(AbstractKotlinUastTest.TEST_KOTLIN_MODEL_DIR, testName).canonicalPath + '.' + ext) + + + private fun UFile.resolvableWithTargets() = object : IndentedPrintingVisitor(KtBlockExpression::class) { + override fun render(element: PsiElement) = element.toUElementOfType()?.let { ref -> + StringBuilder().apply { + val parent = ref.uastParent + append(parent?.asLogString()) + append(" -> ") + append(ref.asLogString()) + append(" -> ") + append(ref.resolve()) + append(": ") + append(ref.resolvedName) + } + } + }.visitUFileAndGetResult(this) + + override fun check(testName: String, file: UFile) { + assertEqualsToFile("resolved", getTestFile(testName, "resolved.txt"), file.resolvableWithTargets()) + } +} \ No newline at end of file diff --git a/plugins/uast-kotlin/tests/KotlinUastResolveEverythingTest.kt b/plugins/uast-kotlin/tests/KotlinUastResolveEverythingTest.kt new file mode 100644 index 00000000000..8b4d57d2f97 --- /dev/null +++ b/plugins/uast-kotlin/tests/KotlinUastResolveEverythingTest.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. 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.uast.test.kotlin + +import org.junit.Test + +class KotlinUastResolveEverythingTest : AbstractKotlinResolveEverythingTest() { + + @Test + fun testClassAnnotation() = doTest("ClassAnnotation") + + @Test + fun testLocalDeclarations() = doTest("LocalDeclarations") + + @Test + fun testConstructors() = doTest("Constructors") + + @Test + fun testSimpleAnnotated() = doTest("SimpleAnnotated") + + @Test + fun testAnonymous() = doTest("Anonymous") + + @Test + fun testTypeReferences() = doTest("TypeReferences") + +} \ No newline at end of file diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/kotlin/IdentifiersTestBase.kt b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/kotlin/IdentifiersTestBase.kt index f8bdf607164..e5dc0c461e3 100644 --- a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/kotlin/IdentifiersTestBase.kt +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/kotlin/IdentifiersTestBase.kt @@ -6,9 +6,7 @@ package org.jetbrains.uast.test.common.kotlin import com.intellij.psi.PsiElement -import com.intellij.psi.PsiElementVisitor import org.jetbrains.kotlin.psi.KtBlockExpression -import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.uast.* import org.jetbrains.uast.test.env.kotlin.assertEqualsToFile import org.jetbrains.uast.visitor.AbstractUastVisitor @@ -19,26 +17,15 @@ import kotlin.test.fail interface IdentifiersTestBase { fun getIdentifiersFile(testName: String): File - private fun UFile.asIdentifiersWithParents(): String { - val builder = StringBuilder() - var level = 0 - (this.psi as KtFile).accept(object : PsiElementVisitor() { - override fun visitElement(element: PsiElement) { - val uIdentifier = element.toUElementOfType() - if (uIdentifier != null) { - builder.append(" ".repeat(level)) - builder.append(uIdentifier.sourcePsiElement!!.text) - builder.append(" -> ") - builder.append(uIdentifier.uastParent?.asLogString()) - builder.appendln() - } - if (element is KtBlockExpression) level++ - element.acceptChildren(this) - if (element is KtBlockExpression) level-- + private fun UFile.asIdentifiersWithParents() = object : IndentedPrintingVisitor(KtBlockExpression::class) { + override fun render(element: PsiElement): CharSequence? = element.toUElementOfType()?.let { uIdentifier -> + StringBuilder().apply { + append(uIdentifier.sourcePsiElement!!.text) + append(" -> ") + append(uIdentifier.uastParent?.asLogString()) } - }) - return builder.toString() - } + } + }.visitUFileAndGetResult(this) private fun UFile.testIdentifiersParents() { accept(object : AbstractUastVisitor() { diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/kotlin/IndentedPrintingVisitor.kt b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/kotlin/IndentedPrintingVisitor.kt new file mode 100644 index 00000000000..9d28c4fe2fe --- /dev/null +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/kotlin/IndentedPrintingVisitor.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. 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.uast.test.common.kotlin + +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiElementVisitor +import com.intellij.psi.PsiFile +import org.jetbrains.uast.UFile +import kotlin.reflect.KClass + +abstract class IndentedPrintingVisitor(val shouldIndent: (PsiElement) -> Boolean) : PsiElementVisitor() { + + constructor(vararg kClasses: KClass<*>) : this({ psi -> kClasses.any { it.isInstance(psi) } }) + + private val builder = StringBuilder() + var level = 0 + private set + + override fun visitElement(element: PsiElement) { + val charSequence = render(element) + if (charSequence != null) { + builder.append(" ".repeat(level)) + builder.append(charSequence) + builder.appendln() + } + + val shouldIndent = shouldIndent(element) + if (shouldIndent) level++ + element.acceptChildren(this) + if (shouldIndent) level-- + } + + protected abstract fun render(element: PsiElement): CharSequence? + + val result: String + get() = builder.toString() +} + +fun IndentedPrintingVisitor.visitUFileAndGetResult(uFile: UFile): String { + (uFile.sourcePsi as PsiFile).accept(this) + return result +} \ No newline at end of file