Skip to content

Instantly share code, notes, and snippets.

@glureau
Created October 1, 2021 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glureau/88ad065fcf76306c42bdd97e10d56022 to your computer and use it in GitHub Desktop.
Save glureau/88ad065fcf76306c42bdd97e10d56022 to your computer and use it in GitHub Desktop.
class ExportCompilerTest {
@Test
fun classWithOneVal() {
assertCompilationOutput(
"""
package foo.bar
import deezer.kmp.Export
@Export
class BasicClass(val id: String)
""", GeneratedFile(
path = "foo/bar/js/BasicClass.kt",
content = """
package foo.bar.js
import kotlin.String
import kotlin.js.JsExport
import foo.bar.BasicClass as CommonBasicClass
@JsExport
public class BasicClass(
public val id: String
)
public fun CommonBasicClass.export() = BasicClass(
id
)
public fun BasicClass.`import`() = CommonBasicClass(
id
)
""".trimIndent()
)
)
}
}
// TestTools.kt
data class GeneratedFile(val path: String, val content: String)
fun assertCompilationOutput(fileContent: String, vararg files: GeneratedFile) {
val compilation = compile(SourceFile.kotlin("Main.kt", fileContent))
files.forEach { expectedFile ->
val targetPath = compilation.kspSourcesDir.path + "/kotlin/" + expectedFile.path
val path = Path(targetPath)
if (path.exists()) {
val generatedContent = Files.readString(path)
assertEquals(expectedFile.content, generatedContent)
} else {
println("--------")
println(compilation.kspSourcesDir.walkTopDown().joinToString())
println("--------")
fail("Expected a fail at path $targetPath but nothing was there.")
}
}
}
private fun prepareCompilation(vararg sourceFiles: SourceFile): KotlinCompilation {
return KotlinCompilation()
.apply {
inheritClassPath = true // Required so that the compiled code also see the annotation
symbolProcessorProviders = listOf(ExportCompilerProvider())
sources = sourceFiles.asList()
}
}
private fun compile(vararg sourceFiles: SourceFile, expectedExitCode: KotlinCompilation.ExitCode = KotlinCompilation.ExitCode.OK): KotlinCompilation {
val compilation = prepareCompilation(*sourceFiles)
val result = compilation.compile()
assertEquals(expectedExitCode, result.exitCode)
return compilation
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment