|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2022 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 Foundation |
| 12 | +import Markdown |
| 13 | +@testable import SwiftDocC |
| 14 | +import XCTest |
| 15 | + |
| 16 | +class RenderContentCompilerTests: XCTestCase { |
| 17 | + func testLinkOverrideTitle() throws { |
| 18 | + let (bundle, context) = try testBundleAndContext(named: "TestBundle") |
| 19 | + var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/path", fragment: nil, sourceLanguage: .swift)) |
| 20 | + |
| 21 | + let source = """ |
| 22 | + [Example](http://example.com) |
| 23 | + |
| 24 | + [Custom Title](doc:UNRESOVLED) |
| 25 | + |
| 26 | + [Custom Title](doc:article) |
| 27 | + |
| 28 | + [Custom Image Content ](doc:article2) |
| 29 | + |
| 30 | + <doc:UNRESOVLED> |
| 31 | + |
| 32 | + <doc:article3> |
| 33 | + """ |
| 34 | + let document = Document(parsing: source) |
| 35 | + let expectedDump = """ |
| 36 | + Document |
| 37 | + ├─ Paragraph |
| 38 | + │ └─ Link destination: "http://example.com" |
| 39 | + │ └─ Text "Example" |
| 40 | + ├─ Paragraph |
| 41 | + │ └─ Link destination: "doc:UNRESOVLED" |
| 42 | + │ └─ Text "Custom Title" |
| 43 | + ├─ Paragraph |
| 44 | + │ └─ Link destination: "doc:article" |
| 45 | + │ └─ Text "Custom Title" |
| 46 | + ├─ Paragraph |
| 47 | + │ └─ Link destination: "doc:article2" |
| 48 | + │ ├─ Text "Custom Image Content " |
| 49 | + │ └─ Image source: "https://example.com/test.png" title: "" |
| 50 | + │ └─ Text "random image" |
| 51 | + ├─ Paragraph |
| 52 | + │ └─ Link destination: "doc:UNRESOVLED" |
| 53 | + │ └─ Text "doc:UNRESOVLED" |
| 54 | + └─ Paragraph |
| 55 | + └─ Link destination: "doc:article3" |
| 56 | + └─ Text "doc:article3" |
| 57 | + """ |
| 58 | + XCTAssertEqual(document.debugDescription(), expectedDump) |
| 59 | + |
| 60 | + let result = document.children.flatMap { compiler.visit($0) } |
| 61 | + XCTAssertEqual(result.count, 6) |
| 62 | + |
| 63 | + do { |
| 64 | + guard case let .paragraph(paragraph) = result[0] as? RenderBlockContent else { |
| 65 | + XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)") |
| 66 | + return |
| 67 | + } |
| 68 | + let link = RenderInlineContent.reference( |
| 69 | + identifier: .init(forExternalLink: "http://example.com"), |
| 70 | + isActive: true, |
| 71 | + overridingTitle: nil, |
| 72 | + overridingTitleInlineContent: nil |
| 73 | + ) |
| 74 | + XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link])) |
| 75 | + } |
| 76 | + do { |
| 77 | + guard case let .paragraph(paragraph) = result[1] as? RenderBlockContent else { |
| 78 | + XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)") |
| 79 | + return |
| 80 | + } |
| 81 | + let text = RenderInlineContent.text("Custom Title") |
| 82 | + XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [text])) |
| 83 | + } |
| 84 | + do { |
| 85 | + guard case let .paragraph(paragraph) = result[2] as? RenderBlockContent else { |
| 86 | + XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)") |
| 87 | + return |
| 88 | + } |
| 89 | + let link = RenderInlineContent.reference( |
| 90 | + identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article"), |
| 91 | + isActive: true, |
| 92 | + overridingTitle: "Custom Title", |
| 93 | + overridingTitleInlineContent: [.text("Custom Title")]) |
| 94 | + XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link])) |
| 95 | + } |
| 96 | + do { |
| 97 | + guard case let .paragraph(paragraph) = result[3] as? RenderBlockContent else { |
| 98 | + XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)") |
| 99 | + return |
| 100 | + } |
| 101 | + let link = RenderInlineContent.reference( |
| 102 | + identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article2"), |
| 103 | + isActive: true, |
| 104 | + overridingTitle: "Custom Image Content ", |
| 105 | + overridingTitleInlineContent: [ |
| 106 | + RenderInlineContent.text("Custom Image Content "), |
| 107 | + RenderInlineContent.image(identifier: .init(forExternalLink: "https://example.com/test.png"), metadata: nil) |
| 108 | + ] |
| 109 | + ) |
| 110 | + XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link])) |
| 111 | + } |
| 112 | + do { |
| 113 | + guard case let .paragraph(paragraph) = result[4] as? RenderBlockContent else { |
| 114 | + XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)") |
| 115 | + return |
| 116 | + } |
| 117 | + let text = RenderInlineContent.text("doc:UNRESOVLED") |
| 118 | + XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [text])) |
| 119 | + } |
| 120 | + do { |
| 121 | + guard case let .paragraph(paragraph) = result[5] as? RenderBlockContent else { |
| 122 | + XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)") |
| 123 | + return |
| 124 | + } |
| 125 | + let link = RenderInlineContent.reference( |
| 126 | + identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article3"), |
| 127 | + isActive: true, |
| 128 | + overridingTitle: nil, |
| 129 | + overridingTitleInlineContent: nil |
| 130 | + ) |
| 131 | + XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link])) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments