|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2021 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 | +import SymbolKit |
| 13 | +import Markdown |
| 14 | +@testable import SwiftDocC |
| 15 | + |
| 16 | +class DocumentationContentRendererTests: XCTestCase { |
| 17 | + func testReplacesTypeIdentifierSubHeadingFragmentWithIdentifierForSwift() throws { |
| 18 | + let subHeadingFragments = documentationContentRenderer |
| 19 | + .subHeadingFragments(for: nodeWithSubheadingAndNavigatorVariants) |
| 20 | + |
| 21 | + XCTAssertEqual( |
| 22 | + subHeadingFragments.defaultValue, |
| 23 | + [ |
| 24 | + DeclarationRenderSection.Token( |
| 25 | + text: "class", |
| 26 | + kind: .keyword, |
| 27 | + identifier: nil, |
| 28 | + preciseIdentifier: nil |
| 29 | + ), |
| 30 | + DeclarationRenderSection.Token( |
| 31 | + text: " ", |
| 32 | + kind: .text, |
| 33 | + identifier: nil, |
| 34 | + preciseIdentifier: nil |
| 35 | + ), |
| 36 | + DeclarationRenderSection.Token( |
| 37 | + text: "ClassInSwift", |
| 38 | + |
| 39 | + // The 'typeIdentifier' value of the symbol's declaration is replaced with an 'identifier'. |
| 40 | + kind: .identifier, |
| 41 | + identifier: nil, |
| 42 | + preciseIdentifier: nil |
| 43 | + ) |
| 44 | + ] |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + func testDoesNotReplaceSubHeadingFragmentsForOtherLanguagesThanSwift() throws { |
| 49 | + let subHeadingFragments = documentationContentRenderer |
| 50 | + .subHeadingFragments(for: nodeWithSubheadingAndNavigatorVariants) |
| 51 | + |
| 52 | + guard case .replace(let fragments) = subHeadingFragments.variants.first?.patch.first else { |
| 53 | + XCTFail("Unexpected patch") |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + XCTAssertEqual( |
| 58 | + fragments, |
| 59 | + [ |
| 60 | + DeclarationRenderSection.Token( |
| 61 | + text: "class", |
| 62 | + kind: .keyword, identifier: nil, preciseIdentifier: nil |
| 63 | + ), |
| 64 | + DeclarationRenderSection.Token( |
| 65 | + text: " ", |
| 66 | + kind: .text, identifier: nil, preciseIdentifier: nil |
| 67 | + ), |
| 68 | + DeclarationRenderSection.Token( |
| 69 | + text: "ClassInAnotherLanguage", |
| 70 | + kind: .typeIdentifier, identifier: nil, preciseIdentifier: nil |
| 71 | + ) |
| 72 | + ] |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + func testReplacesTypeIdentifierNavigatorFragmentWithIdentifierForSwift() throws { |
| 77 | + let navigatorFragments = documentationContentRenderer |
| 78 | + .navigatorFragments(for: nodeWithSubheadingAndNavigatorVariants) |
| 79 | + |
| 80 | + XCTAssertEqual( |
| 81 | + navigatorFragments.defaultValue, |
| 82 | + [ |
| 83 | + DeclarationRenderSection.Token( |
| 84 | + text: "class", |
| 85 | + kind: .keyword, |
| 86 | + identifier: nil, |
| 87 | + preciseIdentifier: nil |
| 88 | + ), |
| 89 | + DeclarationRenderSection.Token( |
| 90 | + text: " ", |
| 91 | + kind: .text, |
| 92 | + identifier: nil, |
| 93 | + preciseIdentifier: nil |
| 94 | + ), |
| 95 | + DeclarationRenderSection.Token( |
| 96 | + text: "ClassInSwift", |
| 97 | + |
| 98 | + // The 'typeIdentifier' value of the symbol's declaration is replaced with an 'identifier'. |
| 99 | + kind: .identifier, |
| 100 | + identifier: nil, |
| 101 | + preciseIdentifier: nil |
| 102 | + ) |
| 103 | + ] |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + func testDoesNotReplacesNavigatorFragmentsForOtherLanguagesThanSwift() throws { |
| 108 | + let navigatorFragments = documentationContentRenderer |
| 109 | + .navigatorFragments(for: nodeWithSubheadingAndNavigatorVariants) |
| 110 | + |
| 111 | + guard case .replace(let fragments) = navigatorFragments.variants.first?.patch.first else { |
| 112 | + XCTFail("Unexpected patch") |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + XCTAssertEqual( |
| 117 | + fragments, |
| 118 | + [ |
| 119 | + DeclarationRenderSection.Token( |
| 120 | + text: "class", |
| 121 | + kind: .keyword, identifier: nil, preciseIdentifier: nil |
| 122 | + ), |
| 123 | + DeclarationRenderSection.Token( |
| 124 | + text: " ", |
| 125 | + kind: .text, identifier: nil, preciseIdentifier: nil |
| 126 | + ), |
| 127 | + DeclarationRenderSection.Token( |
| 128 | + text: "ClassInAnotherLanguage", |
| 129 | + kind: .typeIdentifier, identifier: nil, preciseIdentifier: nil |
| 130 | + ) |
| 131 | + ] |
| 132 | + ) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +private extension DocumentationDataVariantsTrait { |
| 137 | + static var otherLanguage: DocumentationDataVariantsTrait { .init(interfaceLanguage: "otherLanguage") } |
| 138 | +} |
| 139 | + |
| 140 | +private extension DocumentationContentRendererTests { |
| 141 | + var documentationContentRenderer: DocumentationContentRenderer { |
| 142 | + DocumentationContentRenderer( |
| 143 | + documentationContext: try! DocumentationContext(dataProvider: DocumentationWorkspace()), |
| 144 | + bundle: DocumentationBundle( |
| 145 | + info: DocumentationBundle.Info( |
| 146 | + displayName: "Test", |
| 147 | + identifier: "org.swift.test", |
| 148 | + version: Version(arrayLiteral: 1,2,3) |
| 149 | + ), |
| 150 | + baseURL: URL(string: "https://example.com/example")!, |
| 151 | + symbolGraphURLs: [], |
| 152 | + markupURLs: [], |
| 153 | + miscResourceURLs: [] |
| 154 | + ) |
| 155 | + ) |
| 156 | + } |
| 157 | + |
| 158 | + var nodeWithSubheadingAndNavigatorVariants: DocumentationNode { |
| 159 | + var node = DocumentationNode( |
| 160 | + reference: ResolvedTopicReference( |
| 161 | + bundleIdentifier: "org.swift.example", |
| 162 | + path: "/documentation/class", |
| 163 | + fragment: nil, |
| 164 | + sourceLanguage: .swift |
| 165 | + ), |
| 166 | + kind: .class, |
| 167 | + sourceLanguage: .swift, |
| 168 | + availableSourceLanguages: [ |
| 169 | + .swift, |
| 170 | + .init(id: DocumentationDataVariantsTrait.otherLanguage.interfaceLanguage!) |
| 171 | + ], |
| 172 | + name: DocumentationNode.Name.symbol(declaration: AttributedCodeListing.Line()), |
| 173 | + markup: Document(parsing: ""), |
| 174 | + semantic: nil, |
| 175 | + platformNames: nil |
| 176 | + ) |
| 177 | + |
| 178 | + node.semantic = Symbol( |
| 179 | + kindVariants: .init(values: [ |
| 180 | + .swift: SymbolGraph.Symbol.Kind(parsedIdentifier: .class, displayName: "Class"), |
| 181 | + .otherLanguage: SymbolGraph.Symbol.Kind(parsedIdentifier: .class, displayName: "Class"), |
| 182 | + ]), |
| 183 | + titleVariants: .init(values: [ |
| 184 | + .swift: "ClassInSwift", |
| 185 | + .otherLanguage: "ClassInAnotherLanguage", |
| 186 | + ]), |
| 187 | + subHeadingVariants: .init(values: [ |
| 188 | + .swift: [ |
| 189 | + .init(kind: .keyword, spelling: "class", preciseIdentifier: nil), |
| 190 | + .init(kind: .text, spelling: " ", preciseIdentifier: nil), |
| 191 | + .init(kind: .typeIdentifier, spelling: "ClassInSwift", preciseIdentifier: nil), |
| 192 | + ], |
| 193 | + .otherLanguage: [ |
| 194 | + .init(kind: .keyword, spelling: "class", preciseIdentifier: nil), |
| 195 | + .init(kind: .text, spelling: " ", preciseIdentifier: nil), |
| 196 | + .init(kind: .typeIdentifier, spelling: "ClassInAnotherLanguage", preciseIdentifier: nil), |
| 197 | + ], |
| 198 | + ]), |
| 199 | + navigatorVariants: .init(values: [ |
| 200 | + .swift: [ |
| 201 | + .init(kind: .keyword, spelling: "class", preciseIdentifier: nil), |
| 202 | + .init(kind: .text, spelling: " ", preciseIdentifier: nil), |
| 203 | + .init(kind: .typeIdentifier, spelling: "ClassInSwift", preciseIdentifier: nil), |
| 204 | + ], |
| 205 | + .otherLanguage: [ |
| 206 | + .init(kind: .keyword, spelling: "class", preciseIdentifier: nil), |
| 207 | + .init(kind: .text, spelling: " ", preciseIdentifier: nil), |
| 208 | + .init(kind: .typeIdentifier, spelling: "ClassInAnotherLanguage", preciseIdentifier: nil), |
| 209 | + ], |
| 210 | + ]), |
| 211 | + roleHeadingVariants: .init(swiftVariant: ""), |
| 212 | + platformNameVariants: .init(swiftVariant: nil), |
| 213 | + moduleNameVariants: .init(swiftVariant: ""), |
| 214 | + externalIDVariants: .init(swiftVariant: nil), |
| 215 | + accessLevelVariants: .init(swiftVariant: nil), |
| 216 | + availabilityVariants: .init(swiftVariant: Availability(availability: [])), |
| 217 | + deprecatedSummaryVariants: .init(swiftVariant: nil), |
| 218 | + mixinsVariants: .init(swiftVariant: nil), |
| 219 | + abstractSectionVariants: .init(swiftVariant: nil), |
| 220 | + discussionVariants: .init(swiftVariant: nil), |
| 221 | + topicsVariants: .init(swiftVariant: nil), |
| 222 | + seeAlsoVariants: .init(swiftVariant: nil), |
| 223 | + returnsSectionVariants: .init(swiftVariant: nil), |
| 224 | + parametersSectionVariants: .init(swiftVariant: nil), |
| 225 | + redirectsVariants: .init(swiftVariant: nil) |
| 226 | + ) |
| 227 | + |
| 228 | + return node |
| 229 | + } |
| 230 | +} |
0 commit comments