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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2022-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
import Markdown
@testable import SwiftDocC
final class DirectiveArgumentWrappedTests: XCTestCase {
// MARK: - Declarations
// A custom type that is directive-argument-convertible
enum Something: String, CaseIterable, DirectiveArgumentValueConvertible {
case something
}
// These are all declared directly on the test case so that the property wrappers can be easily accessed in the test.
// MARK: Values
// Without default values
@DirectiveArgumentWrapped
var boolean: Bool
@DirectiveArgumentWrapped
var number: Int
@DirectiveArgumentWrapped
var customValue: Something
// With default values
@DirectiveArgumentWrapped
var booleanWithDefault: Bool = false
@DirectiveArgumentWrapped
var numberWithDefault: Int = 0
@DirectiveArgumentWrapped
var customValueWithDefault: Something = .something
// MARK: Optional values
// Without default values
@DirectiveArgumentWrapped
var optionalBoolean: Bool?
@DirectiveArgumentWrapped
var optionalNumber: Int?
@DirectiveArgumentWrapped
var optionalCustomValue: Something?
// With default values
@DirectiveArgumentWrapped
var optionalBooleanWithDefault: Bool? = true
@DirectiveArgumentWrapped
var optionalNumberWithDefault: Int? = 0
@DirectiveArgumentWrapped
var optionalCustomValueWithDefault: Something? = .something
@DirectiveArgumentWrapped
var optionalBooleanWithNilDefault: Bool? = nil
@DirectiveArgumentWrapped
var optionalNumberWithNilDefault: Int? = nil
@DirectiveArgumentWrapped
var optionalCustomValueWithNilDefault: Something? = nil
// MARK: Explicit allowed values
// Non-optional
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var booleanWithAllowedValues: Bool
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var numberWithAllowedValues: Int
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var customValueWithAllowedValues: Something
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var booleanWithAllowedValuesAndDefaultValue: Bool = false
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var numberWithAllowedValuesAndDefaultValue: Int = 0
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var customValueWithAllowedValuesAndDefaultValue: Something = .something
// Optional
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var optionalBooleanWithAllowedValues: Bool?
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var optionalNumberWithAllowedValues: Int?
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var optionalCustomValueWithAllowedValues: Something?
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var optionalBooleanWithAllowedValuesAndDefaultValue: Bool? = false
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var optionalNumberWithAllowedValuesAndDefaultValue: Int? = 0
@DirectiveArgumentWrapped(
parseArgument: { _, _ in nil },
allowedValues: ["one", "two", "three"])
var optionalCustomValueWithAllowedValuesAndDefaultValue: Something? = .something
// MARK: Non-convertible values
// A custom type that _isn't_ directive-argument-convertible
enum SomethingNonConvertible: String {
case someValue
}
@DirectiveArgumentWrapped(
parseArgument: { _, _ in .someValue }
)
var nonConvertibleValueWithCustomParsing: SomethingNonConvertible
@DirectiveArgumentWrapped(
parseArgument: { _, _ in .someValue }
)
var nonConvertibleValueWithCustomParsingAndDefaultValue: SomethingNonConvertible = .someValue
@DirectiveArgumentWrapped(
parseArgument: { _, _ in .someValue }
)
var optionalNonConvertibleValueWithCustomParsing: SomethingNonConvertible?
@DirectiveArgumentWrapped(
parseArgument: { _, _ in .someValue }
)
var optionalNonConvertibleValueWithCustomParsingAndNilDefault: SomethingNonConvertible? = nil
@DirectiveArgumentWrapped(
parseArgument: { _, _ in .someValue }
)
var optionalNonConvertibleValueWithCustomParsingAndDefaultValue: SomethingNonConvertible? = .someValue
// MARK: - Test assertions
func testTypeDisplayName() throws {
// MARK: Values
// Without default values
XCTAssertEqual(_boolean.typeDisplayName, "Bool")
XCTAssertEqual(_boolean.allowedValues, ["true", "false"])
XCTAssertEqual(_boolean.required, true, "Argument without default value is required")
XCTAssertEqual(_number.typeDisplayName, "Int")
XCTAssertEqual(_number.allowedValues, nil)
XCTAssertEqual(_number.required, true, "Argument without default value is required")
XCTAssertEqual(_customValue.typeDisplayName, "Something")
XCTAssertEqual(_customValue.allowedValues, ["something"])
XCTAssertEqual(_customValue.required, true, "Argument without default value is required")
// With default values
XCTAssertEqual(_booleanWithDefault.typeDisplayName, "Bool = false")
XCTAssertEqual(_booleanWithDefault.allowedValues, ["true", "false"])
XCTAssertEqual(_booleanWithDefault.required, false, "Argument has default value to fallback to")
XCTAssertEqual(_numberWithDefault.typeDisplayName, "Int = 0")
XCTAssertEqual(_numberWithDefault.allowedValues, nil)
XCTAssertEqual(_numberWithDefault.required, false, "Argument has default value to fallback to")
XCTAssertEqual(_customValueWithDefault.typeDisplayName, "Something = something")
XCTAssertEqual(_customValueWithDefault.allowedValues, ["something"])
XCTAssertEqual(_customValueWithDefault.required, false, "Argument has default value to fallback to")
// MARK: Optional values
XCTAssertEqual(_optionalBoolean.typeDisplayName, "Bool?")
XCTAssertEqual(_optionalBoolean.allowedValues, ["true", "false"])
XCTAssertEqual(_optionalBoolean.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalNumber.typeDisplayName, "Int?")
XCTAssertEqual(_optionalNumber.allowedValues, nil)
XCTAssertEqual(_optionalNumber.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalCustomValue.typeDisplayName, "Something?")
XCTAssertEqual(_optionalCustomValue.allowedValues, ["something"])
XCTAssertEqual(_optionalCustomValue.required, false, "Argument with optional type is not required")
// With nil default values
XCTAssertEqual(_optionalBooleanWithNilDefault.typeDisplayName, "Bool?")
XCTAssertEqual(_optionalBooleanWithNilDefault.allowedValues, ["true", "false"])
XCTAssertEqual(_optionalBooleanWithNilDefault.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalNumberWithNilDefault.typeDisplayName, "Int?")
XCTAssertEqual(_optionalNumberWithNilDefault.allowedValues, nil)
XCTAssertEqual(_optionalNumberWithNilDefault.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalCustomValueWithNilDefault.typeDisplayName, "Something?")
XCTAssertEqual(_optionalCustomValueWithNilDefault.allowedValues, ["something"])
XCTAssertEqual(_optionalCustomValueWithNilDefault.required, false, "Argument with optional type is not required")
// With default values
XCTAssertEqual(_optionalBooleanWithDefault.typeDisplayName, "Bool = true")
XCTAssertEqual(_optionalBooleanWithDefault.allowedValues, ["true", "false"])
XCTAssertEqual(_optionalBooleanWithDefault.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalNumberWithDefault.typeDisplayName, "Int = 0")
XCTAssertEqual(_optionalNumberWithDefault.allowedValues, nil)
XCTAssertEqual(_optionalNumberWithDefault.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalCustomValueWithDefault.typeDisplayName, "Something = something")
XCTAssertEqual(_optionalCustomValueWithDefault.allowedValues, ["something"])
XCTAssertEqual(_optionalCustomValueWithDefault.required, false, "Argument with optional type is not required")
// MARK: Explicit allowed values
// Non-optional
XCTAssertEqual(_booleanWithAllowedValues.typeDisplayName, "Bool")
XCTAssertEqual(_booleanWithAllowedValues.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_booleanWithAllowedValues.required, true, "Argument without default value is required")
XCTAssertEqual(_numberWithAllowedValues.typeDisplayName, "Int")
XCTAssertEqual(_numberWithAllowedValues.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_numberWithAllowedValues.required, true, "Argument without default value is required")
XCTAssertEqual(_customValueWithAllowedValues.typeDisplayName, "Something")
XCTAssertEqual(_customValueWithAllowedValues.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_customValueWithAllowedValues.required, true, "Argument without default value is required")
XCTAssertEqual(_booleanWithAllowedValuesAndDefaultValue.typeDisplayName, "Bool = false")
XCTAssertEqual(_booleanWithAllowedValuesAndDefaultValue.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_booleanWithAllowedValuesAndDefaultValue.required, false, "Argument has default value to fallback to")
XCTAssertEqual(_numberWithAllowedValuesAndDefaultValue.typeDisplayName, "Int = 0")
XCTAssertEqual(_numberWithAllowedValuesAndDefaultValue.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_numberWithAllowedValuesAndDefaultValue.required, false, "Argument has default value to fallback to")
XCTAssertEqual(_customValueWithAllowedValuesAndDefaultValue.typeDisplayName, "Something = something")
XCTAssertEqual(_customValueWithAllowedValuesAndDefaultValue.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_customValueWithAllowedValuesAndDefaultValue.required, false, "Argument has default value to fallback to")
// Optional
XCTAssertEqual(_optionalBooleanWithAllowedValues.typeDisplayName, "Bool?")
XCTAssertEqual(_optionalBooleanWithAllowedValues.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_optionalBooleanWithAllowedValues.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalNumberWithAllowedValues.typeDisplayName, "Int?")
XCTAssertEqual(_optionalNumberWithAllowedValues.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_optionalNumberWithAllowedValues.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalCustomValueWithAllowedValues.typeDisplayName, "Something?")
XCTAssertEqual(_optionalCustomValueWithAllowedValues.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_optionalCustomValueWithAllowedValues.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalBooleanWithAllowedValuesAndDefaultValue.typeDisplayName, "Bool = false")
XCTAssertEqual(_optionalBooleanWithAllowedValuesAndDefaultValue.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_optionalBooleanWithAllowedValuesAndDefaultValue.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalNumberWithAllowedValuesAndDefaultValue.typeDisplayName, "Int = 0")
XCTAssertEqual(_optionalNumberWithAllowedValuesAndDefaultValue.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_optionalNumberWithAllowedValuesAndDefaultValue.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalCustomValueWithAllowedValuesAndDefaultValue.typeDisplayName, "Something = something")
XCTAssertEqual(_optionalCustomValueWithAllowedValuesAndDefaultValue.allowedValues, ["one", "two", "three"], "Argument has explicitly specified allowed values")
XCTAssertEqual(_optionalCustomValueWithAllowedValuesAndDefaultValue.required, false, "Argument with optional type is not required")
// MARK: Non-convertible values
XCTAssertEqual(_nonConvertibleValueWithCustomParsing.typeDisplayName, "SomethingNonConvertible")
XCTAssertEqual(_nonConvertibleValueWithCustomParsing.allowedValues, nil)
XCTAssertEqual(_nonConvertibleValueWithCustomParsing.required, true, "Argument without default value is required")
XCTAssertEqual(_nonConvertibleValueWithCustomParsingAndDefaultValue.typeDisplayName, "SomethingNonConvertible = someValue")
XCTAssertEqual(_nonConvertibleValueWithCustomParsingAndDefaultValue.allowedValues, nil)
XCTAssertEqual(_nonConvertibleValueWithCustomParsingAndDefaultValue.required, false, "Argument with default value is not required")
XCTAssertEqual(_optionalNonConvertibleValueWithCustomParsing.typeDisplayName, "SomethingNonConvertible?")
XCTAssertEqual(_optionalNonConvertibleValueWithCustomParsing.allowedValues, nil)
XCTAssertEqual(_optionalNonConvertibleValueWithCustomParsing.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalNonConvertibleValueWithCustomParsingAndNilDefault.typeDisplayName, "SomethingNonConvertible?")
XCTAssertEqual(_optionalNonConvertibleValueWithCustomParsingAndNilDefault.allowedValues, nil)
XCTAssertEqual(_optionalNonConvertibleValueWithCustomParsingAndNilDefault.required, false, "Argument with optional type is not required")
XCTAssertEqual(_optionalNonConvertibleValueWithCustomParsingAndDefaultValue.typeDisplayName, "SomethingNonConvertible = someValue")
XCTAssertEqual(_optionalNonConvertibleValueWithCustomParsingAndDefaultValue.allowedValues, nil)
XCTAssertEqual(_optionalNonConvertibleValueWithCustomParsingAndDefaultValue.required, false, "Argument with optional type is not required")
}
}
|