-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathConfirmation.swift
233 lines (222 loc) · 8.6 KB
/
Confirmation.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
//
// 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
//
/// A type that can be used to confirm that an event occurs zero or more times.
public struct Confirmation: Sendable {
/// The number of times ``confirm(count:)`` has been called.
///
/// This property is fileprivate because it may be mutated asynchronously and
/// callers may be tempted to use it in ways that result in data races.
fileprivate var count = Locked(rawValue: 0)
/// Confirm this confirmation.
///
/// - Parameters:
/// - count: The number of times to confirm this instance.
///
/// As a convenience, this method can be called by calling the confirmation
/// directly.
public func confirm(count: Int = 1) {
precondition(count > 0)
self.count.add(count)
}
}
// MARK: -
extension Confirmation {
/// Confirm this confirmation.
///
/// - Parameters:
/// - count: The number of times to confirm this instance.
///
/// Calling a confirmation as a function is shorthand for calling its
/// ``confirm(count:)`` method.
public func callAsFunction(count: Int = 1) {
confirm(count: count)
}
}
// MARK: -
/// Confirm that some event occurs during the invocation of a function.
///
/// - Parameters:
/// - comment: An optional comment to apply to any issues generated by this
/// function.
/// - expectedCount: The number of times the expected event should occur when
/// `body` is invoked. The default value of this argument is `1`, indicating
/// that the event should occur exactly once. Pass `0` if the event should
/// _never_ occur when `body` is invoked.
/// - isolation: The actor to which `body` is isolated, if any.
/// - sourceLocation: The source location to which any recorded issues should
/// be attributed.
/// - body: The function to invoke.
///
/// - Returns: Whatever is returned by `body`.
///
/// - Throws: Whatever is thrown by `body`.
///
/// Use confirmations to check that an event occurs while a test is running in
/// complex scenarios where `#expect()` and `#require()` are insufficient. For
/// example, a confirmation may be useful when an expected event occurs:
///
/// - In a context that cannot be awaited by the calling function such as an
/// event handler or delegate callback;
/// - More than once, or never; or
/// - As a callback that is invoked as part of a larger operation.
///
/// To use a confirmation, pass a closure containing the work to be performed.
/// The testing library will then pass an instance of ``Confirmation`` to the
/// closure. Every time the event in question occurs, the closure should call
/// the confirmation:
///
/// ```swift
/// let n = 10
/// await confirmation("Baked buns", expectedCount: n) { bunBaked in
/// foodTruck.eventHandler = { event in
/// if event == .baked(.cinnamonBun) {
/// bunBaked()
/// }
/// }
/// await foodTruck.bake(.cinnamonBun, count: n)
/// }
/// ```
///
/// When the closure returns, the testing library checks if the confirmation's
/// preconditions have been met, and records an issue if they have not.
public func confirmation<R>(
_ comment: Comment? = nil,
expectedCount: Int = 1,
isolation: isolated (any Actor)? = #isolation,
sourceLocation: SourceLocation = #_sourceLocation,
_ body: (Confirmation) async throws -> sending R
) async rethrows -> R {
try await confirmation(
comment,
expectedCount: expectedCount ... expectedCount,
isolation: isolation,
sourceLocation: sourceLocation,
body
)
}
// MARK: - Ranges as expected counts
/// Confirm that some event occurs during the invocation of a function.
///
/// - Parameters:
/// - comment: An optional comment to apply to any issues generated by this
/// function.
/// - expectedCount: A range of integers indicating the number of times the
/// expected event should occur when `body` is invoked.
/// - isolation: The actor to which `body` is isolated, if any.
/// - sourceLocation: The source location to which any recorded issues should
/// be attributed.
/// - body: The function to invoke.
///
/// - Returns: Whatever is returned by `body`.
///
/// - Throws: Whatever is thrown by `body`.
///
/// Use confirmations to check that an event occurs while a test is running in
/// complex scenarios where `#expect()` and `#require()` are insufficient. For
/// example, a confirmation may be useful when an expected event occurs:
///
/// - In a context that cannot be awaited by the calling function such as an
/// event handler or delegate callback;
/// - More than once, or never; or
/// - As a callback that is invoked as part of a larger operation.
///
/// To use a confirmation, pass a closure containing the work to be performed.
/// The testing library will then pass an instance of ``Confirmation`` to the
/// closure. Every time the event in question occurs, the closure should call
/// the confirmation:
///
/// ```swift
/// let minBuns = 5
/// let maxBuns = 10
/// await confirmation(
/// "Baked between \(minBuns) and \(maxBuns) buns",
/// expectedCount: minBuns ... maxBuns
/// ) { bunBaked in
/// foodTruck.eventHandler = { event in
/// if event == .baked(.cinnamonBun) {
/// bunBaked()
/// }
/// }
/// await foodTruck.bakeTray(of: .cinnamonBun)
/// }
/// ```
///
/// When the closure returns, the testing library checks if the confirmation's
/// preconditions have been met, and records an issue if they have not.
///
/// If an exact count is expected, use
/// ``confirmation(_:expectedCount:isolation:sourceLocation:_:)-5mqz2`` instead.
public func confirmation<R>(
_ comment: Comment? = nil,
expectedCount: some RangeExpression<Int> & Sequence<Int> & Sendable,
isolation: isolated (any Actor)? = #isolation,
sourceLocation: SourceLocation = #_sourceLocation,
_ body: (Confirmation) async throws -> sending R
) async rethrows -> R {
let confirmation = Confirmation()
defer {
let actualCount = confirmation.count.rawValue
if !expectedCount.contains(actualCount) {
let issue = Issue(
kind: .confirmationMiscounted(actual: actualCount, expected: expectedCount),
comments: Array(comment),
sourceContext: .init(backtrace: .current(), sourceLocation: sourceLocation)
)
issue.record()
}
}
return try await body(confirmation)
}
/// An overload of ``confirmation(_:expectedCount:isolation:sourceLocation:_:)-l3il``
/// that handles the unbounded range operator (`...`).
///
/// This overload is necessary because `UnboundedRange` does not conform to
/// `RangeExpression`. It effectively always succeeds because any number of
/// confirmations matches, so it is marked unavailable and is not implemented.
@available(*, unavailable, message: "Unbounded range '...' has no effect when used with a confirmation.")
public func confirmation<R>(
_ comment: Comment? = nil,
expectedCount: UnboundedRange,
isolation: isolated (any Actor)? = #isolation,
sourceLocation: SourceLocation = #_sourceLocation,
_ body: (Confirmation) async throws -> R
) async rethrows -> R {
fatalError("Unsupported")
}
/// An overload of ``confirmation(_:expectedCount:isolation:sourceLocation:_:)-l3il``
/// that handles the partial-range-through operator (`...n`).
///
/// This overload is necessary because the lower bound of `PartialRangeThrough`
/// is ambiguous: does it start at `0` or `1`? Test authors should specify a
@available(*, unavailable, message: "Range expression '...n' is ambiguous without an explicit lower bound")
public func confirmation<R>(
_ comment: Comment? = nil,
expectedCount: PartialRangeThrough<Int>,
isolation: isolated (any Actor)? = #isolation,
sourceLocation: SourceLocation = #_sourceLocation,
_ body: (Confirmation) async throws -> R
) async rethrows -> R {
fatalError("Unsupported")
}
/// An overload of ``confirmation(_:expectedCount:isolation:sourceLocation:_:)-l3il``
/// that handles the partial-range-up-to operator (`..<n`).
///
/// This overload is necessary because the lower bound of `PartialRangeUpTo` is
/// ambiguous: does it start at `0` or `1`? Test authors should specify a
@available(*, unavailable, message: "Range expression '..<n' is ambiguous without an explicit lower bound")
public func confirmation<R>(
_ comment: Comment? = nil,
expectedCount: PartialRangeUpTo<Int>,
isolation: isolated (any Actor)? = #isolation,
sourceLocation: SourceLocation = #_sourceLocation,
_ body: (Confirmation) async throws -> R
) async rethrows -> R {
fatalError("Unsupported")
}