Uast: setup for KotlinUastResolveEverythingTest

This commit is contained in:
Nicolay Mitropolsky
2019-02-26 19:04:31 +03:00
parent c4b69b65bc
commit e1e1e53e4a
4 changed files with 125 additions and 21 deletions
@@ -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<UReferenceExpression>()?.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())
}
}
@@ -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")
}
@@ -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<UIdentifier>()
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<UIdentifier>()?.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() {
@@ -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
}