Skip to content

Commit

Permalink
Added More tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Feb 20, 2018
1 parent 209a8b6 commit 9b409a2
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions Tests/DictionaryCodingTests/DictionaryCodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -56,16 +69,57 @@ 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
let decoder = DictionaryDecoder()
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),
]
}

0 comments on commit 9b409a2

Please sign in to comment.