Skip to content

Commit 34a4c8e

Browse files
committed
Add TestLib ability to represent to-one relationships with the values of their IDs.
1 parent 6aeb859 commit 34a4c8e

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Optional+Literal.swift
3+
// JSONAPITestLib
4+
//
5+
// Created by Mathew Polzin on 11/29/18.
6+
//
7+
8+
extension Optional: ExpressibleByUnicodeScalarLiteral where Wrapped: ExpressibleByUnicodeScalarLiteral {
9+
public typealias UnicodeScalarLiteralType = Wrapped.UnicodeScalarLiteralType
10+
11+
public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {
12+
self = .some(Wrapped(unicodeScalarLiteral: value))
13+
}
14+
}
15+
16+
extension Optional: ExpressibleByExtendedGraphemeClusterLiteral where Wrapped: ExpressibleByExtendedGraphemeClusterLiteral {
17+
public typealias ExtendedGraphemeClusterLiteralType = Wrapped.ExtendedGraphemeClusterLiteralType
18+
19+
public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) {
20+
self = .some(Wrapped(extendedGraphemeClusterLiteral: value))
21+
}
22+
}
23+
24+
extension Optional: ExpressibleByStringLiteral where Wrapped: ExpressibleByStringLiteral {
25+
public typealias StringLiteralType = Wrapped.StringLiteralType
26+
27+
public init(stringLiteral value: StringLiteralType) {
28+
self = .some(Wrapped(stringLiteral: value))
29+
}
30+
}

Sources/JSONAPITestLib/Relationship+Literal.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,35 @@ import JSONAPI
99

1010
extension ToOneRelationship: ExpressibleByNilLiteral where Relatable.WrappedIdentifier: ExpressibleByNilLiteral {
1111
public init(nilLiteral: ()) {
12+
1213
self.init(id: Relatable.WrappedIdentifier(nilLiteral: ()))
1314
}
1415
}
1516

17+
extension ToOneRelationship: ExpressibleByUnicodeScalarLiteral where Relatable.WrappedIdentifier: ExpressibleByUnicodeScalarLiteral {
18+
public typealias UnicodeScalarLiteralType = Relatable.WrappedIdentifier.UnicodeScalarLiteralType
19+
20+
public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {
21+
self.init(id: Relatable.WrappedIdentifier(unicodeScalarLiteral: value))
22+
}
23+
}
24+
25+
extension ToOneRelationship: ExpressibleByExtendedGraphemeClusterLiteral where Relatable.WrappedIdentifier: ExpressibleByExtendedGraphemeClusterLiteral {
26+
public typealias ExtendedGraphemeClusterLiteralType = Relatable.WrappedIdentifier.ExtendedGraphemeClusterLiteralType
27+
28+
public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) {
29+
self.init(id: Relatable.WrappedIdentifier(extendedGraphemeClusterLiteral: value))
30+
}
31+
}
32+
33+
extension ToOneRelationship: ExpressibleByStringLiteral where Relatable.WrappedIdentifier: ExpressibleByStringLiteral {
34+
public typealias StringLiteralType = Relatable.WrappedIdentifier.StringLiteralType
35+
36+
public init(stringLiteral value: StringLiteralType) {
37+
self.init(id: Relatable.WrappedIdentifier(stringLiteral: value))
38+
}
39+
}
40+
1641
extension ToManyRelationship: ExpressibleByArrayLiteral {
1742
public typealias ArrayLiteralElement = Relatable.Identifier
1843

Tests/JSONAPITests/JSONAPITestLib/Relationship+LiteralTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class Relationship_LiteralTests: XCTestCase {
1818
func test_ArrayLiteral() {
1919
XCTAssertEqual(ToManyRelationship<TestEntity>(ids: ["1", "2", "3"]), ["1", "2", "3"])
2020
}
21+
22+
func test_StringLiteral() {
23+
XCTAssertEqual(ToOneRelationship<TestEntity>(id: "123"), "123")
24+
XCTAssertEqual(ToOneRelationship<TestEntity?>(id: "123"), "123")
25+
}
2126
}
2227

2328
// MARK: - Test types

0 commit comments

Comments
 (0)