1
1
package io .github .weakteam .mongo .bson
2
2
3
- import io .github .weakteam .mongo .bson .BsonValue .BsonInt
3
+ import java .util .regex .Pattern
4
+
5
+ import io .github .weakteam .mongo .bson .BsonValue ._
4
6
import org .scalatest .matchers .must .Matchers
5
7
import org .scalatest .wordspec .AnyWordSpec
6
8
import io .github .weakteam .mongo .implicits ._
7
9
import cats .syntax .contravariant ._
8
10
11
+ import scala .util .matching .Regex
12
+
9
13
class BsonWriterSpec extends AnyWordSpec with Matchers {
10
14
" BsonWriter" should {
11
15
" return right value" in {
12
- implicit val intBsonWriter : BsonWriter [Int ] = BsonInt (_)
13
-
14
- 1 .writeBson mustBe BsonInt (1 )
16
+ BsonWriter .instance(BsonInt ).writeBson(1 ) mustBe BsonInt (1 )
15
17
}
16
18
17
19
" return right value with contramap" in {
18
- implicit val intBsonWriter : BsonWriter [Int ] = BsonInt (_)
19
- implicit val anyBsonWriter : BsonWriter [Any ] = intBsonWriter.contramap {
20
+ implicit val anyBsonWriter : BsonWriter [Any ] = BsonWriter .intWriter.contramap {
20
21
case i : Int => i
21
22
case _ => 0
22
23
}
@@ -27,9 +28,85 @@ class BsonWriterSpec extends AnyWordSpec with Matchers {
27
28
}
28
29
29
30
" return right value with manual instance creation" in {
30
- implicit val intBsonWriter : BsonWriter [Int ] = BsonWriter .instance(BsonInt )
31
+ val intBsonWriter : BsonWriter [Int ] = BsonWriter .instance(BsonInt )
31
32
32
- 1 .writeBson mustBe BsonInt (1 )
33
+ intBsonWriter .writeBson( 1 ) mustBe BsonInt (1 )
33
34
}
34
35
}
36
+
37
+ " BsonWriter#instances" should {
38
+ " write identity" in {
39
+ BsonWriter [BsonValue ].writeBson(BsonString (" bson" )) mustBe BsonString (" bson" )
40
+ }
41
+
42
+ " write int" in {
43
+ BsonWriter [Int ].writeBson(42 ) mustBe BsonInt (42 )
44
+ }
45
+
46
+ " write long" in {
47
+ BsonWriter [Long ].writeBson(42L ) mustBe BsonLong (42L )
48
+ }
49
+
50
+ " write boolean" in {
51
+ BsonWriter [Boolean ].writeBson(true ) mustBe BsonBoolean (true )
52
+ }
53
+
54
+ " write double" in {
55
+ BsonWriter [Double ].writeBson(42.52d ) mustBe BsonFloat (42.52d )
56
+ }
57
+
58
+ " write float" in {
59
+ BsonWriter [Float ].writeBson(42.52f ) mustBe BsonFloat (42.52f )
60
+ }
61
+
62
+ " write regex" in {
63
+ BsonWriter [Regex ].writeBson(" foo" .r) mustBe BsonRegex (" foo" )
64
+ }
65
+
66
+ " write pattern" in {
67
+ BsonWriter [Pattern ].writeBson(" foo" .r.pattern) mustBe BsonRegex (" foo" )
68
+ }
69
+ }
70
+
71
+ " BsonWriter#wrapped-instances" should {
72
+ implicit def implConv [A : BsonWriter ](value : A ): BsonValue = value.writeBson
73
+
74
+ " write option" in {
75
+ BsonWriter [Option [Int ]].writeBson(Some (4 )) mustBe BsonInt (4 )
76
+ BsonWriter [Option [Int ]].writeBson(None ) mustBe BsonNull
77
+ }
78
+
79
+ " write either" in {
80
+ BsonWriter [Either [Boolean , String ]].writeBson(Right (" foo" )) mustBe BsonString (" foo" )
81
+ BsonWriter [Either [Boolean , String ]].writeBson(Left (true )) mustBe BsonBoolean (true )
82
+ }
83
+
84
+ " write list" in {
85
+ BsonWriter [List [Int ]].writeBson(List (1 , 4 , 5 )) mustBe BsonArray (1 , 4 , 5 )
86
+ }
87
+
88
+ " write set" in {
89
+ BsonWriter [Set [String ]].writeBson(Set (" foo" , " foo" , " bar" )) mustBe BsonArray (" foo" , " bar" )
90
+ }
91
+
92
+ " write map" in {
93
+ implicit val stringBsonKeyWriter : BsonKeyWriter [String ] = identity(_)
94
+
95
+ BsonWriter [Map [String , Int ]]
96
+ .writeBson(Map (" foo" -> 3 , " foo" -> 4 , " bar" -> 5 )) mustBe BsonDocument (" foo" := 4 , " bar" := 5 )
97
+ }
98
+
99
+ // "write Enum" in {
100
+ // def wr: BsonWriter[TestEnum.Value] = TestEnum
101
+ // wr.writeBson(TestEnum.A) mustBe BsonBinary(UserDefinedSubtype(0x05), 1.toString.getBytes("UTF-8"))
102
+ //
103
+ // wr.writeBson(TestEnum.B) mustBe BsonBinary(UserDefinedSubtype(0x05), 2.toString.getBytes("UTF-8"))
104
+ // }
105
+ }
35
106
}
107
+
108
+ // object BsonWriterSpec {
109
+ // object TestEnum extends Enumeration {
110
+ // val A, B = Value
111
+ // }
112
+ // }
0 commit comments