-
Notifications
You must be signed in to change notification settings - Fork 3
/
GenerateTestClasses.kt
48 lines (37 loc) · 1.71 KB
/
GenerateTestClasses.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package community.flock.wirespec.integration.jackson.kotlin
import community.flock.wirespec.compiler.core.WirespecSpec
import community.flock.wirespec.compiler.core.emit.JavaEmitter
import community.flock.wirespec.compiler.core.emit.KotlinEmitter
import community.flock.wirespec.compiler.core.parse
import community.flock.wirespec.compiler.core.parse.Definition
import community.flock.wirespec.compiler.utils.noLogger
import java.io.File
import kotlin.test.Test
class GenerateTestClasses {
private val basePkg = "community.flock.wirespec.integration.jackson"
private val javaPkg = "${basePkg}.java.generated"
private val kotlinPkg = "${basePkg}.kotlin.generated"
private val javaEmitter = JavaEmitter(javaPkg)
private val kotlinEmitter = KotlinEmitter(kotlinPkg)
private fun pkgToPath(pkg: String) = pkg.split(".").joinToString("/")
private val baseDir = File("src/jvmTest")
private val javaDir = baseDir.resolve("java").resolve(pkgToPath(javaPkg))
private val kotlinDir = baseDir.resolve("kotlin").resolve(pkgToPath(kotlinPkg))
@Test
fun generate() {
val todoFile = File("src/commonTest/resources/wirespec/todos.ws").readText()
val ast = WirespecSpec.parse(todoFile)(noLogger)
.fold({ error("Cannot parse wirespec: ${it.first().message}") }, { it })
.filterIsInstance<Definition>()
val emittedJava = javaEmitter.emit(ast)
val emittedKotlin = kotlinEmitter.emit(ast)
javaDir.mkdirs()
emittedJava.forEach {
javaDir.resolve("${it.typeName}.java").writeText(it.result)
}
kotlinDir.mkdirs()
emittedKotlin.forEach {
kotlinDir.resolve("Todo.kt").writeText(it.result)
}
}
}