Skip to content

Commit a5df133

Browse files
committed
test(codegen-kotlin): KotlinOutputCompilesTest — kotlin-compile-testing gate
1 parent 118886f commit a5df133

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

server/java/codegen-kotlin/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@
6868
<version>1.6.0</version>
6969
<scope>test</scope>
7070
</dependency>
71+
<!-- Required at test scope so kotlin-compile-testing can resolve @Serializable on generated code -->
72+
<dependency>
73+
<groupId>org.jetbrains.kotlinx</groupId>
74+
<artifactId>kotlinx-serialization-core-jvm</artifactId>
75+
<version>1.7.3</version>
76+
<scope>test</scope>
77+
</dependency>
7178
</dependencies>
7279

7380
<build>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.metaobjects.generator.kotlin
2+
3+
import com.metaobjects.metadata.ktx.loadString
4+
import com.tschuchort.compiletesting.KotlinCompilation
5+
import com.tschuchort.compiletesting.SourceFile
6+
import java.nio.file.Files
7+
import kotlin.io.path.isRegularFile
8+
import kotlin.io.path.readText
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
12+
@OptIn(org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi::class)
13+
class KotlinOutputCompilesTest {
14+
15+
private val fixture = """{
16+
"metadata.root": { "package": "acme::demo", "children": [
17+
{ "object.entity": { "name": "Author", "children": [
18+
{ "field.long": { "name": "id" } },
19+
{ "field.string": { "name": "name", "@maxLength": 100, "@required": true } }
20+
] } }
21+
] }
22+
}""".trimIndent()
23+
24+
@Test fun `generated Author kt compiles`() {
25+
val outDir = Files.createTempDirectory("compile-")
26+
try {
27+
val gen = KotlinEntityGenerator()
28+
gen.setArgs(mapOf("outputDir" to outDir.toString()))
29+
gen.execute(loadString("test", fixture))
30+
31+
val sources = Files.walk(outDir).filter { it.isRegularFile() }
32+
.map { SourceFile.kotlin(it.fileName.toString(), it.readText()) }
33+
.toList()
34+
35+
val result = KotlinCompilation().apply {
36+
this.sources = sources
37+
inheritClassPath = true // include kotlinx.serialization-runtime + Exposed if on the test classpath
38+
messageOutputStream = System.out
39+
}.compile()
40+
41+
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode,
42+
"generated Kotlin failed to compile:\n${result.messages}")
43+
} finally {
44+
outDir.toFile().deleteRecursively()
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)