mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
Add tests around nullable attributes and remove unnecessary encoding code
This commit is contained in:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user