1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import XCTest
@testable import SwiftDocC
class RenderNodeDiffingBundleTests: XCTestCase {
let testBundleName = "TestBundle"
let testBundleIdentifier = "org.swift.docc.example"
func testDiffSymbolFromBundleWithDiscussionSectionRemoved() throws {
let pathToSymbol = "/documentation/MyKit"
let modification = { (url: URL) in
let symbolURL = url.appendingPathComponent("documentation/mykit.md")
let text = try String(contentsOf: symbolURL).replacingOccurrences(of: """
## Discussion
MyKit is the best module
""", with: "")
try text.write(to: symbolURL, atomically: true, encoding: .utf8)
}
let differences = try getDiffsFromModifiedDocument(bundleName: testBundleName,
bundleIdentifier: testBundleIdentifier,
topicReferencePath: pathToSymbol,
modification: modification)
XCTAssertFalse(differences.isEmpty, "Both render nodes should be different.")
let expectedSectionDiff = JSONPatchOperation.remove(pointer: JSONPointer(pathComponents: ["primaryContentSections", "0"]))
assertDifferences(differences,
contains: expectedSectionDiff,
valueType: RenderInlineContent.self)
}
func testDiffArticleFromBundleWithTopicSectionAdded() throws {
let pathToArticle = "/documentation/Test-Bundle/article"
let modification = { (url: URL) in
let articleURL = url.appendingPathComponent("article.md")
let text = try String(contentsOf: articleURL).replacingOccurrences(of: "## Topics", with: """
## Topics
### Tutorials
- <doc:/tutorials/Test-Bundle/TestTutorial>
- <doc:/tutorials/Test-Bundle/TestTutorial2>
""")
try text.write(to: articleURL, atomically: true, encoding: .utf8)
}
let differences = try getDiffsFromModifiedDocument(bundleName: testBundleName,
bundleIdentifier: testBundleIdentifier,
topicReferencePath: pathToArticle,
modification: modification)
XCTAssertFalse(differences.isEmpty, "Both render nodes should be different.")
let expectedDiff = JSONPatchOperation.add(pointer: JSONPointer(pathComponents: ["topicSections", "0"]),
value: AnyCodable(TaskGroupRenderSection(title: "Tutorials",
abstract: nil,
discussion: nil,
identifiers: ["doc://org.swift.docc.example/tutorials/Test-Bundle/TestTutorial",
"doc://org.swift.docc.example/tutorials/Test-Bundle/TestTutorial2"],
generated: false,
anchor: "Tutorials")))
assertDifferences(differences,
contains: expectedDiff,
valueType: TaskGroupRenderSection.self)
}
func testDiffArticleFromBundleWithSeeAlsoSectionRemoved() throws {
let pathToArticle = "/documentation/Test-Bundle/article"
let modification = { (url: URL) in
let articleURL = url.appendingPathComponent("article.md")
let text = try String(contentsOf: articleURL).replacingOccurrences(of: """
## See Also
- [Website](https://www.website.com)
""", with: "")
try text.write(to: articleURL, atomically: true, encoding: .utf8)
}
let differences = try getDiffsFromModifiedDocument(bundleName: testBundleName,
bundleIdentifier: testBundleIdentifier,
topicReferencePath: pathToArticle,
modification: modification)
XCTAssertFalse(differences.isEmpty, "Both render nodes should be different.")
let expectedSectionDiff = JSONPatchOperation.remove(pointer: JSONPointer(pathComponents: ["seeAlsoSections", "0"]))
assertDifferences(differences,
contains: expectedSectionDiff,
valueType: RenderInlineContent.self)
let expectedReferenceDiff = JSONPatchOperation.remove(pointer: JSONPointer(pathComponents: ["references",
"https://www.website.com"]))
assertDifferences(differences,
contains: expectedReferenceDiff,
valueType: RenderInlineContent.self)
}
func testDiffSymbolFromBundleWithTopicSectionRemoved() throws {
let pathToSymbol = "/documentation/MyKit"
let modification = { (url: URL) in
let symbolURL = url.appendingPathComponent("documentation/mykit.md")
let text = try String(contentsOf: symbolURL).replacingOccurrences(of: """
### Extensions to other frameworks
- ``SideKit/UncuratedClass/angle``
""", with: "")
try text.write(to: symbolURL, atomically: true, encoding: .utf8)
}
let differences = try getDiffsFromModifiedDocument(bundleName: testBundleName,
bundleIdentifier: testBundleIdentifier,
topicReferencePath: pathToSymbol,
modification: modification)
XCTAssertFalse(differences.isEmpty, "Both render nodes should be different.")
let expectedSectionDiff = JSONPatchOperation.remove(pointer: JSONPointer(pathComponents: ["topicSections", "3"]))
assertDifferences(differences,
contains: expectedSectionDiff,
valueType: RenderInlineContent.self)
let expectedReferenceDiff = JSONPatchOperation.remove(pointer: JSONPointer(pathComponents: ["references",
"doc://org.swift.docc.example/documentation/SideKit/UncuratedClass/angle"]))
assertDifferences(differences,
contains: expectedReferenceDiff,
valueType: RenderInlineContent.self)
}
func testDiffSymbolFromBundleWithAbstractUpdated() throws {
let pathToSymbol = "/documentation/MyKit/MyClass"
let newAbstractValue = "MyClass new abstract."
let modification = { (url: URL) in
let symbolURL = url.appendingPathComponent("documentation/myclass.md")
let text = try String(contentsOf: symbolURL).replacingOccurrences(of: "MyClass abstract.", with: newAbstractValue)
try text.write(to: symbolURL, atomically: true, encoding: .utf8)
}
let differences = try getDiffsFromModifiedDocument(bundleName: testBundleName,
bundleIdentifier: testBundleIdentifier,
topicReferencePath: pathToSymbol,
modification: modification)
XCTAssertFalse(differences.isEmpty, "Both render nodes should be different.")
let expectedAbstractDiff = JSONPatchOperation.add(pointer: JSONPointer(pathComponents: ["abstract", "0"]),
value: AnyCodable(RenderInlineContent.text(newAbstractValue)))
assertDifferences(differences,
contains: expectedAbstractDiff,
valueType: RenderInlineContent.self)
let expectedReferenceDiff = JSONPatchOperation.remove(pointer: JSONPointer(pathComponents: ["references",
"doc://org.swift.docc.example/documentation/MyKit/MyClass",
"abstract",
"0"]))
assertDifferences(differences,
contains: expectedReferenceDiff,
valueType: AnyRenderReference.self)
}
func testDiffSymbolFromBundleWithDeprecationAdded() throws {
let pathToSymbol = "/documentation/MyKit/MyProtocol"
let newDeprecationValue = "This protocol has been deprecated."
let modification = { (url: URL) in
let symbolURL = url.appendingPathComponent("documentation/myprotocol.md")
let text = try String(contentsOf: symbolURL).replacingOccurrences(of: "# <doc:MyKit/MyProtocol>", with: """
# <doc:MyKit/MyProtocol>
@DeprecationSummary {
\(newDeprecationValue)
}
""")
try text.write(to: symbolURL, atomically: true, encoding: .utf8)
}
let differences = try getDiffsFromModifiedDocument(bundleName: testBundleName,
bundleIdentifier: testBundleIdentifier,
topicReferencePath: pathToSymbol,
modification: modification)
XCTAssertFalse(differences.isEmpty, "Both render nodes should be different.")
let expectedDeprecationDiff = JSONPatchOperation.add(pointer: JSONPointer(pathComponents: ["deprecationSummary"]),
value: AnyCodable([RenderBlockContent.paragraph(RenderBlockContent.Paragraph(
inlineContent: [RenderInlineContent.text(newDeprecationValue)]))]))
assertDifferences(differences,
contains: expectedDeprecationDiff,
valueType: [RenderBlockContent].self)
let expectedReferenceDiff = JSONPatchOperation.replace(pointer: JSONPointer(pathComponents: ["references",
"doc://org.swift.docc.example/documentation/MyKit/MyProtocol",
"deprecated"]),
value: AnyCodable(true))
assertDifferences(differences,
contains: expectedReferenceDiff,
valueType: Bool.self)
}
func testDiffSymbolFromBundleWithDisplayNameDirectiveAdded() throws {
let pathToSymbol = "/documentation/MyKit"
let newTitleValue = "My Kit"
let modification = { (url: URL) in
let symbolURL = url.appendingPathComponent("documentation/mykit.md")
let text = try String(contentsOf: symbolURL).replacingOccurrences(of: "# ``MyKit``", with: """
# ``MyKit``
@Metadata {
@DisplayName("\(newTitleValue)")
}
""")
try text.write(to: symbolURL, atomically: true, encoding: .utf8)
}
let differences = try getDiffsFromModifiedDocument(bundleName: testBundleName,
bundleIdentifier: testBundleIdentifier,
topicReferencePath: pathToSymbol,
modification: modification)
XCTAssertFalse(differences.isEmpty, "Both render nodes should be different.")
let expectedTitleDiff = JSONPatchOperation.replace(pointer: JSONPointer(pathComponents: ["metadata", "title"]),
value: AnyCodable(newTitleValue))
assertDifferences(differences,
contains: expectedTitleDiff,
valueType: String.self)
let expectedModuleDiff = JSONPatchOperation.add(pointer: JSONPointer(pathComponents: ["metadata", "modules", "0"]),
value: AnyCodable(RenderMetadata.Module(name: newTitleValue, relatedModules: nil)))
assertDifferences(differences,
contains: expectedModuleDiff,
valueType: RenderMetadata.Module.self)
}
func testDiffArticleFromBundleWithDownloadDirectiveAdded() throws {
let pathToArticle = "/documentation/Test-Bundle/article"
let modification = { (url: URL) in
let articleURL = url.appendingPathComponent("article.md")
let text = try String(contentsOf: articleURL).replacingOccurrences(of: "# My Cool Article", with: """
# My Cool Article
@Metadata {
@CallToAction(file: "Downloads/mykit.svg", purpose: download)
@PageKind(sampleCode)
}
""")
try text.write(to: articleURL, atomically: true, encoding: .utf8)
}
let differences = try getDiffsFromModifiedDocument(bundleName: testBundleName,
bundleIdentifier: testBundleIdentifier,
topicReferencePath: pathToArticle,
modification: modification)
XCTAssertFalse(differences.isEmpty, "Both render nodes should be different.")
let expectedRoleHeadingDiff = JSONPatchOperation.replace(pointer: JSONPointer(pathComponents: ["metadata", "roleHeading"]),
value: AnyCodable("Sample Code"))
assertDifferences(differences,
contains: expectedRoleHeadingDiff,
valueType: String.self)
let expectedRoleDiff = JSONPatchOperation.replace(pointer: JSONPointer(pathComponents: ["metadata", "role"]),
value: AnyCodable("sampleCode"))
assertDifferences(differences,
contains: expectedRoleDiff,
valueType: String.self)
}
func testNoDiffsWhenReconvertingSameBundle() throws {
let (bundle, context) = try testBundleAndContext(named: testBundleName)
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
let converter = DocumentationContextConverter(bundle: bundle, context: context, renderContext: renderContext)
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
let renderNodeFirst = try XCTUnwrap(converter.renderNode(for: entity, at: source))
let renderNodeSecond = try XCTUnwrap(converter.renderNode(for: entity, at: source))
let differences = renderNodeSecond._difference(from: renderNodeFirst)
XCTAssertTrue(differences.isEmpty, "Both render nodes should be identical.")
}
}
func getDiffsFromModifiedDocument(bundleName: String,
bundleIdentifier: String,
topicReferencePath: String,
modification: @escaping (URL) throws -> ()
) throws -> JSONPatchDifferences {
let (bundleOriginal, contextOriginal) = try testBundleAndContext(named: bundleName)
let nodeOriginal = try contextOriginal.entity(with: ResolvedTopicReference(bundleIdentifier: bundleIdentifier,
path: topicReferencePath,
sourceLanguage: .swift))
var renderContext = RenderContext(documentationContext: contextOriginal, bundle: bundleOriginal)
var converter = DocumentationContextConverter(bundle: bundleOriginal, context: contextOriginal, renderContext: renderContext)
var fileURL = try XCTUnwrap(contextOriginal.documentURL(for: nodeOriginal.reference))
let renderNodeOriginal = try XCTUnwrap(converter.renderNode(for: nodeOriginal, at: fileURL))
// Make copy of the bundle on disk, modify the document, and write it
let (_, bundleModified, contextModified) = try testBundleAndContext(copying: bundleName) { url in
try modification(url)
}
let nodeModified = try contextModified.entity(with: ResolvedTopicReference(bundleIdentifier: bundleIdentifier,
path: topicReferencePath,
sourceLanguage: .swift))
renderContext = RenderContext(documentationContext: contextModified, bundle: bundleModified)
converter = DocumentationContextConverter(bundle: bundleModified, context: contextModified, renderContext: renderContext)
fileURL = try XCTUnwrap(contextModified.documentURL(for: nodeModified.reference))
let renderNodeModified = try XCTUnwrap(converter.renderNode(for: nodeModified, at: fileURL))
let differences = renderNodeModified._difference(from: renderNodeOriginal)
return differences
}
func assertDifferences<Value: Equatable>(
_ differences: JSONPatchDifferences,
contains expectedDiff: JSONPatchOperation,
valueType: Value.Type,
file: StaticString = #file,
line: UInt = #line
) {
guard let foundDiff = differences.first(where: { $0.pointer == expectedDiff.pointer &&
$0.operation == expectedDiff.operation }) else {
XCTFail("No diff with pointer \(expectedDiff.pointer) and operation \(expectedDiff.operation)", file: file, line: line)
return
}
switch (expectedDiff, foundDiff) {
case let (.replace(_, expected), .replace(_, found)):
guard let expectedValue = expected.value as? Value else {
XCTFail("Wrong type of value for expected diff \(expected.value)", file: file, line: line)
return
}
guard let foundValue = found.value as? Value else {
XCTFail("Wrong type of value for found diff \(found.value)", file: file, line: line)
return
}
XCTAssertEqual(expectedValue, foundValue, file: file, line: line)
case let (.add(_, expected), .add(_, found)):
guard let expectedValue = expected.value as? Value else {
XCTFail("Wrong type of value for expected diff \(expected.value)", file: file, line: line)
return
}
guard let foundValue = found.value as? Value else {
XCTFail("Wrong type of value for found diff \(found.value)", file: file, line: line)
return
}
XCTAssertEqual(expectedValue, foundValue, file: file, line: line)
case (.remove(_), .remove(_)):
return // The JSON pointers have already been compared above.
default:
XCTFail("Found diff \(foundDiff.operation) doesn't match the expected diff \(expectedDiff.operation).", file: file, line: line)
}
}
}
|