Skip to content

Commit 31ee554

Browse files
Add a new initializer to GenericConstraint (#63)
* Add a new initializer to SymbolGraph.Symbol.Swift.GenericConstraint that allows clients to create these structs directly in memory and not only from decoding JSON. rdar://112219546
1 parent 1a184ae commit 31ee554

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

Sources/SymbolKit/SymbolGraph/Symbol/Swift/GenericConstraint.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -69,13 +69,22 @@ extension SymbolGraph.Symbol.Swift {
6969
*/
7070
public var rightTypeName: String
7171

72+
/// Create a new GenericConstraint for the given kind and type names.
73+
public init(kind: Kind, leftTypeName: String, rightTypeName: String) {
74+
self.kind = kind
75+
self.leftTypeName = leftTypeName
76+
self.rightTypeName = rightTypeName
77+
}
78+
79+
/// Create a new GenericConstraint by decoding a native format.
7280
public init(from decoder: Decoder) throws {
7381
let container = try decoder.container(keyedBy: CodingKeys.self)
7482
kind = try container.decode(Kind.self, forKey: .kind)
7583
leftTypeName = try container.decode(String.self, forKey: .leftTypeName)
7684
rightTypeName = try container.decode(String.self, forKey: .rightTypeName)
7785
}
7886

87+
/// Encode a GenericConstraint to a native format.
7988
public func encode(to encoder: Encoder) throws {
8089
var container = encoder.container(keyedBy: CodingKeys.self)
8190
try container.encode(kind, forKey: .kind)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import XCTest
12+
@testable import SymbolKit
13+
14+
class GenericConstraintTests: XCTestCase {
15+
16+
func testInitializeConformance() throws {
17+
let jsonString = """
18+
{
19+
"kind": "conformance",
20+
"lhs": "Self",
21+
"rhs": "Hashable",
22+
}
23+
"""
24+
let jsonData = Data(jsonString.utf8)
25+
let jsonConstraint = try JSONDecoder().decode(SymbolGraph.Symbol.Swift.GenericConstraint.self, from: jsonData)
26+
let constraint = SymbolGraph.Symbol.Swift.GenericConstraint(
27+
kind: .conformance,
28+
leftTypeName: "Self",
29+
rightTypeName: "Hashable"
30+
)
31+
XCTAssertEqual(jsonConstraint, constraint)
32+
}
33+
34+
func testInitializeSuperclass() throws {
35+
let jsonString = """
36+
{
37+
"kind": "superclass",
38+
"lhs": "Self",
39+
"rhs": "String",
40+
}
41+
"""
42+
let jsonData = Data(jsonString.utf8)
43+
let jsonConstraint = try JSONDecoder().decode(SymbolGraph.Symbol.Swift.GenericConstraint.self, from: jsonData)
44+
let constraint = SymbolGraph.Symbol.Swift.GenericConstraint(
45+
kind: .superclass,
46+
leftTypeName: "Self",
47+
rightTypeName: "String"
48+
)
49+
XCTAssertEqual(jsonConstraint, constraint)
50+
}
51+
52+
func testInitializeSameType() throws {
53+
let jsonString = """
54+
{
55+
"kind": "sameType",
56+
"lhs": "Self",
57+
"rhs": "Problem",
58+
}
59+
"""
60+
let jsonData = Data(jsonString.utf8)
61+
let jsonConstraint = try JSONDecoder().decode(SymbolGraph.Symbol.Swift.GenericConstraint.self, from: jsonData)
62+
let constraint = SymbolGraph.Symbol.Swift.GenericConstraint(
63+
kind: .sameType,
64+
leftTypeName: "Self",
65+
rightTypeName: "Problem"
66+
)
67+
XCTAssertEqual(jsonConstraint, constraint)
68+
}
69+
}

0 commit comments

Comments
 (0)