Skip to content

Commit 6d81acb

Browse files
review: create a test fixture for sameShape constraints
1 parent 338f54c commit 6d81acb

File tree

2 files changed

+340
-22
lines changed

2 files changed

+340
-22
lines changed

Tests/SwiftDocCTests/Rendering/ConstraintsRenderSectionTests.swift

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import Foundation
1212
import XCTest
1313
@testable import SwiftDocC
14+
import SwiftDocCTestUtilities
1415
import SymbolKit
1516

1617
fileprivate let jsonDecoder = JSONDecoder()
@@ -275,40 +276,36 @@ class ConstraintsRenderSectionTests: XCTestCase {
275276
}
276277

277278
func testRenderSameShape() async throws {
278-
let (_, bundle, context) = try await testBundleAndContext(copying: "LegacyBundle_DoNotUseInNewTests", excludingPaths: []) { bundleURL in
279-
// Add constraints to `MyClass`
280-
let graphURL = bundleURL.appendingPathComponent("mykit-iOS.symbols.json")
281-
var graph = try jsonDecoder.decode(SymbolGraph.self, from: try Data(contentsOf: graphURL))
279+
let symbolGraphFile = Bundle.module.url(
280+
forResource: "SameShapeConstraint",
281+
withExtension: "symbols.json",
282+
subdirectory: "Test Resources"
283+
)!
282284

283-
// "Inject" generic constraints
284-
graph.symbols = try graph.symbols.mapValues({ symbol -> SymbolGraph.Symbol in
285-
guard symbol.identifier.precise == "s:5MyKit0A5ClassC10myFunctionyyF" else { return symbol }
286-
var symbol = symbol
287-
symbol.mixins[SymbolGraph.Symbol.Swift.Extension.mixinKey] = try jsonDecoder.decode(SymbolGraph.Symbol.Swift.Extension.self, from: """
288-
{"extendedModule": "MyKit",
289-
"constraints": [
290-
{ "kind" : "sameShape", "lhs" : "Element", "rhs" : "MyProtocol" }
291-
]}
292-
""".data(using: .utf8)!)
293-
return symbol
294-
})
295-
try jsonEncoder.encode(graph).write(to: graphURL)
296-
}
285+
let catalog = Folder(name: "unit-test.docc", content: [
286+
InfoPlist(displayName: "SameShapeConstraint", identifier: "com.test.example"),
287+
CopyOfFile(original: symbolGraphFile),
288+
])
289+
290+
let (bundle, context) = try await loadBundle(catalog: catalog)
297291

298292
// Compile docs and verify contents
299-
let node = try context.entity(with: ResolvedTopicReference(bundleID: bundle.id, path: "/documentation/MyKit/MyClass", sourceLanguage: .swift))
293+
let node = try context.entity(with: ResolvedTopicReference(bundleID: bundle.id, path: "/documentation/SameShapeConstraint/function(_:)", sourceLanguage: .swift))
300294
let symbol = node.semantic as! Symbol
301295
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference)
302296
let renderNode = translator.visitSymbol(symbol) as! RenderNode
303297

304298
guard let renderReference = renderNode.references.first(where: { (key, value) -> Bool in
305-
return key.hasSuffix("myFunction()")
299+
return key.hasSuffix("function(_:)")
306300
})?.value as? TopicRenderReference else {
307-
XCTFail("Did not find render reference to myFunction()")
301+
XCTFail("Did not find render reference to function(_:)")
308302
return
309303
}
310304

311-
XCTAssertEqual(renderReference.conformance?.constraints.map(flattenInlineElements).joined(), "Element is the same shape as MyProtocol.")
305+
// The symbol graph only defines constraints on the `swiftGenerics` mixin,
306+
// which docc doesn't load or render.
307+
// However, this test should still run without crashing on decoding the symbol graph.
308+
XCTAssertEqual(renderReference.conformance?.constraints.map(flattenInlineElements).joined(), nil)
312309
}
313310
}
314311

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
{
2+
"metadata": {
3+
"formatVersion": {
4+
"major": 0,
5+
"minor": 6,
6+
"patch": 0
7+
},
8+
"generator": "Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)"
9+
},
10+
"module": {
11+
"name": "SameShapeConstraint",
12+
"platform": {
13+
"architecture": "arm64",
14+
"vendor": "apple",
15+
"operatingSystem": {
16+
"name": "macosx",
17+
"minimumVersion": {
18+
"major": 12,
19+
"minor": 4
20+
}
21+
}
22+
}
23+
},
24+
"symbols": [
25+
{
26+
"kind": {
27+
"identifier": "swift.func",
28+
"displayName": "Function"
29+
},
30+
"identifier": {
31+
"precise": "s:13SameShapeConstraint8functionyyx_q_txQp_t_tRvzRv_q_Rhzr0_lF",
32+
"interfaceLanguage": "swift"
33+
},
34+
"pathComponents": [
35+
"function(_:)"
36+
],
37+
"names": {
38+
"title": "function(_:)",
39+
"subHeading": [
40+
{
41+
"kind": "keyword",
42+
"spelling": "func"
43+
},
44+
{
45+
"kind": "text",
46+
"spelling": " "
47+
},
48+
{
49+
"kind": "identifier",
50+
"spelling": "function"
51+
},
52+
{
53+
"kind": "text",
54+
"spelling": "<each "
55+
},
56+
{
57+
"kind": "genericParameter",
58+
"spelling": "Element0"
59+
},
60+
{
61+
"kind": "text",
62+
"spelling": ", each "
63+
},
64+
{
65+
"kind": "genericParameter",
66+
"spelling": "Element1"
67+
},
68+
{
69+
"kind": "text",
70+
"spelling": ">(("
71+
},
72+
{
73+
"kind": "keyword",
74+
"spelling": "repeat"
75+
},
76+
{
77+
"kind": "text",
78+
"spelling": " ("
79+
},
80+
{
81+
"kind": "keyword",
82+
"spelling": "each"
83+
},
84+
{
85+
"kind": "text",
86+
"spelling": " "
87+
},
88+
{
89+
"kind": "typeIdentifier",
90+
"spelling": "Element0",
91+
"preciseIdentifier": "s:13SameShapeConstraint8functionyyx_q_txQp_t_tRvzRv_q_Rhzr0_lF8Element0L_xmfp"
92+
},
93+
{
94+
"kind": "text",
95+
"spelling": ", "
96+
},
97+
{
98+
"kind": "keyword",
99+
"spelling": "each"
100+
},
101+
{
102+
"kind": "text",
103+
"spelling": " "
104+
},
105+
{
106+
"kind": "typeIdentifier",
107+
"spelling": "Element1",
108+
"preciseIdentifier": "s:13SameShapeConstraint8functionyyx_q_txQp_t_tRvzRv_q_Rhzr0_lF8Element1L_q_mfp"
109+
},
110+
{
111+
"kind": "text",
112+
"spelling": ")))"
113+
}
114+
]
115+
},
116+
"functionSignature": {
117+
"parameters": [
118+
{
119+
"name": "_",
120+
"declarationFragments": [
121+
{
122+
"kind": "identifier",
123+
"spelling": "_"
124+
},
125+
{
126+
"kind": "text",
127+
"spelling": ": ("
128+
},
129+
{
130+
"kind": "keyword",
131+
"spelling": "repeat"
132+
},
133+
{
134+
"kind": "text",
135+
"spelling": " ("
136+
},
137+
{
138+
"kind": "keyword",
139+
"spelling": "each"
140+
},
141+
{
142+
"kind": "text",
143+
"spelling": " "
144+
},
145+
{
146+
"kind": "typeIdentifier",
147+
"spelling": "Element0",
148+
"preciseIdentifier": "s:13SameShapeConstraint8functionyyx_q_txQp_t_tRvzRv_q_Rhzr0_lF8Element0L_xmfp"
149+
},
150+
{
151+
"kind": "text",
152+
"spelling": ", "
153+
},
154+
{
155+
"kind": "keyword",
156+
"spelling": "each"
157+
},
158+
{
159+
"kind": "text",
160+
"spelling": " "
161+
},
162+
{
163+
"kind": "typeIdentifier",
164+
"spelling": "Element1",
165+
"preciseIdentifier": "s:13SameShapeConstraint8functionyyx_q_txQp_t_tRvzRv_q_Rhzr0_lF8Element1L_q_mfp"
166+
},
167+
{
168+
"kind": "text",
169+
"spelling": "))"
170+
}
171+
]
172+
}
173+
],
174+
"returns": [
175+
{
176+
"kind": "text",
177+
"spelling": "()"
178+
}
179+
]
180+
},
181+
"swiftGenerics": {
182+
"parameters": [
183+
{
184+
"name": "Element0",
185+
"index": 0,
186+
"depth": 0
187+
},
188+
{
189+
"name": "Element1",
190+
"index": 1,
191+
"depth": 0
192+
}
193+
],
194+
"constraints": [
195+
{
196+
"kind": "sameShape",
197+
"lhs": "each Element0",
198+
"rhs": "each Element1"
199+
}
200+
]
201+
},
202+
"declarationFragments": [
203+
{
204+
"kind": "keyword",
205+
"spelling": "func"
206+
},
207+
{
208+
"kind": "text",
209+
"spelling": " "
210+
},
211+
{
212+
"kind": "identifier",
213+
"spelling": "function"
214+
},
215+
{
216+
"kind": "text",
217+
"spelling": "<each "
218+
},
219+
{
220+
"kind": "genericParameter",
221+
"spelling": "Element0"
222+
},
223+
{
224+
"kind": "text",
225+
"spelling": ", each "
226+
},
227+
{
228+
"kind": "genericParameter",
229+
"spelling": "Element1"
230+
},
231+
{
232+
"kind": "text",
233+
"spelling": ">("
234+
},
235+
{
236+
"kind": "externalParam",
237+
"spelling": "_"
238+
},
239+
{
240+
"kind": "text",
241+
"spelling": ": ("
242+
},
243+
{
244+
"kind": "keyword",
245+
"spelling": "repeat"
246+
},
247+
{
248+
"kind": "text",
249+
"spelling": " ("
250+
},
251+
{
252+
"kind": "keyword",
253+
"spelling": "each"
254+
},
255+
{
256+
"kind": "text",
257+
"spelling": " "
258+
},
259+
{
260+
"kind": "typeIdentifier",
261+
"spelling": "Element0",
262+
"preciseIdentifier": "s:13SameShapeConstraint8functionyyx_q_txQp_t_tRvzRv_q_Rhzr0_lF8Element0L_xmfp"
263+
},
264+
{
265+
"kind": "text",
266+
"spelling": ", "
267+
},
268+
{
269+
"kind": "keyword",
270+
"spelling": "each"
271+
},
272+
{
273+
"kind": "text",
274+
"spelling": " "
275+
},
276+
{
277+
"kind": "typeIdentifier",
278+
"spelling": "Element1",
279+
"preciseIdentifier": "s:13SameShapeConstraint8functionyyx_q_txQp_t_tRvzRv_q_Rhzr0_lF8Element1L_q_mfp"
280+
},
281+
{
282+
"kind": "text",
283+
"spelling": "))) "
284+
},
285+
{
286+
"kind": "keyword",
287+
"spelling": "where"
288+
},
289+
{
290+
"kind": "text",
291+
"spelling": " (repeat (each "
292+
},
293+
{
294+
"kind": "typeIdentifier",
295+
"spelling": "Element0"
296+
},
297+
{
298+
"kind": "text",
299+
"spelling": ", each "
300+
},
301+
{
302+
"kind": "typeIdentifier",
303+
"spelling": "Element1"
304+
},
305+
{
306+
"kind": "text",
307+
"spelling": ")) : Any"
308+
}
309+
],
310+
"accessLevel": "public",
311+
"location": {
312+
"uri": "file:///path/to/SameShapeConstraint/SwiftClass.swift",
313+
"position": {
314+
"line": 9,
315+
"character": 12
316+
}
317+
}
318+
}
319+
],
320+
"relationships": []
321+
}

0 commit comments

Comments
 (0)