Skip to content

Commit e2d3efc

Browse files
committed
Add unittests
1 parent e86b079 commit e2d3efc

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

Tests/DictionaryDecoderTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,15 @@ class DictionaryDecoderTests: XCTestCase {
5252
XCTAssertNil(try? container.decode(Bool.self))
5353
}
5454
}
55+
56+
func testOptionalValues() throws {
57+
struct Model: Codable, Equatable {
58+
let int: Int?
59+
let string: String?
60+
let double: Double?
61+
}
62+
63+
XCTAssertEqual(try decoder.decode(Model.self, from: ["int": 0, "string": "test"]), Model(int: 0, string: "test", double: nil))
64+
XCTAssertEqual(try decoder.decode(Model.self, from: ["double": 0.5, "string": "test"]), Model(int: nil, string: "test", double: 0.5))
65+
}
5566
}

Tests/DictionaryEncoderTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ class DictionaryEncoderTests: XCTestCase {
6969
(model: Model(int: 100, string: nil, double: 0.5), count: 2),
7070
(model: Model(int: 100, string: "a", double: 0.5), count: 3),
7171
]
72+
do {
73+
let dictionary = try encoder.encode(seeds[0].model)
74+
XCTAssertNil(dictionary["int"])
75+
XCTAssertNil(dictionary["string"])
76+
XCTAssertNil(dictionary["double"])
77+
}
7278
for seed in seeds {
7379
let dictionary = try encoder.encode(seed.model)
7480
XCTAssertEqual(seed.model.int, dictionary["int"] as? Int)
@@ -77,4 +83,20 @@ class DictionaryEncoderTests: XCTestCase {
7783
XCTAssertEqual(dictionary.keys.count, seed.count)
7884
}
7985
}
86+
87+
func testEncodeArray() throws {
88+
XCTAssertThrowsError(try encoder.encode([1, 2, 3]))
89+
}
90+
91+
func testEncodeAndDecode() throws {
92+
struct Model: Codable, Equatable {
93+
let int: Int?
94+
let string: String?
95+
let double: Double?
96+
}
97+
let value = Model(int: 1, string: nil, double: 0.5)
98+
let encodedValue = try encoder.encode(value)
99+
let decodedValue = try DictionaryDecoder().decode(Model.self, from: encodedValue)
100+
XCTAssertEqual(value, decodedValue)
101+
}
80102
}

0 commit comments

Comments
 (0)