forked from scallop/scallop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubcommandsTest.scala
367 lines (311 loc) · 9.53 KB
/
SubcommandsTest.scala
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package org.rogach.scallop
import org.scalatest.funsuite.AnyFunSuite
import org.rogach.scallop._
import org.rogach.scallop.exceptions._
class SubcommandsTest extends UsefulMatchers {
throwError.value = true
test ("builder") {
val sub = Scallop()
.opt[Boolean]("bananas")
val opts = Scallop()
.opt[Boolean]("apples")
.addSubBuilder(Seq("tree"),sub)
.args(Seq("-a","tree","-b"))
.verify
opts.get("apples") should equal (Some(true))
opts.get("tree\u0000bananas") should equal (Some(true))
opts.getSubcommandName should equal (Some("tree"))
}
test ("conf") {
object Conf extends ScallopConf(Seq("-a", "tree", "-b")) {
val apples = opt[Boolean]("apples")
val tree = new Subcommand("tree") {
val bananas = opt[Boolean]("bananas")
}
addSubcommand(tree)
verify()
}
Conf.apples() should equal (true)
Conf.apples.isSupplied ==== true
Conf.subcommand ==== Some(Conf.tree)
Conf.subcommands ==== List(Conf.tree)
Conf.tree.bananas() should equal (true)
Conf.tree.bananas.isSupplied ==== true
}
test ("options are not supplied") {
object Conf extends ScallopConf(Seq()) {
val apples = opt[Boolean]("apples")
val tree = new Subcommand("tree") {
val bananas = opt[Boolean]("bananas")
}
addSubcommand(tree)
verify()
}
Conf.apples.isSupplied ==== false
Conf.tree.bananas.isSupplied ==== false
}
test ("two nested configs") {
object Conf extends ScallopConf(Seq("palm","-b")) {
val tree = new Subcommand("tree") {
val apples = opt[Boolean]("apples")
}
addSubcommand(tree)
val palm = new Subcommand("palm") {
val bananas = opt[Boolean]("bananas")
}
addSubcommand(palm)
verify()
}
Conf.subcommand ==== Some(Conf.palm)
Conf.palm.bananas() ==== true
Conf.palm.bananas.isSupplied ==== true
// pattern matching on subcommands
(Conf.subcommand match {
case Some(Conf.tree) => false
case Some(Conf.palm) => true
case _ => false
}) ==== true
}
test ("4-level nested subcommands") {
object Conf extends ScallopConf(Seq("sub1", "sub2", "sub3", "sub4", "win!")) {
val sub1 = new Subcommand("sub1") {
val sub2 = new Subcommand("sub2") {
val sub3 = new Subcommand("sub3") {
val sub4 = new Subcommand("sub4") {
val opts = trailArg[List[String]]()
}
addSubcommand(sub4)
}
addSubcommand(sub3)
}
addSubcommand(sub2)
}
addSubcommand(sub1)
verify()
}
Conf.subcommands ==== List(Conf.sub1, Conf.sub1.sub2, Conf.sub1.sub2.sub3, Conf.sub1.sub2.sub3.sub4)
Conf.sub1.sub2.sub3.sub4.opts() ==== List("win!")
}
test ("equal names in two subcommands") {
object Conf extends ScallopConf(Seq("tree","-a")) {
val tree = new Subcommand("tree") {
val apples = opt[Boolean]("apples")
}
addSubcommand(tree)
val palm = new Subcommand("palm") {
val apples = opt[Boolean]("apples")
}
addSubcommand(palm)
verify()
}
Conf.tree.apples() ==== true
}
test ("codependency, inside subcommand, success") {
object Conf extends ScallopConf(Seq("tree", "-a", "-b")) {
val tree = new Subcommand("tree") {
val apples = opt[Boolean]("apples")
val bananas = opt[Boolean]("bananas")
codependent(apples, bananas)
}
addSubcommand(tree)
verify()
}
Conf.tree.apples() ==== true
Conf.tree.bananas() ==== true
}
test ("codependency, inside subcommand, failure") {
expectException(ValidationFailure("Either all or none of the following options should be supplied, because they are co-dependent: apples, bananas")) {
new ScallopConf(Seq("tree", "-a")) {
val tree = new Subcommand("tree") {
val apples = opt[Boolean]("apples")
val bananas = opt[Boolean]("bananas")
codependent(apples, bananas)
}
addSubcommand(tree)
verify()
}
}
}
test ("codependency, across confs, success") {
object Conf extends ScallopConf(Seq("-a", "tree", "-b")) {
val apples = opt[Boolean]("apples")
val tree = new Subcommand("tree") {
val bananas = opt[Boolean]("bananas")
}
addSubcommand(tree)
codependent(apples, tree.bananas)
verify()
}
Conf.apples() ==== true
Conf.tree.bananas() ==== true
}
test ("codependency, across confs, failure") {
expectException(ValidationFailure("Either all or none of the following options should be supplied, because they are co-dependent: apples, bananas")) {
new ScallopConf(Seq("-a", "tree")) {
val apples = opt[Boolean]("apples")
val tree = new Subcommand("tree") {
val bananas = opt[Boolean]("bananas")
}
addSubcommand(tree)
codependent(apples, tree.bananas)
verify()
}
}
}
test ("no-option subcommands") {
object Conf extends ScallopConf(Seq("tree")) {
val tree = new Subcommand("tree") {()}
addSubcommand(tree)
verify()
}
Conf.subcommand ==== Some(Conf.tree)
}
test ("isSupplied, if there are no selected subcommands") {
object Conf extends ScallopConf(Nil) {
val tree = new Subcommand("tree") {
val apples = opt[Int]("apples")
}
addSubcommand(tree)
verify()
}
Conf.tree.apples.isSupplied ==== false
}
test ("isSupplied, for other subcommand") {
object Conf extends ScallopConf(Seq("tree", "-a", "42")) {
val tree = new Subcommand("tree") {
val apples = opt[Int]("apples")
}
addSubcommand(tree)
val palm = new Subcommand("palm") {
val bananas = opt[Int]("bananas")
}
addSubcommand(palm)
verify()
}
Conf.palm.bananas.isSupplied ==== false
}
test ("subcommand name as parameter to other subcommand") {
object Conf extends ScallopConf(Seq("help", "tree")) {
val tree = new Subcommand("tree") {()}
addSubcommand(tree)
val help = new Subcommand("help") {
val command = trailArg[String]()
}
addSubcommand(help)
verify()
}
Conf.help.command() ==== "tree"
}
test ("properties on subcommand") {
object Conf extends ScallopConf(Seq("sub", "-Dkey1=value1")) {
val sub = new Subcommand("sub") {
val properties = props[String]('D')
}
addSubcommand(sub)
verify()
}
Conf.sub.properties ==== Map("key1" -> "value1")
}
test ("subcommand name guessing") {
val conf = new ScallopConf(Seq("tree", "--apples", "42")) {
val tree = new Subcommand("tree") {
val apples = opt[Int]()
}
addSubcommand(tree)
verify()
}
conf.tree.apples.toOption shouldBe Some(42)
}
test ("isSupplied on subcommand options - true") {
val conf = new ScallopConf(Seq("tree", "--apples")) {
val tree = new Subcommand("tree") {
val apples = opt[Boolean]()
}
addSubcommand(tree)
verify()
}
conf.tree.apples.isSupplied shouldBe true
}
test ("isSupplied on subcommand options - false") {
val conf = new ScallopConf(Seq("tree")) {
val tree = new Subcommand("tree") {
val apples = opt[Boolean]()
}
addSubcommand(tree)
verify()
}
conf.tree.apples.isSupplied shouldBe false
}
test ("subcommand aliases") {
object Conf extends ScallopConf(Seq("apple", "-c", "42")) {
val fruit = new Subcommand("fruit", "apple", "banana") {
val count = opt[Int]()
}
addSubcommand(fruit)
verify()
}
Conf.subcommand ==== Some(Conf.fruit)
Conf.subcommands ==== List(Conf.fruit)
Conf.fruit.count() shouldEqual 42
Conf.fruit.count.isSupplied ==== true
}
test ("require subcommand to be present - successfull case") {
object Conf extends ScallopConf(Seq("fruit", "-c", "42")) {
val fruit = new Subcommand("fruit") {
val count = opt[Int]()
}
addSubcommand(fruit)
requireSubcommand()
verify()
}
Conf.subcommand ==== Some(Conf.fruit)
Conf.fruit.count() shouldEqual 42
}
test ("require subcommand to be present - error case") {
expectException(ValidationFailure("Subcommand required")) {
object Conf extends ScallopConf(Seq()) {
val fruit = new Subcommand("fruit") {
val count = opt[Int]()
}
addSubcommand(fruit)
requireSubcommand()
verify()
}
Conf
}
}
test ("require several nested subcommands to be present - successfull case") {
object Conf extends ScallopConf(Seq("fruit", "pick", "-c", "42")) {
val fruit = new Subcommand("fruit") {
val pick = new Subcommand("pick") {
val count = opt[Int]()
}
addSubcommand(pick)
requireSubcommand()
}
addSubcommand(fruit)
requireSubcommand()
verify()
}
Conf.subcommand ==== Some(Conf.fruit)
Conf.subcommands ==== List(Conf.fruit, Conf.fruit.pick)
Conf.fruit.pick.count() shouldEqual 42
}
test ("require several nested subcommands to be present - error case") {
expectException(ValidationFailure("Subcommand required")) {
object Conf extends ScallopConf(Seq("fruit")) {
val fruit = new Subcommand("fruit") {
val pick = new Subcommand("pick") {
val count = opt[Int]()
}
addSubcommand(pick)
requireSubcommand()
}
addSubcommand(fruit)
requireSubcommand()
verify()
}
Conf
}
}
}