forked from scallop/scallop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValueConverterTest.scala
103 lines (85 loc) · 2.88 KB
/
ValueConverterTest.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
package org.rogach.scallop
import org.scalatest.FunSuite
import scala.reflect.runtime.universe.TypeTag
class ValueConverterTest extends FunSuite with UsefulMatchers {
throwError.value = true
test ("optional value - flatMap way") {
implicit def optConv[A](implicit tt: TypeTag[Option[A]], conv: ValueConverter[List[A]]) =
conv.flatMap {
case Nil => Right(None)
case v :: Nil => Right(Option(Option(v)))
case _ => Left("wrong option format")
}
def getcf(args: Seq[String]) = new ScallopConf(args) {
val foo = opt[Option[String]]()
verify()
}
val conf1 = getcf(Nil)
conf1.foo.get ==== None
val conf2 = getcf(List("-f"))
// bad corner case - flatMap doesn't apply when previous converter returned None
conf2.foo.get ==== None
val conf3 = getcf(List("-f", "bar"))
conf3.foo.get ==== Some(Some("bar"))
}
/** https://github.com/Rogach/scallop/issues/57 */
test("issue#57: WrongOptionFormat expected and no NoSuchElementException") {
import java.text.SimpleDateFormat
import java.util.{Date, GregorianCalendar}
import java.util.Calendar._
import org.rogach.scallop.exceptions.WrongOptionFormat
def d(s: String) = new SimpleDateFormat("yyyy-MM-dd").parse(s)
def getcf(args: Seq[String]) = new ScallopConf(args) {
implicit def dateConverter: ValueConverter[Date] = singleArgConverter[Date](new SimpleDateFormat("yyyyMMdd").parse(_))
val from = opt[Date]("from")
val to = opt[Date]("to")
validate(from, to) {
(f, t) =>
if (t after f) Right(Unit)
else Left("value of date to must be after date from")
}
verify()
}
expectException(WrongOptionFormat("from", "201305xx", "wrong arguments format")) {
getcf(List("-f", "201305xx", "-t", "20130515")).from()
}
}
test("optDefault - no call") {
val conf = new ScallopConf() {
val apples = opt[Int]()(optDefault(5))
verify()
}
conf.apples.get ==== None
conf.apples.isSupplied ==== false
}
test("optDefault - empty call") {
val conf = new ScallopConf(Seq("-a")) {
val apples = opt[Int]()(optDefault(5))
verify()
}
conf.apples.get ==== Some(5)
conf.apples.isSupplied ==== true
}
test("optDefault - arg provided") {
val conf = new ScallopConf(Seq("-a", "7")) {
val apples = opt[Int]()(optDefault(5))
verify()
}
conf.apples.get ==== Some(7)
conf.apples.isSupplied ==== true
}
test ("value converter is only called once when option is retrieved multiple times") {
var callCount = 0
val conf = new ScallopConf(Seq("-a", "1")) {
val apples = opt[Int]()(singleArgConverter { str =>
callCount += 1
str.toInt + 2
})
val banans = opt[Int]()
verify()
}
conf.apples.get shouldBe Some(3)
conf.apples.get shouldBe Some(3)
callCount shouldBe 1
}
}