Skip to content

Commit 2bdec33

Browse files
Create README.md
1 parent 891c3e8 commit 2bdec33

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Kotlin Compile Testing
2+
A library for in-process compilation of Kotlin and Java code, in the spirit of [Google Compile Testing](https://github.com/google/compile-testing). For example, you can use this library to test your annotation processors or run Kotlin-script files.
3+
4+
## Use Cases
5+
6+
- Compile Kotlin and Java code in tests
7+
- Test your annotation processors
8+
- Run Kotlin-script files from within your application
9+
10+
## Example
11+
12+
```kotlin
13+
class HostClass {}
14+
15+
@Test
16+
fun `test my annotation processor`() {
17+
val kotlinSource = KotlinCompilation.SourceFile("KClass.kt", """
18+
class KClass {
19+
fun foo() {
20+
// Classes from the test environment are visible to the compiled sources
21+
val hostClass = HostClass()
22+
}
23+
""")
24+
25+
val javaSource = KotlinCompilation.SourceFile("JClass.java", """
26+
public class JClass {
27+
public void bar() {
28+
// compiled Kotlin classes are visible to Java sources
29+
KClass kClass = new KClass();
30+
}
31+
""")
32+
33+
val result = KotlinCompilation().apply {
34+
sources = listOf(kotlinSource, javaSource)
35+
36+
// pass your own instance of an annotation processor
37+
annotationProcessors = listOf(MyAnnotationProcessor()))
38+
39+
inheritClasspath = true
40+
messageOutputStream = System.out // see diagnostics in real time
41+
}.compile()
42+
43+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
44+
45+
// Test diagnostic output of compiler
46+
assertThat(result.messages).contains("My annotation processor was called")
47+
48+
// Load compiled classes and inspect generated code through reflection
49+
val kClazz = result.classloader.loadClass("KClass")
50+
assertThat(kClazz).hasDeclaredMethod("foo")
51+
}
52+
```
53+
54+
55+
## Features
56+
- Mixed-source sets: Compile Kotlin and Java source files in a single run
57+
- Annotation processing:
58+
- Run annotation processors on Kotlin and Java sources
59+
- Generate Kotlin and Java sources
60+
- Both Kotlin and Java sources have access to the generated sources
61+
- Provide your own instances of annotation processors directly to the compiler instead of letting the compiler create them with a service locator
62+
- Debug annotation processors: Since the compilation runs in the same process as your application, you can easily debug it instead of having to attach your IDE's debugger manually to the compilation process
63+
- Inherit classpath: Compiled sources have access to classes in your application
64+
- Project Jigsaw compatible: Kotlin-Compile-Testing works with JDK 8 as well as JDK 9 and later
65+
- JDK-crosscompilation: Provide your own JDK to compile the code against, instead of using the host application's JDK. This allows you to easily test your code on all JDK versions
66+
- Find dependencies automatically on the host classpath
67+
68+
## License
69+
70+
Copyright (C) 2019 Thilo Schuchort
71+
Licensed under the Mozilla Public License 2.0
72+
For custom license agreements contact me directly

0 commit comments

Comments
 (0)