forked from scallop/scallop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptionNameGuessing.scala
127 lines (105 loc) · 3.26 KB
/
OptionNameGuessing.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
package org.rogach.scallop
import org.rogach.scallop._
import org.rogach.scallop.exceptions._
class OptionNameGuessing extends UsefulMatchers {
throwError.value = true
test ("simple") {
object Conf extends ScallopConf(Seq("-a", "1")) {
val appleso = opt[Int]()
val bananaso = opt[Int]()
verify()
}
Conf.appleso() should equal (1)
Conf.bananaso.toOption should equal (None)
}
test ("shadowing with derived option") {
object Conf extends ScallopConf(Seq("--apples", "1")) {
val apples = opt[Int]()
val applesPlus = apples.map(2+)
verify()
}
Conf.apples() should equal (1)
Conf.applesPlus() should equal (3)
Conf.apples.isSupplied shouldEqual true
Conf.applesPlus.isSupplied shouldEqual true
}
test ("tricky") {
object Conf extends ScallopConf(Seq("-a", "1")) {
val appleso = opt[Int]()
val applesPlus = appleso.map(2+)
lazy val applesVal = appleso()
val bananaso = opt[Int]()
val aaa = opt[Int]()
verify()
}
Conf.appleso() should equal (1)
Conf.applesPlus() should equal (3)
Conf.bananaso.toOption should equal (None)
Conf.aaa.toOption should equal (None)
}
test ("camelCase convert") {
object Conf extends ScallopConf(Seq("--apple-treeo", "1")) {
val appleTreeo = opt[Int]()
verify()
}
Conf.appleTreeo() should equal (1)
}
test ("guessing in subcommands") {
object Conf extends ScallopConf(Seq("tree", "--apples", "3")) {
val tree = new Subcommand("tree") {
val apples = opt[Int]()
}
addSubcommand(tree)
verify()
}
Conf.tree.apples() should equal (3)
}
test ("replacing of the name in mapped ScallopOptions") {
object Conf extends ScallopConf(Nil) {
val xs = opt[Int](default = Some(0)) map (1+)
verify()
}
Conf.xs.toOption ==== Some(1)
}
test ("replacing of the name in doubly mapped ScallopOptions") {
object Conf extends ScallopConf(Nil) {
val xs = opt[Int](default = Some(0)) map (1+) map (_.toString)
verify()
}
Conf.xs.toOption ==== Some("1")
}
test("trailArg name guessing") {
object Conf extends ScallopConf(List("1", "foo", "bar", "baz", "bippy")) {
val trailing1 = trailArg[Int]()
val trailing2 = trailArg[List[String]]()
verify()
}
Conf.trailing1.name should be ("trailing1")
Conf.trailing2.name should be ("trailing2")
Conf.trailing1.toOption should be (Some(1))
Conf.trailing2.toOption should be (Some(List("foo", "bar", "baz", "bippy")))
}
test("toggle name guessing") {
object Conf extends ScallopConf(List("--foo", "--nobippy", "-s")) {
val foo = toggle()
val zoop = toggle()
val bippy = toggle()
val scooby = toggle()
verify()
}
Conf.foo.name should be ("foo")
Conf.zoop.name should be ("zoop")
Conf.bippy.name should be ("bippy")
Conf.foo.toOption should be (Some(true))
Conf.zoop.toOption should be (None)
Conf.bippy.toOption should be (Some(false))
Conf.scooby.toOption should be (Some(true))
}
test("decode special symbols") {
object Conf extends ScallopConf(List()) {
val `source-folder` = opt[String]()
verify()
}
Conf.`source-folder`.name shouldBe "source-folder"
}
}