|
18 | 18 |
|
19 | 19 | package com.facebook.litho
|
20 | 20 |
|
21 |
| -import com.tschuchort.compiletesting.CompilationResult |
| 21 | +import com.facebook.litho.common.LithoCompilerConfig |
| 22 | +import com.facebook.litho.util.testData |
| 23 | +import com.tschuchort.compiletesting.JvmCompilationResult |
22 | 24 | import com.tschuchort.compiletesting.KotlinCompilation
|
23 | 25 | import com.tschuchort.compiletesting.PluginOption
|
24 | 26 | import com.tschuchort.compiletesting.SourceFile
|
| 27 | +import org.assertj.core.api.Assertions.assertThat |
25 | 28 | import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
|
26 | 29 |
|
27 | 30 | abstract class AbstractCompilerTest {
|
28 |
| - companion object { |
29 |
| - @JvmField val PLUGIN_ENABLED = PluginOption("com.facebook.litho.compiler", "enabled", "true") |
30 |
| - } |
| 31 | + private val processor = LithoCommandLineProcessor() |
31 | 32 |
|
32 | 33 | fun compile(
|
33 |
| - code: SourceFile, |
34 |
| - options: List<PluginOption> = listOf(PLUGIN_ENABLED), |
| 34 | + vararg code: SourceFile, |
| 35 | + options: List<PluginOption> = listOf(LithoCompilerConfig.ENABLED.asOption("true")), |
35 | 36 | useK2: Boolean = false
|
36 |
| - ): CompilationResult { |
| 37 | + ): JvmCompilationResult { |
37 | 38 | val compilation =
|
38 |
| - KotlinCompilation().apply { |
39 |
| - sources = listOf(code) |
| 39 | + newCompilation(useK2) { |
| 40 | + sources = code.asList() |
40 | 41 | compilerPluginRegistrars = listOf(LithoComponentRegistrar())
|
41 |
| - commandLineProcessors = listOf(LithoCommandLineProcessor()) |
42 | 42 | pluginOptions = options
|
43 |
| - inheritClassPath = true |
44 |
| - languageVersion = if (useK2) "2.0" else "1.9" |
45 |
| - supportsK2 = useK2 |
46 | 43 | }
|
47 | 44 | return compilation.compile()
|
48 | 45 | }
|
| 46 | + |
| 47 | + fun runTest( |
| 48 | + path: String, |
| 49 | + options: List<PluginOption> = listOf(LithoCompilerConfig.ENABLED.asOption("true")), |
| 50 | + useK2: Boolean = false |
| 51 | + ) { |
| 52 | + val testData = testData(path) |
| 53 | + val compilation = |
| 54 | + newCompilation(useK2) { |
| 55 | + sources = testData.sourceFiles |
| 56 | + compilerPluginRegistrars = |
| 57 | + listOf(LithoComponentRegistrar(), TestComponentRegistrar(testData.processor)) |
| 58 | + pluginOptions = options |
| 59 | + } |
| 60 | + val compilationResult = compilation.compile() |
| 61 | + val processorActualResult = testData.processor.getProcessorResult() |
| 62 | + val compilerActualResult = testData.processor.extractCompilerResult(compilationResult) |
| 63 | + assertThat(processorActualResult).isEqualTo(testData.processorExpectedOutput) |
| 64 | + assertThat(compilerActualResult).isEqualTo(testData.compilerExpectedOutput) |
| 65 | + } |
| 66 | + |
| 67 | + fun LithoCompilerConfig<*>.asOption(value: String): PluginOption { |
| 68 | + return PluginOption(processor.pluginId, cliOption.optionName, value) |
| 69 | + } |
| 70 | + |
| 71 | + private inline fun newCompilation(useK2: Boolean, configure: KotlinCompilation.() -> Unit) = |
| 72 | + KotlinCompilation().apply { |
| 73 | + configure() |
| 74 | + verbose = false |
| 75 | + commandLineProcessors = listOf(processor) |
| 76 | + inheritClassPath = true |
| 77 | + languageVersion = if (useK2) "2.0" else "1.9" |
| 78 | + supportsK2 = useK2 |
| 79 | + } |
49 | 80 | }
|
0 commit comments