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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
|
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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
//
@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing
@Test(/* name unspecified */ .hidden)
@Sendable func freeSyncFunction() {}
@Sendable func freeAsyncFunction() async {}
@Test("Named Free Sync Function", .hidden)
@Sendable func namedFreeSyncFunction() {}
@Sendable func freeSyncFunctionParameterized(i: Int) {}
@Sendable func freeAsyncFunctionParameterized(_ s: String) async {}
@Sendable func freeSyncFunctionParameterized2(_ i: Int, _ j: String) {}
// This type ensures the parser can correctly infer that f() is a member
// function even though @Test is preceded by another attribute or is embedded in
// a #if statement.
@Suite(.hidden) struct TestWithPrecedingAttribute {
@inlinable @Test(.hidden) func f() {}
#if true
@Test(.hidden)
#endif
func g() {}
}
@`Suite`(.hidden) struct `SuiteWithBackticks` {
@`Test`(.hidden, .`tags`(.namedConstant)) func `testWithBackticks`() {
#`expect`(Bool(true))
}
}
private enum FixtureData {
static let zeroUpTo100 = 0 ..< 100
static let smallStringArray = ["a", "b", "c", "d"]
static let stringReturningClosureArray = [{ @Sendable in "" }]
}
@Suite(.hidden)
@_nonSendable
private struct NonSendableTests {
@Test(.hidden)
func succeeds() throws {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
func parameterized(i: Int) throws {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100, FixtureData.smallStringArray)
func parameterized2(i: Int, j k: String) throws {}
}
@Suite(.hidden)
struct SendableTests: Sendable {
@Test(.hidden)
func succeeds() throws {}
@Test(.hidden)
func succeedsAsync() async throws {}
@Test(.hidden, .disabled("Some comment"))
func disabled() throws {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
func parameterized(i: Int) async throws {}
@Test(.hidden) static func `static`() async throws {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
static func `reserved1`(`reserved2`: Int) async throws {}
@Suite(.hidden, .tags(.namedConstant))
struct NestedSendableTests: Sendable {
@Test(.hidden, .tags(.anotherConstant))
func succeeds() throws {}
@Test(.hidden)
func otherSucceeds() throws {}
}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
func parameterizedOwned(i: __owned Int) {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
func parameterizedShared(i: __shared Int) {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
func parameterizedBorrowing(i: borrowing Int) {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
func parameterizedBorrowingAsync(i: borrowing Int) async {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
func parameterizedConsuming(i: consuming Int) {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
func parameterizedConsumingAsync(i: consuming Int) async { }
@Test(.hidden, arguments: FixtureData.stringReturningClosureArray)
func parameterizedAcceptingFunction(f: @Sendable () -> String) {}
}
@Suite("Named Sendable test type", .hidden)
struct NamedSendableTests: Sendable {}
#if !SWT_NO_GLOBAL_ACTORS
@Suite(.hidden)
@MainActor
struct MainActorIsolatedTests {
@Test(.hidden)
func succeeds() throws {}
@Test(.hidden)
func succeedsAsync() async throws {}
@Test(.hidden)
nonisolated func succeedsNonisolated() {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
func parameterized(i: Int) async throws {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
nonisolated func parameterizedNonisolated(i: Int) async throws {}
}
#endif
@Suite(.hidden)
actor ActorTests {
@Test(.hidden)
func succeeds() throws {}
@Test(.hidden)
func succeedsAsync() async throws {}
@Test(.hidden)
nonisolated func succeedsNonisolated() {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
func parameterized(i: Int) async throws {}
@Test(.hidden, arguments: FixtureData.zeroUpTo100)
nonisolated func parameterizedNonisolated(i: Int) async throws {}
}
@Suite(.hidden) class NonFinalClassTests {
@Test(.hidden) func f() {}
}
@Suite(.hidden)
struct TestsWithStaticMemberAccessBySelfKeyword {
static let x = 0 ..< 100
@Sendable static func f(max: Int) -> Range<Int> {
0 ..< max
}
@Test(.hidden, arguments: Self.x)
func f(i: Int) {}
#if FIXED_135346598
@Test(.hidden, arguments: Self.f(max: 100))
func g(i: Int) {}
@Test(.hidden, arguments: [Self.f(max:)])
func h(i: @Sendable (Int) -> Range<Int>) {}
struct Box<RawValue>: Sendable, RawRepresentable where RawValue: Sendable {
var rawValue: RawValue
}
@Test(.hidden, arguments: [Box(rawValue: Self.f(max:))])
func j(i: Box<@Sendable (Int) -> Range<Int>>) {}
#endif
struct Nested {
static let x = 0 ..< 100
}
@Test(.hidden, arguments: Self.Nested.x)
func i(i: Int) {}
}
@Test(.hidden, arguments: [0]) func A(🙃: Int) {}
@Test(.hidden, arguments: [0]) func A(🙂: Int) {}
func asyncTrait() async -> some SuiteTrait & TestTrait {
.bug("")
}
@Suite(.hidden, await asyncTrait())
struct TestsWithAsyncArguments {
static func asyncCollection() async throws -> [Int] { [] }
@Test(.hidden, await asyncTrait(), arguments: try await asyncCollection())
func f(i: Int) {}
@Test(.hidden, arguments: try await asyncCollection(), try await asyncCollection())
func g(i: Int, j: Int) {}
}
@Test(
arguments: [0] // Meaningful trivia: This line comment should be omitted during macro expansion
)
func parameterizedTestWithTrailingComment(value: Int) {}
@Test(.hidden) func // Meaningful trivia: intentional newline before name
globalMultiLineTestDecl() async {}
@Suite(.hidden)
struct MultiLineSuite {
@Test(.hidden) func // Meaningful trivia: intentional newline before name
multiLineTestDecl() async {}
@Test(.hidden) static func // Meaningful trivia: intentional newline before name
staticMultiLineTestDecl() async {}
}
@Suite("Miscellaneous tests")
struct MiscellaneousTests {
@Test("Free function's name")
func unnamedFreeFunctionTest() async throws {
let testFunction = try #require(await Test.all.first(where: { $0.name.contains("freeSyncFunction") }))
#expect(testFunction.name == "freeSyncFunction()")
}
@Test("Test suite type's name")
func unnamedMemberFunctionTest() async throws {
let testType = try #require(await test(for: SendableTests.self))
#expect(testType.name == "SendableTests")
}
@Test("Free function has custom display name")
func namedFreeFunctionTest() async throws {
#expect(await Test.all.first { $0.displayName == "Named Free Sync Function" && !$0.isSuite && $0.containingTypeInfo == nil } != nil)
}
@Test("Member function has custom display name")
func namedMemberFunctionTest() async throws {
let testType = try #require(await test(for: NamedSendableTests.self))
#expect(testType.displayName == "Named Sendable test type")
}
@Test("Free functions are runnable")
func freeFunction() async throws {
await Test(testFunction: freeSyncFunction).run()
await Test(testFunction: freeSyncFunction).run()
await Test(testFunction: freeAsyncFunction).run()
}
@Test("Instance methods are runnable")
func instanceMethod() async throws {
await runTestFunction(named: "succeeds()", in: SendableTests.self)
await runTestFunction(named: "succeedsAsync()", in: SendableTests.self)
}
static var testSuiteTypes: [Any.Type] {
var result: [Any.Type] = [
NonSendableTests.self,
SendableTests.self,
]
#if !SWT_NO_GLOBAL_ACTORS
result.append(MainActorIsolatedTests.self)
#endif
return result
}
@Test("Test suite types are runnable", arguments: testSuiteTypes)
func suiteTypeIsRunnable(_ type: Any.Type) async throws {
await runTest(for: type)
}
@Test("Parameterized free functions are runnable")
func parameterizedFreeFunction() async throws {
await Test(arguments: FixtureData.zeroUpTo100, testFunction: freeSyncFunctionParameterized).run()
await Test(arguments: FixtureData.smallStringArray, testFunction: freeAsyncFunctionParameterized).run()
await Test(arguments: FixtureData.zeroUpTo100, FixtureData.smallStringArray, testFunction: freeSyncFunctionParameterized2).run()
}
@Test("Parameterized member functions are runnable")
func parameterizedFunctionsOnTypes() async throws {
await runTestFunction(named: "parameterized(i:)", in: NonSendableTests.self)
await runTestFunction(named: "parameterized2(i:j:)", in: NonSendableTests.self)
await runTestFunction(named: "parameterized(i:)", in: SendableTests.self)
#if !SWT_NO_GLOBAL_ACTORS
await runTestFunction(named: "parameterized(i:)", in: MainActorIsolatedTests.self)
await runTestFunction(named: "parameterizedNonisolated(i:)", in: MainActorIsolatedTests.self)
#endif
}
@Test("Parameterized cases are all executed (1 argument)")
func parameterizedCasesAreExecuted() async throws {
let range = 0 ..< 100
await confirmation("Iterated \(range.count) times", expectedCount: range.count) { iteratedNTimes in
let test = Test(arguments: range) { i in
iteratedNTimes()
}
await test.run()
}
}
@Test("Parameterized cases are all executed (2 arguments)")
func parameterized2CasesAreExecuted() async throws {
struct Case: Sendable, Hashable {
var i: Int
var j: String
}
actor ValueGrid {
var cells = [Case: Bool]()
init(range: Range<Int>, strings: [String]) {
for i in range {
for j in strings {
cells[.init(i: i, j: j)] = false
}
}
let cellCount = cells.count
#expect(range.count * strings.count == cellCount)
}
func setCell(_ i: Int, _ j: String) {
cells[.init(i: i, j: j)] = true
}
func validateCells() {
#expect(cells.map(\.value).allSatisfy { $0 })
}
}
let range = 0 ..< 10
let strings = ["a", "b", "c", "d", "e", "f", "g"]
let valueGrid = ValueGrid(range: range, strings: strings)
let test = Test(.tags(.red), .enabled(if: true), arguments: range, strings) { i, j in
await valueGrid.setCell(i, j)
}
await test.run()
await valueGrid.validateCells()
}
@Test("Test.parameters property")
func parametersProperty() async throws {
do {
let theTest = try #require(await test(for: SendableTests.self))
#expect(theTest.parameters == nil)
}
do {
let test = try #require(await testFunction(named: "succeeds()", in: SendableTests.self))
let parameters = try #require(test.parameters)
#expect(parameters.isEmpty)
} catch {}
do {
let test = try #require(await testFunction(named: "parameterized(i:)", in: NonSendableTests.self))
let parameters = try #require(test.parameters)
#expect(parameters.count == 1)
let firstParameter = try #require(parameters.first)
#expect(firstParameter.index == 0)
#expect(firstParameter.firstName == "i")
#expect(firstParameter.secondName == nil)
let firstParameterTypeInfo = try #require(firstParameter.typeInfo)
#expect(firstParameterTypeInfo.fullyQualifiedName == "Swift.Int")
#expect(firstParameterTypeInfo.unqualifiedName == "Int")
} catch {}
do {
let test = try #require(await testFunction(named: "parameterized2(i:j:)", in: NonSendableTests.self))
let parameters = try #require(test.parameters)
#expect(parameters.count == 2)
let firstParameter = try #require(parameters.first)
#expect(firstParameter.index == 0)
#expect(firstParameter.firstName == "i")
#expect(firstParameter.secondName == nil)
let secondParameter = try #require(parameters.last)
#expect(secondParameter.index == 1)
#expect(secondParameter.firstName == "j")
#expect(secondParameter.secondName == "k")
let secondParameterTypeInfo = try #require(secondParameter.typeInfo)
#expect(secondParameterTypeInfo.fullyQualifiedName == "Swift.String")
#expect(secondParameterTypeInfo.unqualifiedName == "String")
} catch {}
}
@Test("Test.sourceLocation.column is used when sorting", arguments: 0 ..< 10)
func testsSortByColumn(_: Int) async throws {
// Keep these test declarations on one line! We're testing that they sort
// correctly with the same file ID and line number.
var tests = [Test(name: "A") {}, Test(name: "B") {}, Test(name: "C") {}, Test(name: "D") {}, Test(name: "E") {}, Test(name: "F") {}, Test(name: "G") {},]
tests.shuffle()
tests.sort { $0.sourceLocation < $1.sourceLocation }
#expect(tests.map(\.displayName) == ["A", "B", "C", "D", "E", "F", "G"])
}
@Test("Parameterizing over a collection with a poor underestimatedCount property")
func testParameterizedOverCollectionWithBadUnderestimatedCount() async throws {
struct PoorlyEstimatedCollection<C>: Collection where C: Collection & Sendable {
var collection: C
var startIndex: C.Index {
collection.startIndex
}
var endIndex: C.Index {
collection.endIndex
}
func index(after i: C.Index) -> C.Index {
collection.index(after: i)
}
subscript(position: C.Index) -> C.Element {
collection[position]
}
var underestimatedCount: Int {
0
}
}
let range = PoorlyEstimatedCollection(collection: 1 ... 100)
let test = Test(arguments: range) { i in
#expect(i > 0)
}
await test.run()
}
@Test("Properties related to parameterization")
func parameterizationRelatedProperties() async throws {
do {
let test = Test.__type(SendableTests.self, displayName: "", traits: [], sourceLocation: #_sourceLocation)
#expect(!test.isParameterized)
#expect(test.testCases == nil)
#expect(test.parameters == nil)
}
do {
let test = Test {}
#expect(!test.isParameterized)
let testCases = try #require(test.testCases)
#expect(testCases.underestimatedCount == 1)
let parameters = try #require(test.parameters)
#expect(parameters.isEmpty)
}
do {
let test = Test(arguments: 0 ..< 100, parameters: [Test.Parameter(index: 0, firstName: "i", type: Int.self)]) { _ in }
#expect(test.isParameterized)
let testCases = try #require(test.testCases)
#expect(testCases.underestimatedCount == 100)
let parameters = try #require(test.parameters)
#expect(parameters.count == 1)
let firstParameter = try #require(parameters.first)
#expect(firstParameter.firstName == "i")
}
do {
let test = Test(arguments: 0 ..< 100, 0 ..< 100, parameters: [
Test.Parameter(index: 0, firstName: "i", type: Int.self),
Test.Parameter(index: 1, firstName: "j", secondName: "value", type: Int.self),
]) { _, _ in }
#expect(test.isParameterized)
let testCases = try #require(test.testCases)
#expect(testCases.underestimatedCount == 100 * 100)
let parameters = try #require(test.parameters)
#expect(parameters.count == 2)
let firstParameter = try #require(parameters.first)
#expect(firstParameter.firstName == "i")
let lastParameter = try #require(parameters.last)
#expect(lastParameter.firstName == "j")
#expect(lastParameter.secondName == "value")
}
}
@Test("Test.id property")
func id() async throws {
let typeTest = Test.__type(SendableTests.self, displayName: "SendableTests", traits: [], sourceLocation: #_sourceLocation)
#expect(String(describing: typeTest.id) == "TestingTests.SendableTests")
let fileID = "Module/Y.swift"
let filePath = "/Y.swift"
let line = 12345
let column = 67890
let sourceLocation = SourceLocation(fileID: fileID, filePath: filePath, line: line, column: column)
let testFunction = Test.__function(named: "myTestFunction()", in: nil, xcTestCompatibleSelector: nil, displayName: nil, traits: [], sourceLocation: sourceLocation) {}
#expect(String(describing: testFunction.id) == "Module.myTestFunction()/Y.swift:12345:67890")
}
@Test("Test.ID.parent property")
func idParent() async throws {
let idWithParent = Test.ID(["A", "B"])
#expect(idWithParent.parent != nil)
#expect(idWithParent.parent?.parent == nil)
let functionIDWithParent = Test.ID(moduleName: "A", nameComponents: ["B"], sourceLocation: #_sourceLocation)
let parentOfFunctionID = try #require(functionIDWithParent.parent)
#expect(parentOfFunctionID.moduleName == "A")
#expect(parentOfFunctionID.nameComponents == ["B"])
#expect(parentOfFunctionID.sourceLocation == nil)
}
@Test("Test.ID.init() with no arguments")
func idWithNoArguments() {
let id = Test.ID([])
#expect(id.moduleName == "")
#expect(id.nameComponents.isEmpty)
#expect(id.sourceLocation == nil)
#expect(id.keyPathRepresentation == [""])
}
@Test("Test.all deduping")
func allTestDeduping() {
let tests = [Test(name: "A") {}, Test(name: "B") {}, Test(name: "C") {}, Test(name: "D") {}, Test(name: "E") {}, Test(name: "F") {}, Test(name: "G") {},]
var duplicatedTests = tests
duplicatedTests += tests
duplicatedTests.shuffle()
let mappedTests = Test.testsByID(duplicatedTests)
#expect(mappedTests.count == tests.count)
#expect(mappedTests.values.allSatisfy { tests.contains($0) })
}
@Test("failureBreakpoint() call")
func failureBreakpointCall() {
failureBreakpointValue = 0
failureBreakpoint()
#expect(failureBreakpointValue == 1)
}
@available(_clockAPI, *)
@Test("Repeated calls to #expect() run in reasonable time", .disabled("time-sensitive"))
func repeatedlyExpect() {
let duration = Test.Clock().measure {
for _ in 0 ..< 1_000_000 {
#expect(true as Bool)
}
}
#expect(duration < .seconds(1))
}
}
|