mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
Add support for Poly8/Poly9 and Include8/Include9
This commit is contained in:
@@ -123,3 +123,17 @@ extension Includes where I: _Poly7 {
|
||||
}
|
||||
|
||||
// MARK: - 8 includes
|
||||
public typealias Include8 = Poly8
|
||||
extension Includes where I: _Poly8 {
|
||||
public subscript(_ lookup: I.H.Type) -> [I.H] {
|
||||
return values.compactMap { $0.h }
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 9 includes
|
||||
public typealias Include9 = Poly9
|
||||
extension Includes where I: _Poly9 {
|
||||
public subscript(_ lookup: I.I.Type) -> [I.I] {
|
||||
return values.compactMap { $0.i }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -799,3 +799,360 @@ extension Poly7: CustomStringConvertible {
|
||||
return "Poly(\(str))"
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 8 types
|
||||
public protocol _Poly8: _Poly7 {
|
||||
associatedtype H: EntityType
|
||||
var h: H? { get }
|
||||
|
||||
init(_ h: H)
|
||||
}
|
||||
|
||||
public extension _Poly8 {
|
||||
subscript(_ lookup: H.Type) -> H? {
|
||||
return h
|
||||
}
|
||||
}
|
||||
|
||||
public enum Poly8<A: EntityType, B: EntityType, C: EntityType, D: EntityType, E: EntityType, F: EntityType, G: EntityType, H: EntityType>: _Poly8 {
|
||||
case a(A)
|
||||
case b(B)
|
||||
case c(C)
|
||||
case d(D)
|
||||
case e(E)
|
||||
case f(F)
|
||||
case g(G)
|
||||
case h(H)
|
||||
|
||||
public var a: A? {
|
||||
guard case let .a(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ a: A) {
|
||||
self = .a(a)
|
||||
}
|
||||
|
||||
public var b: B? {
|
||||
guard case let .b(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ b: B) {
|
||||
self = .b(b)
|
||||
}
|
||||
|
||||
public var c: C? {
|
||||
guard case let .c(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ c: C) {
|
||||
self = .c(c)
|
||||
}
|
||||
|
||||
public var d: D? {
|
||||
guard case let .d(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ d: D) {
|
||||
self = .d(d)
|
||||
}
|
||||
|
||||
public var e: E? {
|
||||
guard case let .e(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ e: E) {
|
||||
self = .e(e)
|
||||
}
|
||||
|
||||
public var f: F? {
|
||||
guard case let .f(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ f: F) {
|
||||
self = .f(f)
|
||||
}
|
||||
|
||||
public var g: G? {
|
||||
guard case let .g(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ g: G) {
|
||||
self = .g(g)
|
||||
}
|
||||
|
||||
public var h: H? {
|
||||
guard case let .h(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ h: H) {
|
||||
self = .h(h)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
|
||||
let attempts = [
|
||||
try decode(A.self, from: container).map { Poly8.a($0) },
|
||||
try decode(B.self, from: container).map { Poly8.b($0) },
|
||||
try decode(C.self, from: container).map { Poly8.c($0) },
|
||||
try decode(D.self, from: container).map { Poly8.d($0) },
|
||||
try decode(E.self, from: container).map { Poly8.e($0) },
|
||||
try decode(F.self, from: container).map { Poly8.f($0) },
|
||||
try decode(G.self, from: container).map { Poly8.g($0) },
|
||||
try decode(H.self, from: container).map { Poly8.h($0) }]
|
||||
|
||||
let maybeVal: Poly8<A, B, C, D, E, F, G, H>? = attempts
|
||||
.compactMap { $0.value }
|
||||
.first
|
||||
|
||||
guard let val = maybeVal else {
|
||||
throw EncodingError.invalidValue(Poly8<A, B, C, D, E, F, G, H>.self, .init(codingPath: decoder.codingPath,
|
||||
debugDescription: "Failed to find an include of the expected type. Attempts: \(attempts.map { $0.error }.compactMap { $0 })"))
|
||||
}
|
||||
|
||||
self = val
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
|
||||
switch self {
|
||||
case .a(let a):
|
||||
try container.encode(a)
|
||||
case .b(let b):
|
||||
try container.encode(b)
|
||||
case .c(let c):
|
||||
try container.encode(c)
|
||||
case .d(let d):
|
||||
try container.encode(d)
|
||||
case .e(let e):
|
||||
try container.encode(e)
|
||||
case .f(let f):
|
||||
try container.encode(f)
|
||||
case .g(let g):
|
||||
try container.encode(g)
|
||||
case .h(let h):
|
||||
try container.encode(h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Poly8: CustomStringConvertible {
|
||||
public var description: String {
|
||||
let str: String
|
||||
switch self {
|
||||
case .a(let a):
|
||||
str = String(describing: a)
|
||||
case .b(let b):
|
||||
str = String(describing: b)
|
||||
case .c(let c):
|
||||
str = String(describing: c)
|
||||
case .d(let d):
|
||||
str = String(describing: d)
|
||||
case .e(let e):
|
||||
str = String(describing: e)
|
||||
case .f(let f):
|
||||
str = String(describing: f)
|
||||
case .g(let g):
|
||||
str = String(describing: g)
|
||||
case .h(let h):
|
||||
str = String(describing: h)
|
||||
}
|
||||
|
||||
return "Poly(\(str))"
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 9 types
|
||||
public protocol _Poly9: _Poly8 {
|
||||
associatedtype I: EntityType
|
||||
var i: I? { get }
|
||||
|
||||
init(_ i: I)
|
||||
}
|
||||
|
||||
public extension _Poly9 {
|
||||
subscript(_ lookup: I.Type) -> I? {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
public enum Poly9<A: EntityType, B: EntityType, C: EntityType, D: EntityType, E: EntityType, F: EntityType, G: EntityType, H: EntityType, I: EntityType>: _Poly9 {
|
||||
case a(A)
|
||||
case b(B)
|
||||
case c(C)
|
||||
case d(D)
|
||||
case e(E)
|
||||
case f(F)
|
||||
case g(G)
|
||||
case h(H)
|
||||
case i(I)
|
||||
|
||||
public var a: A? {
|
||||
guard case let .a(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ a: A) {
|
||||
self = .a(a)
|
||||
}
|
||||
|
||||
public var b: B? {
|
||||
guard case let .b(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ b: B) {
|
||||
self = .b(b)
|
||||
}
|
||||
|
||||
public var c: C? {
|
||||
guard case let .c(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ c: C) {
|
||||
self = .c(c)
|
||||
}
|
||||
|
||||
public var d: D? {
|
||||
guard case let .d(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ d: D) {
|
||||
self = .d(d)
|
||||
}
|
||||
|
||||
public var e: E? {
|
||||
guard case let .e(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ e: E) {
|
||||
self = .e(e)
|
||||
}
|
||||
|
||||
public var f: F? {
|
||||
guard case let .f(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ f: F) {
|
||||
self = .f(f)
|
||||
}
|
||||
|
||||
public var g: G? {
|
||||
guard case let .g(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ g: G) {
|
||||
self = .g(g)
|
||||
}
|
||||
|
||||
public var h: H? {
|
||||
guard case let .h(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ h: H) {
|
||||
self = .h(h)
|
||||
}
|
||||
|
||||
public var i: I? {
|
||||
guard case let .i(ret) = self else { return nil }
|
||||
return ret
|
||||
}
|
||||
|
||||
public init(_ i: I) {
|
||||
self = .i(i)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
|
||||
let attempts = [
|
||||
try decode(A.self, from: container).map { Poly9.a($0) },
|
||||
try decode(B.self, from: container).map { Poly9.b($0) },
|
||||
try decode(C.self, from: container).map { Poly9.c($0) },
|
||||
try decode(D.self, from: container).map { Poly9.d($0) },
|
||||
try decode(E.self, from: container).map { Poly9.e($0) },
|
||||
try decode(F.self, from: container).map { Poly9.f($0) },
|
||||
try decode(G.self, from: container).map { Poly9.g($0) },
|
||||
try decode(H.self, from: container).map { Poly9.h($0) },
|
||||
try decode(I.self, from: container).map { Poly9.i($0) }]
|
||||
|
||||
let maybeVal: Poly9<A, B, C, D, E, F, G, H, I>? = attempts
|
||||
.compactMap { $0.value }
|
||||
.first
|
||||
|
||||
guard let val = maybeVal else {
|
||||
throw EncodingError.invalidValue(Poly9<A, B, C, D, E, F, G, H, I>.self, .init(codingPath: decoder.codingPath,
|
||||
debugDescription: "Failed to find an include of the expected type. Attempts: \(attempts.map { $0.error }.compactMap { $0 })"))
|
||||
}
|
||||
|
||||
self = val
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
|
||||
switch self {
|
||||
case .a(let a):
|
||||
try container.encode(a)
|
||||
case .b(let b):
|
||||
try container.encode(b)
|
||||
case .c(let c):
|
||||
try container.encode(c)
|
||||
case .d(let d):
|
||||
try container.encode(d)
|
||||
case .e(let e):
|
||||
try container.encode(e)
|
||||
case .f(let f):
|
||||
try container.encode(f)
|
||||
case .g(let g):
|
||||
try container.encode(g)
|
||||
case .h(let h):
|
||||
try container.encode(h)
|
||||
case .i(let i):
|
||||
try container.encode(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Poly9: CustomStringConvertible {
|
||||
public var description: String {
|
||||
let str: String
|
||||
switch self {
|
||||
case .a(let a):
|
||||
str = String(describing: a)
|
||||
case .b(let b):
|
||||
str = String(describing: b)
|
||||
case .c(let c):
|
||||
str = String(describing: c)
|
||||
case .d(let d):
|
||||
str = String(describing: d)
|
||||
case .e(let e):
|
||||
str = String(describing: e)
|
||||
case .f(let f):
|
||||
str = String(describing: f)
|
||||
case .g(let g):
|
||||
str = String(describing: g)
|
||||
case .h(let h):
|
||||
str = String(describing: h)
|
||||
case .i(let i):
|
||||
str = String(describing: i)
|
||||
}
|
||||
|
||||
return "Poly(\(str))"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,6 +139,45 @@ class IncludedTests: XCTestCase {
|
||||
test_DecodeEncodeEquality(type: Includes<Include7<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7>>.self,
|
||||
data: seven_different_type_includes)
|
||||
}
|
||||
|
||||
func test_EightDifferentIncludes() {
|
||||
let includes = decoded(type: Includes<Include8<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8>>.self,
|
||||
data: eight_different_type_includes)
|
||||
|
||||
XCTAssertEqual(includes[TestEntity.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity2.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity3.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity4.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity5.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity6.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity7.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity8.self].count, 1)
|
||||
}
|
||||
|
||||
func test_EightDifferentIncludes_encode() {
|
||||
test_DecodeEncodeEquality(type: Includes<Include8<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8>>.self,
|
||||
data: eight_different_type_includes)
|
||||
}
|
||||
|
||||
func test_NineDifferentIncludes() {
|
||||
let includes = decoded(type: Includes<Include9<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9>>.self,
|
||||
data: nine_different_type_includes)
|
||||
|
||||
XCTAssertEqual(includes[TestEntity.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity2.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity3.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity4.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity5.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity6.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity7.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity8.self].count, 1)
|
||||
XCTAssertEqual(includes[TestEntity9.self].count, 1)
|
||||
}
|
||||
|
||||
func test_NineDifferentIncludes_encode() {
|
||||
test_DecodeEncodeEquality(type: Includes<Include9<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9>>.self,
|
||||
data: nine_different_type_includes)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Test types
|
||||
@@ -232,4 +271,26 @@ extension IncludedTests {
|
||||
}
|
||||
|
||||
typealias TestEntity7 = BasicEntity<TestEntityType7>
|
||||
|
||||
enum TestEntityType8: EntityDescription {
|
||||
|
||||
typealias Attributes = NoAttributes
|
||||
|
||||
public static var type: String { return "test_entity8" }
|
||||
|
||||
typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
typealias TestEntity8 = BasicEntity<TestEntityType8>
|
||||
|
||||
enum TestEntityType9: EntityDescription {
|
||||
|
||||
typealias Attributes = NoAttributes
|
||||
|
||||
public static var type: String { return "test_entity9" }
|
||||
|
||||
typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
typealias TestEntity9 = BasicEntity<TestEntityType9>
|
||||
}
|
||||
|
||||
@@ -354,3 +354,161 @@ let seven_different_type_includes = """
|
||||
}
|
||||
]
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let eight_different_type_includes = """
|
||||
[
|
||||
{
|
||||
"type": "test_entity1",
|
||||
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
|
||||
"attributes": {
|
||||
"foo": "Hello",
|
||||
"bar": 123
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "test_entity2",
|
||||
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
|
||||
"attributes": {
|
||||
"foo": "World",
|
||||
"bar": 456
|
||||
},
|
||||
"relationships": {
|
||||
"entity1": {
|
||||
"data": {
|
||||
"type": "test_entity1",
|
||||
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "test_entity3",
|
||||
"id": "11223B69-4DF1-467F-B52E-B0C9E44FC443",
|
||||
"relationships": {
|
||||
"entity1": {
|
||||
"data": {
|
||||
"type": "test_entity1",
|
||||
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
|
||||
}
|
||||
},
|
||||
"entity2": {
|
||||
"data": [
|
||||
{
|
||||
"type": "test_entity2",
|
||||
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "test_entity6",
|
||||
"id": "11113B69-4DF1-467F-B52E-B0C9E44FC444",
|
||||
"relationships": {
|
||||
"entity4": {
|
||||
"data": {
|
||||
"type": "test_entity4",
|
||||
"id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "test_entity5",
|
||||
"id": "A24B3B69-4DF1-467F-B52E-B0C9E44F436A"
|
||||
},
|
||||
{
|
||||
"type": "test_entity4",
|
||||
"id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E"
|
||||
},
|
||||
{
|
||||
"type": "test_entity7",
|
||||
"id": "364B3B69-4DF1-222F-B52E-B0C9E44F666E"
|
||||
},
|
||||
{
|
||||
"type": "test_entity8",
|
||||
"id": "364B3B69-4DF1-222F-B52E-B0C9E44F266F"
|
||||
}
|
||||
]
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let nine_different_type_includes = """
|
||||
[
|
||||
{
|
||||
"type": "test_entity1",
|
||||
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
|
||||
"attributes": {
|
||||
"foo": "Hello",
|
||||
"bar": 123
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "test_entity2",
|
||||
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
|
||||
"attributes": {
|
||||
"foo": "World",
|
||||
"bar": 456
|
||||
},
|
||||
"relationships": {
|
||||
"entity1": {
|
||||
"data": {
|
||||
"type": "test_entity1",
|
||||
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "test_entity3",
|
||||
"id": "11223B69-4DF1-467F-B52E-B0C9E44FC443",
|
||||
"relationships": {
|
||||
"entity1": {
|
||||
"data": {
|
||||
"type": "test_entity1",
|
||||
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
|
||||
}
|
||||
},
|
||||
"entity2": {
|
||||
"data": [
|
||||
{
|
||||
"type": "test_entity2",
|
||||
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "test_entity6",
|
||||
"id": "11113B69-4DF1-467F-B52E-B0C9E44FC444",
|
||||
"relationships": {
|
||||
"entity4": {
|
||||
"data": {
|
||||
"type": "test_entity4",
|
||||
"id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "test_entity5",
|
||||
"id": "A24B3B69-4DF1-467F-B52E-B0C9E44F436A"
|
||||
},
|
||||
{
|
||||
"type": "test_entity4",
|
||||
"id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E"
|
||||
},
|
||||
{
|
||||
"type": "test_entity7",
|
||||
"id": "364B3B69-4DF1-222F-B52E-B0C9E44F666E"
|
||||
},
|
||||
{
|
||||
"type": "test_entity8",
|
||||
"id": "364B3B69-4DF1-222F-B52E-B0C9E44F266F"
|
||||
},
|
||||
{
|
||||
"type": "test_entity9",
|
||||
"id": "364B3B69-4DF1-218F-B52E-B0C9E44F2661"
|
||||
}
|
||||
]
|
||||
""".data(using: .utf8)!
|
||||
|
||||
@@ -230,6 +230,191 @@ class PolyTests: XCTestCase {
|
||||
XCTAssertNil(poly7.e)
|
||||
XCTAssertNil(poly7.f)
|
||||
}
|
||||
|
||||
func test_init_Poly8() {
|
||||
let entity = TestEntity5(attributes: .none, relationships: .none, meta: .none, links: .none)
|
||||
let poly = Poly8<TestEntity5, TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity7, TestEntity8>(entity)
|
||||
XCTAssertEqual(poly.a, entity)
|
||||
XCTAssertNil(poly.b)
|
||||
XCTAssertNil(poly.c)
|
||||
XCTAssertNil(poly.d)
|
||||
XCTAssertNil(poly.e)
|
||||
XCTAssertNil(poly.f)
|
||||
XCTAssertNil(poly.g)
|
||||
XCTAssertNil(poly.h)
|
||||
|
||||
let poly2 = Poly8<TestEntity, TestEntity5, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity7, TestEntity8>(entity)
|
||||
XCTAssertEqual(poly2.b, entity)
|
||||
XCTAssertNil(poly2.a)
|
||||
XCTAssertNil(poly2.c)
|
||||
XCTAssertNil(poly2.d)
|
||||
XCTAssertNil(poly2.e)
|
||||
XCTAssertNil(poly2.f)
|
||||
XCTAssertNil(poly2.g)
|
||||
XCTAssertNil(poly2.h)
|
||||
|
||||
let poly3 = Poly8<TestEntity, TestEntity2, TestEntity5, TestEntity3, TestEntity4, TestEntity6, TestEntity7, TestEntity8>(entity)
|
||||
XCTAssertEqual(poly3.c, entity)
|
||||
XCTAssertNil(poly3.a)
|
||||
XCTAssertNil(poly3.b)
|
||||
XCTAssertNil(poly3.d)
|
||||
XCTAssertNil(poly3.e)
|
||||
XCTAssertNil(poly3.f)
|
||||
XCTAssertNil(poly3.g)
|
||||
XCTAssertNil(poly3.h)
|
||||
|
||||
let poly4 = Poly8<TestEntity, TestEntity2, TestEntity3, TestEntity5, TestEntity4, TestEntity6, TestEntity7, TestEntity8>(entity)
|
||||
XCTAssertEqual(poly4.d, entity)
|
||||
XCTAssertNil(poly4.a)
|
||||
XCTAssertNil(poly4.b)
|
||||
XCTAssertNil(poly4.c)
|
||||
XCTAssertNil(poly4.e)
|
||||
XCTAssertNil(poly4.f)
|
||||
XCTAssertNil(poly4.g)
|
||||
XCTAssertNil(poly4.h)
|
||||
|
||||
let poly5 = Poly8<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8>(entity)
|
||||
XCTAssertEqual(poly5.e, entity)
|
||||
XCTAssertNil(poly5.a)
|
||||
XCTAssertNil(poly5.b)
|
||||
XCTAssertNil(poly5.c)
|
||||
XCTAssertNil(poly5.d)
|
||||
XCTAssertNil(poly5.f)
|
||||
XCTAssertNil(poly5.g)
|
||||
XCTAssertNil(poly5.h)
|
||||
|
||||
let poly6 = Poly8<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity5, TestEntity7, TestEntity8>(entity)
|
||||
XCTAssertEqual(poly6.f, entity)
|
||||
XCTAssertNil(poly6.a)
|
||||
XCTAssertNil(poly6.b)
|
||||
XCTAssertNil(poly6.c)
|
||||
XCTAssertNil(poly6.d)
|
||||
XCTAssertNil(poly6.e)
|
||||
XCTAssertNil(poly6.g)
|
||||
XCTAssertNil(poly6.h)
|
||||
|
||||
let poly7 = Poly8<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity7, TestEntity5, TestEntity8>(entity)
|
||||
XCTAssertEqual(poly7.g, entity)
|
||||
XCTAssertNil(poly7.a)
|
||||
XCTAssertNil(poly7.b)
|
||||
XCTAssertNil(poly7.c)
|
||||
XCTAssertNil(poly7.d)
|
||||
XCTAssertNil(poly7.e)
|
||||
XCTAssertNil(poly7.f)
|
||||
XCTAssertNil(poly7.h)
|
||||
|
||||
let poly8 = Poly8<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity7, TestEntity8, TestEntity5>(entity)
|
||||
XCTAssertEqual(poly8.h, entity)
|
||||
XCTAssertNil(poly8.a)
|
||||
XCTAssertNil(poly8.b)
|
||||
XCTAssertNil(poly8.c)
|
||||
XCTAssertNil(poly8.d)
|
||||
XCTAssertNil(poly8.e)
|
||||
XCTAssertNil(poly8.f)
|
||||
XCTAssertNil(poly8.g)
|
||||
}
|
||||
|
||||
func test_init_Poly9() {
|
||||
let entity = TestEntity5(attributes: .none, relationships: .none, meta: .none, links: .none)
|
||||
let poly = Poly9<TestEntity5, TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity7, TestEntity8, TestEntity9>(entity)
|
||||
XCTAssertEqual(poly.a, entity)
|
||||
XCTAssertNil(poly.b)
|
||||
XCTAssertNil(poly.c)
|
||||
XCTAssertNil(poly.d)
|
||||
XCTAssertNil(poly.e)
|
||||
XCTAssertNil(poly.f)
|
||||
XCTAssertNil(poly.g)
|
||||
XCTAssertNil(poly.h)
|
||||
XCTAssertNil(poly.i)
|
||||
|
||||
let poly2 = Poly9<TestEntity, TestEntity5, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity7, TestEntity8, TestEntity9>(entity)
|
||||
XCTAssertEqual(poly2.b, entity)
|
||||
XCTAssertNil(poly2.a)
|
||||
XCTAssertNil(poly2.c)
|
||||
XCTAssertNil(poly2.d)
|
||||
XCTAssertNil(poly2.e)
|
||||
XCTAssertNil(poly2.f)
|
||||
XCTAssertNil(poly2.g)
|
||||
XCTAssertNil(poly2.h)
|
||||
XCTAssertNil(poly2.i)
|
||||
|
||||
let poly3 = Poly9<TestEntity, TestEntity2, TestEntity5, TestEntity3, TestEntity4, TestEntity6, TestEntity7, TestEntity8, TestEntity9>(entity)
|
||||
XCTAssertEqual(poly3.c, entity)
|
||||
XCTAssertNil(poly3.a)
|
||||
XCTAssertNil(poly3.b)
|
||||
XCTAssertNil(poly3.d)
|
||||
XCTAssertNil(poly3.e)
|
||||
XCTAssertNil(poly3.f)
|
||||
XCTAssertNil(poly3.g)
|
||||
XCTAssertNil(poly3.h)
|
||||
XCTAssertNil(poly3.i)
|
||||
|
||||
let poly4 = Poly9<TestEntity, TestEntity2, TestEntity3, TestEntity5, TestEntity4, TestEntity6, TestEntity7, TestEntity8, TestEntity9>(entity)
|
||||
XCTAssertEqual(poly4.d, entity)
|
||||
XCTAssertNil(poly4.a)
|
||||
XCTAssertNil(poly4.b)
|
||||
XCTAssertNil(poly4.c)
|
||||
XCTAssertNil(poly4.e)
|
||||
XCTAssertNil(poly4.f)
|
||||
XCTAssertNil(poly4.g)
|
||||
XCTAssertNil(poly4.h)
|
||||
XCTAssertNil(poly4.i)
|
||||
|
||||
let poly5 = Poly9<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9>(entity)
|
||||
XCTAssertEqual(poly5.e, entity)
|
||||
XCTAssertNil(poly5.a)
|
||||
XCTAssertNil(poly5.b)
|
||||
XCTAssertNil(poly5.c)
|
||||
XCTAssertNil(poly5.d)
|
||||
XCTAssertNil(poly5.f)
|
||||
XCTAssertNil(poly5.g)
|
||||
XCTAssertNil(poly5.h)
|
||||
XCTAssertNil(poly5.i)
|
||||
|
||||
let poly6 = Poly9<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity5, TestEntity7, TestEntity8, TestEntity9>(entity)
|
||||
XCTAssertEqual(poly6.f, entity)
|
||||
XCTAssertNil(poly6.a)
|
||||
XCTAssertNil(poly6.b)
|
||||
XCTAssertNil(poly6.c)
|
||||
XCTAssertNil(poly6.d)
|
||||
XCTAssertNil(poly6.e)
|
||||
XCTAssertNil(poly6.g)
|
||||
XCTAssertNil(poly6.h)
|
||||
XCTAssertNil(poly6.i)
|
||||
|
||||
let poly7 = Poly9<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity7, TestEntity5, TestEntity8, TestEntity9>(entity)
|
||||
XCTAssertEqual(poly7.g, entity)
|
||||
XCTAssertNil(poly7.a)
|
||||
XCTAssertNil(poly7.b)
|
||||
XCTAssertNil(poly7.c)
|
||||
XCTAssertNil(poly7.d)
|
||||
XCTAssertNil(poly7.e)
|
||||
XCTAssertNil(poly7.f)
|
||||
XCTAssertNil(poly7.h)
|
||||
XCTAssertNil(poly7.i)
|
||||
|
||||
let poly8 = Poly9<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity7, TestEntity8, TestEntity5, TestEntity9>(entity)
|
||||
XCTAssertEqual(poly8.h, entity)
|
||||
XCTAssertNil(poly8.a)
|
||||
XCTAssertNil(poly8.b)
|
||||
XCTAssertNil(poly8.c)
|
||||
XCTAssertNil(poly8.d)
|
||||
XCTAssertNil(poly8.e)
|
||||
XCTAssertNil(poly8.f)
|
||||
XCTAssertNil(poly8.g)
|
||||
XCTAssertNil(poly8.i)
|
||||
|
||||
let poly9 = Poly9<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity5>(entity)
|
||||
XCTAssertEqual(poly9.i, entity)
|
||||
XCTAssertNil(poly9.a)
|
||||
XCTAssertNil(poly9.b)
|
||||
XCTAssertNil(poly9.c)
|
||||
XCTAssertNil(poly9.d)
|
||||
XCTAssertNil(poly9.e)
|
||||
XCTAssertNil(poly9.f)
|
||||
XCTAssertNil(poly9.g)
|
||||
XCTAssertNil(poly9.h)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - subscript lookup
|
||||
@@ -303,6 +488,35 @@ extension PolyTests {
|
||||
XCTAssertNil(poly[TestEntity6.self])
|
||||
XCTAssertEqual(entity, poly[TestEntity7.self])
|
||||
}
|
||||
|
||||
func test_Poly8_lookup() {
|
||||
let entity = decoded(type: TestEntity8.self, data: poly_entity8)
|
||||
let poly = decoded(type: Poly8<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8>.self, data: poly_entity8)
|
||||
|
||||
XCTAssertNil(poly[TestEntity.self])
|
||||
XCTAssertNil(poly[TestEntity2.self])
|
||||
XCTAssertNil(poly[TestEntity3.self])
|
||||
XCTAssertNil(poly[TestEntity4.self])
|
||||
XCTAssertNil(poly[TestEntity5.self])
|
||||
XCTAssertNil(poly[TestEntity6.self])
|
||||
XCTAssertNil(poly[TestEntity7.self])
|
||||
XCTAssertEqual(entity, poly[TestEntity8.self])
|
||||
}
|
||||
|
||||
func test_Poly9_lookup() {
|
||||
let entity = decoded(type: TestEntity9.self, data: poly_entity9)
|
||||
let poly = decoded(type: Poly9<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9>.self, data: poly_entity9)
|
||||
|
||||
XCTAssertNil(poly[TestEntity.self])
|
||||
XCTAssertNil(poly[TestEntity2.self])
|
||||
XCTAssertNil(poly[TestEntity3.self])
|
||||
XCTAssertNil(poly[TestEntity4.self])
|
||||
XCTAssertNil(poly[TestEntity5.self])
|
||||
XCTAssertNil(poly[TestEntity6.self])
|
||||
XCTAssertNil(poly[TestEntity7.self])
|
||||
XCTAssertNil(poly[TestEntity8.self])
|
||||
XCTAssertEqual(entity, poly[TestEntity9.self])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - failures
|
||||
@@ -342,6 +556,14 @@ extension PolyTests {
|
||||
func test_Poly7_decode_throws_typeNotFound() {
|
||||
XCTAssertThrowsError(try JSONDecoder().decode(Poly7<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7>.self, from: poly_entity8))
|
||||
}
|
||||
|
||||
func test_Poly8_decode_throws_typeNotFound() {
|
||||
XCTAssertThrowsError(try JSONDecoder().decode(Poly8<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8>.self, from: poly_entity9))
|
||||
}
|
||||
|
||||
func test_Poly9_decode_throws_typeNotFound() {
|
||||
XCTAssertThrowsError(try JSONDecoder().decode(Poly9<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9>.self, from: poly_entity10))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Test types
|
||||
@@ -435,4 +657,26 @@ extension PolyTests {
|
||||
}
|
||||
|
||||
typealias TestEntity7 = BasicEntity<TestEntityType7>
|
||||
|
||||
enum TestEntityType8: EntityDescription {
|
||||
|
||||
typealias Attributes = NoAttributes
|
||||
|
||||
public static var type: String { return "test_entity8" }
|
||||
|
||||
typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
typealias TestEntity8 = BasicEntity<TestEntityType8>
|
||||
|
||||
enum TestEntityType9: EntityDescription {
|
||||
|
||||
typealias Attributes = NoAttributes
|
||||
|
||||
public static var type: String { return "test_entity9" }
|
||||
|
||||
typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
typealias TestEntity9 = BasicEntity<TestEntityType9>
|
||||
}
|
||||
|
||||
@@ -100,3 +100,17 @@ let poly_entity8 = """
|
||||
"id": "A24B3444-4DF1-467F-B52E-B0C9E44F436A"
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let poly_entity9 = """
|
||||
{
|
||||
"type": "test_entity9",
|
||||
"id": "A24B3444-4DF1-467F-B52E-B0C2B44F436A"
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let poly_entity10 = """
|
||||
{
|
||||
"type": "test_entity10",
|
||||
"id": "A24B3444-4DF1-467F-B52E-B0C9E12F436A"
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
@@ -269,10 +269,14 @@ extension Id_LiteralTests {
|
||||
|
||||
extension IncludedTests {
|
||||
static let __allTests = [
|
||||
("test_EightDifferentIncludes", test_EightDifferentIncludes),
|
||||
("test_EightDifferentIncludes_encode", test_EightDifferentIncludes_encode),
|
||||
("test_FiveDifferentIncludes", test_FiveDifferentIncludes),
|
||||
("test_FiveDifferentIncludes_encode", test_FiveDifferentIncludes_encode),
|
||||
("test_FourDifferentIncludes", test_FourDifferentIncludes),
|
||||
("test_FourDifferentIncludes_encode", test_FourDifferentIncludes_encode),
|
||||
("test_NineDifferentIncludes", test_NineDifferentIncludes),
|
||||
("test_NineDifferentIncludes_encode", test_NineDifferentIncludes_encode),
|
||||
("test_OneInclude", test_OneInclude),
|
||||
("test_OneInclude_encode", test_OneInclude_encode),
|
||||
("test_SevenDifferentIncludes", test_SevenDifferentIncludes),
|
||||
@@ -334,6 +338,8 @@ extension PolyTests {
|
||||
("test_init_Poly5", test_init_Poly5),
|
||||
("test_init_Poly6", test_init_Poly6),
|
||||
("test_init_Poly7", test_init_Poly7),
|
||||
("test_init_Poly8", test_init_Poly8),
|
||||
("test_init_Poly9", test_init_Poly9),
|
||||
("test_Poly0_decode_throws", test_Poly0_decode_throws),
|
||||
("test_Poly0_encode_throws", test_Poly0_encode_throws),
|
||||
("test_Poly1_decode_throws_typeNotFound", test_Poly1_decode_throws_typeNotFound),
|
||||
@@ -350,6 +356,10 @@ extension PolyTests {
|
||||
("test_Poly6_lookup", test_Poly6_lookup),
|
||||
("test_Poly7_decode_throws_typeNotFound", test_Poly7_decode_throws_typeNotFound),
|
||||
("test_Poly7_lookup", test_Poly7_lookup),
|
||||
("test_Poly8_decode_throws_typeNotFound", test_Poly8_decode_throws_typeNotFound),
|
||||
("test_Poly8_lookup", test_Poly8_lookup),
|
||||
("test_Poly9_decode_throws_typeNotFound", test_Poly9_decode_throws_typeNotFound),
|
||||
("test_Poly9_lookup", test_Poly9_lookup),
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user