Skip to content

Commit 2a47840

Browse files
review: add a wider variety of tests for overload diff highlighting
1 parent a942e9c commit 2a47840

File tree

2 files changed

+2524
-132
lines changed

2 files changed

+2524
-132
lines changed

Tests/SwiftDocCTests/Rendering/DeclarationsRenderSectionTests.swift

Lines changed: 128 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -171,31 +171,134 @@ class DeclarationsRenderSectionTests: XCTestCase {
171171

172172
let (_, bundle, context) = try loadBundle(from: tempURL)
173173

174-
let reference = ResolvedTopicReference(
175-
bundleIdentifier: bundle.identifier,
176-
path: "/documentation/FancyOverloads/MyClass/myFunc(param:)-2rd6z",
177-
sourceLanguage: .swift
178-
)
179-
let symbol = try XCTUnwrap(context.entity(with: reference).semantic as? Symbol)
180-
var translator = RenderNodeTranslator(
181-
context: context,
182-
bundle: bundle,
183-
identifier: reference,
184-
source: nil
185-
)
186-
let renderNode = try XCTUnwrap(translator.visitSymbol(symbol) as? RenderNode)
187-
let declarationsSection = try XCTUnwrap(renderNode.primaryContentSections.compactMap({ $0 as? DeclarationsRenderSection }).first)
188-
XCTAssertEqual(declarationsSection.declarations.count, 1)
189-
let declarations = try XCTUnwrap(declarationsSection.declarations.first)
174+
// Make sure that type decorators like arrays, dictionaries, and optionals are correctly highlighted.
175+
do {
176+
// func overload1(param: Int) {} // <- overload group
177+
// func overload1(param: Int?) {}
178+
// func overload1(param: [Int]) {}
179+
// func overload1(param: [Int]?) {}
180+
// func overload1(param: Set<Int>) {}
181+
// func overload1(param: [Int: Int]) {}
182+
let reference = ResolvedTopicReference(
183+
bundleIdentifier: bundle.identifier,
184+
path: "/documentation/FancyOverloads/overload1(param:)-8nk5z",
185+
sourceLanguage: .swift
186+
)
187+
let symbol = try XCTUnwrap(context.entity(with: reference).semantic as? Symbol)
188+
var translator = RenderNodeTranslator(
189+
context: context,
190+
bundle: bundle,
191+
identifier: reference,
192+
source: nil
193+
)
194+
let renderNode = try XCTUnwrap(translator.visitSymbol(symbol) as? RenderNode)
195+
let declarationsSection = try XCTUnwrap(renderNode.primaryContentSections.compactMap({ $0 as? DeclarationsRenderSection }).first)
196+
XCTAssertEqual(declarationsSection.declarations.count, 1)
197+
let declarations = try XCTUnwrap(declarationsSection.declarations.first)
190198

191-
XCTAssertEqual(
192-
ComparisonDeclaration(tokens: declarations.tokens).tokens,
193-
["func myFunc(param: ", .hl("Int"), ")"]
194-
)
199+
XCTAssertEqual(
200+
ComparisonDeclaration(tokens: declarations.tokens).tokens,
201+
["func overload1(param: Int)"]
202+
)
195203

196-
XCTAssertEqual(declarations.otherDeclarations?.declarations.map({ ComparisonDeclaration(tokens: $0.tokens).tokens }), [
197-
["func myFunc", .hl("<S>"), "(param: ", .hl("S"), ") ", .hl("where S : StringProtocol")]
198-
])
204+
XCTAssertEqual(
205+
declarations.otherDeclarations?.declarations.map({ ComparisonDeclaration(tokens: $0.tokens).tokens }),
206+
[
207+
["func overload1(param: Int", .hl("?"), ")"],
208+
["func overload1(param: ", .hl("Set<"), "Int", .hl(">"), ")"],
209+
["func overload1(param: ", .hl("["), "Int ", .hl(": Int]"), ")"],
210+
["func overload1(param: ", .hl("["), "Int", .hl("]"), ")"],
211+
["func overload1(param: ", .hl("["), "Int", .hl("]?"), ")"],
212+
]
213+
)
214+
}
215+
216+
// Verify the behavior of the highlighter in the face of tuples and closures, which can
217+
// confuse the differencing code with excess parentheses and commas.
218+
do {
219+
// func overload2(p1: Int, p2: Int) {}
220+
// func overload2(p1: (Int, Int), p2: Int) {}
221+
// func overload2(p1: Int, p2: (Int, Int)) {}
222+
// func overload2(p1: (Int) -> (), p2: Int) {}
223+
// func overload2(p1: (Int) -> Int, p2: Int) {}
224+
// func overload2(p1: (Int) -> Int?, p2: Int) {}
225+
// func overload2(p1: ((Int) -> Int)?, p2: Int) {} // <- overload group
226+
let reference = ResolvedTopicReference(
227+
bundleIdentifier: bundle.identifier,
228+
path: "/documentation/FancyOverloads/overload2(p1:p2:)-4p1sq",
229+
sourceLanguage: .swift
230+
)
231+
let symbol = try XCTUnwrap(context.entity(with: reference).semantic as? Symbol)
232+
var translator = RenderNodeTranslator(
233+
context: context,
234+
bundle: bundle,
235+
identifier: reference,
236+
source: nil
237+
)
238+
let renderNode = try XCTUnwrap(translator.visitSymbol(symbol) as? RenderNode)
239+
let declarationsSection = try XCTUnwrap(renderNode.primaryContentSections.compactMap({ $0 as? DeclarationsRenderSection }).first)
240+
XCTAssertEqual(declarationsSection.declarations.count, 1)
241+
let declarations = try XCTUnwrap(declarationsSection.declarations.first)
242+
243+
XCTAssertEqual(
244+
ComparisonDeclaration(tokens: declarations.tokens).tokens,
245+
["func overload2(p1: ", .hl("(("), "Int", .hl(") -> Int)?"), ", p2: Int)"]
246+
)
247+
248+
XCTAssertEqual(
249+
declarations.otherDeclarations?.declarations.map({ ComparisonDeclaration(tokens: $0.tokens).tokens }),
250+
[
251+
["func overload2(p1: ", .hl("("), "Int", .hl(") -> ()"), ", p2: Int)"],
252+
["func overload2(p1: ", .hl("("), "Int", .hl(") -> Int"), ", p2: Int)"],
253+
["func overload2(p1: ", .hl("("), "Int", .hl(") -> Int?"), ", p2: Int)"],
254+
// FIXME: adjust the token processing so that the comma inside the tuple isn't treated as common?
255+
// (it breaks the declaration pretty-printer in Swift-DocC-Render and causes it to skip pretty-printing)
256+
["func overload2(p1: ", .hl("("), "Int, ", .hl("Int),"), " p2: Int)"],
257+
// FIXME: adjust the token processing so that the common parenthesis is always the final one
258+
["func overload2(p1: Int, p2: ", .hl("("), "Int", .hl(", Int"), ")", .hl(")")],
259+
["func overload2(p1: Int, p2: Int)"],
260+
]
261+
)
262+
}
263+
264+
// Verify that the presence of type parameters doesn't cause the opening parenthesis of an
265+
// argument list to also be highlighted, since it is combined into the same token as the
266+
// closing angle bracket in the symbol graph. Also ensure that the leading space of the
267+
// rendered where clause is not highlighted.
268+
do {
269+
// func overload3(_ p: [Int: Int]) {} // <- overload group
270+
// func overload3<T: Hashable>(_ p: [T: T]) {}
271+
// func overload3<K: Hashable, V>(_ p: [K: V]) {}
272+
let reference = ResolvedTopicReference(
273+
bundleIdentifier: bundle.identifier,
274+
path: "/documentation/FancyOverloads/overload3(_:)-xql2",
275+
sourceLanguage: .swift
276+
)
277+
let symbol = try XCTUnwrap(context.entity(with: reference).semantic as? Symbol)
278+
var translator = RenderNodeTranslator(
279+
context: context,
280+
bundle: bundle,
281+
identifier: reference,
282+
source: nil
283+
)
284+
let renderNode = try XCTUnwrap(translator.visitSymbol(symbol) as? RenderNode)
285+
let declarationsSection = try XCTUnwrap(renderNode.primaryContentSections.compactMap({ $0 as? DeclarationsRenderSection }).first)
286+
XCTAssertEqual(declarationsSection.declarations.count, 1)
287+
let declarations = try XCTUnwrap(declarationsSection.declarations.first)
288+
289+
XCTAssertEqual(
290+
ComparisonDeclaration(tokens: declarations.tokens).tokens,
291+
["func overload3(_ p: [", .hl("Int"), " : ", .hl("Int"), "])"]
292+
)
293+
294+
XCTAssertEqual(
295+
declarations.otherDeclarations?.declarations.map({ ComparisonDeclaration(tokens: $0.tokens).tokens }),
296+
[
297+
["func overload3", .hl("<K, V>"), "(_ p: [", .hl("K"), " : ", .hl("V"), "]) ", .hl("where K : Hashable")],
298+
["func overload3", .hl("<T>"), "(_ p: [", .hl("T"), " : ", .hl("T"), "]) ", .hl("where T : Hashable")],
299+
]
300+
)
301+
}
199302
}
200303

201304
func testDontHighlightWhenOverloadsAreDisabled() throws {
@@ -214,10 +317,10 @@ class DeclarationsRenderSectionTests: XCTestCase {
214317

215318
let (_, bundle, context) = try loadBundle(from: tempURL)
216319

217-
for hash in ["1dd3k", "4alrf"] {
320+
for hash in ["7eht8", "8p1lo", "858ja"] {
218321
let reference = ResolvedTopicReference(
219322
bundleIdentifier: bundle.identifier,
220-
path: "/documentation/FancyOverloads/MyClass/myFunc(param:)-\(hash)",
323+
path: "/documentation/FancyOverloads/overload3(_:)-\(hash)",
221324
sourceLanguage: .swift
222325
)
223326
let symbol = try XCTUnwrap(context.entity(with: reference).semantic as? Symbol)

0 commit comments

Comments
 (0)