diff --git a/README.md b/README.md index 2ca765d..cf8631e 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ To create an Xcode project for JSONAPI, run - [x] Roll my own `Result` or find an alternative that doesn't use `Foundation`. - [ ] Create more descriptive errors that are easier to use for troubleshooting. - [x] Make it easier to construct `Attributes` and `Relationships` values in test cases (literal expressibility). +- [ ] Make `TransformedAttribute` a Monad (or at least a Functor). ## Usage diff --git a/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift b/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift new file mode 100644 index 0000000..5484214 --- /dev/null +++ b/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift @@ -0,0 +1,61 @@ +// +// ComputedPropertiesTests.swift +// JSONAPITests +// +// Created by Mathew Polzin on 11/28/18. +// + +import XCTest +import JSONAPI +import JSONAPITestLib + +class ComputedPropertiesTests: XCTestCase { + func test_DecodeIgnoresComputed() { + let entity = decoded(type: TestType.self, data: computed_property_attribute) + + XCTAssertEqual(entity.id, "1234") + XCTAssertEqual(entity[\.name], "Sarah") + XCTAssertEqual(entity ~> \.other, "5678") + XCTAssertNoThrow(try TestType.check(entity)) + } + + func test_EncodeIgnoresComputed() { + test_DecodeEncodeEquality(type: TestType.self, data: computed_property_attribute) + } + + func test_ComputedAttributeAccess() { + let entity = decoded(type: TestType.self, data: computed_property_attribute) + + XCTAssertEqual(entity[\.computed], "Sarah") + } + + func test_ComputedRelationshipAccess() { + let entity = decoded(type: TestType.self, data: computed_property_attribute) + + XCTAssertEqual(entity ~> \.computed, "5678") + } +} + +// MARK: Test types +extension ComputedPropertiesTests { + public enum TestTypeDescription: EntityDescription { + public static var type: String { return "test" } + + public struct Attributes: JSONAPI.Attributes { + public let name: Attribute + public var computed: Attribute { + return name + } + } + + public struct Relationships: JSONAPI.Relationships { + public let other: ToOneRelationship + + public var computed: ToOneRelationship { + return other + } + } + } + + public typealias TestType = Entity +} diff --git a/Tests/JSONAPITests/Computed Properties/stubs/ComputedPropertiesStubs.swift b/Tests/JSONAPITests/Computed Properties/stubs/ComputedPropertiesStubs.swift new file mode 100644 index 0000000..11ef3a0 --- /dev/null +++ b/Tests/JSONAPITests/Computed Properties/stubs/ComputedPropertiesStubs.swift @@ -0,0 +1,24 @@ +// +// ComputedPropertiesStubs.swift +// JSONAPITests +// +// Created by Mathew Polzin on 11/28/18. +// + +let computed_property_attribute = """ +{ + "id": "1234", + "type": "test", + "attributes": { + "name": "Sarah" + }, + "relationships": { + "other": { + "data": { + "id": "5678", + "type": "test" + } + } + } +} +""".data(using: .utf8)!