|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import XCTest |
| 12 | +@testable import SwiftDocC |
| 13 | + |
| 14 | +class URLReferenceTests: XCTestCase { |
| 15 | + struct MockReference: URLReference { |
| 16 | + static var baseURL: URL = URL(string: "/mocks/")! |
| 17 | + } |
| 18 | + func testRenderURLIgnoresAbsoluteWebURLs() throws { |
| 19 | + let testUrl = try XCTUnwrap(URL(string: "https://example.com/")) |
| 20 | + let urlReference = MockReference() |
| 21 | + XCTAssertEqual(urlReference.renderURL(for: testUrl, prefixComponent: nil), testUrl) |
| 22 | + } |
| 23 | + |
| 24 | + func testRenderURLIgnoresPrefacedURLs() throws { |
| 25 | + let testUrl = try XCTUnwrap(URL(string: "/mocks/example.com/mock-name")) |
| 26 | + let urlReference = MockReference() |
| 27 | + XCTAssertEqual(urlReference.renderURL(for: testUrl, prefixComponent: nil), testUrl) |
| 28 | + } |
| 29 | + |
| 30 | + func testRenderURLPreparesUnprefacedURLs() throws { |
| 31 | + let testUrl = try XCTUnwrap(URL(string: "file://full/path/to/mock-name")) |
| 32 | + let expectedUrl = try XCTUnwrap(URL(string: "/mocks/mock-name")) |
| 33 | + let urlReference = MockReference() |
| 34 | + XCTAssertEqual(urlReference.renderURL(for: testUrl, prefixComponent: nil), expectedUrl) |
| 35 | + } |
| 36 | + |
| 37 | + func testImageReferenceRoundtripsAcrossBundles() throws { |
| 38 | + // Encode the reference in bundle 1 |
| 39 | + var encoder = RenderJSONEncoder.makeEncoder(assetPrefixComponent: "com.example.bundle1") |
| 40 | + var asset = DataAsset() |
| 41 | + asset.register(URL(string: "image.png")!, with: .init()) |
| 42 | + let reference = ImageReference(identifier: .init("image"), imageAsset: asset) |
| 43 | + |
| 44 | + // Verify it was encoded correctly |
| 45 | + var jsonData = try XCTUnwrap(try encoder.encode(reference)) |
| 46 | + var decodedReference = try RenderJSONDecoder.makeDecoder().decode(ImageReference.self, from: jsonData) |
| 47 | + XCTAssertEqual(Array(decodedReference.asset.metadata.keys), [URL(string: "/images/com.example.bundle1/image.png")!]) |
| 48 | + |
| 49 | + // Re-encode the reference from bundle 1 in bundle 2 and ensure that the URL has not changed |
| 50 | + encoder = RenderJSONEncoder.makeEncoder(assetPrefixComponent: "com.example.bundle2") |
| 51 | + jsonData = try XCTUnwrap(try encoder.encode(decodedReference)) |
| 52 | + decodedReference = try RenderJSONDecoder.makeDecoder().decode(ImageReference.self, from: jsonData) |
| 53 | + XCTAssertEqual(Array(decodedReference.asset.metadata.keys), [URL(string: "/images/com.example.bundle1/image.png")!]) |
| 54 | + } |
| 55 | + |
| 56 | + func testDownloadReferenceRoundtripsAcrossBundles() throws { |
| 57 | + // Encode the reference in bundle 1 |
| 58 | + var encoder = RenderJSONEncoder.makeEncoder(assetPrefixComponent: "com.example.bundle1") |
| 59 | + let reference = DownloadReference(identifier: .init("download"), renderURL: URL(string: "download.zip")!, checksum: nil) |
| 60 | + |
| 61 | + // Verify it was encoded correctly |
| 62 | + var jsonData = try XCTUnwrap(try encoder.encode(reference)) |
| 63 | + var decodedReference = try RenderJSONDecoder.makeDecoder().decode(DownloadReference.self, from: jsonData) |
| 64 | + XCTAssertEqual(decodedReference.url, URL(string: "/downloads/com.example.bundle1/download.zip")!) |
| 65 | + |
| 66 | + // Re-encode the reference from bundle 1 in bundle 2 and ensure that the URL has not changed |
| 67 | + encoder = RenderJSONEncoder.makeEncoder(assetPrefixComponent: "com.example.bundle2") |
| 68 | + jsonData = try XCTUnwrap(try encoder.encode(decodedReference)) |
| 69 | + decodedReference = try RenderJSONDecoder.makeDecoder().decode(DownloadReference.self, from: jsonData) |
| 70 | + XCTAssertEqual(decodedReference.url, URL(string: "/downloads/com.example.bundle1/download.zip")!) |
| 71 | + } |
| 72 | + |
| 73 | + func testVideoReferenceRoundtripsAcrossBundles() throws { |
| 74 | + // Encode the reference in bundle 1 |
| 75 | + var encoder = RenderJSONEncoder.makeEncoder(assetPrefixComponent: "com.example.bundle1") |
| 76 | + var asset = DataAsset() |
| 77 | + asset.register(URL(string: "video.mov")!, with: .init()) |
| 78 | + let reference = VideoReference(identifier: .init("video"), videoAsset: asset, poster: nil) |
| 79 | + |
| 80 | + // Verify it was encoded correctly |
| 81 | + var jsonData = try XCTUnwrap(try encoder.encode(reference)) |
| 82 | + var decodedReference = try RenderJSONDecoder.makeDecoder().decode(VideoReference.self, from: jsonData) |
| 83 | + XCTAssertEqual(Array(decodedReference.asset.metadata.keys), [URL(string: "/videos/com.example.bundle1/video.mov")!]) |
| 84 | + |
| 85 | + // Re-encode the reference from bundle 1 in bundle 2 and ensure that the URL has not changed |
| 86 | + encoder = RenderJSONEncoder.makeEncoder(assetPrefixComponent: "com.example.bundle2") |
| 87 | + jsonData = try XCTUnwrap(try encoder.encode(decodedReference)) |
| 88 | + decodedReference = try RenderJSONDecoder.makeDecoder().decode(VideoReference.self, from: jsonData) |
| 89 | + XCTAssertEqual(Array(decodedReference.asset.metadata.keys), [URL(string: "/videos/com.example.bundle1/video.mov")!]) |
| 90 | + } |
| 91 | +} |
0 commit comments