-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMonocleSpec.scala
57 lines (43 loc) · 1.39 KB
/
MonocleSpec.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
import reactivemongo.api.bson._
final class MonocleSpec extends org.specs2.mutable.Specification {
"Monocle".title
// Requires import
import reactivemongo.api.bson.monocle._
val barDoc = BSONDocument("lorem" -> 2, "ipsum" -> BSONDocument("dolor" -> 3))
val topDoc = BSONDocument("foo" -> 1, "bar" -> barDoc)
"Lens" should {
"manage simple field" >> {
"as BSON value" in {
val lens = field[BSONInteger]("foo")
lens.set(BSONInteger(2))(topDoc) must_=== (topDoc ++ ("foo" -> 2))
}
"as supported value" in {
val lens = field[Float]("foo")
lens.set(2.3F)(topDoc) must_=== (topDoc ++ ("foo" -> 2.3F))
}
}
"manage nested field" >> {
"using composition" in {
val lens =
field[BSONDocument]("bar") composeOptional field[Double]("lorem")
lens.set(1.23D)(
topDoc
) must_=== (topDoc ++ ("bar" -> (barDoc ++ ("lorem" -> 1.23D))))
}
"using operator '\\'" in {
val lens = "bar" \ field[String]("lorem")
lens.set("VALUE")(
topDoc
) must_=== (topDoc ++ ("bar" -> (barDoc ++ ("lorem" -> "VALUE"))))
}
}
"manage deep field" in {
val lens = "bar" \ "ipsum" \ field[Long]("dolor")
lens.set(4L)(
topDoc
) must_=== (topDoc ++ ("bar" -> (barDoc ++ ("ipsum" -> BSONDocument(
"dolor" -> 4L
)))))
}
}
}