@@ -41,6 +41,18 @@ class Test_Map_JSON: XCTestCase, PBTestHelpers {
41
41
// Decode should work same regardless of order
42
42
assertJSONDecodeSucceeds ( " { \" mapInt32Int32 \" :{ \" 1 \" :2, \" 3 \" :4}} " ) { $0. mapInt32Int32 == [ 1 : 2 , 3 : 4 ] }
43
43
assertJSONDecodeSucceeds ( " { \" mapInt32Int32 \" :{ \" 3 \" :4, \" 1 \" :2}} " ) { $0. mapInt32Int32 == [ 1 : 2 , 3 : 4 ] }
44
+ // In range values succeed
45
+ assertJSONDecodeSucceeds ( " { \" mapInt32Int32 \" :{ \" 2147483647 \" :2147483647}} " ) {
46
+ $0. mapInt32Int32 == [ 2147483647 : 2147483647 ]
47
+ }
48
+ assertJSONDecodeSucceeds ( " { \" mapInt32Int32 \" :{ \" -2147483648 \" :-2147483648}} " ) {
49
+ $0. mapInt32Int32 == [ - 2147483648 : - 2147483648 ]
50
+ }
51
+ // Out of range values fail
52
+ assertJSONDecodeFails ( " { \" mapInt32Int32 \" :{ \" 2147483647 \" :2147483648}} " )
53
+ assertJSONDecodeFails ( " { \" mapInt32Int32 \" :{ \" 2147483648 \" :2147483647}} " )
54
+ assertJSONDecodeFails ( " { \" mapInt32Int32 \" :{ \" -2147483649 \" :2147483647}} " )
55
+ assertJSONDecodeFails ( " { \" mapInt32Int32 \" :{ \" 2147483647 \" :-2147483649}} " )
44
56
// JSON RFC does not allow trailing comma
45
57
assertJSONDecodeFails ( " { \" mapInt32Int32 \" :{ \" 3 \" :4, \" 1 \" :2,}} " )
46
58
// Int values should support being quoted or unquoted
@@ -61,54 +73,137 @@ class Test_Map_JSON: XCTestCase, PBTestHelpers {
61
73
assertJSONEncode ( " { \" mapInt64Int64 \" :{ \" 1 \" : \" 2 \" }} " ) { ( o: inout MessageTestType ) in
62
74
o. mapInt64Int64 = [ 1 : 2 ]
63
75
}
76
+ assertJSONEncode ( " { \" mapInt64Int64 \" :{ \" 9223372036854775807 \" : \" -9223372036854775808 \" }} " ) { ( o: inout MessageTestType ) in
77
+ o. mapInt64Int64 = [ 9223372036854775807 : - 9223372036854775808 ]
78
+ }
79
+ assertJSONDecodeSucceeds ( " { \" mapInt64Int64 \" :{ \" 9223372036854775807 \" :-9223372036854775808}} " ) {
80
+ $0. mapInt64Int64 == [ 9223372036854775807 : - 9223372036854775808 ]
81
+ }
82
+ assertJSONDecodeFails ( " { \" mapInt64Int64 \" :{ \" 9223372036854775807 \" :9223372036854775808}} " )
64
83
}
65
84
66
85
func testMapUInt32UInt32( ) throws {
67
86
assertJSONEncode ( " { \" mapUint32Uint32 \" :{ \" 1 \" :2}} " ) { ( o: inout MessageTestType ) in
68
87
o. mapUint32Uint32 = [ 1 : 2 ]
69
88
}
89
+ assertJSONDecodeFails ( " { \" mapUint32Uint32 \" :{ \" 1 \" :-2}} " )
90
+ assertJSONDecodeFails ( " { \" mapUint32Uint32 \" :{ \" -1 \" :2}} " )
91
+ assertJSONDecodeFails ( " { \" mapUint32Uint32 \" :{1:2}} " )
92
+ assertJSONDecodeSucceeds ( " { \" mapUint32Uint32 \" :{ \" 1 \" : \" 2 \" }} " ) {
93
+ $0. mapUint32Uint32 == [ 1 : 2 ]
94
+ }
70
95
}
71
96
72
97
func testMapUInt64UInt64( ) throws {
73
98
assertJSONEncode ( " { \" mapUint64Uint64 \" :{ \" 1 \" : \" 2 \" }} " ) { ( o: inout MessageTestType ) in
74
99
o. mapUint64Uint64 = [ 1 : 2 ]
75
100
}
101
+ assertJSONEncode ( " { \" mapUint64Uint64 \" :{ \" 1 \" : \" 18446744073709551615 \" }} " ) { ( o: inout MessageTestType ) in
102
+ o. mapUint64Uint64 = [ 1 : 18446744073709551615 as UInt64 ]
103
+ }
104
+ assertJSONDecodeSucceeds ( " { \" mapUint64Uint64 \" :{ \" 1 \" :18446744073709551615}} " ) {
105
+ $0. mapUint64Uint64 == [ 1 : 18446744073709551615 as UInt64 ]
106
+ }
107
+ assertJSONDecodeFails ( " { \" mapUint64Uint64 \" :{ \" 1 \" : \" 18446744073709551616 \" }} " )
108
+ assertJSONDecodeFails ( " { \" mapUint64Uint64 \" :{1: \" 18446744073709551615 \" }} " )
76
109
}
77
110
78
111
func testMapSInt32SInt32( ) throws {
79
112
assertJSONEncode ( " { \" mapSint32Sint32 \" :{ \" 1 \" :2}} " ) { ( o: inout MessageTestType ) in
80
113
o. mapSint32Sint32 = [ 1 : 2 ]
81
114
}
115
+ assertJSONDecodeSucceeds ( " { \" mapSint32Sint32 \" :{ \" 1 \" : \" -2 \" }} " ) {
116
+ $0. mapSint32Sint32 == [ 1 : - 2 ]
117
+ }
118
+ assertJSONDecodeFails ( " { \" mapSint32Sint32 \" :{1:-2}} " )
119
+ // In range values succeed
120
+ assertJSONDecodeSucceeds ( " { \" mapSint32Sint32 \" :{ \" 2147483647 \" :2147483647}} " ) {
121
+ $0. mapSint32Sint32 == [ 2147483647 : 2147483647 ]
122
+ }
123
+ assertJSONDecodeSucceeds ( " { \" mapSint32Sint32 \" :{ \" -2147483648 \" :-2147483648}} " ) {
124
+ $0. mapSint32Sint32 == [ - 2147483648 : - 2147483648 ]
125
+ }
126
+ // Out of range values fail
127
+ assertJSONDecodeFails ( " { \" mapSint32Sint32 \" :{ \" 2147483647 \" :2147483648}} " )
128
+ assertJSONDecodeFails ( " { \" mapSint32Sint32 \" :{ \" 2147483648 \" :2147483647}} " )
129
+ assertJSONDecodeFails ( " { \" mapSint32Sint32 \" :{ \" -2147483649 \" :2147483647}} " )
130
+ assertJSONDecodeFails ( " { \" mapSint32Sint32 \" :{ \" 2147483647 \" :-2147483649}} " )
82
131
}
83
132
84
133
func testMapSInt64SInt64( ) throws {
85
134
assertJSONEncode ( " { \" mapSint64Sint64 \" :{ \" 1 \" : \" 2 \" }} " ) { ( o: inout MessageTestType ) in
86
135
o. mapSint64Sint64 = [ 1 : 2 ]
87
136
}
137
+ assertJSONEncode ( " { \" mapSint64Sint64 \" :{ \" 9223372036854775807 \" : \" -9223372036854775808 \" }} " ) { ( o: inout MessageTestType ) in
138
+ o. mapSint64Sint64 = [ 9223372036854775807 : - 9223372036854775808 ]
139
+ }
140
+ assertJSONDecodeSucceeds ( " { \" mapSint64Sint64 \" :{ \" 9223372036854775807 \" :-9223372036854775808}} " ) {
141
+ $0. mapSint64Sint64 == [ 9223372036854775807 : - 9223372036854775808 ]
142
+ }
143
+ assertJSONDecodeFails ( " { \" mapSint64Sint64 \" :{ \" 9223372036854775807 \" :9223372036854775808}} " )
88
144
}
89
145
90
146
func testFixed32Fixed32( ) throws {
91
147
assertJSONEncode ( " { \" mapFixed32Fixed32 \" :{ \" 1 \" :2}} " ) { ( o: inout MessageTestType ) in
92
148
o. mapFixed32Fixed32 = [ 1 : 2 ]
93
149
}
150
+ assertJSONEncode ( " { \" mapFixed32Fixed32 \" :{ \" 0 \" :0}} " ) { ( o: inout MessageTestType ) in
151
+ o. mapFixed32Fixed32 = [ 0 : 0 ]
152
+ }
153
+ // In range values succeed
154
+ assertJSONDecodeSucceeds ( " { \" mapFixed32Fixed32 \" :{ \" 4294967295 \" :4294967295}} " ) {
155
+ $0. mapFixed32Fixed32 == [ 4294967295 : 4294967295 ]
156
+ }
157
+ // Out of range values fail
158
+ assertJSONDecodeFails ( " { \" mapFixed32Fixed32 \" :{ \" 4294967295 \" :4294967296}} " )
159
+ assertJSONDecodeFails ( " { \" mapFixed32Fixed32 \" :{ \" 4294967296 \" :4294967295}} " )
160
+ assertJSONDecodeFails ( " { \" mapFixed32Fixed32 \" :{ \" -1 \" :4294967295}} " )
161
+ assertJSONDecodeFails ( " { \" mapFixed32Fixed32 \" :{ \" 4294967295 \" :-1}} " )
94
162
}
95
163
96
164
func testFixed64Fixed64( ) throws {
97
165
assertJSONEncode ( " { \" mapFixed64Fixed64 \" :{ \" 1 \" : \" 2 \" }} " ) { ( o: inout MessageTestType ) in
98
166
o. mapFixed64Fixed64 = [ 1 : 2 ]
99
167
}
168
+ assertJSONEncode ( " { \" mapFixed64Fixed64 \" :{ \" 1 \" : \" 18446744073709551615 \" }} " ) { ( o: inout MessageTestType ) in
169
+ o. mapFixed64Fixed64 = [ 1 : 18446744073709551615 as UInt64 ]
170
+ }
171
+ assertJSONDecodeSucceeds ( " { \" mapFixed64Fixed64 \" :{ \" 1 \" :18446744073709551615}} " ) {
172
+ $0. mapFixed64Fixed64 == [ 1 : 18446744073709551615 as UInt64 ]
173
+ }
174
+ assertJSONDecodeFails ( " { \" mapFixed64Fixed64 \" :{ \" 1 \" : \" 18446744073709551616 \" }} " )
175
+ assertJSONDecodeFails ( " { \" mapFixed64Fixed64 \" :{1: \" 18446744073709551615 \" }} " )
100
176
}
101
177
102
178
func testSFixed32SFixed32( ) throws {
103
179
assertJSONEncode ( " { \" mapSfixed32Sfixed32 \" :{ \" 1 \" :2}} " ) { ( o: inout MessageTestType ) in
104
180
o. mapSfixed32Sfixed32 = [ 1 : 2 ]
105
181
}
182
+ // In range values succeed
183
+ assertJSONDecodeSucceeds ( " { \" mapSfixed32Sfixed32 \" :{ \" 2147483647 \" :2147483647}} " ) {
184
+ $0. mapSfixed32Sfixed32 == [ 2147483647 : 2147483647 ]
185
+ }
186
+ assertJSONDecodeSucceeds ( " { \" mapSfixed32Sfixed32 \" :{ \" -2147483648 \" :-2147483648}} " ) {
187
+ $0. mapSfixed32Sfixed32 == [ - 2147483648 : - 2147483648 ]
188
+ }
189
+ // Out of range values fail
190
+ assertJSONDecodeFails ( " { \" mapSfixed32Sfixed32 \" :{ \" 2147483647 \" :2147483648}} " )
191
+ assertJSONDecodeFails ( " { \" mapSfixed32Sfixed32 \" :{ \" 2147483648 \" :2147483647}} " )
192
+ assertJSONDecodeFails ( " { \" mapSfixed32Sfixed32 \" :{ \" -2147483649 \" :2147483647}} " )
193
+ assertJSONDecodeFails ( " { \" mapSfixed32Sfixed32 \" :{ \" 2147483647 \" :-2147483649}} " )
106
194
}
107
195
108
196
func testSFixed64SFixed64( ) throws {
109
197
assertJSONEncode ( " { \" mapSfixed64Sfixed64 \" :{ \" 1 \" : \" 2 \" }} " ) { ( o: inout MessageTestType ) in
110
198
o. mapSfixed64Sfixed64 = [ 1 : 2 ]
111
199
}
200
+ assertJSONEncode ( " { \" mapSfixed64Sfixed64 \" :{ \" 9223372036854775807 \" : \" -9223372036854775808 \" }} " ) { ( o: inout MessageTestType ) in
201
+ o. mapSfixed64Sfixed64 = [ 9223372036854775807 : - 9223372036854775808 ]
202
+ }
203
+ assertJSONDecodeSucceeds ( " { \" mapSfixed64Sfixed64 \" :{ \" 9223372036854775807 \" :-9223372036854775808}} " ) {
204
+ $0. mapSfixed64Sfixed64 == [ 9223372036854775807 : - 9223372036854775808 ]
205
+ }
206
+ assertJSONDecodeFails ( " { \" mapSfixed64Sfixed64 \" :{ \" 9223372036854775807 \" :9223372036854775808}} " )
112
207
}
113
208
114
209
func test_mapInt32Float( ) {
@@ -119,6 +214,10 @@ class Test_Map_JSON: XCTestCase, PBTestHelpers {
119
214
assertJSONEncode ( " { \" mapInt32Float \" :{ \" 1 \" :1}} " ) {
120
215
$0. mapInt32Float [ 1 ] = Float ( 1.0 )
121
216
}
217
+
218
+ assertJSONDecodeSucceeds ( " { \" mapInt32Float \" :{ \" 1 \" :3.141592}} " ) {
219
+ $0. mapInt32Float [ 1 ] == 3.141592 as Float
220
+ }
122
221
}
123
222
124
223
func test_mapInt32Double( ) {
@@ -130,6 +229,9 @@ class Test_Map_JSON: XCTestCase, PBTestHelpers {
130
229
$0. mapInt32Double [ 1 ] = Double ( 1.0 )
131
230
}
132
231
232
+ assertJSONDecodeSucceeds ( " { \" mapInt32Double \" :{ \" 1 \" :3.141592}} " ) {
233
+ $0. mapInt32Double [ 1 ] == 3.141592
234
+ }
133
235
}
134
236
135
237
func test_mapBoolBool( ) {
0 commit comments