aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/model/annotations/KotlinAnnotationsForParametersTest.kt
blob: c459cc3490ecce277539ea8a259b3144c2b82f28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package model.annotations

import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.annotations
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.utilities.cast
import org.junit.jupiter.api.Test
import utils.AbstractModelTest
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class KotlinAnnotationsForParametersTest : AbstractModelTest("/src/main/kotlin/annotations/Test.kt", "annotations") {
    @Test
    fun `generic receiver with annotations`() {
        inlineModelTest(
            """
             |@Target(AnnotationTarget.TYPE_PARAMETER)
             |annotation class Hello(val bar: String)
             |fun <@Hello("abc") T> foo(arg: String): List<T> = TODO()
            """.trimIndent()
        ) {
            with((this / "annotations" / "foo").cast<DFunction>()) {
                val annotations = generics.first().extra[Annotations]?.directAnnotations?.flatMap { it.value }
                val driOfHello = DRI("annotations", "Hello")
                val annotationsValues = annotations?.flatMap { it.params.values }?.map { it.toString() }?.toList()

                assertEquals(listOf(driOfHello), annotations?.map { it.dri })
                assertEquals(listOf("abc"), annotationsValues)
            }
        }
    }

    @Test
    fun `generic receiver with annotated bounds`() {
        inlineModelTest(
            """
             |@Target(AnnotationTarget.TYPE_PARAMETER)
             |annotation class Hello(val bar: String)
             |fun <T: @Hello("abc") String> foo(arg: String): List<T> = TODO()
            """.trimIndent()
        ) {
            with((this / "annotations" / "foo").cast<DFunction>()) {
                val annotations = (generics.first().bounds.first() as GenericTypeConstructor)
                    .extra[Annotations]?.directAnnotations?.flatMap { it.value }
                val driOfHello = DRI("annotations", "Hello")
                val annotationsValues = annotations?.flatMap { it.params.values }?.map { it.toString() }?.toList()

                assertEquals(listOf(driOfHello), annotations?.map { it.dri })
                assertEquals(listOf("abc"), annotationsValues)
            }
        }
    }

    @Test
    fun `type parameter annotations should be visible even if type declaration has none`() {
        inlineModelTest(
            """
             |@Target(AnnotationTarget.TYPE_PARAMETER, AnnotationTarget.TYPE)
             |annotation class Hello
             |
             |fun <T> foo(param: List<@Hello T>) {}
            """.trimIndent()
        ) {
            with((this / "annotations" / "foo").cast<DFunction>()) {
                val paramAnnotations = parameters.first()
                    .type.cast<GenericTypeConstructor>()
                    .projections
                    .first().cast<Invariance<TypeParameter>>()
                    .inner.cast<TypeParameter>()
                    .annotations()
                    .values
                    .flatten()

                assertEquals(1, paramAnnotations.size)
                assertEquals(DRI("annotations", "Hello"), paramAnnotations[0].dri)
            }
        }
    }

    @Test
    fun `type parameter annotations should not be propagated from resolved type`() {
        inlineModelTest(
            """
             |@Target(AnnotationTarget.TYPE_PARAMETER, AnnotationTarget.TYPE)
             |annotation class Hello
             |
             |fun <@Hello T> foo(param: List<T>) {}
            """.trimIndent()
        ) {
            with((this / "annotations" / "foo").cast<DFunction>()) {
                val paramAnnotations = parameters.first()
                    .type.cast<GenericTypeConstructor>()
                    .projections.first().cast<Invariance<TypeParameter>>()
                    .inner.cast<TypeParameter>()
                    .annotations()

                assertTrue(paramAnnotations.isEmpty())
            }
        }
    }
}