I'm writing a DSL with a complex DSL based on type...
# compiler
c
I'm writing a DSL with a complex DSL based on type parameters. Is there a simple way I can have a test that some code doesn't compile? I'd like to be sure that illegal cases are indeed caught by the compiler going forward.
h
You could use the compiler test framework
👍 1
It requires some kind of setup but it is small
c
Do you have a very small example of what it would look like? Even just a screenshot of an existing similar test would be useful
d
It would be something like this
Copy code
fun f(x: Any) = x <!UNCHECKED_CAST!>as Array<String><!>
So the test itself is a pair of
.kt
files with the code you want to test and generated method with
@Test
annotation. When you run the test, the framework runs the compiler over
.kt
file and renders all reported warnings/errors directly inside this
.kt
file in
<!DIAGNOSTIC_NAME!>...code on which error is reported...<!>
format
h
I use this code: https://github.com/hfhbd/validation/tree/main/kotlin-plugin see the test fixtures folder. It is the official kotlin compiler test framework only with more Gradle standard setup (instead using the JPMS folder style). The tests are in the resources folder.
👍 1
box tests requires a fun main: String returning „OK“, diagnostic tests are usable for, well, diagnostic, like failed compilations.