-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProfigSpec.scala
173 lines (162 loc) · 5.02 KB
/
ProfigSpec.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
package spec
import profig._
import fabric._
import fabric.io.{Format, JsonParser}
import fabric.rw._
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import scala.language.implicitConversions
class ProfigSpec extends AnyWordSpec with Matchers {
"Profig" should {
"init" in {
Profig.reset()
Profig.init()
}
"verify classloading not set" in {
Profig("test.classloading").opt[String] should be(None)
}
"verify files not set" in {
Profig("test.files").opt[String] should be(None)
}
"verify `opt` usage" in {
Profig("test.files").store("yes")
Profig("test.files").opt[String] should be(Some("yes"))
}
"verify `as` with default" in {
Profig("test.files").asOr[String]("no") should be("yes")
Profig("test.other").asOr[String]("no") should be("no")
}
"merge arguments" in {
Profig.merge(List("-this.is.an.argument", "Wahoo!"))
}
"load a String argument" in {
Profig("this.is.an.argument").as[String] should be("Wahoo!")
Profig("this")("is")("an")("argument").as[String] should be("Wahoo!")
}
"load JVM information from properties" in {
val info = Profig("java").as[JVMInfo]
info.specification.vendor should be("Oracle Corporation")
}
"store a single String" in {
Profig("people", "me", "name").store("Matt")
}
"load a case class from a path with default arguments" in {
val person = Profig("people.me").as[Person]
person should be(Person("Matt", None))
}
"storage a case class" in {
Profig("people", "john").store(Person("John Doe", Some(123)))
}
"load the stored case class from path" in {
val person = Profig("people")("john").as[Person]
person should be(Person("John Doe", Some(123)))
}
"load an optional value that is not there" in {
val value = Profig("this.does.not.exist").opt[String]
value should be(None)
}
"verify that test.value was loaded" in {
Profig("test.value").store(true)
val value = Profig("test.value").opt[Boolean]
value should be(Some(true))
}
"remove a value" in {
Profig("people.john.age").opt[Int] should be(Some(123))
Profig("people.john.age").remove()
Profig("people.john.age").opt[Int] should be(None)
}
"add a value back" in {
Profig("people.john.age").opt[Int] should be(None)
Profig("people.john.age").store(321)
Profig("people.john.age").opt[Int] should be(Some(321))
}
"see no spill-over in orphaned Profig" in {
val orphan = Profig.empty
orphan("people.john.age").opt[Int] should be(None)
}
"validate loading a String value of true as Boolean" in {
Profig("test.boolean").merge(bool(true))
Profig("test.boolean").as[Boolean] should be(true)
}
"validate overwrite" in {
val profig = Profig.empty
profig.json should be(obj())
profig.merge(obj(
"test" -> "one"
), MergeType.Overwrite)
profig.json should be(obj("test" -> "one"))
profig.merge(obj(
"test" -> "two"
), MergeType.Overwrite)
profig.json should be(obj("test" -> "two"))
}
"validate add" in {
val profig = Profig.empty
profig.json should be(obj())
profig.merge(obj(
"test" -> "one"
), MergeType.Add)
profig.json should be(obj("test" -> "one"))
profig.merge(obj(
"test" -> "two"
), MergeType.Add)
profig.json should be(obj("test" -> "one"))
}
"merge two Json objects" in {
val json1 = JsonParser(
"""{
| "one": 1,
| "two": 2,
| "three": 3
|}""".stripMargin, Format.Json)
val json2 = JsonParser(
"""{
| "three": "tres",
| "four": "quatro",
| "five": "cinco"
|}""".stripMargin, Format.Json)
val merged = Json.merge(json1, json2)
merged should be(obj(
"one" -> 1,
"two" -> 2,
"three" -> "tres",
"four" -> "quatro",
"five" -> "cinco"
))
}
"map values" in {
val p = Profig.empty
p.merge(obj(
"one" -> obj(
"two" -> obj(
"three" -> 3
)
)
))
p("one").map(
"two.three" -> "threeValue",
"two.three.four" -> "fourValue"
)
p("one.threeValue").get() should be(Some(num(3)))
}
"load a value that doesn't exist from defaults" in {
val p = Profig("information").as[Information]
}
}
case class Person(name: String, age: Option[Int] = None)
object Person {
implicit val rw: RW[Person] = RW.gen
}
case class JVMInfo(version: String, specification: Specification)
object JVMInfo {
implicit val rw: RW[JVMInfo] = RW.gen
}
case class Specification(vendor: String, name: String, version: String)
object Specification {
implicit val rw: RW[Specification] = RW.gen
}
case class Information(description: String = "default")
object Information {
implicit val rw: RW[Information] = RW.gen
}
}