From 5fa9848f0270fb7fcc8a7d1d1683d769e61a5356 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Fri, 21 Dec 2018 08:45:51 -0800 Subject: [PATCH] test some attribute encoding a bit further --- Sources/JSONAPI/Resource/Attribute.swift | 2 +- .../Attribute/AttributeTests.swift | 36 +++++++++++++++++++ .../Test Helpers/EncodedAttributeTest.swift | 34 ++++++++++++++++++ .../EncodedEntityPropertyTest.swift | 17 --------- 4 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 Tests/JSONAPITests/Test Helpers/EncodedAttributeTest.swift diff --git a/Sources/JSONAPI/Resource/Attribute.swift b/Sources/JSONAPI/Resource/Attribute.swift index e59b3f9..823a8af 100644 --- a/Sources/JSONAPI/Resource/Attribute.swift +++ b/Sources/JSONAPI/Resource/Attribute.swift @@ -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: AttributeType where Transformer.From == RawValue { - private let rawValue: RawValue + let rawValue: RawValue public let value: Transformer.To diff --git a/Tests/JSONAPITests/Attribute/AttributeTests.swift b/Tests/JSONAPITests/Attribute/AttributeTests.swift index 06a9c5d..4c5710a 100644 --- a/Tests/JSONAPITests/Attribute/AttributeTests.swift +++ b/Tests/JSONAPITests/Attribute/AttributeTests.swift @@ -30,6 +30,18 @@ class AttributeTests: XCTestCase { XCTAssertNoThrow(try TransformedAttribute(transformedValue: 10)) } + func test_EncodedPrimitives() { + testEncodedPrimitive(attribute: Attribute(value: 10)) + testEncodedPrimitive(attribute: Attribute(value: false)) + testEncodedPrimitive(attribute: Attribute(value: 10.2)) + + testEncodedPrimitive(attribute: try! TransformedAttribute(rawValue: 10)) + testEncodedPrimitive(attribute: try! TransformedAttribute(rawValue: 10)) + testEncodedPrimitive(attribute: try! TransformedAttribute(rawValue: 10)) + testEncodedPrimitive(attribute: try! TransformedAttribute(rawValue: "10")) + testEncodedPrimitive(attribute: try! TransformedAttribute>(rawValue: "10")) + } + func test_NullableIsNullIfNil() { struct Wrapper: Codable { let dummy: Attribute @@ -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: Transformer { + public static func transform(_ from: T?) -> String { + return String(describing: from) + } + } } diff --git a/Tests/JSONAPITests/Test Helpers/EncodedAttributeTest.swift b/Tests/JSONAPITests/Test Helpers/EncodedAttributeTest.swift new file mode 100644 index 0000000..de0a502 --- /dev/null +++ b/Tests/JSONAPITests/Test Helpers/EncodedAttributeTest.swift @@ -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: Codable where Value == Transform.From { + let x: TransformedAttribute + + init(x: TransformedAttribute) { + self.x = x + } +} + +/// This function attempts to just cast to the type, so it only works +/// for Attributes of primitive types. +func testEncodedPrimitive(attribute: TransformedAttribute) { + let encodedAttributeData = encoded(value: Wrapper(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) +} diff --git a/Tests/JSONAPITests/Test Helpers/EncodedEntityPropertyTest.swift b/Tests/JSONAPITests/Test Helpers/EncodedEntityPropertyTest.swift index 22752a9..b623ca8 100644 --- a/Tests/JSONAPITests/Test Helpers/EncodedEntityPropertyTest.swift +++ b/Tests/JSONAPITests/Test Helpers/EncodedEntityPropertyTest.swift @@ -49,20 +49,3 @@ func testEncoded(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 {}