From 9b409a28f813371483c57b149c44df37f8d4cc2a Mon Sep 17 00:00:00 2001 From: Sam Deane Date: Tue, 20 Feb 2018 15:04:56 +0000 Subject: [PATCH] Added More tests. --- .../DictionaryCodingTests.swift | 60 ++++++++++++++++++- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/Tests/DictionaryCodingTests/DictionaryCodingTests.swift b/Tests/DictionaryCodingTests/DictionaryCodingTests.swift index 1838250..e06ce48 100644 --- a/Tests/DictionaryCodingTests/DictionaryCodingTests.swift +++ b/Tests/DictionaryCodingTests/DictionaryCodingTests.swift @@ -11,16 +11,29 @@ struct Person : Codable { let pets : [Pet] } +struct Test : Codable { + let name : String + let label : String? +} + class DictionaryCodingTests: XCTestCase { - func testEncoding() throws { + func testEncodingAsNSDictionary() throws { let test = Person(name: "Sam", age: 48, pets:[Pet(name: "Morven"), Pet(name: "Rebus")]) let encoder = DictionaryEncoder() - let encoded = try encoder.encode(test) + let encoded = try encoder.encode(test) as NSDictionary XCTAssertEqual(encoded["name"] as? String, "Sam") XCTAssertEqual(encoded["age"] as? Int, 48) } + func testEncodingAsSwiftDictionary() throws { + let test = Person(name: "Sam", age: 48, pets:[Pet(name: "Morven"), Pet(name: "Rebus")]) + let encoder = DictionaryEncoder() + let encoded = try encoder.encode(test) as [String:Any] + XCTAssertEqual(encoded["name"] as? String, "Sam") + XCTAssertEqual(encoded["age"] as? Int, 48) + } + func testDecodingNSDictionary() throws { let pet1 : NSMutableDictionary = NSMutableDictionary() pet1["name"] = "Morven" @@ -56,6 +69,19 @@ class DictionaryCodingTests: XCTestCase { XCTAssertEqual(decoded.pets[0].name, "Morven") XCTAssertEqual(decoded.pets[1].name, "Rebus") } + + func testDecodingSwiftDictionary() throws { + let dict : [String:Any] = [ "name" : "Sam", "age" : 48, "pets" : [ ["name" : "Morven"], ["name" : "Rebus"]]] + + let decoder = DictionaryDecoder() + let decoded = try decoder.decode(Person.self, from: dict) + + XCTAssertEqual(decoded.name, "Sam") + XCTAssertEqual(decoded.age, 48) + XCTAssertEqual(decoded.pets.count, 2) + XCTAssertEqual(decoded.pets[0].name, "Morven") + XCTAssertEqual(decoded.pets[1].name, "Rebus") + } func testFailureWithMissingKeys() { let dict = [ "name" : "Sam", "age" : 48 ] as NSDictionary @@ -63,9 +89,37 @@ class DictionaryCodingTests: XCTestCase { XCTAssertThrowsError(try decoder.decode(Person.self, from: dict)) } + func testDecodingOptionalValues() throws { + // the dictionary is missing some keys, but decoding shouldn't fail + // as they correspond to properties that are optional in the struct + + let dict : [String:Any] = [ "name" : "Sam" ] + + let decoder = DictionaryDecoder() + let decoded = try decoder.decode(Test.self, from: dict) + + XCTAssertEqual(decoded.name, "Sam") + XCTAssertNil(decoded.label) + } + + func testEncodingOptionalValues() throws { + // the struct's optional values should not get written into the dictionary + // if they are nil + + let test = Test(name: "Sam", label: nil) + let encoder = DictionaryEncoder() + let encoded = try encoder.encode(test) as NSDictionary + XCTAssertEqual(encoded["name"] as? String, "Sam") + XCTAssertEqual(encoded.allKeys.count, 1) + } + static var allTests = [ - ("testEncoding", testEncoding), + ("testEncodingAsNSDictionary", testEncodingAsNSDictionary), + ("testEncodingAsSwiftDictionary", testEncodingAsSwiftDictionary), ("testDecodingNSDictionary", testDecodingNSDictionary), ("testDecodingCFDictionary", testDecodingCFDictionary), + ("testFailureWithMissingKeys", testFailureWithMissingKeys), + ("testDecodingOptionalValues", testDecodingOptionalValues), + ("testEncodingOptionalValues", testEncodingOptionalValues), ] }