Evaluate parents of UAST elements lazily

Also add tests for UAST consistency in various cases
This commit is contained in:
Dmitry Jemerov
2017-09-12 17:58:27 +02:00
parent 91459bbd6a
commit 40daeb13d1
52 changed files with 281 additions and 205 deletions
@@ -1,15 +1,18 @@
package org.jetbrains.uast.test.kotlin
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiRecursiveElementVisitor
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.utils.addToStdlib.assertedCast
import org.jetbrains.uast.UDeclaration
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UFile
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.kotlin.KotlinUastLanguagePlugin
import org.jetbrains.uast.test.common.RenderLogTestBase
import org.jetbrains.uast.visitor.UastVisitor
import org.junit.Assert
import java.io.File
import java.util.*
abstract class AbstractKotlinRenderLogTest : AbstractKotlinUastTest(), RenderLogTestBase {
override fun getTestFile(testName: String, ext: String) =
@@ -17,6 +20,34 @@ abstract class AbstractKotlinRenderLogTest : AbstractKotlinUastTest(), RenderLog
override fun check(testName: String, file: UFile) {
super.check(testName, file)
file.psi.accept(object : PsiRecursiveElementVisitor() {
override fun visitElement(element: PsiElement) {
KotlinUastLanguagePlugin().convertElementWithParent(element, null)
super.visitElement(element)
}
})
file.accept(object : UastVisitor {
private val parentStack = Stack<UElement>()
override fun visitElement(node: UElement): Boolean {
val parent = node.uastParent
if (parent == null) {
Assert.assertTrue("Wrong parent of $node", parentStack.empty())
}
else {
Assert.assertEquals("Wrong parent of $node", parentStack.peek(), parent)
}
parentStack.push(node)
return false
}
override fun afterVisitElement(node: UElement) {
super.afterVisitElement(node)
parentStack.pop()
}
})
file.checkContainingFileForAllElements()
}
@@ -36,4 +67,4 @@ abstract class AbstractKotlinRenderLogTest : AbstractKotlinUastTest(), RenderLog
}
})
}
}
}
@@ -1,11 +1,14 @@
package org.jetbrains.uast.test.kotlin
import com.intellij.psi.PsiModifier
import com.intellij.testFramework.UsefulTestCase
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
import org.jetbrains.uast.*
import org.jetbrains.uast.kotlin.KotlinUastLanguagePlugin
import org.jetbrains.uast.test.env.findElementByText
import org.junit.Assert
import org.junit.Test
@@ -112,4 +115,14 @@ class KotlinUastApiTest : AbstractKotlinUastTest() {
assertEquals(42, witDefaultValue.findAttributeValue(null)!!.evaluate())
}
}
@Test fun testIfCondition() {
doTest("IfStatement") { _, file ->
val psiFile = file.psi
val element = psiFile.findElementAt(psiFile.text.indexOf("\"abc\""))!!
val binaryExpression = element.getParentOfType<KtBinaryExpression>(false)!!
val uBinaryExpression = KotlinUastLanguagePlugin().convertElementWithParent(binaryExpression, null)!!
UsefulTestCase.assertInstanceOf(uBinaryExpression.uastParent, UIfExpression::class.java)
}
}
}
@@ -31,6 +31,8 @@ class SimpleKotlinRenderLogTest : AbstractKotlinRenderLogTest() {
@Test fun testPropertyWithAnnotation() = doTest("PropertyWithAnnotation")
@Test fun testIfStatement() = doTest("IfStatement")
@Test fun testInnerClasses() = doTest("InnerClasses")
@Test fun testSimpleScript() = doTest("SimpleScript")