• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tools.metalava.model.psi
18 
19 import com.android.tools.metalava.model.testsuite.BaseModelTest
20 import com.android.tools.metalava.testing.createAndroidModuleDescription
21 import com.android.tools.metalava.testing.createCommonModuleDescription
22 import com.android.tools.metalava.testing.createProjectDescription
23 import com.android.tools.metalava.testing.kotlin
24 import kotlin.test.Test
25 import kotlin.test.assertEquals
26 import kotlin.test.assertFalse
27 import kotlin.test.assertNotNull
28 import kotlin.test.assertNull
29 import kotlin.test.assertSame
30 import kotlin.test.assertTrue
31 
32 class PsiParameterItemTest : BaseModelTest() {
33     @Test
primary constructor parameters have propertiesnull34     fun `primary constructor parameters have properties`() {
35         runCodebaseTest(kotlin("class Foo(val property: Int, parameter: Int)")) {
36             val constructorItem = codebase.assertClass("Foo").constructors().single()
37             val propertyParameter = constructorItem.parameters().single { it.name() == "property" }
38             val regularParameter = constructorItem.parameters().single { it.name() == "parameter" }
39 
40             assertNull(regularParameter.property)
41             assertNotNull(propertyParameter.property)
42             assertSame(propertyParameter, propertyParameter.property?.constructorParameter)
43         }
44     }
45 
46     @Test
actuals get params from expectsnull47     fun `actuals get params from expects`() {
48         val commonSource =
49             kotlin(
50                 "commonMain/src/Expect.kt",
51                 """
52                     expect suspend fun String.testFun(param: String = "")
53                     expect class Test(param: String = "") {
54                         fun something(
55                             param: String = "",
56                             otherParam: String = param + "",
57                             required: Int
58                         )
59                     }
60                 """
61             )
62         val androidSource =
63             kotlin(
64                 "androidMain/src/Actual.kt",
65                 """
66                     actual suspend fun String.testFun(param: String) {}
67                     actual class Test actual constructor(param: String) {
68                         actual fun something(
69                             param: String = "ignored",
70                             otherParam: String,
71                             required: Int
72                         ) {}
73                     }
74                     """
75             )
76         runCodebaseTest(
77             inputSet(
78                 androidSource,
79                 commonSource,
80             ),
81             projectDescription =
82                 createProjectDescription(
83                     createAndroidModuleDescription(arrayOf(androidSource)),
84                     createCommonModuleDescription(arrayOf(commonSource)),
85                 )
86         ) {
87             // Expect classes are ignored by UAST/Kotlin light classes, verify we test actual
88             // classes.
89             val actualFile = codebase.assertClass("ActualKt").sourceFile()
90 
91             val functionItem = codebase.assertClass("ActualKt").methods().single()
92             with(functionItem) {
93                 val parameters = parameters()
94                 assertEquals(3, parameters.size)
95 
96                 // receiver
97                 assertFalse(parameters[0].hasDefaultValue())
98 
99                 val parameter = parameters[1]
100                 assertTrue(parameter.hasDefaultValue())
101 
102                 // continuation
103                 assertFalse(parameters[2].hasDefaultValue())
104             }
105 
106             val classItem = codebase.assertClass("Test")
107             assertEquals(actualFile, classItem.sourceFile())
108 
109             val constructorItem = classItem.constructors().single()
110             with(constructorItem) {
111                 val parameter = parameters().single()
112                 assertTrue(parameter.hasDefaultValue())
113             }
114 
115             val methodItem = classItem.methods().single()
116             with(methodItem) {
117                 val parameters = parameters()
118                 assertEquals(3, parameters.size)
119 
120                 assertTrue(parameters[0].hasDefaultValue())
121 
122                 assertTrue(parameters[1].hasDefaultValue())
123 
124                 assertFalse(parameters[2].hasDefaultValue())
125             }
126         }
127     }
128 }
129