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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2023 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
class SampleDownloadTests: XCTestCase {
func testDecodeSampleDownloadSymbol() throws {
let downloadSymbolURL = Bundle.module.url(
forResource: "sample-download-symbol", withExtension: "json",
subdirectory: "Rendering Fixtures")!
let data = try Data(contentsOf: downloadSymbolURL)
let symbol = try RenderNode.decode(fromJSON: data)
//
// Sample Download Details
//
guard let section = symbol.sampleDownload else {
XCTFail("Download section not decoded")
return
}
guard case RenderInlineContent.reference(let identifier, let isActive, let overridingTitle, let overridingTitleInlineContent) = section.action else {
XCTFail("Could not decode action reference")
return
}
XCTAssertEqual(identifier.identifier, "doc://org.swift.docc.example/downloads/sample.zip")
XCTAssertTrue(isActive)
XCTAssertEqual(overridingTitle, "Download")
XCTAssertEqual(overridingTitleInlineContent, [.text("Download")])
XCTAssertTrue(section.headings.isEmpty)
XCTAssertTrue(section.rawIndexableTextContent(references: [:]).isEmpty)
XCTAssertEqual(symbol.projectFiles()?.url.absoluteString, "/downloads/project.zip")
XCTAssertEqual(symbol.projectFiles()?.checksum, "ad4adacc8ad53230b59d")
}
func testDecodeSampleDownloadUnavailableSymbol() throws {
let downloadSymbolURL = Bundle.module.url(
forResource: "sample-download-unavailable-symbol", withExtension: "json",
subdirectory: "Rendering Fixtures")!
let data = try Data(contentsOf: downloadSymbolURL)
let symbol = try RenderNode.decode(fromJSON: data)
//
// Unavailable Sample Download Details
//
guard let section = symbol.downloadNotAvailableSummary else {
XCTFail("Download not available section not decoded.")
return
}
XCTAssertEqual(section.count, 1)
guard case let .paragraph(contentParagraph) = section.first else {
XCTFail("Section is not a paragraph.")
return
}
let text = contentParagraph.inlineContent.rawIndexableTextContent(references: symbol.references)
XCTAssertEqual(text, "You can experiment with the code. Just use WiFi Access on your Mac to download WiFi access sample code.")
}
func testParseSampleDownload() throws {
let renderNode = try renderNodeFromSampleBundle(at: "/documentation/SampleBundle/MySample")
let sampleCodeDownload = try XCTUnwrap(renderNode.sampleDownload)
guard case .reference(identifier: let ident, isActive: true, overridingTitle: "Download", overridingTitleInlineContent: nil) = sampleCodeDownload.action else {
XCTFail("Unexpected action in callToAction")
return
}
XCTAssertEqual(ident.identifier, "https://example.com/sample.zip")
}
func testParseSampleLocalDownload() throws {
let renderNode = try renderNodeFromSampleBundle(at: "/documentation/SampleBundle/MyLocalSample")
let sampleCodeDownload = try XCTUnwrap(renderNode.sampleDownload)
guard case .reference(identifier: let ident, isActive: true, overridingTitle: "Download", overridingTitleInlineContent: nil) = sampleCodeDownload.action else {
XCTFail("Unexpected action in callToAction")
return
}
XCTAssertEqual(ident.identifier, "plus.svg")
}
func testSampleDownloadRoundtrip() throws {
let renderNode = try renderNodeFromSampleBundle(at: "/documentation/SampleBundle/MySample")
let encoder = JSONEncoder()
let decoder = JSONDecoder()
let encodedNode = try encoder.encode(renderNode)
let decodedNode = try decoder.decode(RenderNode.self, from: encodedNode)
guard case let .reference(
identifier: origIdent,
isActive: _,
overridingTitle: _,
overridingTitleInlineContent: _
) = renderNode.sampleDownload?.action,
case let .reference(
identifier: decodedIdent,
isActive: _,
overridingTitle: _,
overridingTitleInlineContent: _
) = decodedNode.sampleDownload?.action
else {
XCTFail("RenderNode should have callToAction both before and after roundtrip")
return
}
XCTAssertEqual(origIdent, decodedIdent)
}
private func renderNodeFromSampleBundle(at referencePath: String) throws -> RenderNode {
let (bundle, context) = try testBundleAndContext(named: "SampleBundle")
let reference = ResolvedTopicReference(
bundleIdentifier: bundle.identifier,
path: referencePath,
sourceLanguage: .swift
)
let article = try XCTUnwrap(context.entity(with: reference).semantic as? Article)
var translator = RenderNodeTranslator(
context: context,
bundle: bundle,
identifier: reference,
source: nil
)
return try XCTUnwrap(translator.visitArticle(article) as? RenderNode)
}
func testSampleDownloadRelativeURL() throws {
let renderNode = try renderNodeFromSampleBundle(at: "/documentation/SampleBundle/RelativeURLSample")
let sampleCodeDownload = try XCTUnwrap(renderNode.sampleDownload)
guard case .reference(identifier: let ident, isActive: true, overridingTitle: "Download", overridingTitleInlineContent: nil) = sampleCodeDownload.action else {
XCTFail("Unexpected action in callToAction")
return
}
XCTAssertEqual(ident.identifier, "files/ExternalSample.zip")
// Ensure that the encoded URL still references the entered URL
let downloadReference = try XCTUnwrap(renderNode.references[ident.identifier] as? DownloadReference)
XCTAssertEqual(downloadReference.url.description, "files/ExternalSample.zip")
}
func testExternalLocationRoundtrip() throws {
let renderNode = try renderNodeFromSampleBundle(at: "/documentation/SampleBundle/RelativeURLSample")
let sampleCodeDownload = try XCTUnwrap(renderNode.sampleDownload)
guard case .reference(identifier: let ident, isActive: true, overridingTitle: "Download", overridingTitleInlineContent: nil) = sampleCodeDownload.action else {
XCTFail("Unexpected action in callToAction")
return
}
XCTAssertEqual(ident.identifier, "files/ExternalSample.zip")
// Make sure that the reference data survives a round-trip encoding/decoding.
let downloadReference = try XCTUnwrap(renderNode.references[ident.identifier] as? DownloadReference)
let encoder = JSONEncoder()
encoder.outputFormatting.insert(.sortedKeys)
let decoder = JSONDecoder()
let encodedReference = try encoder.encode(downloadReference)
let firstJson = String(data: encodedReference, encoding: .utf8)
let decodedReference = try decoder.decode(DownloadReference.self, from: encodedReference)
let reEncodedReference = try encoder.encode(decodedReference)
let finalJson = String(data: reEncodedReference, encoding: .utf8)
XCTAssertEqual(firstJson, finalJson)
}
func testExternalLinkOnSampleCodePage() throws {
let renderNode = try renderNodeFromSampleBundle(at: "/documentation/SampleBundle/MyExternalSample")
let sampleCodeDownload = try XCTUnwrap(renderNode.sampleDownload)
guard case .reference(identifier: let identifier, isActive: true, overridingTitle: "View Source", overridingTitleInlineContent: nil) = sampleCodeDownload.action else {
XCTFail("Unexpected action in callToAction")
return
}
XCTAssertEqual(identifier.identifier, "https://www.example.com/source-repository.git")
let reference = try XCTUnwrap(renderNode.references[identifier.identifier] as? DownloadReference)
XCTAssertEqual(reference.url.description, "https://www.example.com/source-repository.git")
}
func testExternalLinkOnRegularArticlePage() throws {
let renderNode = try renderNodeFromSampleBundle(at: "/documentation/SampleBundle/MyArticle")
let sampleCodeDownload = try XCTUnwrap(renderNode.sampleDownload)
guard case .reference(identifier: let identifier, isActive: true, overridingTitle: "Visit", overridingTitleInlineContent: nil) = sampleCodeDownload.action else {
XCTFail("Unexpected action in callToAction")
return
}
XCTAssertEqual(identifier.identifier, "https://www.example.com")
let reference = try XCTUnwrap(renderNode.references[identifier.identifier] as? DownloadReference)
XCTAssertEqual(reference.url.description, "https://www.example.com")
}
/// Ensure that a DownloadReference where the URL is different from the reference identifier
/// can still round-trip through an ExternalLocationReference with the URL and reference identifier intact.
func testRoundTripVerbatimUrl() throws {
let baseReference = DownloadReference(identifier: .init("DownloadReference.zip"), verbatimURL: .init(string: "https://example.com/DownloadReference.zip")!, checksum: nil)
let encoder = JSONEncoder()
let decoder = JSONDecoder()
let encodedReference = try encoder.encode(baseReference)
let roundTripReference = try decoder.decode(DownloadReference.self, from: encodedReference)
XCTAssertEqual(baseReference, roundTripReference)
}
/// Ensure that an ExternalLocationReference loaded from JSON continues to encode the same
/// information after being decoded and re-encoded.
func testRoundTripExternalLocationFromFixture() throws {
let downloadSymbolURL = Bundle.module.url(
forResource: "external-location-custom-url", withExtension: "json",
subdirectory: "Rendering Fixtures")!
let originalData = try Data(contentsOf: downloadSymbolURL)
let originalRenderNode = try RenderNode.decode(fromJSON: originalData)
let encodedRenderNode = try JSONEncoder().encode(originalRenderNode)
let symbol = try RenderNode.decode(fromJSON: encodedRenderNode)
//
// Sample Download Details
//
guard let section = symbol.sampleDownload else {
XCTFail("Download section not decoded")
return
}
guard case RenderInlineContent.reference(let identifier, _, _, _) = section.action else {
XCTFail("Could not decode action reference")
return
}
XCTAssertEqual(identifier.identifier, "doc://org.swift.docc.example/downloads/sample.zip")
let externalReference = try XCTUnwrap(symbol.references[identifier.identifier] as? DownloadReference)
XCTAssertEqual(externalReference.url.description, "https://example.com/ExternalLocation.zip")
}
func testRoundTripDownloadReferenceWithModifiedUrl() throws {
let identifier = RenderReferenceIdentifier("/test/sample.zip")
let originalURL = try XCTUnwrap(URL(string: "/test/sample.zip"))
var reference = DownloadReference(identifier: identifier, verbatimURL: originalURL, checksum: nil)
XCTAssertEqual(reference.url.description, "/test/sample.zip")
let newURL = try XCTUnwrap(URL(string: "https://swift.org/documentation/test/sample.zip"))
reference.url = newURL
let encodedReference = try JSONEncoder().encode(reference)
let decodedReference = try JSONDecoder().decode(DownloadReference.self, from: encodedReference)
XCTAssertEqual(decodedReference.identifier.identifier, "/test/sample.zip")
XCTAssertEqual(decodedReference.url, newURL)
}
func testProjectFilesForCallToActionDirectives() throws {
// Make sure that the `projectFiles()` method correctly returns the DownloadReference
// created by the `@CallToAction` directive.
let renderNode = try renderNodeFromSampleBundle(at: "/documentation/SampleBundle/MySample")
let downloadReference = try XCTUnwrap(renderNode.projectFiles())
XCTAssertEqual(downloadReference.url.description, "https://example.com/sample.zip")
}
func testExplicitNullChecksum() throws {
let identifier = RenderReferenceIdentifier("/test/sample.zip")
let originalURL = try XCTUnwrap(URL(string: "/test/sample.zip"))
let reference = DownloadReference(identifier: identifier, verbatimURL: originalURL, checksum: nil)
let encodedReference = try JSONEncoder().encode(reference)
let encodedJson = try XCTUnwrap(String(data: encodedReference, encoding: .utf8))
XCTAssert(encodedJson.contains(#""checksum":null"#))
}
}
|