Skip to content

Commit ece66f2

Browse files
bnormtschuchortdev
authored andcommitted
Add basic tests for KotlinJsCompilation
1 parent e5a3907 commit ece66f2

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package com.tschuchort.compiletesting
2+
3+
import com.nhaarman.mockitokotlin2.*
4+
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode
5+
import com.tschuchort.compiletesting.MockitoAdditionalMatchersKotlin.Companion.not
6+
import org.assertj.core.api.Assertions.assertThat
7+
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
8+
import org.jetbrains.kotlin.compiler.plugin.CliOption
9+
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
10+
import org.junit.Rule
11+
import org.junit.Test
12+
import org.junit.rules.TemporaryFolder
13+
import java.io.File
14+
15+
@Suppress("MemberVisibilityCanBePrivate")
16+
class KotlinJsCompilationTests {
17+
@Rule @JvmField val temporaryFolder = TemporaryFolder()
18+
19+
@Test
20+
fun `runs with only kotlin sources`() {
21+
val result = defaultJsCompilerConfig().apply {
22+
sources = listOf(SourceFile.kotlin("kSource.kt", "class KSource"))
23+
}.compile()
24+
25+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
26+
assertThat(result.compiledClassAndResourceFiles).hasSize(1)
27+
val jsFile = result.compiledClassAndResourceFiles[0]
28+
assertThat(jsFile.readText()).contains("function KSource()")
29+
}
30+
31+
@Test
32+
fun `runs with no sources`() {
33+
val result = defaultJsCompilerConfig().apply {
34+
sources = emptyList()
35+
}.compile()
36+
37+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
38+
}
39+
40+
@Test
41+
fun `runs with SourceFile from path`() {
42+
val sourceFile = temporaryFolder.newFile("KSource.kt").apply {
43+
writeText("class KSource")
44+
}
45+
46+
val result = defaultJsCompilerConfig().apply {
47+
sources = listOf(SourceFile.fromPath(sourceFile))
48+
}.compile()
49+
50+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
51+
assertThat(result.compiledClassAndResourceFiles).hasSize(1)
52+
val jsFile = result.compiledClassAndResourceFiles[0]
53+
assertThat(jsFile.readText()).contains("function KSource()")
54+
}
55+
56+
@Test
57+
fun `runs with SourceFile from paths with filename conflicts`() {
58+
temporaryFolder.newFolder("a")
59+
val sourceFileA = temporaryFolder.newFile("a/KSource.kt").apply {
60+
writeText("package a\n\nclass KSource")
61+
}
62+
63+
temporaryFolder.newFolder("b")
64+
val sourceFileB = temporaryFolder.newFile("b/KSource.kt").apply {
65+
writeText("package b\n\nclass KSource")
66+
}
67+
68+
val result = defaultJsCompilerConfig().apply {
69+
sources = listOf(
70+
SourceFile.fromPath(sourceFileA),
71+
SourceFile.fromPath(sourceFileB))
72+
}.compile()
73+
74+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
75+
assertThat(result.compiledClassAndResourceFiles).hasSize(1)
76+
val jsFile = result.compiledClassAndResourceFiles[0]
77+
assertThat(jsFile.readText()).contains("package\$a.KSource = KSource;")
78+
assertThat(jsFile.readText()).contains("package\$b.KSource = KSource_0;")
79+
}
80+
81+
@Test
82+
fun `Kotlin can access browser window`() {
83+
val source = SourceFile.kotlin("kSource.kt", """
84+
import kotlinx.browser.window
85+
86+
fun main(addKotlincArgs: Array<String>) {
87+
println(window.document)
88+
}
89+
""")
90+
91+
val result = defaultJsCompilerConfig().apply {
92+
sources = listOf(source)
93+
}.compile()
94+
95+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
96+
assertThat(result.compiledClassAndResourceFiles).hasSize(1)
97+
val jsFile = result.compiledClassAndResourceFiles[0]
98+
println(jsFile.readText())
99+
assertThat(jsFile.readText()).contains("println(window.document);")
100+
}
101+
102+
@Test
103+
fun `detects the plugin provided for compilation via pluginClasspaths property`() {
104+
val result = defaultJsCompilerConfig().apply {
105+
sources = listOf(SourceFile.kotlin("kSource.kt", "class KSource"))
106+
pluginClasspaths = listOf(classpathOf("kotlin-scripting-compiler-${BuildConfig.KOTLIN_VERSION}"))
107+
}.compile()
108+
109+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
110+
assertThat(result.messages).contains(
111+
"provided plugin org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar"
112+
)
113+
}
114+
115+
@Test
116+
fun `returns an internal error when adding a non existing plugin for compilation`() {
117+
val result = defaultJsCompilerConfig().apply {
118+
sources = listOf(SourceFile.kotlin("kSource.kt", "class KSource"))
119+
pluginClasspaths = listOf(File("./non-existing-plugin.jar"))
120+
}.compile()
121+
122+
assertThat(result.exitCode).isEqualTo(ExitCode.INTERNAL_ERROR)
123+
assertThat(result.messages).contains("non-existing-plugin.jar not found")
124+
}
125+
126+
@Test
127+
fun `Custom plugin receives CLI argument`() {
128+
val kSource = SourceFile.kotlin(
129+
"KSource.kt", """
130+
package com.tschuchort.compiletesting;
131+
class KSource()
132+
""".trimIndent()
133+
)
134+
135+
val cliProcessor = spy(object : CommandLineProcessor {
136+
override val pluginId = "myPluginId"
137+
override val pluginOptions = listOf(CliOption("test_option_name", "", ""))
138+
})
139+
140+
val result = defaultJsCompilerConfig().apply {
141+
sources = listOf(kSource)
142+
inheritClassPath = false
143+
pluginOptions = listOf(PluginOption("myPluginId", "test_option_name", "test_value"))
144+
commandLineProcessors = listOf(cliProcessor)
145+
}.compile()
146+
147+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
148+
149+
verify(cliProcessor, atLeastOnce()).processOption(argWhere<AbstractCliOption> { it.optionName == "test_option_name" }, eq("test_value"), any())
150+
verify(cliProcessor, never()).processOption(argWhere<AbstractCliOption> { it.optionName == "test_option_name" }, not(eq("test_value")), any())
151+
verify(cliProcessor, never()).processOption(argWhere<AbstractCliOption> { it.optionName != "test_option_name" }, any(), any())
152+
}
153+
}

0 commit comments

Comments
 (0)