1
1
/*
2
2
This source file is part of the Swift.org open source project
3
3
4
- Copyright (c) 2021 Apple Inc. and the Swift project authors
4
+ Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
5
5
Licensed under Apache License v2.0 with Runtime Library Exception
6
6
7
7
See https://swift.org/LICENSE.txt for license information
@@ -12,6 +12,7 @@ import Foundation
12
12
import XCTest
13
13
import Markdown
14
14
@testable import SwiftDocC
15
+ import SwiftDocCTestUtilities
15
16
16
17
class TermListTests : XCTestCase {
17
18
@@ -46,6 +47,112 @@ class TermListTests: XCTestCase {
46
47
XCTAssertEqual ( l. items. count, 4 )
47
48
}
48
49
50
+ func testLinksAndCodeVoiceAsTerms( ) throws {
51
+ let tempURL = try createTempFolder ( content: [
52
+ Folder ( name: " unit-test.docc " , content: [
53
+ TextFile ( name: " Article.md " , utf8Content: """
54
+ # Article
55
+
56
+ An article with a term definition list with links as terms.
57
+
58
+ - term ``someFunction(_:)``: First definition
59
+ - term <doc:ModuleName>: Second definition
60
+ - term `someFunction(_:)`: Third definition
61
+ - term <doc://unit-test/documentation/ModuleName>: Fourth definition
62
+ - term <doc://com.external.testbundle/path/to/something>: Fifth definition
63
+
64
+ """ ) ,
65
+
66
+ JSONFile ( name: " ModuleName.symbols.json " , content: makeSymbolGraph (
67
+ moduleName: " ModuleName " ,
68
+ symbols: [
69
+ . init(
70
+ identifier: . init( precise: " some-symbol-id " , interfaceLanguage: SourceLanguage . swift. id) ,
71
+ names: . init( title: " someFunction(_:) " , navigator: nil , subHeading: nil , prose: nil ) ,
72
+ pathComponents: [ " someFunction(_:) " ] ,
73
+ docComment: nil ,
74
+ accessLevel: . public,
75
+ kind: . init( parsedIdentifier: . func, displayName: " Kind Display Name " ) ,
76
+ mixins: [ : ]
77
+ )
78
+ ]
79
+ ) ) ,
80
+ ] ) ,
81
+ ] )
82
+
83
+ let resolver = TestMultiResultExternalReferenceResolver ( )
84
+ resolver. entitiesToReturn [ " /path/to/something " ] = . success(
85
+ . init( referencePath: " /path/to/something " )
86
+ )
87
+
88
+ let ( _, bundle, context) = try loadBundle ( from: tempURL, externalResolvers: [ " com.external.testbundle " : resolver] )
89
+
90
+ let reference = ResolvedTopicReference ( bundleIdentifier: bundle. identifier, path: " /documentation/unit-test/Article " , sourceLanguage: . swift)
91
+ let entity = try context. entity ( with: reference)
92
+
93
+ let converter = DocumentationNodeConverter ( bundle: bundle, context: context)
94
+ let renderNode = try converter. convert ( entity, at: nil )
95
+
96
+ let overviewSection = try XCTUnwrap ( renderNode. primaryContentSections. first as? ContentRenderSection )
97
+
98
+ guard case RenderBlockContent . termList( let termList) = try XCTUnwrap ( overviewSection. content. dropFirst ( /* the "Overview" heading */) . first) else {
99
+ XCTFail ( " Unexpected kind of rendered content. Expected term list. Got \( overviewSection. content. dropFirst ( ) . first ?? " <nil> " ) " )
100
+ return
101
+ }
102
+
103
+ XCTAssertEqual ( termList. items. count, 5 )
104
+
105
+ do {
106
+ let item = termList. items. first
107
+ XCTAssertEqual ( item? . term. inlineContent, [
108
+ . reference( identifier: RenderReferenceIdentifier ( " doc://unit-test/documentation/ModuleName/someFunction(_:) " ) , isActive: true , overridingTitle: nil , overridingTitleInlineContent: nil )
109
+ ] )
110
+ XCTAssertEqual ( item? . definition. content, [
111
+ . paragraph( . init( inlineContent: [ . text( " First definition " ) ] ) )
112
+ ] )
113
+ }
114
+
115
+ do {
116
+ let item = termList. items. dropFirst ( ) . first
117
+ XCTAssertEqual ( item? . term. inlineContent, [
118
+ . reference( identifier: RenderReferenceIdentifier ( " doc://unit-test/documentation/ModuleName " ) , isActive: true , overridingTitle: nil , overridingTitleInlineContent: nil )
119
+ ] )
120
+ XCTAssertEqual ( item? . definition. content, [
121
+ . paragraph( . init( inlineContent: [ . text( " Second definition " ) ] ) )
122
+ ] )
123
+ }
124
+
125
+ do {
126
+ let item = termList. items. dropFirst ( 2 ) . first
127
+ XCTAssertEqual ( item? . term. inlineContent, [
128
+ . codeVoice( code: " someFunction(_:) " )
129
+ ] )
130
+ XCTAssertEqual ( item? . definition. content, [
131
+ . paragraph( . init( inlineContent: [ . text( " Third definition " ) ] ) )
132
+ ] )
133
+ }
134
+
135
+ do {
136
+ let item = termList. items. dropFirst ( 3 ) . first
137
+ XCTAssertEqual ( item? . term. inlineContent, [
138
+ . reference( identifier: RenderReferenceIdentifier ( " doc://unit-test/documentation/ModuleName " ) , isActive: true , overridingTitle: nil , overridingTitleInlineContent: nil )
139
+ ] )
140
+ XCTAssertEqual ( item? . definition. content, [
141
+ . paragraph( . init( inlineContent: [ . text( " Fourth definition " ) ] ) )
142
+ ] )
143
+ }
144
+
145
+ do {
146
+ let item = termList. items. dropFirst ( 4 ) . first
147
+ XCTAssertEqual ( item? . term. inlineContent, [
148
+ . reference( identifier: RenderReferenceIdentifier ( " doc://com.external.testbundle/path/to/something " ) , isActive: true , overridingTitle: nil , overridingTitleInlineContent: nil )
149
+ ] )
150
+ XCTAssertEqual ( item? . definition. content, [
151
+ . paragraph( . init( inlineContent: [ . text( " Fifth definition " ) ] ) )
152
+ ] )
153
+ }
154
+ }
155
+
49
156
func testRenderingListWithAllTermListItems( ) throws {
50
157
let jsonFixtureItems = try discussionContents ( fileName: " term-lists-2 " )
51
158
guard jsonFixtureItems. count == 1 else {
0 commit comments