Uast: tests for UClass.uastSuperTypes

This commit is contained in:
Nicolay Mitropolsky
2018-01-18 18:48:55 +03:00
parent 7b212c5650
commit d5d49c65b4
6 changed files with 106 additions and 13 deletions
+10
View File
@@ -0,0 +1,10 @@
import java.io.Closeable
import java.io.InputStream
fun foo() {
val runnable = object : Runnable { override fun run() {} }
runnable.run()
val closeableRunnable = object : Runnable, Closeable { override fun close() {} override fun run() {} }
val runnableIs = object : InputStream(), Runnable { override fun read(): Int = 0; override fun run() {} }
}
+36
View File
@@ -0,0 +1,36 @@
UFile (package = )
UImportStatement (isOnDemand = false)
UImportStatement (isOnDemand = false)
UClass (name = AnonymousKt)
UAnnotationMethod (name = foo)
UBlockExpression
UDeclarationsExpression
ULocalVariable (name = runnable)
UObjectLiteralExpression
UClass (name = null)
UAnnotationMethod (name = run)
UBlockExpression
UAnnotationMethod (name = AnonymousKt$foo$runnable$1)
UQualifiedReferenceExpression
USimpleNameReferenceExpression (identifier = runnable)
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0))
UIdentifier (Identifier (run))
USimpleNameReferenceExpression (identifier = run)
UDeclarationsExpression
ULocalVariable (name = closeableRunnable)
UObjectLiteralExpression
UClass (name = null)
UAnnotationMethod (name = close)
UBlockExpression
UAnnotationMethod (name = run)
UBlockExpression
UAnnotationMethod (name = AnonymousKt$foo$closeableRunnable$1)
UDeclarationsExpression
ULocalVariable (name = runnableIs)
UObjectLiteralExpression
UClass (name = null)
UAnnotationMethod (name = read)
ULiteralExpression (value = 0)
UAnnotationMethod (name = run)
UBlockExpression
UAnnotationMethod (name = AnonymousKt$foo$runnableIs$1)
+11
View File
@@ -0,0 +1,11 @@
import java.io.Closeable
import java.io.InputStream
public final class AnonymousKt {
public static final fun foo() : void {
var runnable: <ErrorType> = anonymous object : Runnable { override fun run() {} }
runnable.run()
var closeableRunnable: <ErrorType> = anonymous object : Runnable, Closeable { override fun close() {} override fun run() {} }
var runnableIs: <ErrorType> = anonymous object : InputStream(), Runnable { override fun read(): Int = 0; override fun run() {} }
}
}
+35 -5
View File
@@ -3,11 +3,7 @@ package org.jetbrains.uast.test.kotlin
import com.intellij.psi.PsiModifier
import com.intellij.testFramework.UsefulTestCase
import org.jetbrains.kotlin.asJava.toLightAnnotation
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.KtUserType
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
import org.jetbrains.kotlin.utils.addToStdlib.cast
@@ -227,6 +223,40 @@ class KotlinUastApiTest : AbstractKotlinUastTest() {
}
}
}
fun UFile.checkUastSuperTypes(refText: String, superTypes: List<String>) {
findElementByTextFromPsi<UClass>(refText, false).let {
assertEquals("base classes", superTypes, it.uastSuperTypes.map { it.getQualifiedName() })
}
}
@Test
fun testSuperTypes() {
doTest("SuperCalls") { _, file ->
file.checkUastSuperTypes("B", listOf("A"))
file.checkUastSuperTypes("O", listOf("A"))
file.checkUastSuperTypes("innerObject ", listOf("A"))
file.checkUastSuperTypes("InnerClass", listOf("A"))
file.checkUastSuperTypes("object : A(\"textForAnon\")", listOf("A"))
}
}
@Test
fun testAnonymousSuperTypes() {
doTest("Anonymous") { _, file ->
file.checkUastSuperTypes("object : Runnable { override fun run() {} }", listOf("java.lang.Runnable"))
file.checkUastSuperTypes(
"object : Runnable, Closeable { override fun close() {} override fun run() {} }",
listOf("java.lang.Runnable", "java.io.Closeable")
)
file.checkUastSuperTypes(
"object : InputStream(), Runnable { override fun read(): Int = 0; override fun run() {} }",
listOf("java.io.InputStream", "java.lang.Runnable")
)
}
}
}
fun <T, R> Iterable<T>.assertedFind(value: R, transform: (T) -> R): T = find { transform(it) == value } ?: throw AssertionError("'$value' not found, only ${this.joinToString { transform(it).toString() }}")
@@ -67,4 +67,10 @@ class SimpleKotlinRenderLogTest : AbstractKotlinRenderLogTest() {
@Test
fun testReceiverFun() = doTest("ReceiverFun")
@Test
fun testAnonymous() = doTest("Anonymous") { testName, file ->
//Disabled because cant convert IMPORT_DIRECTIVE
check(testName, file, false)
}
}
@@ -65,16 +65,16 @@ fun <T> UElement.findElementByText(refText: String, cls: Class<T>): T {
inline fun <reified T : Any> UElement.findElementByText(refText: String): T = findElementByText(refText, T::class.java)
inline fun <reified T : UElement> UElement.findElementByTextFromPsi(refText: String): T = (this.psi ?: fail("no psi for $this")).findUElementByTextFromPsi(refText)
inline fun <reified T : UElement> UElement.findElementByTextFromPsi(refText: String, strict: Boolean = true): T =
(this.psi ?: fail("no psi for $this")).findUElementByTextFromPsi(refText, strict)
inline fun <reified T : UElement> PsiElement.findUElementByTextFromPsi(refText: String): T {
inline fun <reified T : UElement> PsiElement.findUElementByTextFromPsi(refText: String, strict: Boolean = true): T {
val elementAtStart = this.findElementAt(this.text.indexOf(refText))
?: throw AssertionError("requested text '$refText' was not found in $this")
val uElementContainingText = elementAtStart.parentsWithSelf.
dropWhile { !it.text.contains(refText) }.
mapNotNull { it.toUElementOfType<T>() }.
first()
if (uElementContainingText.psi != null && uElementContainingText.psi?.text != refText) {
?: throw AssertionError("requested text '$refText' was not found in $this")
val uElementContainingText = elementAtStart.parentsWithSelf.let {
if (strict) it.dropWhile { !it.text.contains(refText) } else it
}.mapNotNull { it.toUElementOfType<T>() }.first()
if (strict && uElementContainingText.psi != null && uElementContainingText.psi?.text != refText) {
throw AssertionError("requested text '$refText' found as '${uElementContainingText.psi?.text}' in $uElementContainingText")
}
return uElementContainingText;