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
|
//
// 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:_:)`` instead.
@_spi(Experimental)
public func confirmation<R>(
_ comment: Comment? = nil,
expectedCount: some Confirmation.ExpectedCount,
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) {
Issue.record(
expectedCount.issueKind(forActualCount: actualCount),
comments: Array(comment),
backtrace: .current(),
sourceLocation: sourceLocation
)
}
}
return try await body(confirmation)
}
@_spi(Experimental)
extension Confirmation {
/// A protocol that describes a range expression that can be used with
/// ``confirmation(_:expectedCount:isolation:sourceLocation:_:)-9rt6m``.
///
/// This protocol represents any expression that describes a range of
/// confirmation counts. For example, the expression `1 ..< 10` automatically
/// conforms to it.
///
/// You do not generally need to add conformances to this type yourself. It is
/// used by the testing library to abstract away the different range types
/// provided by the Swift standard library.
public protocol ExpectedCount: Sendable, RangeExpression<Int> {}
}
extension Confirmation.ExpectedCount {
/// Get an instance of ``Issue/Kind-swift.enum`` corresponding to this value.
///
/// - Parameters:
/// - actualCount: The actual count for the failed confirmation.
///
/// - Returns: An instance of ``Issue/Kind-swift.enum`` that describes `self`.
fileprivate func issueKind(forActualCount actualCount: Int) -> Issue.Kind {
switch self {
case let expectedCount as ClosedRange<Int> where expectedCount.lowerBound == expectedCount.upperBound:
return .confirmationMiscounted(actual: actualCount, expected: expectedCount.lowerBound)
case let expectedCount as Range<Int> where expectedCount.lowerBound == expectedCount.upperBound - 1:
return .confirmationMiscounted(actual: actualCount, expected: expectedCount.lowerBound)
default:
return .confirmationOutOfRange(actual: actualCount, expected: self)
}
}
}
@_spi(Experimental)
extension ClosedRange<Int>: Confirmation.ExpectedCount {}
@_spi(Experimental)
extension PartialRangeFrom<Int>: Confirmation.ExpectedCount {}
@_spi(Experimental)
extension PartialRangeThrough<Int>: Confirmation.ExpectedCount {}
@_spi(Experimental)
extension PartialRangeUpTo<Int>: Confirmation.ExpectedCount {}
@_spi(Experimental)
extension Range<Int>: Confirmation.ExpectedCount {}
|