2018-12-21 08:45:51 -08:00
|
|
|
//
|
|
|
|
|
// EncodedAttributeTest.swift
|
|
|
|
|
// JSONAPITests
|
|
|
|
|
//
|
|
|
|
|
// Created by Mathew Polzin on 12/21/18.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import XCTest
|
|
|
|
|
@testable import JSONAPI
|
2019-01-13 18:35:20 -08:00
|
|
|
import JSONAPITesting
|
2018-12-21 08:45:51 -08:00
|
|
|
|
2018-12-29 23:07:14 -08:00
|
|
|
private struct TransformedWrapper<Value: Equatable & Codable, Transform: Transformer>: Codable where Value == Transform.From {
|
2018-12-21 08:45:51 -08:00
|
|
|
let x: TransformedAttribute<Value, Transform>
|
|
|
|
|
|
|
|
|
|
init(x: TransformedAttribute<Value, Transform>) {
|
|
|
|
|
self.x = x
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 23:07:14 -08:00
|
|
|
private struct Wrapper<Value: Equatable & Codable>: Codable {
|
|
|
|
|
let x: Attribute<Value>
|
|
|
|
|
|
|
|
|
|
init(x: Attribute<Value>) {
|
|
|
|
|
self.x = x
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 08:45:51 -08:00
|
|
|
/// This function attempts to just cast to the type, so it only works
|
2018-12-21 08:53:55 -08:00
|
|
|
/// for Attributes of primitive types (primitive to JSON).
|
2018-12-21 08:45:51 -08:00
|
|
|
func testEncodedPrimitive<Value: Equatable & Codable, Transform: Transformer>(attribute: TransformedAttribute<Value, Transform>) {
|
2018-12-29 23:07:14 -08:00
|
|
|
let encodedAttributeData = encoded(value: TransformedWrapper<Value, Transform>(x: attribute))
|
2018-12-21 08:45:51 -08:00
|
|
|
let wrapperObject = try! JSONSerialization.jsonObject(with: encodedAttributeData, options: []) as! [String: Any]
|
|
|
|
|
let jsonObject = wrapperObject["x"]
|
|
|
|
|
|
2019-11-15 08:30:17 -08:00
|
|
|
XCTAssert(jsonObject is Transform.From)
|
2018-12-21 08:45:51 -08:00
|
|
|
|
2019-11-15 08:30:17 -08:00
|
|
|
XCTAssertEqual(attribute.rawValue, jsonObject as? Transform.From)
|
2018-12-21 08:45:51 -08:00
|
|
|
}
|
2018-12-29 23:07:14 -08:00
|
|
|
|
|
|
|
|
/// This function attempts to just cast to the type, so it only works
|
|
|
|
|
/// for Attributes of primitive types (primitive to JSON).
|
|
|
|
|
func testEncodedPrimitive<Value: Equatable & Codable>(attribute: Attribute<Value>) {
|
|
|
|
|
let encodedAttributeData = encoded(value: Wrapper<Value>(x: attribute))
|
|
|
|
|
let wrapperObject = try! JSONSerialization.jsonObject(with: encodedAttributeData, options: []) as! [String: Any]
|
|
|
|
|
let jsonObject = wrapperObject["x"]
|
|
|
|
|
|
2019-11-15 08:30:17 -08:00
|
|
|
XCTAssert(jsonObject is Value)
|
2018-12-29 23:07:14 -08:00
|
|
|
|
2019-11-15 08:30:17 -08:00
|
|
|
XCTAssertEqual(attribute.value, jsonObject as? Value)
|
2018-12-29 23:07:14 -08:00
|
|
|
}
|