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
|
/*
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 VariantCollectionTests: XCTestCase {
let testCollection = VariantCollection(defaultValue: "default value", objectiveCValue: "Objective-C value")
let testCollectionWithMultipleVariants = VariantCollection(
defaultValue: "default value",
variants: [
.init(
traits: [.interfaceLanguage("language A")],
patch: [.replace(value: "language A value")]
),
.init(
traits: [.interfaceLanguage("language B")],
patch: [.replace(value: "language B value")]
),
]
)
func testCreatesObjectiveCVariant() {
XCTAssertEqual(testCollection.defaultValue, "default value")
guard case .replace(let value) = testCollection.variants[0].patch[0] else {
XCTFail("Unexpected patch value")
return
}
XCTAssertEqual(value, "Objective-C value")
}
func testEncodesDefaultValueAndAddsVariantsInEncoder() throws {
let encoder = RenderJSONEncoder.makeEncoder()
let encodedAndDecodedValue = try JSONDecoder()
.decode(VariantCollection<String>.self, from: encoder.encode(testCollectionWithMultipleVariants))
XCTAssertEqual(encodedAndDecodedValue.defaultValue, "default value")
XCTAssert(encodedAndDecodedValue.variants.isEmpty)
let variants = try XCTUnwrap((encoder.userInfo[.variantOverrides] as? VariantOverrides)?.values)
XCTAssertEqual(variants.count, 2)
for (index, variant) in variants.enumerated() {
let expectedLanguage: String
let expectedValue: String
switch index {
case 0:
expectedLanguage = "language A"
expectedValue = "language A value"
case 1:
expectedLanguage = "language B"
expectedValue = "language B value"
default: continue
}
XCTAssertEqual(variant.traits, [.interfaceLanguage(expectedLanguage)])
guard case .replace(_, let value) = variant.patch[0] else {
XCTFail("Unexpected patch operation")
return
}
XCTAssertEqual(value.value as! String, expectedValue)
}
}
func testMapValues() {
let testCollection = testCollection.mapValues { value -> String? in
if value == "default value" {
return "default value transformed"
}
return nil
}
XCTAssertEqual(testCollection.defaultValue, "default value transformed")
guard case .replace(let value)? = testCollection.variants.first?.patch.first else {
XCTFail()
return
}
XCTAssertNil(value)
}
}
|