Hey,
I wanted to ask if it’s possible to compile Kotlin code if it’s represented as a String.
This should happen at runtime in a JUnit test.
Suppose I have a test code snippet and I just want to check whether it’s valid Kotlin code or not.
Is there any build-in mechanism to do this?
In Java this is possible with the following code by getting the Java compiler from the ToolProvider and compile a file for instance.
Thanks for your help. That seems very promising.
My use case is that I have a JUnit test like this. I need to check if my Kotlin code generator generates valid Kotlin code. To acccomplish that I wanted to compile and thus verify it with the help of the Kotlin compiler.
I have something in mind which comes close to the following example. What would you recommend to use in my case?
@Test fun lintClassName() {
val generatedCode = api.generateCode()
val successful = KotlinCompiler.compile(generatedCode)
assertThat(successful).isTrue
}