Skip to content

Commit

Permalink
Add unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
tattn committed Apr 8, 2019
1 parent e86b079 commit e2d3efc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Tests/DictionaryDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,15 @@ class DictionaryDecoderTests: XCTestCase {
XCTAssertNil(try? container.decode(Bool.self))
}
}

func testOptionalValues() throws {
struct Model: Codable, Equatable {
let int: Int?
let string: String?
let double: Double?
}

XCTAssertEqual(try decoder.decode(Model.self, from: ["int": 0, "string": "test"]), Model(int: 0, string: "test", double: nil))
XCTAssertEqual(try decoder.decode(Model.self, from: ["double": 0.5, "string": "test"]), Model(int: nil, string: "test", double: 0.5))
}
}
22 changes: 22 additions & 0 deletions Tests/DictionaryEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class DictionaryEncoderTests: XCTestCase {
(model: Model(int: 100, string: nil, double: 0.5), count: 2),
(model: Model(int: 100, string: "a", double: 0.5), count: 3),
]
do {
let dictionary = try encoder.encode(seeds[0].model)
XCTAssertNil(dictionary["int"])
XCTAssertNil(dictionary["string"])
XCTAssertNil(dictionary["double"])
}
for seed in seeds {
let dictionary = try encoder.encode(seed.model)
XCTAssertEqual(seed.model.int, dictionary["int"] as? Int)
Expand All @@ -77,4 +83,20 @@ class DictionaryEncoderTests: XCTestCase {
XCTAssertEqual(dictionary.keys.count, seed.count)
}
}

func testEncodeArray() throws {
XCTAssertThrowsError(try encoder.encode([1, 2, 3]))
}

func testEncodeAndDecode() throws {
struct Model: Codable, Equatable {
let int: Int?
let string: String?
let double: Double?
}
let value = Model(int: 1, string: nil, double: 0.5)
let encodedValue = try encoder.encode(value)
let decodedValue = try DictionaryDecoder().decode(Model.self, from: encodedValue)
XCTAssertEqual(value, decodedValue)
}
}

0 comments on commit e2d3efc

Please sign in to comment.