-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathTest.swift
393 lines (351 loc) · 13.7 KB
/
Test.swift
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
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 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
//
#if _runtime(_ObjC)
public import ObjectiveC
#endif
/// A type representing a test or suite.
///
/// An instance of this type may represent:
///
/// - A type containing zero or more tests (i.e. a _test suite_);
/// - An individual test function (possibly contained within a type); or
/// - A test function parameterized over one or more sequences of inputs.
///
/// Two instances of this type are considered to be equal if the values of their
/// ``Test/id-swift.property`` properties are equal.
public struct Test: Sendable {
/// The name of this instance.
///
/// The value of this property is equal to the name of the symbol to which the
/// ``Test`` attribute is applied (that is, the name of the type or function.)
/// To get the customized display name specified as part of the ``Test``
/// attribute, use the ``Test/displayName`` property.
public var name: String
/// The customized display name of this instance, if specified.
public var displayName: String?
/// The set of traits added to this instance when it was initialized.
public var traits: [any Trait] {
willSet {
// Prevent programmatically adding suite traits to test functions or test
// traits to test suites.
func traitsAreCorrectlyTyped() -> Bool {
if isSuite {
return newValue.allSatisfy { $0 is any SuiteTrait }
} else {
return newValue.allSatisfy { $0 is any TestTrait }
}
}
precondition(traitsAreCorrectlyTyped(), "Programmatically added an inapplicable trait to test \(self)")
}
}
/// The source location of this test.
public var sourceLocation: SourceLocation
/// Information about the type containing this test, if any.
///
/// If a test is associated with a free function or static function, the value
/// of this property is `nil`. To determine if a specific instance of ``Test``
/// refers to this type itself, check the ``isSuite`` property.
var containingTypeInfo: TypeInfo?
/// The XCTest-compatible Objective-C selector corresponding to this
/// instance's underlying test function.
///
/// On platforms that do not support Objective-C interop, the value of this
/// property is always `nil`.
@_spi(ForToolsIntegrationOnly)
public nonisolated(unsafe) var xcTestCompatibleSelector: __XCTestCompatibleSelector?
/// An enumeration describing the evaluation state of a test's cases.
///
/// This use of `@unchecked Sendable` and of `AnySequence` in this type's
/// cases is necessary because it is not currently possible to express
/// `Sequence<Test.Case> & Sendable` as an existential (`any`)
/// ([96960993](rdar://96960993)). It is also not possible to have a value of
/// an underlying generic sequence type without specifying its generic
/// parameters.
fileprivate enum TestCasesState: @unchecked Sendable {
/// The test's cases have not yet been evaluated.
///
/// - Parameters:
/// - function: The function to call to evaluate the test's cases. The
/// result is a sequence of test cases.
case unevaluated(_ function: @Sendable () async throws -> AnySequence<Test.Case>)
/// The test's cases have been evaluated.
///
/// - Parameters:
/// - testCases: The test's cases.
case evaluated(_ testCases: AnySequence<Test.Case>)
/// An error was thrown when the testing library attempted to evaluate the
/// test's cases.
///
/// - Parameters:
/// - error: The thrown error.
case failed(_ error: any Error)
}
/// The evaluation state of this test's cases, if any.
///
/// If this test represents a suite type, the value of this property is `nil`.
fileprivate var testCasesState: TestCasesState?
/// The set of test cases associated with this test, if any.
///
/// - Precondition: This property may only be accessed on test instances
/// representing suites, or on test functions whose ``testCaseState``
/// indicates a successfully-evaluated state.
///
/// For parameterized tests, each test case is associated with a single
/// combination of parameterized inputs. For non-parameterized tests, a single
/// test case is synthesized. For test suite types (as opposed to test
/// functions), the value of this property is `nil`.
var testCases: (some Sequence<Test.Case>)? {
testCasesState.map { testCasesState in
guard case let .evaluated(testCases) = testCasesState else {
// Callers are expected to first attempt to evaluate a test's cases by
// calling `evaluateTestCases()`, and are never expected to access this
// property after evaluating a test's cases if that evaluation threw an
// error (because the test cannot be run.) If an error was thrown, a
// `Runner.Plan` is expected to record issue for the test, rather than
// attempt to run it, and thus never access this property.
preconditionFailure("Attempting to access test cases with invalid state. Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new and include this information: \(String(reflecting: testCasesState))")
}
return testCases
}
}
/// Equivalent to ``testCases``, but without requiring that the test cases be
/// evaluated first.
///
/// Most callers should not use this property and should prefer ``testCases``
/// since it will help catch logic errors in the testing library. Use this
/// property if you are interested in the test's test cases, but the test has
/// not been evaluated by an instance of ``Runner/Plan`` (e.g. if you are
/// implementing `swift test list`.)
var uncheckedTestCases: (some Sequence<Test.Case>)? {
testCasesState.flatMap { testCasesState in
if case let .evaluated(testCases) = testCasesState {
return testCases
}
return nil
}
}
/// Evaluate this test's cases if they have not been evaluated yet.
///
/// The arguments of a test are captured into a closure so they can be lazily
/// evaluated only if the test will run to avoid unnecessary work. This
/// function may be called once that determination has been made, to perform
/// this evaluation once. The resulting arguments are stored on this instance
/// so that subsequent calls to ``testCases`` do not cause the arguments to be
/// re-evaluated.
///
/// - Throws: Any error caught while first evaluating the test arguments.
mutating func evaluateTestCases() async throws {
if case let .unevaluated(function) = testCasesState {
do {
let sequence = try await function()
self.testCasesState = .evaluated(sequence)
} catch {
self.testCasesState = .failed(error)
throw error
}
}
}
/// Whether or not this test is parameterized.
public var isParameterized: Bool {
parameters?.isEmpty == false
}
/// The test function parameters, if any.
///
/// If this instance represents a test function, the value of this property is
/// an array of values describing its parameters, which may be empty if the
/// test function is non-parameterized. If this instance represents a test
/// suite, the value of this property is `nil`.
@_spi(Experimental) @_spi(ForToolsIntegrationOnly)
public var parameters: [Parameter]?
/// Whether or not this instance is a test suite containing other tests.
///
/// Instances of ``Test`` attached to types rather than functions are test
/// suites. They do not contain any test logic of their own, but they may
/// have traits added to them that also apply to their subtests.
///
/// A test suite can be declared using the ``Suite(_:_:)`` macro.
public var isSuite: Bool {
containingTypeInfo != nil && testCasesState == nil
}
/// Whether or not this instance was synthesized at runtime.
///
/// During test planning, suites that are not explicitly marked with the
/// `@Suite` attribute are synthesized from available type information before
/// being added to the plan. For such suites, the value of this property is
/// `true`.
@_spi(ForToolsIntegrationOnly)
public var isSynthesized: Bool = false
/// Initialize an instance of this type representing a test suite type.
init(
displayName: String? = nil,
traits: [any Trait],
sourceLocation: SourceLocation,
containingTypeInfo: TypeInfo,
isSynthesized: Bool = false
) {
self.name = containingTypeInfo.unqualifiedName
self.displayName = displayName
self.traits = traits
self.sourceLocation = sourceLocation
self.containingTypeInfo = containingTypeInfo
self.isSynthesized = isSynthesized
}
/// Initialize an instance of this type representing a test function.
init<S>(
name: String,
displayName: String? = nil,
traits: [any Trait],
sourceLocation: SourceLocation,
containingTypeInfo: TypeInfo? = nil,
xcTestCompatibleSelector: __XCTestCompatibleSelector? = nil,
testCases: @escaping @Sendable () async throws -> Test.Case.Generator<S>,
parameters: [Parameter]
) {
self.name = name
self.displayName = displayName
self.traits = traits
self.sourceLocation = sourceLocation
self.containingTypeInfo = containingTypeInfo
self.xcTestCompatibleSelector = xcTestCompatibleSelector
self.testCasesState = .unevaluated { .init(try await testCases()) }
self.parameters = parameters
}
/// Initialize an instance of this type representing a test function.
init<S>(
name: String,
displayName: String? = nil,
traits: [any Trait],
sourceLocation: SourceLocation,
containingTypeInfo: TypeInfo? = nil,
xcTestCompatibleSelector: __XCTestCompatibleSelector? = nil,
testCases: Test.Case.Generator<S>,
parameters: [Parameter]
) {
self.name = name
self.displayName = displayName
self.traits = traits
self.sourceLocation = sourceLocation
self.containingTypeInfo = containingTypeInfo
self.xcTestCompatibleSelector = xcTestCompatibleSelector
self.testCasesState = .evaluated(.init(testCases))
self.parameters = parameters
}
}
// MARK: - Equatable, Hashable
extension Test: Equatable, Hashable {
public static func ==(lhs: Test, rhs: Test) -> Bool {
lhs.id == rhs.id
}
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
#if !SWT_NO_SNAPSHOT_TYPES
// MARK: - Snapshotting
extension Test {
/// A serializable snapshot of a ``Test`` instance.
@_spi(ForToolsIntegrationOnly)
public struct Snapshot: Sendable, Codable, Identifiable {
private enum CodingKeys: String, CodingKey {
case id
case name
case displayName
case sourceLocation
case testCases
case parameters
case comments
case tags
case associatedBugs
case _timeLimit = "timeLimit"
}
/// The ID of this test.
public var id: Test.ID
/// The name of this test.
///
/// ## See Also
///
/// - ``Test/name``
public var name: String
/// The customized display name of this test, if any.
public var displayName: String?
/// The source location of this test.
public var sourceLocation: SourceLocation
/// The set of test cases associated with this test, if any.
///
/// If the ``Test`` this instance was snapshotted from represented a
/// parameterized test function but its test cases had not yet been
/// evaluated when the snapshot was taken, or the evaluation attempt failed,
/// the value of this property will be an empty array.
public var testCases: [Test.Case.Snapshot]?
/// The test function parameters, if any.
///
/// ## See Also
///
/// - ``Test/parameters``
public var parameters: [Parameter]?
/// The complete set of comments about this test from all of its traits.
public var comments: [Comment]
/// The complete, unique set of tags associated with this test.
public var tags: Set<Tag>
/// The set of bugs associated with this test.
///
/// For information on how to associate a bug with a test, see the
/// documentation for ``Bug``.
public var associatedBugs: [Bug]
// Backing storage for ``Test/Snapshot/timeLimit``.
private var _timeLimit: TimeValue?
/// The maximum amount of time a test may run for before timing out.
@available(_clockAPI, *)
public var timeLimit: Duration? {
_timeLimit.map(Duration.init)
}
/// Initialize an instance of this type by snapshotting the specified test.
///
/// - Parameters:
/// - test: The original test to snapshot.
public init(snapshotting test: borrowing Test) {
id = test.id
name = test.name
displayName = test.displayName
sourceLocation = test.sourceLocation
parameters = test.parameters
comments = test.comments
tags = test.tags
associatedBugs = test.associatedBugs
if #available(_clockAPI, *) {
_timeLimit = test.timeLimit.map(TimeValue.init)
}
testCases = test.testCasesState.map { testCasesState in
if case let .evaluated(testCases) = testCasesState {
testCases.map(Test.Case.Snapshot.init(snapshotting:))
} else {
[]
}
}
}
/// Whether or not this test is parameterized.
///
/// ## See Also
///
/// - ``Test/isParameterized``
public var isParameterized: Bool {
parameters?.isEmpty == false
}
/// Whether or not this instance is a test suite containing other tests.
///
/// ## See Also
///
/// - ``Test/isSuite``
public var isSuite: Bool {
testCases == nil
}
}
}
#endif