diff --git a/Sources/Nimble/Matchers/ThrowAssertion.swift b/Sources/Nimble/Matchers/ThrowAssertion.swift index 5bb790a96..2653329c0 100644 --- a/Sources/Nimble/Matchers/ThrowAssertion.swift +++ b/Sources/Nimble/Matchers/ThrowAssertion.swift @@ -6,7 +6,7 @@ import CwlPreconditionTesting import CwlPosixPreconditionTesting #endif -public func throwAssertion() -> Predicate { +public func throwAssertion() -> Predicate { return Predicate { actualExpression in #if arch(x86_64) && canImport(Darwin) let message = ExpectationMessage.expectedTo("throw an assertion") @@ -30,7 +30,7 @@ public func throwAssertion() -> Predicate { } #endif do { - try actualExpression.evaluate() + _ = try actualExpression.evaluate() } catch { actualError = error } diff --git a/Sources/Nimble/Matchers/ThrowError.swift b/Sources/Nimble/Matchers/ThrowError.swift index d294ba39d..80df1db6b 100644 --- a/Sources/Nimble/Matchers/ThrowError.swift +++ b/Sources/Nimble/Matchers/ThrowError.swift @@ -11,7 +11,7 @@ import Foundation /// /// nil arguments indicates that the matcher should not attempt to match against /// that parameter. -public func throwError() -> Predicate { +public func throwError() -> Predicate { return Predicate { actualExpression in var actualError: Error? do { @@ -39,7 +39,7 @@ public func throwError() -> Predicate { /// /// nil arguments indicates that the matcher should not attempt to match against /// that parameter. -public func throwError(_ error: T, closure: ((Error) -> Void)? = nil) -> Predicate { +public func throwError(_ error: T, closure: ((Error) -> Void)? = nil) -> Predicate { return Predicate { actualExpression in var actualError: Error? do { @@ -87,7 +87,7 @@ public func throwError(_ error: T, closure: ((Error) -> Void)? = nil) /// /// nil arguments indicates that the matcher should not attempt to match against /// that parameter. -public func throwError(_ error: T, closure: ((T) -> Void)? = nil) -> Predicate { +public func throwError(_ error: T, closure: ((T) -> Void)? = nil) -> Predicate { return Predicate { actualExpression in var actualError: Error? do { @@ -135,9 +135,10 @@ public func throwError(_ error: T, closure: ((T) -> Void)? /// /// nil arguments indicates that the matcher should not attempt to match against /// that parameter. -public func throwError( +public func throwError( errorType: T.Type, - closure: ((T) -> Void)? = nil) -> Predicate { + closure: ((T) -> Void)? = nil +) -> Predicate { return Predicate { actualExpression in var actualError: Error? do { @@ -198,7 +199,7 @@ public func throwError( /// values of the existential type `Error` in the closure. /// /// The closure only gets called when an error was thrown. -public func throwError(closure: @escaping ((Error) -> Void)) -> Predicate { +public func throwError(closure: @escaping ((Error) -> Void)) -> Predicate { return Predicate { actualExpression in var actualError: Error? do { @@ -234,7 +235,7 @@ public func throwError(closure: @escaping ((Error) -> Void)) -> Predicate { /// values of the existential type `Error` in the closure. /// /// The closure only gets called when an error was thrown. -public func throwError(closure: @escaping ((T) -> Void)) -> Predicate { +public func throwError(closure: @escaping ((T) -> Void)) -> Predicate { return Predicate { actualExpression in var actualError: Error? do { diff --git a/Tests/NimbleTests/Matchers/ThrowAssertionTest.swift b/Tests/NimbleTests/Matchers/ThrowAssertionTest.swift index 0741fb659..1e58c357b 100644 --- a/Tests/NimbleTests/Matchers/ThrowAssertionTest.swift +++ b/Tests/NimbleTests/Matchers/ThrowAssertionTest.swift @@ -62,4 +62,10 @@ final class ThrowAssertionTest: XCTestCase { } #endif } + + func testNonVoidClosure() { + #if canImport(Darwin) + expect { () -> Int in fatalError() }.to(throwAssertion()) + #endif + } } diff --git a/Tests/NimbleTests/Matchers/ThrowErrorTest.swift b/Tests/NimbleTests/Matchers/ThrowErrorTest.swift index 08092cf83..36ec3b3d3 100644 --- a/Tests/NimbleTests/Matchers/ThrowErrorTest.swift +++ b/Tests/NimbleTests/Matchers/ThrowErrorTest.swift @@ -6,7 +6,7 @@ enum NimbleError: Error { case cry } -enum EquatableError: Error { +enum EquatableError: Error, Equatable { case parameterized(x: Int) } @@ -19,16 +19,6 @@ extension EquatableError: CustomDebugStringConvertible { } } -extension EquatableError: Equatable { -} - -func == (lhs: EquatableError, rhs: EquatableError) -> Bool { - switch (lhs, rhs) { - case (.parameterized(let l), .parameterized(let r)): - return l == r - } -} - enum CustomDebugStringConvertibleError: Error { // swiftlint:disable identifier_name case a @@ -151,4 +141,27 @@ final class ThrowErrorTest: XCTestCase { expect { throw NimbleError.laugh }.to(throwError(NimbleError.laugh, closure: closure)) } } + + func testNonVoidClosure() { + expect { return 1 }.toNot(throwError()) + expect { return 2 }.toNot(throwError(NimbleError.laugh)) + expect { return 3 }.toNot(throwError(errorType: NimbleError.self)) + expect { return 4 }.toNot(throwError(EquatableError.parameterized(x: 1))) + expect { return 5 }.toNot(throwError(EquatableError.parameterized(x: 2))) + + // swiftlint:disable unused_closure_parameter + + // Generic typed closure + expect { return "1" }.toNot(throwError { error in }) + // Explicit typed closure + expect { return "2" }.toNot(throwError { (error: EquatableError) in }) + // Typed closure over errorType argument + expect { return "3" }.toNot(throwError(errorType: EquatableError.self) { error in }) + // Typed closure over error argument + expect { return "4" }.toNot(throwError(NimbleError.laugh) { (error: Error) in }) + // Typed closure over error argument + expect { return "5" }.toNot(throwError(NimbleError.laugh) { (error: Error) in }) + + // swiftlint:enable unused_closure_parameter + } } diff --git a/Tests/NimbleTests/XCTestManifests.swift b/Tests/NimbleTests/XCTestManifests.swift index dc4a74aaf..3879b1e2c 100644 --- a/Tests/NimbleTests/XCTestManifests.swift +++ b/Tests/NimbleTests/XCTestManifests.swift @@ -422,6 +422,7 @@ extension ThrowAssertionTest { ("testErrorThrown", testErrorThrown), ("testNegativeMatch", testNegativeMatch), ("testNegativeMessage", testNegativeMessage), + ("testNonVoidClosure", testNonVoidClosure), ("testPositiveMatch", testPositiveMatch), ("testPositiveMessage", testPositiveMessage), ("testPostAssertionCodeNotRun", testPostAssertionCodeNotRun), @@ -437,6 +438,7 @@ extension ThrowErrorTest { ("testNegativeMatchesDoNotCallClosureWithoutError", testNegativeMatchesDoNotCallClosureWithoutError), ("testNegativeMatchesWithClosure", testNegativeMatchesWithClosure), ("testNegativeNegatedMatches", testNegativeNegatedMatches), + ("testNonVoidClosure", testNonVoidClosure), ("testPositiveMatches", testPositiveMatches), ("testPositiveMatchesWithClosures", testPositiveMatchesWithClosures), ("testPositiveNegatedMatches", testPositiveNegatedMatches),