@@ -4,7 +4,70 @@ import Nimble
4
4
import NIO
5
5
import XCTest
6
6
7
+ extension CodingUserInfoKey {
8
+ static let barInfo = CodingUserInfoKey ( rawValue: " bar " ) !
9
+ }
10
+
7
11
open class ExtendedJSONConversionTestCase : BSONTestCase {
12
+ func testExtendedJSONDecoderAndEncoder( ) throws {
13
+ // Setup
14
+ struct Test : Codable , Equatable {
15
+ let x : Bool
16
+ let y : Int32
17
+ let z : BSONRegularExpression
18
+ }
19
+
20
+ let regexStr = " { \" $regularExpression \" :{ \" pattern \" : \" p \" , \" options \" : \" i \" }} "
21
+ let extJSON = " { \" x \" :true, \" y \" :{ \" $numberInt \" : \" 5 \" }, \" z \" : \( regexStr) } "
22
+ let data = extJSON. data ( using: . utf8) !
23
+ let regexObj = BSONRegularExpression ( pattern: " p " , options: " i " )
24
+ let test = Test ( x: true , y: 5 , z: regexObj)
25
+
26
+ // Test canonical encoder
27
+ let encoder = ExtendedJSONEncoder ( )
28
+ encoder. mode = . canonical
29
+ let encoded : Data = try encoder. encode ( test)
30
+
31
+ let encodedStr = String ( data: encoded, encoding: . utf8)
32
+ expect ( encodedStr) . to ( contain ( " \" x \" :true " ) )
33
+ expect ( encodedStr) . to ( contain ( " \" y \" :{ \" $numberInt \" : \" 5 \" } " ) )
34
+ expect ( encodedStr) . to ( contain ( " \" z \" :{ \" $regularExpression \" " ) )
35
+ expect ( encodedStr) . to ( contain ( " \" pattern \" : \" p \" " ) )
36
+ expect ( encodedStr) . to ( contain ( " \" options \" : \" i \" " ) )
37
+
38
+ // Test relaxed encoder
39
+ encoder. mode = . relaxed
40
+ let relaxedEncoded : Data = try encoder. encode ( test)
41
+ let relaxedEncodedStr = String ( data: relaxedEncoded, encoding: . utf8)
42
+ expect ( relaxedEncodedStr) . to ( contain ( " \" x \" :true " ) )
43
+ expect ( relaxedEncodedStr) . to ( contain ( " \" y \" :5 " ) )
44
+
45
+ // Test decoder
46
+ let decoder = ExtendedJSONDecoder ( )
47
+ let decoded = try decoder. decode ( Test . self, from: data)
48
+ expect ( decoded) . to ( equal ( test) )
49
+ }
50
+
51
+ func testExtendedJSONEncodingWithUserInfo( ) throws {
52
+ struct Foo : Codable , Equatable {
53
+ func encode( to encoder: Encoder ) throws {
54
+ let barInfo = encoder. userInfo [ . barInfo] as? Bool
55
+ var container = encoder. singleValueContainer ( )
56
+ try container. encode ( [ barInfo] )
57
+ }
58
+ }
59
+
60
+ let encoder = ExtendedJSONEncoder ( )
61
+
62
+ encoder. userInfo [ . barInfo] = true
63
+ let fooBarEncoded = try encoder. encode ( Foo ( ) )
64
+ expect ( String ( data: fooBarEncoded, encoding: . utf8) ) . to ( contain ( " true " ) )
65
+
66
+ encoder. userInfo [ . barInfo] = false
67
+ let fooEncoded = try encoder. encode ( Foo ( ) )
68
+ expect ( String ( data: fooEncoded, encoding: . utf8) ) . to ( contain ( " false " ) )
69
+ }
70
+
8
71
func testAnyExtJSON( ) throws {
9
72
// Success cases
10
73
expect ( try BSON ( fromExtJSON: " hello " , keyPath: [ ] ) ) . to ( equal ( BSON . string ( " hello " ) ) )
@@ -14,19 +77,6 @@ open class ExtendedJSONConversionTestCase: BSONTestCase {
14
77
expect ( document. documentValue![ " extra " ] ) . to ( equal ( . int32( 1 ) ) )
15
78
}
16
79
17
- func testExtendedJSONDecoder( ) throws {
18
- struct Foo : Decodable , Equatable {
19
- let x : Bool
20
- let y : Int
21
- let z : BSONRegularExpression
22
- }
23
- let regexStr = " { \" $regularExpression \" : { \" pattern \" : \" p \" , \" options \" : \" i \" }} "
24
- let regexObj = BSONRegularExpression ( pattern: " p " , options: " i " )
25
- let data = " { \" x \" : true, \" y \" : { \" $numberInt \" : \" 5 \" }, \" z \" : \( regexStr) } " . data ( using: . utf8) !
26
- let decoder = ExtendedJSONDecoder ( )
27
- expect ( try decoder. decode ( Foo . self, from: data) ) . to ( equal ( Foo ( x: true , y: 5 , z: regexObj) ) )
28
- }
29
-
30
80
func testObjectId( ) throws {
31
81
let oid = " 5F07445CFBBBBBBBBBFAAAAA "
32
82
0 commit comments