1+ package com.github.eventhorizonlab.spi.extensions
2+
3+ import com.tschuchort.compiletesting.KotlinCompilation
4+ import com.tschuchort.compiletesting.SourceFile
5+ import io.kotest.core.spec.style.FunSpec
6+ import io.kotest.datatest.withData
7+ import io.kotest.matchers.shouldBe
8+ import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
9+ import javax.annotation.processing.AbstractProcessor
10+ import javax.annotation.processing.ProcessingEnvironment
11+ import javax.annotation.processing.RoundEnvironment
12+ import javax.lang.model.element.TypeElement
13+
14+ @OptIn(ExperimentalCompilerApi ::class )
15+ class ProcessingEnvironmentExtensionsTests : FunSpec ({
16+
17+ data class Case (
18+ val description: String ,
19+ val source: SourceFile ,
20+ val fqName: String ,
21+ val expectedBinaryName: String
22+ )
23+
24+ val cases = listOf(
25+ Case (
26+ "Top -level Kotlin class",
27+ SourceFile .kotlin("TopLevel .kt", """
28+ package com.test
29+ class TopLevel
30+ """.trimIndent()),
31+ fqName = "com.test.TopLevel ",
32+ expectedBinaryName = "com.test.TopLevel "
33+ ),
34+ Case (
35+ "Nested Kotlin class",
36+ SourceFile .kotlin("Outer .kt", """
37+ package com.test
38+ class Outer {
39+ class Inner
40+ }
41+ """.trimIndent()),
42+ fqName = "com.test.Outer .Inner ",
43+ expectedBinaryName = $$"com.test.Outer $Inner "
44+ ),
45+ Case (
46+ "Inner (non-static) Java class",
47+ SourceFile .java("OuterJava .java", """
48+ package com.test;
49+ public class OuterJava {
50+ public class InnerJava {}
51+ }
52+ """.trimIndent()),
53+ fqName = "com.test.OuterJava .InnerJava ",
54+ expectedBinaryName = $$"com.test.OuterJava $InnerJava "
55+ )
56+ )
57+
58+ withData(nameFn = { it.description }, cases) { case ->
59+ var actual : String? = null
60+
61+ val processor = object : AbstractProcessor () {
62+ override fun init (processingEnv: ProcessingEnvironment ) {
63+ super.init (processingEnv)
64+ val type = processingEnv.elementUtils.getTypeElement(case.fqName)
65+ if (type != null) {
66+ actual = processingEnv.getStringifiedBinaryName(type)
67+ }
68+ }
69+ override fun process(
70+ annotations: MutableSet <out TypeElement >,
71+ roundEnv: RoundEnvironment
72+ ) = false
73+ }
74+
75+ val result = KotlinCompilation ().apply {
76+ sources = listOf(case.source)
77+ annotationProcessors = listOf(processor)
78+ inheritClassPath = true
79+ }.compile()
80+
81+ result.exitCode shouldBe KotlinCompilation .ExitCode .OK
82+ actual shouldBe case.expectedBinaryName
83+ }
84+ })
0 commit comments