-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathSubcommandsTests.kt
163 lines (142 loc) · 6.37 KB
/
SubcommandsTests.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(ExperimentalCli::class)
package kotlinx.cli
import kotlin.test.*
class SubcommandsTests {
@Test
fun testSubcommand() {
val argParser = ArgParser("testParser")
val output by argParser.option(ArgType.String, "output", "o", "Output file")
val defaultOption by argParser.option(ArgType.String, "format", "f", "Output format").default("CSV")
class Summary: Subcommand("summary", "Calculate summary") {
val invert by option(ArgType.Boolean, "invert", "i", "Invert results")
val addendums by argument(ArgType.Int, "addendums", description = "Addendums").vararg()
var result: Int = 0
override fun execute() {
result = addendums.sum()
result = if (invert!!) -1 * result else result
}
}
val action = Summary()
argParser.subcommands(action)
argParser.parse(arrayOf("summary", "-o", "out.txt", "-i", "2", "3", "5"))
assertEquals("out.txt", output)
assertEquals("CSV", defaultOption)
assertEquals(-10, action.result)
}
@Test
fun testStrictSubcommandOptionsOrder() {
val argParserStrictFail = ArgParser("testParser", strictSubcommandOptionsOrder = true).avoidProcessExit()
argParserStrictFail.option(ArgType.String, "output", "o", "Output file")
class Summary: Subcommand("summary", "Calculate summary") {
val invert by option(ArgType.Boolean, "invert", "i", "Invert results")
val addendums by argument(ArgType.Int, "addendums", description = "Addendums").vararg()
var result: Int = 0
override fun execute() {
result = addendums.sum()
result = if (invert!!) -1 * result else result
}
}
argParserStrictFail.subcommands(Summary())
assertFailsWith(IllegalStateException::class) {
argParserStrictFail.parse(arrayOf("summary", "-i", "2", "3", "5", "-o", "out.txt"))
}
val argParserStrict = ArgParser("testParser", strictSubcommandOptionsOrder = true)
val action = Summary()
val outputValid by argParserStrict.option(ArgType.String, "output", "o", "Output file")
val defaultOption by argParserStrict.option(ArgType.String, "format", "f", "Output format").default("CSV")
argParserStrict.subcommands(action)
argParserStrict.parse(arrayOf("-o", "out.txt", "summary", "-i", "2", "3", "5"))
assertEquals("out.txt", outputValid)
assertEquals("CSV", defaultOption)
assertEquals(-10, action.result)
}
@Test
fun testCommonOptions() {
abstract class CommonOptions(name: String, actionDescription: String): Subcommand(name, actionDescription) {
val numbers by argument(ArgType.Int, "numbers", description = "Numbers").vararg()
}
class Summary: CommonOptions("summary", "Calculate summary") {
val invert by option(ArgType.Boolean, "invert", "i", "Invert results")
var result: Int = 0
override fun execute() {
result = numbers.sum()
result = invert?.let { -1 * result } ?: result
}
}
class Subtraction : CommonOptions("sub", "Calculate subtraction") {
var result: Int = 0
override fun execute() {
result = numbers.map { -it }.sum()
}
}
val summaryAction = Summary()
val subtractionAction = Subtraction()
val argParser = ArgParser("testParser")
argParser.subcommands(summaryAction, subtractionAction)
argParser.parse(arrayOf("summary", "2", "3", "5"))
assertEquals(10, summaryAction.result)
val argParserSubtraction = ArgParser("testParser")
argParserSubtraction.subcommands(summaryAction, subtractionAction)
argParserSubtraction.parse(arrayOf("sub", "8", "-2", "3"))
assertEquals(-9, subtractionAction.result)
}
@Test
fun testRecursiveSubcommands() {
val argParser = ArgParser("testParser")
class Summary: Subcommand("summary", "Calculate summary") {
val addendums by argument(ArgType.Int, "addendums", description = "Addendums").vararg()
var result: Int = 0
override fun execute() {
result = addendums.sum()
}
}
class Calculation: Subcommand("calc", "Execute calculation") {
init {
subcommands(Summary())
}
val invert by option(ArgType.Boolean, "invert", "i", "Invert results")
var result: Int = 0
override fun execute() {
result = (subcommands["summary"] as Summary).result
result = if (invert!!) -1 * result else result
}
}
val action = Calculation()
argParser.subcommands(action)
argParser.parse(arrayOf("calc", "-i", "summary", "2", "3", "5"))
assertEquals(-10, action.result)
}
@Test
fun testCommonDefaultInSubcommand() {
val parser = ArgParser("testParser")
val output by parser.option(ArgType.String, "output", "o", "Output file")
.default("any_file")
class Summary: Subcommand("summary", "Calculate summary") {
val invert by option(ArgType.Boolean, "invert", "i", "Invert results").default(false)
val addendums by argument(ArgType.Int, "addendums", description = "Addendums").vararg()
var result: Int = 0
override fun execute() {
result = addendums.sum()
result = if (invert) -1 * result else result
println("result is: $result and will output to $output")
}
}
class Multiply: Subcommand("mul", "Multiply") {
val numbers by argument(ArgType.Int, description = "Addendums").vararg()
var result: Int = 0
override fun execute() {
result = numbers.reduce{ acc, it -> acc * it }
}
}
val summary = Summary()
val multiple = Multiply()
parser.subcommands(summary, multiple)
parser.parse(arrayOf("summary", "1", "2", "4"))
assertEquals("any_file", output)
assertEquals(7, summary.result)
}
}