|
| 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