diff --git a/Sources/JSONAPIArbitrary/Attribute+Arbitrary.swift b/Sources/JSONAPIArbitrary/Attribute+Arbitrary.swift new file mode 100644 index 0000000..5c3ddb5 --- /dev/null +++ b/Sources/JSONAPIArbitrary/Attribute+Arbitrary.swift @@ -0,0 +1,20 @@ +// +// Attribute+Arbitrary.swift +// JSONAPIArbitrary +// +// Created by Mathew Polzin on 1/15/19. +// + +import SwiftCheck +import JSONAPI + +extension Attribute: Arbitrary where RawValue: Arbitrary { + public static var arbitrary: Gen> { + return RawValue.arbitrary.map { .init(value: $0) } + } +} + +// Cannot extend TransformedAttribute here +// because there is no way to guarantee that an arbitrary +// RawValue will successfully transform or that an +// arbitrary Value will successfully reverse-transform. diff --git a/Sources/JSONAPIArbitrary/Entity+Arbitrary.swift b/Sources/JSONAPIArbitrary/Entity+Arbitrary.swift new file mode 100644 index 0000000..b686057 --- /dev/null +++ b/Sources/JSONAPIArbitrary/Entity+Arbitrary.swift @@ -0,0 +1,45 @@ +// +// Entity+Arbitrary.swift +// JSONAPIArbitrary +// +// Created by Mathew Polzin on 1/14/19. +// + +import SwiftCheck +import JSONAPI + +extension NoMetadata: Arbitrary { + public static var arbitrary: Gen { + return Gen.pure(.none) + } +} + +extension NoLinks: Arbitrary { + public static var arbitrary: Gen { + return Gen.pure(.none) + } +} + +extension NoAttributes: Arbitrary { + public static var arbitrary: Gen { + return Gen.pure(.none) + } +} + +extension NoRelationships: Arbitrary { + public static var arbitrary: Gen { + return Gen.pure(.none) + } +} + +extension Entity: Arbitrary where MetaType: Arbitrary, LinksType: Arbitrary, Description.Attributes: Arbitrary, Description.Relationships: Arbitrary, EntityRawIdType: Arbitrary { + public static var arbitrary: Gen> { + return Gen.compose { c in + Entity(id: c.generate(), + attributes: c.generate(), + relationships: c.generate(), + meta: c.generate(), + links: c.generate()) + } + } +} diff --git a/Sources/JSONAPIArbitrary/Id+Arbitrary.swift b/Sources/JSONAPIArbitrary/Id+Arbitrary.swift index 17b7e23..fbf6c32 100644 --- a/Sources/JSONAPIArbitrary/Id+Arbitrary.swift +++ b/Sources/JSONAPIArbitrary/Id+Arbitrary.swift @@ -8,6 +8,12 @@ import SwiftCheck import JSONAPI +extension Unidentified: Arbitrary { + public static var arbitrary: Gen { + return Gen.pure(.init()) + } +} + extension Id: Arbitrary where RawType: Arbitrary { public static var arbitrary: Gen> { return RawType.arbitrary.map { Id(rawValue: $0) } diff --git a/Sources/JSONAPIArbitrary/Relationship+Arbitrary.swift b/Sources/JSONAPIArbitrary/Relationship+Arbitrary.swift new file mode 100644 index 0000000..df1dd75 --- /dev/null +++ b/Sources/JSONAPIArbitrary/Relationship+Arbitrary.swift @@ -0,0 +1,52 @@ +// +// Relationship+Arbitrary.swift +// JSONAPIArbitrary +// +// Created by Mathew Polzin on 1/15/19. +// + +import SwiftCheck +import JSONAPI + +extension ToOneRelationship: Arbitrary where Identifiable.Identifier: Arbitrary, MetaType: Arbitrary, LinksType: Arbitrary { + public static var arbitrary: Gen> { + return Gen.compose { c in + return .init(id: c.generate(), + meta: c.generate(), + links: c.generate()) + } + } +} + +extension ToOneRelationship where MetaType: Arbitrary, LinksType: Arbitrary { + public static func arbitrary(givenEntities: [E]) -> Gen> where E.Id == Identifiable.Identifier { + + return Gen.compose { c in + let idGen = Gen.fromElements(of: givenEntities).map { $0.id } + return .init(id: c.generate(using: idGen), + meta: c.generate(), + links: c.generate()) + } + } +} + +extension ToManyRelationship: Arbitrary where Relatable.Identifier: Arbitrary, MetaType: Arbitrary, LinksType: Arbitrary { + public static var arbitrary: Gen> { + return Gen.compose { c in + return .init(ids: c.generate(), + meta: c.generate(), + links: c.generate()) + } + } +} + +extension ToManyRelationship where MetaType: Arbitrary, LinksType: Arbitrary { + public static func arbitrary(givenEntities: [E]) -> Gen> where E.Id == Relatable.Identifier { + return Gen.compose { c in + let idsGen = Gen.fromElements(of: givenEntities).map { $0.id }.proliferate + return .init(ids: c.generate(using: idsGen), + meta: c.generate(), + links: c.generate()) + } + } +}