Skip to content

Commit ad8a047

Browse files
kuanyingchouting-yuan
authored andcommitted
Test hasBackingField
1 parent bde5e8a commit ad8a047

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ class KSPAATest : AbstractKSPAATest() {
145145
runTest("../test-utils/testData/api/builtInTypes.kt")
146146
}
147147

148+
@TestMetadata("A.kt")
149+
@Test
150+
fun testA() {
151+
runTest("../test-utils/testData/api/A.kt")
152+
}
153+
154+
@TestMetadata("B.kt")
155+
@Test
156+
fun testB() {
157+
runTest("../test-utils/testData/api/B.kt")
158+
}
159+
148160
@Disabled
149161
@TestMetadata("checkOverride.kt")
150162
@Test
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.google.devtools.ksp.processor
2+
3+
import com.google.devtools.ksp.KspExperimental
4+
import com.google.devtools.ksp.getClassDeclarationByName
5+
import com.google.devtools.ksp.getDeclaredProperties
6+
import com.google.devtools.ksp.processing.Resolver
7+
import com.google.devtools.ksp.symbol.KSAnnotated
8+
9+
open class AProcessor : AbstractTestProcessor() {
10+
val results = mutableListOf<String>()
11+
override fun toResult(): List<String> {
12+
return results
13+
}
14+
15+
@OptIn(KspExperimental::class)
16+
override fun process(resolver: Resolver): List<KSAnnotated> {
17+
resolver.getClassDeclarationByName("BaseClass")!!.let { cls ->
18+
println(cls.getDeclaredProperties().map { "${it.simpleName.asString()}(${it.hasBackingField})" }.toList())
19+
}
20+
return emptyList()
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.google.devtools.ksp.processor
2+
3+
import com.google.devtools.ksp.KspExperimental
4+
import com.google.devtools.ksp.getClassDeclarationByName
5+
import com.google.devtools.ksp.getDeclaredProperties
6+
import com.google.devtools.ksp.processing.Resolver
7+
import com.google.devtools.ksp.symbol.KSAnnotated
8+
9+
open class BProcessor : AbstractTestProcessor() {
10+
val results = mutableListOf<String>()
11+
override fun toResult(): List<String> {
12+
return results
13+
}
14+
15+
@OptIn(KspExperimental::class)
16+
override fun process(resolver: Resolver): List<KSAnnotated> {
17+
resolver.getClassDeclarationByName("BaseClass")!!.let { cls ->
18+
println(cls.getDeclaredProperties().map { "${it.simpleName.asString()}(${it.hasBackingField})" }.toList())
19+
// `hasBackingField` is true when running the test individually but is false when running the whole KSPAATest.
20+
}
21+
return emptyList()
22+
}
23+
}

test-utils/testData/api/A.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// WITH_RUNTIME
19+
// TEST PROCESSOR: AProcessor
20+
// EXPECTED:
21+
// END
22+
// MODULE: lib
23+
// FILE: Test.kt
24+
open class BaseClass<T>(val genericProp : T) {
25+
fun baseMethod(input: T) {}
26+
}
27+
class SubClass(x : Int) : BaseClass<Int>(x) {
28+
val subClassProp : String = "abc"
29+
}
30+
// MODULE: main(lib)
31+
// FILE: Main.kt
32+
class Main

test-utils/testData/api/B.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// WITH_RUNTIME
19+
// TEST PROCESSOR: BProcessor
20+
// EXPECTED:
21+
// END
22+
// MODULE: lib
23+
// FILE: Test.kt
24+
open class BaseClass(
25+
open val value : List<Int>
26+
)
27+
class SubClass(
28+
override val value : MutableList<Int>
29+
) : BaseClass(value)
30+
// MODULE: main(lib)
31+
// FILE: Main.kt
32+
class Main

0 commit comments

Comments
 (0)