Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Nimble/Matchers/ThrowAssertion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import CwlPreconditionTesting
import CwlPosixPreconditionTesting
#endif

public func throwAssertion() -> Predicate<Void> {
public func throwAssertion<Out>() -> Predicate<Out> {
return Predicate { actualExpression in
#if arch(x86_64) && canImport(Darwin)
let message = ExpectationMessage.expectedTo("throw an assertion")
Expand All @@ -30,7 +30,7 @@ public func throwAssertion() -> Predicate<Void> {
}
#endif
do {
try actualExpression.evaluate()
_ = try actualExpression.evaluate()
} catch {
actualError = error
}
Expand Down
15 changes: 8 additions & 7 deletions Sources/Nimble/Matchers/ThrowError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
///
/// nil arguments indicates that the matcher should not attempt to match against
/// that parameter.
public func throwError() -> Predicate<Any> {
public func throwError<Out>() -> Predicate<Out> {
return Predicate { actualExpression in
var actualError: Error?
do {
Expand Down Expand Up @@ -39,7 +39,7 @@ public func throwError() -> Predicate<Any> {
///
/// nil arguments indicates that the matcher should not attempt to match against
/// that parameter.
public func throwError<T: Error>(_ error: T, closure: ((Error) -> Void)? = nil) -> Predicate<Any> {
public func throwError<T: Error, Out>(_ error: T, closure: ((Error) -> Void)? = nil) -> Predicate<Out> {
return Predicate { actualExpression in
var actualError: Error?
do {
Expand Down Expand Up @@ -87,7 +87,7 @@ public func throwError<T: Error>(_ error: T, closure: ((Error) -> Void)? = nil)
///
/// nil arguments indicates that the matcher should not attempt to match against
/// that parameter.
public func throwError<T: Error & Equatable>(_ error: T, closure: ((T) -> Void)? = nil) -> Predicate<Any> {
public func throwError<T: Error & Equatable, Out>(_ error: T, closure: ((T) -> Void)? = nil) -> Predicate<Out> {
return Predicate { actualExpression in
var actualError: Error?
do {
Expand Down Expand Up @@ -135,9 +135,10 @@ public func throwError<T: Error & Equatable>(_ error: T, closure: ((T) -> Void)?
///
/// nil arguments indicates that the matcher should not attempt to match against
/// that parameter.
public func throwError<T: Error>(
public func throwError<T: Error, Out>(
errorType: T.Type,
closure: ((T) -> Void)? = nil) -> Predicate<Any> {
closure: ((T) -> Void)? = nil
) -> Predicate<Out> {
return Predicate { actualExpression in
var actualError: Error?
do {
Expand Down Expand Up @@ -198,7 +199,7 @@ public func throwError<T: Error>(
/// 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<Any> {
public func throwError<Out>(closure: @escaping ((Error) -> Void)) -> Predicate<Out> {
return Predicate { actualExpression in
var actualError: Error?
do {
Expand Down Expand Up @@ -234,7 +235,7 @@ public func throwError(closure: @escaping ((Error) -> Void)) -> Predicate<Any> {
/// values of the existential type `Error` in the closure.
///
/// The closure only gets called when an error was thrown.
public func throwError<T: Error>(closure: @escaping ((T) -> Void)) -> Predicate<Any> {
public func throwError<T: Error, Out>(closure: @escaping ((T) -> Void)) -> Predicate<Out> {
return Predicate { actualExpression in
var actualError: Error?
do {
Expand Down
6 changes: 6 additions & 0 deletions Tests/NimbleTests/Matchers/ThrowAssertionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ final class ThrowAssertionTest: XCTestCase {
}
#endif
}

func testNonVoidClosure() {
#if canImport(Darwin)
expect { () -> Int in fatalError() }.to(throwAssertion())
#endif
}
}
35 changes: 24 additions & 11 deletions Tests/NimbleTests/Matchers/ThrowErrorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ enum NimbleError: Error {
case cry
}

enum EquatableError: Error {
enum EquatableError: Error, Equatable {
case parameterized(x: Int)
}

Expand All @@ -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
Expand Down Expand Up @@ -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 })
Comment thread
ikesyo marked this conversation as resolved.
// Explicit typed closure
expect { return "2" }.toNot(throwError { (error: EquatableError) in })
Comment thread
ikesyo marked this conversation as resolved.
// Typed closure over errorType argument
expect { return "3" }.toNot(throwError(errorType: EquatableError.self) { error in })
Comment thread
ikesyo marked this conversation as resolved.
// Typed closure over error argument
expect { return "4" }.toNot(throwError(NimbleError.laugh) { (error: Error) in })
Comment thread
ikesyo marked this conversation as resolved.
// Typed closure over error argument
expect { return "5" }.toNot(throwError(NimbleError.laugh) { (error: Error) in })
Comment thread
ikesyo marked this conversation as resolved.

// swiftlint:enable unused_closure_parameter
}
}
2 changes: 2 additions & 0 deletions Tests/NimbleTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ extension ThrowAssertionTest {
("testErrorThrown", testErrorThrown),
("testNegativeMatch", testNegativeMatch),
("testNegativeMessage", testNegativeMessage),
("testNonVoidClosure", testNonVoidClosure),
("testPositiveMatch", testPositiveMatch),
("testPositiveMessage", testPositiveMessage),
("testPostAssertionCodeNotRun", testPostAssertionCodeNotRun),
Expand All @@ -437,6 +438,7 @@ extension ThrowErrorTest {
("testNegativeMatchesDoNotCallClosureWithoutError", testNegativeMatchesDoNotCallClosureWithoutError),
("testNegativeMatchesWithClosure", testNegativeMatchesWithClosure),
("testNegativeNegatedMatches", testNegativeNegatedMatches),
("testNonVoidClosure", testNonVoidClosure),
("testPositiveMatches", testPositiveMatches),
("testPositiveMatchesWithClosures", testPositiveMatchesWithClosures),
("testPositiveNegatedMatches", testPositiveNegatedMatches),
Expand Down