From e3d6211da1095a1be8459d0963b538f78d9aa862 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Thu, 7 Sep 2023 23:53:25 +0200 Subject: [PATCH] Allow `CliktCommand.test` to take a `vararg` (#451) --- .../ajalt/clikt/testing/CliktTesting.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/testing/CliktTesting.kt b/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/testing/CliktTesting.kt index a2cabac9..836fafb5 100644 --- a/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/testing/CliktTesting.kt +++ b/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/testing/CliktTesting.kt @@ -8,6 +8,7 @@ import com.github.ajalt.clikt.parsers.shlex import com.github.ajalt.mordant.rendering.AnsiLevel import com.github.ajalt.mordant.terminal.Terminal import com.github.ajalt.mordant.terminal.TerminalRecorder +import kotlin.jvm.JvmName data class CliktCommandTestResult( /** Standard output captured from the command */ @@ -52,6 +53,33 @@ fun CliktCommand.test( return test(argvArray, stdin, envvars, includeSystemEnvvars, ansiLevel, width, height) } +/** + * Test this command, returning a result that captures the output and result status code. + * + * Note that only output printed with [echo][CliktCommand.echo] will be captured. Anything printed with [print] or + * [println] is not. + * + * @param argv The command line to send to the command + * @param stdin Content of stdin that will be read by prompt options. Multiple inputs should be separated by `\n`. + * @param envvars A map of environment variable name to value for envvars that can be read by the command + * @param includeSystemEnvvars Set to true to include the environment variables from the system in addition to those + * defined in [envvars] + * @param ansiLevel Defaults to no colored output; set to [AnsiLevel.TRUECOLOR] to include ANSI codes in the output. + * @param width The width of the terminal, used to wrap text + * @param height The height of the terminal + */ +@JvmName("varargTest") +fun CliktCommand.test( + vararg argv: String, + stdin: String = "", + envvars: Map = emptyMap(), + includeSystemEnvvars: Boolean = false, + ansiLevel: AnsiLevel = AnsiLevel.NONE, + width: Int = 79, + height: Int = 24, +): CliktCommandTestResult { + return test(argv.asList(), stdin, envvars, includeSystemEnvvars, ansiLevel, width, height) +} /** * Test this command, returning a result that captures the output and result status code.