Skip to content

Commit 2552d4e

Browse files
authored
Merge pull request #579 from Quick/refactor-throwassertion-matcher
[7.x] Refactor `throwAssertion` matcher using `Predicate.init`
2 parents 515471b + 6708e5c commit 2552d4e

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

Sources/Nimble/Matchers/ThrowAssertion.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import Foundation
22

33
public func throwAssertion() -> Predicate<Void> {
4-
return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in
4+
return Predicate { actualExpression in
55
#if arch(x86_64) && (os(macOS) || os(iOS) || os(tvOS) || os(watchOS)) && !SWIFT_PACKAGE
6-
failureMessage.postfixMessage = "throw an assertion"
7-
failureMessage.actualValue = nil
8-
9-
var succeeded = true
6+
let message = ExpectationMessage.expectedTo("throw an assertion")
107

8+
var actualError: Error?
119
let caughtException: BadInstructionException? = catchBadInstruction {
1210
#if os(tvOS)
1311
if !NimbleEnvironment.activeInstance.suppressTVOSAssertionWarning {
@@ -27,21 +25,19 @@ public func throwAssertion() -> Predicate<Void> {
2725
#endif
2826
do {
2927
try actualExpression.evaluate()
30-
} catch let error {
31-
succeeded = false
32-
failureMessage.postfixMessage += "; threw error instead <\(error)>"
28+
} catch {
29+
actualError = error
3330
}
3431
}
3532

36-
if !succeeded {
37-
return false
38-
}
39-
40-
if caughtException == nil {
41-
return false
33+
if let actualError = actualError {
34+
return PredicateResult(
35+
bool: false,
36+
message: message.appended(message: "; threw error instead <\(actualError)>")
37+
)
38+
} else {
39+
return PredicateResult(bool: caughtException != nil, message: message)
4240
}
43-
44-
return true
4541
#elseif SWIFT_PACKAGE
4642
fatalError("The throwAssertion Nimble matcher does not currently support Swift CLI." +
4743
" You can silence this error by placing the test case inside an #if !SWIFT_PACKAGE" +

Tests/NimbleTests/Matchers/ThrowAssertionTest.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import Nimble
44

55
#if (os(macOS) || os(iOS) || os(tvOS) || os(watchOS)) && !SWIFT_PACKAGE
66

7+
private let error: Error = NSError(domain: "test", code: 0, userInfo: nil)
8+
79
final class ThrowAssertionTest: XCTestCase, XCTestCaseProvider {
810
static var allTests: [(String, (ThrowAssertionTest) -> () throws -> Void)] {
911
return [
@@ -21,7 +23,7 @@ final class ThrowAssertionTest: XCTestCase, XCTestCaseProvider {
2123
}
2224

2325
func testErrorThrown() {
24-
expect { throw NSError(domain: "test", code: 0, userInfo: nil) }.toNot(throwAssertion())
26+
expect { throw error }.toNot(throwAssertion())
2527
}
2628

2729
func testPostAssertionCodeNotRun() {
@@ -50,6 +52,10 @@ final class ThrowAssertionTest: XCTestCase, XCTestCaseProvider {
5052
failsWithErrorMessage("expected to throw an assertion") {
5153
expect { () -> Void? in return }.to(throwAssertion())
5254
}
55+
56+
failsWithErrorMessage("expected to throw an assertion; threw error instead <\(error)>") {
57+
expect { throw error }.to(throwAssertion())
58+
}
5359
}
5460

5561
func testNegativeMessage() {

0 commit comments

Comments
 (0)