diff --git a/plugins/uast-kotlin/testData/Delegate.kt b/plugins/uast-kotlin/testData/Delegate.kt new file mode 100644 index 00000000000..1281a12210b --- /dev/null +++ b/plugins/uast-kotlin/testData/Delegate.kt @@ -0,0 +1,9 @@ +class MyColor(val x: Int, val y: Int, val z: Int) + +class Some { + val delegate by lazy { MyColor(0x12 /* constant = 18 */, 2, 3) } + + val lambda = lazy { MyColor(1, 2, 3) } + + val nonLazy = MyColor(1, 2, 3) +} \ No newline at end of file diff --git a/plugins/uast-kotlin/testData/Delegate.values.txt b/plugins/uast-kotlin/testData/Delegate.values.txt new file mode 100644 index 00000000000..4461cb7388e --- /dev/null +++ b/plugins/uast-kotlin/testData/Delegate.values.txt @@ -0,0 +1,65 @@ +UFile (package = ) [public final class MyColor {...] + UClass (name = MyColor) [public final class MyColor {...}] + UField (name = x) [@org.jetbrains.annotations.NotNull private final var x: int] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UField (name = y) [@org.jetbrains.annotations.NotNull private final var y: int] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UField (name = z) [@org.jetbrains.annotations.NotNull private final var z: int] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UAnnotationMethod (name = getX) [public final fun getX() : int = UastEmptyExpression] + UAnnotationMethod (name = getY) [public final fun getY() : int = UastEmptyExpression] + UAnnotationMethod (name = getZ) [public final fun getZ() : int = UastEmptyExpression] + UAnnotationMethod (name = MyColor) [public fun MyColor(@org.jetbrains.annotations.NotNull x: int, @org.jetbrains.annotations.NotNull y: int, @org.jetbrains.annotations.NotNull z: int) = UastEmptyExpression] + UParameter (name = x) [@org.jetbrains.annotations.NotNull var x: int] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UParameter (name = y) [@org.jetbrains.annotations.NotNull var y: int] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UParameter (name = z) [@org.jetbrains.annotations.NotNull var z: int] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UClass (name = Some) [public final class Some {...}] + UField (name = delegate$delegate) [@org.jetbrains.annotations.NotNull private final var delegate$delegate: kotlin.Lazy] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 1)) [lazy({ ...})] = external lazy({ + (18, 2, 3) +})(Undetermined) + UIdentifier (Identifier (lazy)) [UIdentifier (Identifier (lazy))] + USimpleNameReferenceExpression (identifier = lazy) [lazy] = external lazy({ + (18, 2, 3) +})(Undetermined) + ULambdaExpression [{ ...}] = Undetermined + UBlockExpression [{...}] = external (18, 2, 3)(18, 2, 3) + UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 3)) [(18, 2, 3)] = external (18, 2, 3)(18, 2, 3) + UIdentifier (Identifier (MyColor)) [UIdentifier (Identifier (MyColor))] + USimpleNameReferenceExpression (identifier = ) [] = external (18, 2, 3)(18, 2, 3) + ULiteralExpression (value = 18) [18] = 18 + ULiteralExpression (value = 2) [2] = 2 + ULiteralExpression (value = 3) [3] = 3 + UField (name = lambda) [@org.jetbrains.annotations.NotNull private final var lambda: kotlin.Lazy = lazy({ ...})] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 1)) [lazy({ ...})] = external lazy({ + (1, 2, 3) +})(Undetermined) + UIdentifier (Identifier (lazy)) [UIdentifier (Identifier (lazy))] + USimpleNameReferenceExpression (identifier = lazy) [lazy] = external lazy({ + (1, 2, 3) +})(Undetermined) + ULambdaExpression [{ ...}] = Undetermined + UBlockExpression [{...}] = external (1, 2, 3)(1, 2, 3) + UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 3)) [(1, 2, 3)] = external (1, 2, 3)(1, 2, 3) + UIdentifier (Identifier (MyColor)) [UIdentifier (Identifier (MyColor))] + USimpleNameReferenceExpression (identifier = ) [] = external (1, 2, 3)(1, 2, 3) + ULiteralExpression (value = 1) [1] = 1 + ULiteralExpression (value = 2) [2] = 2 + ULiteralExpression (value = 3) [3] = 3 + UField (name = nonLazy) [@org.jetbrains.annotations.NotNull private final var nonLazy: MyColor = (1, 2, 3)] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 3)) [(1, 2, 3)] = external (1, 2, 3)(1, 2, 3) + UIdentifier (Identifier (MyColor)) [UIdentifier (Identifier (MyColor))] + USimpleNameReferenceExpression (identifier = ) [] = external (1, 2, 3)(1, 2, 3) + ULiteralExpression (value = 1) [1] = 1 + ULiteralExpression (value = 2) [2] = 2 + ULiteralExpression (value = 3) [3] = 3 + UAnnotationMethod (name = getDelegate) [public final fun getDelegate() : MyColor = UastEmptyExpression] + UAnnotationMethod (name = getLambda) [public final fun getLambda() : kotlin.Lazy = UastEmptyExpression] + UAnnotationMethod (name = getNonLazy) [public final fun getNonLazy() : MyColor = UastEmptyExpression] + UAnnotationMethod (name = Some) [public fun Some() = UastEmptyExpression] diff --git a/plugins/uast-kotlin/tests/AbstractKotlinExpressionValueTest.kt b/plugins/uast-kotlin/tests/AbstractKotlinExpressionValueTest.kt new file mode 100644 index 00000000000..11bea4b7571 --- /dev/null +++ b/plugins/uast-kotlin/tests/AbstractKotlinExpressionValueTest.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2010-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.jetbrains.uast.UElement +import org.jetbrains.uast.UExpression +import org.jetbrains.uast.UFile +import org.jetbrains.uast.evaluation.uValueOf +import org.jetbrains.uast.visitor.UastVisitor + +abstract class AbstractKotlinExpressionValueTest : AbstractKotlinUastTest() { + + override fun check(testName: String, file: UFile) { + var valuesFound = 0 + file.accept(object : UastVisitor { + override fun visitElement(node: UElement): Boolean { + for (comment in node.comments) { + val text = comment.text.removePrefix("/* ").removeSuffix(" */") + val parts = text.split(" = ") + if (parts.size != 2) continue + when (parts[0]) { + "constant" -> { + val expectedValue = parts[1] + val actualValue = + (node as? UExpression)?.uValueOf()?.toConstant()?.toString() + ?: "cannot evaluate $node of ${node.javaClass}" + assertEquals(expectedValue, actualValue) + valuesFound++ + } + } + } + return false + } + }) + assertTrue("No values found, add some /* constant = ... */ to the input file", valuesFound > 0) + } +} \ No newline at end of file diff --git a/plugins/uast-kotlin/tests/KotlinExpressionValueTest.kt b/plugins/uast-kotlin/tests/KotlinExpressionValueTest.kt new file mode 100644 index 00000000000..4658cdd9b37 --- /dev/null +++ b/plugins/uast-kotlin/tests/KotlinExpressionValueTest.kt @@ -0,0 +1,13 @@ +/* + * Copyright 2010-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 KotlinExpressionValueTest : AbstractKotlinExpressionValueTest() { + @Test + fun testDelegate() = doTest("Delegate") +} \ No newline at end of file diff --git a/plugins/uast-kotlin/tests/KotlinUastValuesTest.kt b/plugins/uast-kotlin/tests/KotlinUastValuesTest.kt index d4dcbbac620..eadbe06a59c 100644 --- a/plugins/uast-kotlin/tests/KotlinUastValuesTest.kt +++ b/plugins/uast-kotlin/tests/KotlinUastValuesTest.kt @@ -4,11 +4,18 @@ import org.junit.Test class KotlinUastValuesTest : AbstractKotlinValuesTest() { - @Test fun testAssertion() = doTest("Assertion") + @Test + fun testAssertion() = doTest("Assertion") - @Test fun testIn() = doTest("In") + @Test + fun testDelegate() = doTest("Delegate") - @Test fun testLocalDeclarations() = doTest("LocalDeclarations") + @Test + fun testIn() = doTest("In") - @Test fun testSimple() = doTest("Simple") + @Test + fun testLocalDeclarations() = doTest("LocalDeclarations") + + @Test + fun testSimple() = doTest("Simple") } \ No newline at end of file