mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
test some attribute encoding a bit further
This commit is contained in:
@@ -11,7 +11,7 @@ public protocol AttributeType: Codable {
|
||||
// MARK: TransformedAttribute
|
||||
/// A TransformedAttribute takes a Codable type and attempts to turn it into another type.
|
||||
public struct TransformedAttribute<RawValue: Codable, Transformer: JSONAPI.Transformer>: AttributeType where Transformer.From == RawValue {
|
||||
private let rawValue: RawValue
|
||||
let rawValue: RawValue
|
||||
|
||||
public let value: Transformer.To
|
||||
|
||||
|
||||
@@ -30,6 +30,18 @@ class AttributeTests: XCTestCase {
|
||||
XCTAssertNoThrow(try TransformedAttribute<String, TestTransformer>(transformedValue: 10))
|
||||
}
|
||||
|
||||
func test_EncodedPrimitives() {
|
||||
testEncodedPrimitive(attribute: Attribute<Int>(value: 10))
|
||||
testEncodedPrimitive(attribute: Attribute<Bool>(value: false))
|
||||
testEncodedPrimitive(attribute: Attribute<Double>(value: 10.2))
|
||||
|
||||
testEncodedPrimitive(attribute: try! TransformedAttribute<Int, IntToString>(rawValue: 10))
|
||||
testEncodedPrimitive(attribute: try! TransformedAttribute<Int, IntToInt>(rawValue: 10))
|
||||
testEncodedPrimitive(attribute: try! TransformedAttribute<Int, IntToDouble>(rawValue: 10))
|
||||
testEncodedPrimitive(attribute: try! TransformedAttribute<String, TestTransformer>(rawValue: "10"))
|
||||
testEncodedPrimitive(attribute: try! TransformedAttribute<String?, OptionalToString<String>>(rawValue: "10"))
|
||||
}
|
||||
|
||||
func test_NullableIsNullIfNil() {
|
||||
struct Wrapper: Codable {
|
||||
let dummy: Attribute<String?>
|
||||
@@ -68,4 +80,28 @@ extension AttributeTests {
|
||||
return String(value)
|
||||
}
|
||||
}
|
||||
|
||||
enum IntToString: Transformer {
|
||||
public static func transform(_ from: Int) -> String {
|
||||
return String(from)
|
||||
}
|
||||
}
|
||||
|
||||
enum IntToInt: Transformer {
|
||||
public static func transform(_ from: Int) -> Int {
|
||||
return from + 100
|
||||
}
|
||||
}
|
||||
|
||||
enum IntToDouble: Transformer {
|
||||
public static func transform(_ from: Int) -> Double {
|
||||
return Double(from)
|
||||
}
|
||||
}
|
||||
|
||||
enum OptionalToString<T>: Transformer {
|
||||
public static func transform(_ from: T?) -> String {
|
||||
return String(describing: from)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// EncodedAttributeTest.swift
|
||||
// JSONAPITests
|
||||
//
|
||||
// Created by Mathew Polzin on 12/21/18.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import XCTest
|
||||
@testable import JSONAPI
|
||||
import JSONAPITestLib
|
||||
|
||||
private struct Wrapper<Value: Equatable & Codable, Transform: Transformer>: Codable where Value == Transform.From {
|
||||
let x: TransformedAttribute<Value, Transform>
|
||||
|
||||
init(x: TransformedAttribute<Value, Transform>) {
|
||||
self.x = x
|
||||
}
|
||||
}
|
||||
|
||||
/// This function attempts to just cast to the type, so it only works
|
||||
/// for Attributes of primitive types.
|
||||
func testEncodedPrimitive<Value: Equatable & Codable, Transform: Transformer>(attribute: TransformedAttribute<Value, Transform>) {
|
||||
let encodedAttributeData = encoded(value: Wrapper<Value, Transform>(x: attribute))
|
||||
let wrapperObject = try! JSONSerialization.jsonObject(with: encodedAttributeData, options: []) as! [String: Any]
|
||||
let jsonObject = wrapperObject["x"]
|
||||
|
||||
guard let jsonAttribute = jsonObject as? Transform.From else {
|
||||
XCTFail("Attribute did not encode to the correct type")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(attribute.rawValue, jsonAttribute)
|
||||
}
|
||||
@@ -49,20 +49,3 @@ func testEncoded<E: EntityType>(entity: E) {
|
||||
XCTAssertNotNil(jsonLinks)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Extensions to help with identifying structure of Mirror
|
||||
private protocol OptionalAttributeType {}
|
||||
extension Optional: OptionalAttributeType where Wrapped: AttributeType {}
|
||||
|
||||
private protocol OptionalArray {}
|
||||
extension Optional: OptionalArray where Wrapped: ArrayType {}
|
||||
|
||||
private protocol AttributeTypeWithOptionalArray {}
|
||||
extension TransformedAttribute: AttributeTypeWithOptionalArray where RawValue: OptionalArray {}
|
||||
|
||||
private protocol OptionalRelationshipType {}
|
||||
extension Optional: OptionalRelationshipType where Wrapped: RelationshipType {}
|
||||
|
||||
private protocol _RelationshipType {}
|
||||
extension ToOneRelationship: _RelationshipType {}
|
||||
extension ToManyRelationship: _RelationshipType {}
|
||||
|
||||
Reference in New Issue
Block a user