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
|
/*
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
class ResolvedTopicReferenceTests: XCTestCase {
func testReferenceURL() {
let firstTopicReference = ResolvedTopicReference(
bundleIdentifier: "bundleID",
path: "/path/sub-path",
fragment: "fragment",
sourceLanguage: .swift
)
XCTAssertEqual(firstTopicReference.absoluteString, "doc://bundleID/path/sub-path#fragment")
let secondTopicReference = ResolvedTopicReference(
bundleIdentifier: "new-bundleID",
path: "/new-path/sub-path",
fragment: firstTopicReference.fragment,
sourceLanguage: firstTopicReference.sourceLanguage
)
XCTAssertEqual(secondTopicReference.absoluteString, "doc://new-bundleID/new-path/sub-path#fragment")
let thirdTopicReference = secondTopicReference.withFragment(nil)
XCTAssertEqual(thirdTopicReference.absoluteString, "doc://new-bundleID/new-path/sub-path")
// Changing the language does not change the url
XCTAssertEqual(thirdTopicReference.addingSourceLanguages([.metal]).absoluteString, "doc://new-bundleID/new-path/sub-path")
}
func testAppendingReferenceWithEmptyPath() {
// An empty path
do {
let resolvedOriginal = ResolvedTopicReference(bundleIdentifier: "bundleID", path: "/path/sub-path", fragment: "fragment", sourceLanguage: .swift)
let unresolved = UnresolvedTopicReference(topicURL: ValidatedURL(parsingExact: "doc://host-name")!)
XCTAssert(unresolved.path.isEmpty)
let appended = resolvedOriginal.appendingPathOfReference(unresolved)
XCTAssertEqual(appended.path, resolvedOriginal.path)
}
// A path with no url path allowed characters
do {
let resolvedOriginal = ResolvedTopicReference(bundleIdentifier: "bundleID", path: "/path/sub-path", fragment: "fragment", sourceLanguage: .swift)
var components = URLComponents()
components.scheme = "doc"
components.host = "host.name"
components.percentEncodedPath = "%20%20%20" // 3 spaces
let unresolved = UnresolvedTopicReference(topicURL: ValidatedURL(components: components))
XCTAssertFalse(unresolved.path.isEmpty)
let appended = resolvedOriginal.appendingPathOfReference(unresolved)
XCTAssertEqual(appended.path, resolvedOriginal.appendingPath("---").path)
}
}
func testStorageIsConcurrentlyAccessible() throws {
let topicReference = ResolvedTopicReference(
bundleIdentifier: "com.apple.example",
path: "/documentation/path/sub-path",
fragment: nil,
sourceLanguage: .swift
)
// TSan should not report a data race for these three accesses.
// TODO: Run TSan in Swift-CI (rdar://90157829).
DispatchQueue.concurrentPerform(iterations: 5) { _ in
_ = topicReference.url
_ = topicReference.pathComponents
_ = topicReference.absoluteString
}
}
}
|