All Projects → tschuchortdev → Kotlin Compile Testing

tschuchortdev / Kotlin Compile Testing

Licence: mpl-2.0
A library for testing Kotlin and Java annotation processors, compiler plugins and code generation

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Kotlin Compile Testing

Snapdragon
snapdragon is an extremely pluggable, powerful and easy-to-use parser-renderer factory.
Stars: ✭ 180 (-26.53%)
Mutual labels:  compiler, compile
Shadow Rs
A build-time information stored in your rust project.(binary,lib,cdylib,dylib)
Stars: ✭ 117 (-52.24%)
Mutual labels:  compiler, compile
Mockito Scala
Mockito for Scala language
Stars: ✭ 231 (-5.71%)
Mutual labels:  testing-tools
Asmjit
Machine code generation for C++
Stars: ✭ 2,874 (+1073.06%)
Mutual labels:  compiler
Swaks
Swaks - Swiss Army Knife for SMTP
Stars: ✭ 239 (-2.45%)
Mutual labels:  testing-tools
Moxy
Moxy is MVP library for Android with incremental annotation processor and ktx features
Stars: ✭ 234 (-4.49%)
Mutual labels:  annotation-processor
Objectexporter
Object Exporter lets you export out an object while debugging in Visual Studio, the object can be serialized in either C#, JSON or XML.
Stars: ✭ 240 (-2.04%)
Mutual labels:  testing-tools
Testrocket
Super simple Ruby testing library
Stars: ✭ 229 (-6.53%)
Mutual labels:  testing-tools
Zinc
Scala incremental compiler library, originally part of sbt
Stars: ✭ 246 (+0.41%)
Mutual labels:  compiler
Adlik
Adlik: Toolkit for Accelerating Deep Learning Inference
Stars: ✭ 237 (-3.27%)
Mutual labels:  compiler
Vuepack
Publish .vue files in NPM packages
Stars: ✭ 242 (-1.22%)
Mutual labels:  compiler
Cproc
C11 compiler (mirror)
Stars: ✭ 238 (-2.86%)
Mutual labels:  compiler
Gwion
🎵 strongly-timed musical programming language
Stars: ✭ 235 (-4.08%)
Mutual labels:  compiler
Openj9
Eclipse OpenJ9: A Java Virtual Machine for OpenJDK that's optimized for small footprint, fast start-up, and high throughput. Builds on Eclipse OMR (https://github.com/eclipse/omr) and combines with the Extensions for OpenJDK for OpenJ9 repo.
Stars: ✭ 2,802 (+1043.67%)
Mutual labels:  compiler
Ulfberht
🗡️ A small but powerful & opinionated DI library. Written in Kotlin, and powered by annotation processing.
Stars: ✭ 234 (-4.49%)
Mutual labels:  annotation-processor
Moshix
Moshi Extensions
Stars: ✭ 243 (-0.82%)
Mutual labels:  annotation-processor
Ts Llvm
TypeScript to LLVM compiler (abandoned)
Stars: ✭ 230 (-6.12%)
Mutual labels:  compiler
Mond
A scripting language for .NET Core
Stars: ✭ 237 (-3.27%)
Mutual labels:  compiler
Create Your Own Lang With Rust
Create your own programming language with Rust (WIP)
Stars: ✭ 236 (-3.67%)
Mutual labels:  compiler
Artiq
A leading-edge control system for quantum information experiments
Stars: ✭ 245 (+0%)
Mutual labels:  compiler

Kotlin Compile Testing

Maven Central GitHub Maintenance Contributions Welcome Build Status

A library for in-process compilation of Kotlin and Java code, in the spirit of Google Compile Testing. For example, you can use this library to test your annotation processor or compiler plugin.

Use Cases

  • Compile Kotlin and Java code in tests
  • Test annotation processors
  • Test compiler plugins
  • Test Kotlin code generation

Example

Create sources

class TestEnvClass {}

@Test
fun `test my annotation processor`() {
    val kotlinSource = SourceFile.kotlin("KClass.kt", """
        class KClass {
            fun foo() {
                // Classes from the test environment are visible to the compiled sources
                val testEnvClass = TestEnvClass() 
            }
        }
    """)   
      
    val javaSource = SourceFile.java("JClass.java", """
        public class JClass {
            public void bar() {
                // compiled Kotlin classes are visible to Java sources
                KClass kClass = new KClass(); 
            }
    """)

Configure compilation

    val result = KotlinCompilation().apply {
        sources = listOf(kotlinSource, javaSource)
        
        // pass your own instance of an annotation processor
        annotationProcessors = listOf(MyAnnotationProcessor()) 

        // pass your own instance of a compiler plugin
        compilerPlugins = listOf(MyComponentRegistrar())
	commandLineProcessors = listOf(MyCommandlineProcessor())
        
        inheritClassPath = true
        messageOutputStream = System.out // see diagnostics in real time
    }.compile()

Assert results

    assertThat(result.exitCode).isEqualTo(ExitCode.OK)	
    
    // Test diagnostic output of compiler
    assertThat(result.messages).contains("My annotation processor was called") 
    
    // Load compiled classes and inspect generated code through reflection
    val kClazz = result.classLoader.loadClass("KClass")
    assertThat(kClazz).hasDeclaredMethods("foo")
}

Features

  • Mixed-source sets: Compile Kotlin and Java source files in a single run
  • Annotation processing:
    • Run annotation processors on Kotlin and Java sources
    • Generate Kotlin and Java sources
    • Both Kotlin and Java sources have access to the generated sources
    • Provide your own instances of annotation processors directly to the compiler instead of letting the compiler create them with a service locator
    • 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
  • Inherit classpath: Compiled sources have access to classes in your application
  • Project Jigsaw compatible: Kotlin-Compile-Testing works with JDK 8 as well as JDK 9 and later
  • 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
  • Find dependencies automatically on the host classpath

Installation

The package is available on mavenCentral and jitpack.

Add dependency to your module's build.gradle file:

dependencies {
        // ...
	implementation 'com.github.tschuchortdev:kotlin-compile-testing:1.3.6'
}

Remember to leave a star if you found it useful

Compatible Compiler Versions

Kotlin-Compile-Testing is compatible with all local compiler versions. It does not matter what compiler you use to compile your project.

However, if your project or any of its dependencies depend directly on compiler artifacts such as kotlin-compiler-embeddable or kotlin-annotation-processing-embeddable then they have to be the same version as the one used by Kotlin-Compile-Testing or there will be a transitive dependency conflict.

  • Current kotlin-compiler-embeddable version: 1.4.30

Because the internal APIs of the Kotlin compiler often change between versions, we can only support one kotlin-compiler-embeddable version at a time.

Kotlin Symbol Processing API Support

Kotlin Symbol Processing (KSP) is a new annotation processing pipeline that builds on top of the plugin architecture of the Kotlin Compiler, instead of delegating to javac as kapt does.

To test KSP processors, you need to use the KSP dependency:

dependencies {
    implementation 'com.github.tschuchortdev:kotlin-compile-testing-ksp:1.3.6'
}

This module adds a new function to the KotlinCompilation to specify KSP processors:

class MySymbolProcessor : SymbolProcessor {
    // implementation of the SymbolProcessor from the KSP API
}
val compilation = KotlinCompilation().apply {
    sources = listOf(source)
    symbolProcessors = listOf(MySymbolProcessor())
}
val result = compilation.compile()

All code generated by the KSP processor will be written into the KotlinCompilation.kspSourcesDir directory.

Projects that use Kotlin-Compile-Testing

License

Copyright (C) 2019 Thilo Schuchort

Licensed under the Mozilla Public License 2.0

For custom license agreements contact me directly

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].