Add tests around nullable attributes and remove unnecessary encoding code

This commit is contained in:
Mathew Polzin
2018-12-08 20:04:11 -08:00
parent c9d388579f
commit 8f279ce191
3 changed files with 25 additions and 9 deletions
-9
View File
@@ -80,15 +80,6 @@ extension TransformedAttribute {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
// See note in decode above about the weirdness
// going on here.
// let anyNil: Any? = nil
// let nilRawValue = anyNil as? Transformer.From
// guard rawValue != nilRawValue else {
// try container.encodeNil()
// return
// }
try container.encode(rawValue)
}
@@ -29,6 +29,29 @@ class AttributeTests: XCTestCase {
func test_TransformedAttributeReversNoThrow() {
XCTAssertNoThrow(try TransformedAttribute<String, TestTransformer>(transformedValue: 10))
}
func test_NullableIsNullIfNil() {
struct Wrapper: Codable {
let dummy: Attribute<String?>
}
let data = encoded(value: Wrapper(dummy: .init(value: nil)))
let string = String(data: data, encoding: .utf8)!
XCTAssertEqual(string, "{\"dummy\":null}")
}
func test_NullableIsEqualToNonNullableIfNotNil() {
struct Wrapper1: Codable {
let dummy: Attribute<String?>
}
struct Wrapper2: Codable {
let dummy: Attribute<String>
}
let data1 = encoded(value: Wrapper1(dummy: .init(value: "hello")))
let data2 = encoded(value: Wrapper2(dummy: .init(value: "hello")))
XCTAssertEqual(data1, data2)
}
}
// MARK: Test types
@@ -207,6 +207,8 @@ extension EntityTests {
XCTAssertNil(entity[\.maybeHere])
XCTAssertNil(entity[\.maybeNull])
XCTAssertNoThrow(try TestEntity6.check(entity))
print(encodable: entity)
}
func test_entityOneNullAndOneOmittedAttribute_encode() {