Skip to content

Commit 40d9c1c

Browse files
author
angu
committed
Account for possible data races when evaluating Assert expressions when throwing an error to be compatible with Swift 6.0
1 parent a44e4f3 commit 40d9c1c

3 files changed

Lines changed: 22 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## HEAD
44

5+
## 1.1.0
6+
7+
* Ensure compatibility with Swift 6.0
8+
* Account for possible data races when evaluating expressions in case they throwing an error to be
9+
510
## 1.0.1
611

712
* Fixed `AsyncAssertThrowsError(_:_:_:)` would not execute the error handler closure in case the evaluated expression throws an error.

Sources/SwiftAsyncAssert/AssertExpressionEvaluator.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88
import XCTest
99

1010
enum AssertExpressionEvaluator {
11-
12-
/// Evaluated in case an evaluated expression throws an error
13-
static var failurePrompt: (String, StaticString, UInt) -> Void = { XCTFail($0, file: $1, line: $2) }
14-
11+
1512
static func evaluate(_ expression: () async throws -> Void,
16-
_ message: @autoclosure () -> String = "",
17-
function: StaticString = #function,
18-
file: StaticString = #filePath,
19-
line: UInt = #line) async {
13+
_ message: @autoclosure () -> String = "",
14+
function: StaticString = #function,
15+
file: StaticString = #filePath,
16+
line: UInt = #line,
17+
failureEvaluation: (String, StaticString, UInt) -> Void = { XCTFail($0, file: $1, line: $2) }) async {
2018
do {
2119
try await expression()
2220
} catch {
23-
Self.failurePrompt(ThrownErrorFormatter.formattedStringForThrownError(error,
24-
in: "\(function)",
25-
message: message()),
26-
file,
27-
line)
21+
failureEvaluation(ThrownErrorFormatter.formattedStringForThrownError(error,
22+
in: "\(function)",
23+
message: message()),
24+
file,
25+
line)
2826
}
2927
}
3028
}

Tests/SwiftAsyncAssertTests/AssertExpressionEvaluatorTest.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,23 @@ import XCTest
1111

1212
final class AssertExpressionEvaluatorTest: XCTestCase {
1313

14-
func test_should_succeed_when_evaluated_expression_executes_successfully() async throws {
15-
var isFailurePromptCalled = false
16-
17-
AssertExpressionEvaluator.failurePrompt = { _, _, _ in
18-
isFailurePromptCalled = true
19-
}
14+
private var isFailurePromptCalled = false
2015

16+
func test_should_succeed_when_evaluated_expression_executes_successfully() async throws {
2117
await AssertExpressionEvaluator.evaluate({
2218
// Here gets code executed that does not throw an error
19+
}, failureEvaluation: { _, _, _ in
20+
self.isFailurePromptCalled = true
2321
})
2422

2523
XCTAssertFalse(isFailurePromptCalled)
2624
}
2725

2826
func test_should_fail_when_evaluated_expression_throws_error() async throws {
29-
var isFailurePromptCalled = false
30-
31-
AssertExpressionEvaluator.failurePrompt = { _, _, _ in
32-
isFailurePromptCalled = true
33-
}
34-
3527
await AssertExpressionEvaluator.evaluate({
3628
throw TestingError.systemFailedToWork
29+
}, failureEvaluation: { _, _, _ in
30+
self.isFailurePromptCalled = true
3731
})
3832

3933
XCTAssertTrue(isFailurePromptCalled)

0 commit comments

Comments
 (0)