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
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 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 XCTest
@testable import SwiftDocC
import Markdown
extension XCTestCase {
/// Asserts that a rendered node's content matches expectations.
func assertExpectedContent(
_ renderNode: RenderNode,
sourceLanguage expectedSourceLanguage: String,
symbolKind expectedSymbolKind: String? = nil,
title expectedTitle: String,
navigatorTitle expectedNavigatorTitle: String?,
abstract expectedAbstract: String?,
attributes expectedAttributes: [RenderAttribute]? = nil,
declarationTokens expectedDeclarationTokens: [String]?,
endpointTokens expectedEndpointTokens: [String]? = nil,
httpParameters expectedHTTPParameters: [String]? = nil,
httpBodyType expectedHTTPBodyType: String? = nil,
httpBodyParameters expectedHTTPBodyParameters: [String]? = nil,
httpResponses expectedHTTPResponses: [UInt]? = nil,
discussionSection expectedDiscussionSection: [String]?,
topicSectionIdentifiers expectedTopicSectionIdentifiers: [String],
seeAlsoSectionIdentifiers expectedSeeAlsoSectionIdentifiers: [String]? = nil,
referenceTitles expectedReferenceTitles: [String],
referenceFragments expectedReferenceFragments: [String],
failureMessage failureMessageForField: (_ field: String) -> String,
file: StaticString = #file,
line: UInt = #line
) {
XCTAssertEqual(
renderNode.abstract?.plainText,
expectedAbstract,
failureMessageForField("abstract"),
file: file,
line: line
)
XCTAssertEqual(
(renderNode.primaryContentSections.last as? ContentRenderSection)?.content.paragraphText,
expectedDiscussionSection,
failureMessageForField("discussion section"),
file: file,
line: line
)
XCTAssertEqual(
renderNode.identifier.sourceLanguage.id,
expectedSourceLanguage,
failureMessageForField("source language id"),
file: file,
line: line
)
let attributesSection = renderNode.primaryContentSections.compactMap { $0 as? AttributesRenderSection }.first
XCTAssertEqual(
attributesSection?.attributes,
expectedAttributes,
failureMessageForField("attributes"),
file: file,
line: line
)
XCTAssertEqual(
(renderNode.primaryContentSections.first as? DeclarationsRenderSection)?
.declarations
.flatMap(\.tokens)
.map(\.text),
expectedDeclarationTokens,
failureMessageForField("declaration tokens"),
file: file,
line: line
)
XCTAssertEqual(
(renderNode.primaryContentSections.compactMap { $0 as? RESTEndpointRenderSection })
.flatMap(\.tokens)
.map(\.text),
expectedEndpointTokens ?? [], // compactMap gives an empty [], but should treat it as match for nil, too
failureMessageForField("rest endpoint tokens"),
file: file,
line: line
)
XCTAssertEqual(
(renderNode.primaryContentSections.compactMap { $0 as? RESTParametersRenderSection })
.flatMap { section in
section.items.map { "\($0.name)@\(section.source.rawValue)" }
},
expectedHTTPParameters ?? [], // compactMap gives an empty [], but should treat it as match for nil, too
failureMessageForField("rest parameters"),
file: file,
line: line
)
XCTAssertEqual(
(renderNode.primaryContentSections.first(where: { nil != $0 as? RESTBodyRenderSection }) as? RESTBodyRenderSection)?
.mimeType,
expectedHTTPBodyType,
failureMessageForField("rest body media type"),
file: file,
line: line
)
XCTAssertEqual(
(renderNode.primaryContentSections.first(where: { nil != $0 as? RESTBodyRenderSection }) as? RESTBodyRenderSection)?
.parameters?
.map(\.name) ?? [],
expectedHTTPBodyParameters ?? [],
failureMessageForField("rest body parameters"),
file: file,
line: line
)
XCTAssertEqual(
(renderNode.primaryContentSections.compactMap { $0 as? RESTResponseRenderSection })
.flatMap(\.items)
.map(\.status),
expectedHTTPResponses ?? [], // compactMap gives an empty [], but should treat it as match for nil, too
failureMessageForField("rest responses"),
file: file,
line: line
)
XCTAssertEqual(
renderNode.metadata.navigatorTitle?.map(\.text).joined(),
expectedNavigatorTitle,
failureMessageForField("navigator title"),
file: file,
line: line
)
XCTAssertEqual(
renderNode.metadata.title,
expectedTitle,
failureMessageForField("title"),
file: file,
line: line
)
XCTAssertEqual(
renderNode.metadata.symbolKind,
expectedSymbolKind,
failureMessageForField("symbol kind"),
file: file,
line: line
)
XCTAssertEqual(
renderNode.topicSections.flatMap(\.identifiers),
expectedTopicSectionIdentifiers,
failureMessageForField("topic sections identifiers"),
file: file,
line: line
)
if let expectedSeeAlsoSectionIdentifiers {
XCTAssertEqual(
renderNode.seeAlsoSections.flatMap(\.identifiers),
expectedSeeAlsoSectionIdentifiers,
failureMessageForField("see also sections identifiers"),
file: file,
line: line
)
}
XCTAssertEqual(
renderNode.references.map(\.value).compactMap { reference in
(reference as? TopicRenderReference)?.title
}.sorted(),
expectedReferenceTitles,
failureMessageForField("reference titles"),
file: file,
line: line
)
XCTAssertEqual(
renderNode.references.map(\.value).compactMap { reference in
(reference as? TopicRenderReference)?.fragments?.map(\.text).joined()
}.sorted(),
expectedReferenceFragments,
failureMessageForField("reference fragments"),
file: file,
line: line
)
}
}
|