From c5fa7fb2176c49c466391a79631f2eae7ef53636 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Sun, 22 Oct 2017 10:48:48 +0300 Subject: [PATCH] UAST: tests utils for bottom-top finding UElements moved to `AbstractUastTest.kt` --- plugins/uast-kotlin/tests/KotlinUastApiTest.kt | 18 +++++------------- .../uast/test/env/AbstractUastTest.kt | 12 ++++++++++++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/plugins/uast-kotlin/tests/KotlinUastApiTest.kt b/plugins/uast-kotlin/tests/KotlinUastApiTest.kt index 681f83b8622..a150fca297d 100644 --- a/plugins/uast-kotlin/tests/KotlinUastApiTest.kt +++ b/plugins/uast-kotlin/tests/KotlinUastApiTest.kt @@ -6,11 +6,11 @@ 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.psi.psiUtil.parentsWithSelf 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.jetbrains.uast.test.env.findElementByTextFromPsi import org.junit.Assert import org.junit.Test @@ -131,19 +131,19 @@ class KotlinUastApiTest : AbstractKotlinUastTest() { fun testWhenStringLiteral() { doTest("WhenStringLiteral") { _, file -> - file.findFromPsi("abc").let { literalExpression -> + file.findElementByTextFromPsi("abc").let { literalExpression -> val psi = literalExpression.psi!! Assert.assertTrue(psi is KtLiteralStringTemplateEntry) UsefulTestCase.assertInstanceOf(literalExpression.uastParent, USwitchClauseExpressionWithBody::class.java) } - file.findFromPsi("def").let { literalExpression -> + file.findElementByTextFromPsi("def").let { literalExpression -> val psi = literalExpression.psi!! Assert.assertTrue(psi is KtLiteralStringTemplateEntry) UsefulTestCase.assertInstanceOf(literalExpression.uastParent, USwitchClauseExpressionWithBody::class.java) } - file.findFromPsi("def1").let { literalExpression -> + file.findElementByTextFromPsi("def1").let { literalExpression -> val psi = literalExpression.psi!! Assert.assertTrue(psi is KtLiteralStringTemplateEntry) UsefulTestCase.assertInstanceOf(literalExpression.uastParent, UBlockExpression::class.java) @@ -157,7 +157,7 @@ class KotlinUastApiTest : AbstractKotlinUastTest() { fun testWhenAndDestructing() { doTest("WhenAndDestructing") { _, file -> - file.findFromPsi("val (bindingContext, statementFilter) = arr").let { e -> + file.findElementByTextFromPsi("val (bindingContext, statementFilter) = arr").let { e -> val uBlockExpression = e.getParentOfType() Assert.assertNotNull(uBlockExpression) val uMethod = uBlockExpression!!.getParentOfType() @@ -167,11 +167,3 @@ class KotlinUastApiTest : AbstractKotlinUastTest() { } } } - - - -inline fun UFile.findFromPsi(refText: String): T = - (this.psi.findElementAt(this.psi.text.indexOf(refText)) ?: throw AssertionError("requested text '$refText' was not found in ${this.psi.name}")) - .parentsWithSelf.dropWhile { !it.text.contains(refText) }.mapNotNull { it.toUElementOfType() }.first().also { - if (it.psi != null && it.psi?.text != refText) throw AssertionError("requested text '$refText' found as '${it.psi?.text}' in $it") -} diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt index 5c5a76000f6..0f0343fa443 100644 --- a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt @@ -17,10 +17,14 @@ package org.jetbrains.uast.test.env import com.intellij.openapi.vfs.VirtualFile +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf import org.jetbrains.uast.UElement import org.jetbrains.uast.UFile +import org.jetbrains.uast.toUElementOfType import org.jetbrains.uast.visitor.UastVisitor import java.io.File +import kotlin.test.fail abstract class AbstractUastTest : AbstractTestWithCoreEnvironment() { protected companion object { @@ -60,3 +64,11 @@ fun UElement.findElementByText(refText: String, cls: Class): T { } inline fun UElement.findElementByText(refText: String): T = findElementByText(refText, T::class.java) + +inline fun UElement.findElementByTextFromPsi(refText: String): T = (this.psi ?: fail("no psi for $this")).findUElementByTextFromPsi(refText) + +inline fun PsiElement.findUElementByTextFromPsi(refText: String): T = + (this.findElementAt(this.text.indexOf(refText)) ?: throw AssertionError("requested text '$refText' was not found in $this")) + .parentsWithSelf.dropWhile { !it.text.contains(refText) }.mapNotNull { it.toUElementOfType() }.first().also { + if (it.psi != null && it.psi?.text != refText) throw AssertionError("requested text '$refText' found as '${it.psi?.text}' in $it") + }