181: Uast: parents for UIdentifiers from toUElement

This commit is contained in:
Nicolay Mitropolsky
2018-01-17 17:46:08 +03:00
committed by Nikolay Krasko
parent 4e078fe94b
commit 96ac913a6d
7 changed files with 168 additions and 2 deletions
@@ -0,0 +1,15 @@
package org.jetbrains.uast.test.kotlin
import org.jetbrains.uast.test.common.IdentifiersTestBase
import java.io.File
abstract class AbstractKotlinIdentifiersTest : AbstractKotlinUastTest(), IdentifiersTestBase {
private fun getTestFile(testName: String, ext: String) =
File(File(AbstractKotlinUastTest.TEST_KOTLIN_MODEL_DIR, testName).canonicalPath + '.' + ext)
override fun getIdentifiersFile(testName: String): File = getTestFile(testName, "identifiers.txt")
}
@@ -0,0 +1,22 @@
/*
* Copyright 2000-2018 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 KotlinUastIdentifiersTest : AbstractKotlinIdentifiersTest() {
@Test
fun testClassAnnotation() = doTest("ClassAnnotation")
@Test
fun testLocalDeclarations() = doTest("LocalDeclarations")
@Test
fun testConstructors() = doTest("Constructors")
}
@@ -0,0 +1,49 @@
/*
* Copyright 2000-2018 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
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.UFile
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.sourcePsiElement
import org.jetbrains.uast.test.env.assertEqualsToFile
import org.jetbrains.uast.toUElementOfType
import java.io.File
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--
}
})
return builder.toString()
}
fun check(testName: String, file: UFile) {
val valuesFile = getIdentifiersFile(testName)
assertEqualsToFile("Identifiers", valuesFile, file.asIdentifiersWithParents())
}
}