Skip to content

Commit

Permalink
Add tests for pokoPluginArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
drewhamilton committed Jan 12, 2025
1 parent c6eae25 commit bff19f2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class PokoCommandLineProcessor : CommandLineProcessor {
.associate { arg ->
arg.split(BuildConfig.POKO_PLUGIN_ARGS_ITEM_DELIMITER).let {
require(it.size == 2) {
"Invalid syntax for ${it.first()}: " +
"Invalid syntax for <${it.firstOrNull()}>: " +
"must be in `key=value` property format"
}
it.first() to it.last()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,51 @@ class PokoCompilerPluginTest(
}
}

@Test fun `compilation with unknown pokoPluginArg yields warning`() {
testCompilation(
pokoPluginArgs = "poko.nothing=value",
) { result ->
val warning = "w: Ignoring unknown Poko plugin arg: poko.nothing"
assertThat(result.messages).contains(warning)
}
}

@Test fun `compilation with invalid pokoPluginArg fails`() {
testCompilation(
pokoPluginArgs = "poko.nothing",
expectedExitCode = KotlinCompilation.ExitCode.COMPILATION_ERROR,
) { result ->
val error = "Invalid syntax for <poko.nothing>: must be in `key=value` property format"
assertThat(result.messages).contains(error)
}
}

private inline fun testCompilation(
vararg sourceFileNames: String,
pokoAnnotationName: String = "dev/drewhamilton/poko/Poko",
pokoPluginArgs: String? = null,
expectedExitCode: KotlinCompilation.ExitCode = KotlinCompilation.ExitCode.OK,
additionalTesting: (JvmCompilationResult) -> Unit = {}
) = testCompilation(
*sourceFileNames.map { SourceFile.fromPath("src/test/resources/$it.kt") }.toTypedArray(),
pokoAnnotationName = pokoAnnotationName,
pokoPluginArgs = pokoPluginArgs,
expectedExitCode = expectedExitCode,
additionalTesting = additionalTesting
)

private inline fun testCompilation(
vararg sourceFiles: SourceFile,
pokoAnnotationName: String,
pokoPluginArgs: String? = null,
expectedExitCode: KotlinCompilation.ExitCode = KotlinCompilation.ExitCode.OK,
additionalTesting: (JvmCompilationResult) -> Unit = {}
) {
val result =
prepareCompilation(*sourceFiles, pokoAnnotationName = pokoAnnotationName).compile()
val result = prepareCompilation(
*sourceFiles,
pokoAnnotationName = pokoAnnotationName,
pokoPluginArgs = pokoPluginArgs,
).compile()
if (
expectedExitCode == KotlinCompilation.ExitCode.OK &&
result.exitCode != KotlinCompilation.ExitCode.OK
Expand Down Expand Up @@ -224,6 +249,7 @@ class PokoCompilerPluginTest(
private fun prepareCompilation(
vararg sourceFiles: SourceFile,
pokoAnnotationName: String,
pokoPluginArgs: String?,
) = KotlinCompilation().apply {
workingDir = temporaryFolder.root
compilerPluginRegistrars = listOf(PokoCompilerPluginRegistrar())
Expand All @@ -240,13 +266,24 @@ class PokoCompilerPluginTest(

val commandLineProcessor = PokoCommandLineProcessor()
commandLineProcessors = listOf(commandLineProcessor)
pluginOptions = listOf(
val standardPluginOptions = listOf(
commandLineProcessor.option(CompilerOptions.ENABLED, true),
commandLineProcessor.option(CompilerOptions.POKO_ANNOTATION, pokoAnnotationName)
)
pluginOptions = if (pokoPluginArgs == null) {
standardPluginOptions
} else {
standardPluginOptions + commandLineProcessor.option(
key = CompilerOptions.POKO_PLUGIN_ARGS,
value = pokoPluginArgs
)
}
}

private fun CommandLineProcessor.option(key: CompilerConfigurationKey<*>, value: Any?) = PluginOption(
private fun CommandLineProcessor.option(
key: CompilerConfigurationKey<*>,
value: Any?,
) = PluginOption(
pluginId,
key.toString(),
value.toString()
Expand Down

0 comments on commit bff19f2

Please sign in to comment.