@@ -187,6 +187,12 @@ class FunctionsSerializerTests: XCTestCase {
187
187
XCTAssertEqual ( input, try serializer. encode ( input) as? NSArray )
188
188
}
189
189
190
+ func testEncodeArrayWithInvalidElements( ) {
191
+ let input = [ " TEST " , CustomObject ( ) ] as NSArray
192
+
193
+ try assert ( serializer. encode ( input) , throwsUnsupportedTypeErrorWithName: " CustomObject " )
194
+ }
195
+
190
196
func testDecodeArray( ) throws {
191
197
let input = [
192
198
1 as Int64 ,
@@ -198,7 +204,13 @@ class FunctionsSerializerTests: XCTestCase {
198
204
XCTAssertEqual ( expected, try serializer. decode ( input) as? NSArray )
199
205
}
200
206
201
- func testEncodeMap( ) {
207
+ func testDecodeArrayWithInvalidElements( ) {
208
+ let input = [ " TEST " , CustomObject ( ) ] as NSArray
209
+
210
+ try assert ( serializer. decode ( input) , throwsUnsupportedTypeErrorWithName: " CustomObject " )
211
+ }
212
+
213
+ func testEncodeDictionary( ) throws {
202
214
let input = [
203
215
" foo " : 1 as Int32 ,
204
216
" bar " : " hello " ,
@@ -213,12 +225,38 @@ class FunctionsSerializerTests: XCTestCase {
213
225
XCTAssertEqual ( expected, try serializer. encode ( input) as? NSDictionary )
214
226
}
215
227
216
- func testDecodeMap( ) {
228
+ func testEncodeDictionaryWithInvalidElements( ) {
229
+ let input = [ " TEST_CustomObj " : CustomObject ( ) ] as NSDictionary
230
+
231
+ try assert ( serializer. encode ( input) , throwsUnsupportedTypeErrorWithName: " CustomObject " )
232
+ }
233
+
234
+ func testEncodeDictionaryWithInvalidNestedDictionary( ) {
235
+ let input =
236
+ [ " TEST_NestedDict " : [ " TEST_CustomObj " : CustomObject ( ) ] as NSDictionary ] as NSDictionary
237
+
238
+ try assert ( serializer. encode ( input) , throwsUnsupportedTypeErrorWithName: " CustomObject " )
239
+ }
240
+
241
+ func testDecodeDictionary( ) throws {
217
242
let input = [ " foo " : 1 , " bar " : " hello " , " baz " : [ 3 , 9_876_543_210 ] ] as NSDictionary
218
243
let expected = [ " foo " : 1 , " bar " : " hello " , " baz " : [ 3 , 9_876_543_210 ] ] as NSDictionary
219
244
XCTAssertEqual ( expected, try serializer. decode ( input) as? NSDictionary )
220
245
}
221
246
247
+ func testDecodeDictionaryWithInvalidElements( ) {
248
+ let input = [ " TEST_CustomObj " : CustomObject ( ) ] as NSDictionary
249
+
250
+ try assert ( serializer. decode ( input) , throwsUnsupportedTypeErrorWithName: " CustomObject " )
251
+ }
252
+
253
+ func testDecodeDictionaryWithInvalidNestedDictionary( ) {
254
+ let input =
255
+ [ " TEST_NestedDict " : [ " TEST_CustomObj " : CustomObject ( ) ] as NSDictionary ] as NSDictionary
256
+
257
+ try assert ( serializer. decode ( input) , throwsUnsupportedTypeErrorWithName: " CustomObject " )
258
+ }
259
+
222
260
func testEncodeUnknownType( ) {
223
261
let input = [ " @type " : " unknown " , " value " : " whatever " ] as NSDictionary
224
262
XCTAssertEqual ( input, try serializer. encode ( input) as? NSDictionary )
@@ -237,37 +275,34 @@ class FunctionsSerializerTests: XCTestCase {
237
275
func testEncodeUnsupportedType( ) {
238
276
let input = CustomObject ( )
239
277
240
- do {
241
- let _ = try serializer. encode ( input)
242
- XCTFail ( " Expected an error " )
243
- } catch {
244
- guard case let . unsupportedType( typeName: typeName) = error as? FunctionsSerializer . Error
245
- else {
246
- return XCTFail ( " Unexpected error: \( error) " )
247
- }
248
-
249
- XCTAssertEqual ( typeName, " CustomObject " )
250
- }
278
+ try assert ( serializer. encode ( input) , throwsUnsupportedTypeErrorWithName: " CustomObject " )
251
279
}
252
280
253
281
func testDecodeUnsupportedType( ) {
254
282
let input = CustomObject ( )
255
283
256
- do {
257
- let _ = try serializer. decode ( input)
258
- XCTFail ( " Expected an error " )
259
- } catch {
260
- guard case let . unsupportedType( typeName: typeName) = error as? FunctionsSerializer . Error
261
- else {
262
- return XCTFail ( " Unexpected error: \( error) " )
284
+ try assert ( serializer. decode ( input) , throwsUnsupportedTypeErrorWithName: " CustomObject " )
285
+ }
286
+ }
287
+
288
+ // MARK: - Utilities
289
+
290
+ extension FunctionsSerializerTests {
291
+ private func assert< T> ( _ expression: @autoclosure ( ) throws -> T ,
292
+ throwsUnsupportedTypeErrorWithName expectedTypeName: String ,
293
+ line: UInt = #line) {
294
+ XCTAssertThrowsError ( try expression ( ) , line: line) { error in
295
+ guard case let . unsupportedType( typeName: typeName) = error as? FunctionsSerializer
296
+ . Error else {
297
+ return XCTFail ( " Unexpected error: \( error) " , line: line)
263
298
}
264
299
265
- XCTAssertEqual ( typeName, " CustomObject " )
300
+ XCTAssertEqual ( typeName, expectedTypeName , line : line )
266
301
}
267
302
}
268
303
}
269
304
270
305
/// Used to represent a type that cannot be encoded or decoded.
271
- private struct CustomObject {
306
+ private class CustomObject {
272
307
let id = 123
273
308
}
0 commit comments