Skip to content

Commit fd931f0

Browse files
committed
Use TestCommand in all tests
1 parent 4425fe2 commit fd931f0

23 files changed

+542
-622
lines changed

clikt/src/main/kotlin/com/github/ajalt/clikt/core/CliktCommand.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import com.github.ajalt.clikt.parameters.options.Option
1010
import com.github.ajalt.clikt.parameters.options.helpOption
1111
import com.github.ajalt.clikt.parameters.options.option
1212
import com.github.ajalt.clikt.parsers.Parser
13-
import java.util.Collections.emptyList
14-
import java.util.Collections.emptyMap
1513
import kotlin.system.exitProcess
1614

1715
/**

clikt/src/main/kotlin/com/github/ajalt/clikt/core/NoRunCliktCommand.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ package com.github.ajalt.clikt.core
33
/**
44
* A [CliktCommand] that has a default implementation of [CliktCommand.run] that is a no-op.
55
*/
6-
open class NoRunCliktCommand(help: String = "",
7-
epilog: String = "",
8-
name: String? = null)
9-
: CliktCommand(help, epilog, name, false) {
6+
open class NoRunCliktCommand(
7+
help: String = "",
8+
epilog: String = "",
9+
name: String? = null,
10+
invokeWithoutSubcommand: Boolean = false,
11+
printHelpOnEmptyArgs: Boolean = false,
12+
helpTags: Map<String, String> = emptyMap(),
13+
autoCompleteEnvvar: String? = ""
14+
) : CliktCommand(help, epilog, name, invokeWithoutSubcommand, printHelpOnEmptyArgs, helpTags, autoCompleteEnvvar) {
1015
override fun run() = Unit
1116
}

clikt/src/test/kotlin/com/github/ajalt/clikt/core/CliktCommandTest.kt

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package com.github.ajalt.clikt.core
33
import com.github.ajalt.clikt.parameters.arguments.argument
44
import com.github.ajalt.clikt.parameters.arguments.multiple
55
import com.github.ajalt.clikt.parameters.options.option
6-
import com.github.ajalt.clikt.testing.NeverCalledCliktCommand
7-
import com.github.ajalt.clikt.testing.splitArgv
6+
import com.github.ajalt.clikt.testing.TestCommand
87
import io.kotlintest.data.forall
98
import io.kotlintest.shouldBe
109
import io.kotlintest.shouldThrow
@@ -15,74 +14,38 @@ import org.junit.Test
1514
class CliktCommandTest {
1615
@Test
1716
fun `invokeWithoutSubcommand=false`() {
18-
class C : CliktCommand(name = "foo") {
19-
var ran = false
20-
override fun run() {
21-
ran = true
22-
}
23-
}
24-
25-
C().apply {
26-
parse(emptyArray())
27-
ran shouldBe true
28-
}
29-
30-
var child = C()
31-
C().subcommands(child).apply {
32-
shouldThrow<PrintHelpMessage> {
33-
parse(emptyArray())
34-
}
35-
ran shouldBe false
36-
child.ran shouldBe false
17+
shouldThrow<PrintHelpMessage> {
18+
TestCommand(called = false).subcommands(TestCommand(called = false)).parse("")
3719
}
3820

39-
child = C()
40-
C().subcommands(child).apply {
41-
parse(splitArgv("foo"))
42-
ran shouldBe true
21+
val child = TestCommand(called = true, name = "foo")
22+
TestCommand(called = true).subcommands(child).apply {
23+
parse("foo")
4324
context.invokedSubcommand shouldBe child
44-
child.ran shouldBe true
4525
child.context.invokedSubcommand shouldBe null
4626
}
4727
}
4828

4929
@Test
5030
fun `invokeWithoutSubcommand=true`() {
51-
class C : CliktCommand(name = "foo", invokeWithoutSubcommand = true) {
52-
var ran = false
53-
override fun run() {
54-
ran = true
55-
}
56-
}
57-
58-
C().apply {
59-
parse(emptyArray())
60-
ran shouldBe true
61-
}
62-
63-
var child = C()
64-
C().subcommands(listOf(child)).apply {
65-
parse(emptyArray())
66-
ran shouldBe true
31+
TestCommand(called = true, invokeWithoutSubcommand = true).subcommands(TestCommand(called = false)).apply {
32+
parse("")
6733
context.invokedSubcommand shouldBe null
68-
child.ran shouldBe false
6934
}
7035

71-
child = C()
72-
C().subcommands(child).apply {
73-
parse(splitArgv("foo"))
74-
ran shouldBe true
36+
val child = TestCommand(called = true, name = "foo")
37+
TestCommand(called = true, invokeWithoutSubcommand = true).subcommands(child).apply {
38+
parse("foo")
7539
context.invokedSubcommand shouldBe child
76-
child.ran shouldBe true
7740
child.context.invokedSubcommand shouldBe null
7841
}
7942
}
8043

8144

8245
@Test
8346
fun `printHelpOnEmptyArgs = true`() {
84-
class C : NeverCalledCliktCommand(printHelpOnEmptyArgs = true)
85-
shouldThrow<PrintHelpMessage> { C().parse(splitArgv("")) }
47+
class C : TestCommand(called = false, printHelpOnEmptyArgs = true)
48+
shouldThrow<PrintHelpMessage> { C().parse("") }
8649
}
8750

8851
@Test
@@ -94,10 +57,10 @@ class CliktCommandTest {
9457
row("recurse", null, listOf("recurse")),
9558
row("recurse2", "foo", listOf("recurse", "recurse2"))
9659
) { argv, ex, ey ->
97-
class C : CliktCommand() {
60+
class C : TestCommand() {
9861
val x by option("-x", "--xx")
9962
val y by argument().multiple()
100-
override fun run() {
63+
override fun run_() {
10164
x shouldBe ex
10265
y shouldBe ey
10366
}
@@ -111,17 +74,17 @@ class CliktCommandTest {
11174
)
11275
}
11376

114-
C().parse(splitArgv(argv))
77+
C().parse(argv)
11578
}
11679

11780
@Test
11881
fun `command usage`() {
119-
class Parent : NeverCalledCliktCommand() {
82+
class Parent : TestCommand(called = false) {
12083
val arg by argument()
12184
}
12285

12386
shouldThrow<UsageError> {
124-
Parent().parse(splitArgv(""))
87+
Parent().parse("")
12588
}.helpMessage() shouldBe """
12689
|Usage: parent [OPTIONS] ARG
12790
|

clikt/src/test/kotlin/com/github/ajalt/clikt/core/ContextTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.github.ajalt.clikt.core
22

3-
import com.github.ajalt.clikt.testing.splitArgv
3+
import com.github.ajalt.clikt.testing.TestCommand
44
import io.kotlintest.shouldBe
55
import io.kotlintest.shouldThrow
66
import org.junit.Test
77

88
class ContextTest {
99
@Test
1010
fun `find functions single context`() {
11-
class C : CliktCommand() {
11+
class C : TestCommand() {
1212
val o1 by findObject<String>()
1313
val o2 by findObject { "foo" }
1414
val o3 by findObject<String>()
1515
val o4 by findObject<Int>()
1616

17-
override fun run() {
17+
override fun run_() {
1818
context.findRoot() shouldBe context
1919
}
2020
}
@@ -33,13 +33,13 @@ class ContextTest {
3333

3434
val foo = Foo()
3535

36-
class C : CliktCommand(invokeWithoutSubcommand = true) {
36+
class C : TestCommand(invokeWithoutSubcommand = true) {
3737
val o1 by findObject<Foo>()
3838
val o2 by findObject { foo }
3939
val o3 by findObject<Foo>()
4040
val o4 by findObject<Int>()
4141

42-
override fun run() {
42+
override fun run_() {
4343
context.findRoot() shouldBe context
4444
}
4545
}
@@ -58,15 +58,15 @@ class ContextTest {
5858

5959
@Test
6060
fun `default help option names`() {
61-
class C : NoRunCliktCommand()
61+
class C : TestCommand()
6262

63-
shouldThrow<PrintHelpMessage> { C().parse(splitArgv("--help")) }
64-
shouldThrow<PrintHelpMessage> { C().parse(splitArgv("-h")) }
63+
shouldThrow<PrintHelpMessage> { C().parse("--help") }
64+
shouldThrow<PrintHelpMessage> { C().parse("-h") }
6565
shouldThrow<PrintHelpMessage> {
66-
C().context { helpOptionNames = setOf("-x") }.parse(splitArgv("-x"))
66+
C().context { helpOptionNames = setOf("-x") }.parse("-x")
6767
}
6868
shouldThrow<NoSuchOption> {
69-
C().context { helpOptionNames = setOf("--x") }.parse(splitArgv("--help"))
69+
C().context { helpOptionNames = setOf("--x") }.parse("--help")
7070
}
7171
}
7272
}

clikt/src/test/kotlin/com/github/ajalt/clikt/output/CliktHelpFormatterTest.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.ajalt.clikt.output
22

3-
import com.github.ajalt.clikt.core.CliktCommand
43
import com.github.ajalt.clikt.core.NoRunCliktCommand
54
import com.github.ajalt.clikt.core.context
65
import com.github.ajalt.clikt.core.subcommands
@@ -10,7 +9,6 @@ import com.github.ajalt.clikt.parameters.arguments.multiple
109
import com.github.ajalt.clikt.parameters.groups.OptionGroup
1110
import com.github.ajalt.clikt.parameters.groups.cooccurring
1211
import com.github.ajalt.clikt.parameters.groups.mutuallyExclusiveOptions
13-
import com.github.ajalt.clikt.parameters.groups.required
1412
import com.github.ajalt.clikt.parameters.options.*
1513
import com.github.ajalt.clikt.parameters.types.int
1614
import io.kotlintest.shouldBe
@@ -337,18 +335,14 @@ class CliktHelpFormatterTest {
337335
}
338336
}
339337

340-
class Sub : CliktCommand(help = """
338+
class Sub : NoRunCliktCommand(help = """
341339
a subcommand
342340
343341
with extra help
344342
""",
345-
helpTags = mapOf("deprecated" to "")) {
346-
override fun run() = Unit
347-
}
343+
helpTags = mapOf("deprecated" to ""))
348344

349-
class Sub2 : CliktCommand(help = "another command") {
350-
override fun run() = Unit
351-
}
345+
class Sub2 : NoRunCliktCommand(help = "another command")
352346

353347
val c = C()
354348
.versionOption("1.0")

0 commit comments

Comments
 (0)