Add a playground page to show off JSONAPITestLib and add nil literal expressibility to ToOneRelationship

This commit is contained in:
Mathew Polzin
2018-11-27 11:45:15 -08:00
parent 9df9efc2dc
commit dcabafd583
9 changed files with 84 additions and 8 deletions
@@ -0,0 +1,16 @@
//: [Previous](@previous)
import Foundation
import JSONAPI
import JSONAPITestLib
/*******
Please enjoy these examples, but allow me the forced casting and the lack of error checking for the sake of brevity.
********/
// MARK: - Literal Expressibility
// The JSONAPITestLib provides literal expressibility for key types to
// make creating tests easier
let dog = Dog(id: "1234", attributes: Dog.Attributes(name: "Buddy"), relationships: Dog.Relationships(owner: nil))
+19
View File
@@ -34,12 +34,23 @@ public enum PersonDescription: EntityDescription {
public struct Attributes: JSONAPI.Attributes {
public let name: Attribute<[String]>
public let favoriteColor: Attribute<String>
public init(name: Attribute<[String]>, favoriteColor: Attribute<String>) {
self.name = name
self.favoriteColor = favoriteColor
}
}
public struct Relationships: JSONAPI.Relationships {
public let friends: ToManyRelationship<Person>
public let dogs: ToManyRelationship<Dog>
public let home: ToOneRelationship<House>
public init(friends: ToManyRelationship<Person>, dogs: ToManyRelationship<Dog>, home: ToOneRelationship<House>) {
self.friends = friends
self.dogs = dogs
self.home = home
}
}
}
@@ -57,10 +68,18 @@ public enum DogDescription: EntityDescription {
public struct Attributes: JSONAPI.Attributes {
public let name: Attribute<String>
public init(name: Attribute<String>) {
self.name = name
}
}
public struct Relationships: JSONAPI.Relationships {
public let owner: ToOneRelationship<Person?>
public init(owner: ToOneRelationship<Person?>) {
self.owner = owner
}
}
}
+5 -2
View File
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
<playground version='6.0' target-platform='macos' executeOnSourceChanges='false'>
<pages>
<page name='Test Library'/>
<page name='Usage'/>
</pages>
</playground>
@@ -12,10 +12,6 @@
public struct ToOneRelationship<Relatable: JSONAPI.OptionalRelatable>: Equatable, Codable {
public let id: Relatable.WrappedIdentifier
public var ids: [Relatable.WrappedIdentifier] {
return [id]
}
public init(id: Relatable.WrappedIdentifier) {
self.id = id
@@ -0,0 +1,14 @@
//
// Relationship+Literal.swift
// JSONAPITestLib
//
// Created by Mathew Polzin on 11/27/18.
//
import JSONAPI
extension ToOneRelationship: ExpressibleByNilLiteral where Relatable.WrappedIdentifier: ExpressibleByNilLiteral {
public init(nilLiteral: ()) {
self.init(id: Relatable.WrappedIdentifier(nilLiteral: ()))
}
}
+1 -1
View File
@@ -37,7 +37,7 @@ class EntityTests: XCTestCase {
let entity1 = TestEntity1()
let entity2 = TestEntity2(other: entity1.pointer)
XCTAssertEqual(entity2.relationships.other.ids, [entity1.id])
XCTAssertEqual(entity2.relationships.other.id, entity1.id)
}
}
@@ -40,7 +40,6 @@ extension RelationshipTests {
data: to_one_relationship)
XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
XCTAssertEqual(relationship.ids.count, 1)
}
func test_ToOneRelationship_encode() {
@@ -0,0 +1,29 @@
//
// Relationship+LiteralTests.swift
// JSONAPITests
//
// Created by Mathew Polzin on 11/27/18.
//
import XCTest
import JSONAPI
import JSONAPITestLib
class Relationship_LiteralTests: XCTestCase {
func test_NilLiteral() {
XCTAssertEqual(ToOneRelationship<TestEntity?>(id: nil), nil)
}
}
// MARK: - Test types
extension Relationship_LiteralTests {
enum TestDescription: EntityDescription {
public static var type: String { return "test" }
public typealias Attributes = NoAttributes
public typealias Relationships = NoRelationships
}
typealias TestEntity = Entity<TestDescription>
}