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
|
/*
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 XCTest
@testable import SwiftDocC
class ReferenceURLTests: XCTestCase {
let imageURL = Bundle.module.url(
forResource: "image file", withExtension: "png", subdirectory: "Test Resources")!
let videoURL = Bundle.module.url(
forResource: "video file", withExtension: "mov", subdirectory: "Test Resources")!
func testImageFileNamesWithSpaceURL() throws {
var asset = DataAsset()
asset.variants = [DataTraitCollection.init(userInterfaceStyle: .light, displayScale: .standard): imageURL]
let reference = ImageReference(identifier: RenderReferenceIdentifier("image"), imageAsset: asset)
let encoder = JSONEncoder()
let data = try encoder.encode(reference)
let object = try JSONSerialization.jsonObject(with: data, options: [])
guard let jsonReference = object as? [String: Any],
let variants = jsonReference["variants"] as? [[String: Any]],
let variant = variants.first,
let url = variant["url"] as? String else {
XCTFail("Could not parse encoded image reference.")
return
}
XCTAssertEqual("/images/image%20file.png", url)
}
func testVideoFileNamesWithSpaceURL() throws {
var asset = DataAsset()
asset.variants = [DataTraitCollection.init(userInterfaceStyle: .light, displayScale: .standard): videoURL]
let reference = VideoReference(identifier: RenderReferenceIdentifier("video"), videoAsset: asset, poster: nil)
let encoder = JSONEncoder()
let data = try encoder.encode(reference)
let object = try JSONSerialization.jsonObject(with: data, options: [])
guard let jsonReference = object as? [String: Any],
let variants = jsonReference["variants"] as? [[String: Any]],
let variant = variants.first,
let url = variant["url"] as? String else {
XCTFail("Could not parse encoded image reference.")
return
}
XCTAssertEqual("/videos/video%20file.mov", url)
}
func testImageVariantsEncodingOrder() throws {
let lightImageURL = URL(string: "/images/figure@2x.png")!
let darkImageURL = URL(string: "/images/figure~dark@2x.png")!
var asset = DataAsset()
asset.variants = [
DataTraitCollection(userInterfaceStyle: .light, displayScale: .double): lightImageURL,
DataTraitCollection(userInterfaceStyle: .dark, displayScale: .double): darkImageURL
]
let reference = ImageReference(identifier: RenderReferenceIdentifier("image"), imageAsset: asset)
let encoder = JSONEncoder()
for _ in 0..<100 {
let data = try encoder.encode(reference)
let object = try JSONSerialization.jsonObject(with: data)
let jsonReference = try XCTUnwrap(object as? [String: Any])
let variants = try XCTUnwrap(jsonReference["variants"] as? [[String: Any]])
let urls = variants.compactMap { $0["url"] as? String }
XCTAssertEqual(urls, [
lightImageURL.path,
darkImageURL.path
])
}
}
func testVideoVariantsEncodingOrder() throws {
let lightImageURL = URL(string: "/videos/video.png")!
let darkImageURL = URL(string: "/videos/video~dark.png")!
var asset = DataAsset()
asset.variants = [
DataTraitCollection(userInterfaceStyle: .light): lightImageURL,
DataTraitCollection(userInterfaceStyle: .dark): darkImageURL
]
let reference = VideoReference(identifier: RenderReferenceIdentifier("video"), videoAsset: asset, poster: nil)
let encoder = JSONEncoder()
for _ in 0..<100 {
let data = try encoder.encode(reference)
let object = try JSONSerialization.jsonObject(with: data)
let jsonReference = try XCTUnwrap(object as? [String: Any])
let variants = try XCTUnwrap(jsonReference["variants"] as? [[String: Any]])
let urls = variants.compactMap { $0["url"] as? String }
XCTAssertEqual(urls, [
lightImageURL.path,
darkImageURL.path
])
}
}
}
|