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
|
/*
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 Foundation
import Markdown
@testable import SwiftDocC
import XCTest
class RenderContentCompilerTests: XCTestCase {
func testLinkOverrideTitle() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/path", fragment: nil, sourceLanguage: .swift))
let source = """
[Example](http://example.com)
[Custom Title](doc:UNRESOVLED)
[Custom Title](doc:article)
[Custom Image Content ](doc:article2)
<doc:UNRESOVLED>
<doc:article3>
"""
let document = Document(parsing: source)
let expectedDump = """
Document
├─ Paragraph
│ └─ Link destination: "http://example.com"
│ └─ Text "Example"
├─ Paragraph
│ └─ Link destination: "doc:UNRESOVLED"
│ └─ Text "Custom Title"
├─ Paragraph
│ └─ Link destination: "doc:article"
│ └─ Text "Custom Title"
├─ Paragraph
│ └─ Link destination: "doc:article2"
│ ├─ Text "Custom Image Content "
│ └─ Image source: "https://example.com/test.png"
│ └─ Text "random image"
├─ Paragraph
│ └─ Link destination: "doc:UNRESOVLED"
│ └─ Text "doc:UNRESOVLED"
└─ Paragraph
└─ Link destination: "doc:article3"
└─ Text "doc:article3"
"""
XCTAssertEqual(document.debugDescription(), expectedDump)
let result = document.children.flatMap { compiler.visit($0) }
XCTAssertEqual(result.count, 6)
do {
guard case let .paragraph(paragraph) = result[0] as? RenderBlockContent else {
XCTFail("RenderContent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let link = RenderInlineContent.reference(
identifier: .init(forExternalLink: "http://example.com"),
isActive: true,
overridingTitle: nil,
overridingTitleInlineContent: nil
)
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
}
do {
guard case let .paragraph(paragraph) = result[1] as? RenderBlockContent else {
XCTFail("RenderContent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let text = RenderInlineContent.text("Custom Title")
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [text]))
}
do {
guard case let .paragraph(paragraph) = result[2] as? RenderBlockContent else {
XCTFail("RenderContent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let link = RenderInlineContent.reference(
identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article"),
isActive: true,
overridingTitle: "Custom Title",
overridingTitleInlineContent: [.text("Custom Title")])
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
}
do {
guard case let .paragraph(paragraph) = result[3] as? RenderBlockContent else {
XCTFail("RenderContent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let link = RenderInlineContent.reference(
identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article2"),
isActive: true,
overridingTitle: "Custom Image Content ",
overridingTitleInlineContent: [
RenderInlineContent.text("Custom Image Content "),
RenderInlineContent.image(identifier: .init(forExternalLink: "https://example.com/test.png"), metadata: nil)
]
)
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
}
do {
guard case let .paragraph(paragraph) = result[4] as? RenderBlockContent else {
XCTFail("RenderContent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let text = RenderInlineContent.text("doc:UNRESOVLED")
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [text]))
}
do {
guard case let .paragraph(paragraph) = result[5] as? RenderBlockContent else {
XCTFail("RenderContent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let link = RenderInlineContent.reference(
identifier: .init("doc://org.swift.docc.example/documentation/Test-Bundle/article3"),
isActive: true,
overridingTitle: nil,
overridingTitleInlineContent: nil
)
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
}
}
func testLineBreak() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/path", fragment: nil, sourceLanguage: .swift))
let source = #"""
Backslash before new line\
is an explicit hard line break.
Two spaces before new line
is a hard line break.
Paragraph can't end with hard line break.\
# Headings can't end with hard line break.\
Code blocks ignore\
hard line breaks.
A single space before new line
is a soft line break.
"""#
let document = Document(parsing: source)
let expectedDump = #"""
Document
├─ Paragraph
│ ├─ Text "Backslash before new line"
│ ├─ LineBreak
│ └─ Text "is an explicit hard line break."
├─ Paragraph
│ ├─ Text "Two spaces before new line"
│ ├─ LineBreak
│ └─ Text "is a hard line break."
├─ Paragraph
│ └─ Text "Paragraph can’t end with hard line break.\"
├─ Heading level: 1
│ └─ Text "Headings can’t end with hard line break.\"
├─ CodeBlock language: none
│ Code blocks ignore\
│ hard line breaks.
└─ Paragraph
├─ Text "A single space before new line"
├─ SoftBreak
└─ Text "is a soft line break."
"""#
XCTAssertEqual(document.debugDescription(), expectedDump)
let result = document.children.flatMap { compiler.visit($0) }
XCTAssertEqual(result.count, 6)
do {
guard case let .paragraph(paragraph) = result[0] as? RenderBlockContent else {
XCTFail("RenderContent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let text = RenderInlineContent.text("\n")
XCTAssertEqual(paragraph.inlineContent[1], text)
}
do {
guard case let .paragraph(paragraph) = result[1] as? RenderBlockContent else {
XCTFail("RenderContent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let text = RenderInlineContent.text("\n")
XCTAssertEqual(paragraph.inlineContent[1], text)
}
}
func testThematicBreak() throws {
let (bundle, context) = try testBundleAndContext()
var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/path", fragment: nil, sourceLanguage: .swift))
let source = #"""
---
"""#
let document = Document(parsing: source)
let expectedDump = #"""
Document
└─ ThematicBreak
"""#
XCTAssertEqual(document.debugDescription(), expectedDump)
let result = document.children.flatMap { compiler.visit($0) }
XCTAssertEqual(result.count, 1)
do {
let thematicBreak = RenderBlockContent.thematicBreak
let documentThematicBreak = try XCTUnwrap(result[0] as? RenderBlockContent)
XCTAssertEqual(documentThematicBreak, thematicBreak)
}
}
}
|