Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Refactor throwError matcher using Predicate.init #580

Merged
merged 1 commit into from Jul 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 45 additions & 39 deletions Sources/Nimble/Matchers/ThrowError.swift
Expand Up @@ -12,22 +12,19 @@ import Foundation
/// nil arguments indicates that the matcher should not attempt to match against
/// that parameter.
public func throwError() -> Predicate<Any> {
return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in

return Predicate { actualExpression in
var actualError: Error?
do {
_ = try actualExpression.evaluate()
} catch let catchedError {
actualError = catchedError
} catch {
actualError = error
}

failureMessage.postfixMessage = "throw any error"
if let actualError = actualError {
failureMessage.actualValue = "<\(actualError)>"
return PredicateResult(bool: true, message: .expectedCustomValueTo("throw any error", "<\(actualError)>"))
} else {
failureMessage.actualValue = "no error"
return PredicateResult(bool: false, message: .expectedCustomValueTo("throw any error", "no error"))
}
return actualError != nil
}
}

Expand All @@ -43,36 +40,39 @@ 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> {
return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in

return Predicate { actualExpression in
var actualError: Error?
do {
_ = try actualExpression.evaluate()
} catch let catchedError {
actualError = catchedError
} catch {
actualError = error
}

let failureMessage = FailureMessage()
setFailureMessageForError(
failureMessage,
actualError: actualError,
error: error,
errorType: nil,
closure: closure
)

var matches = false
if let actualError = actualError, errorMatchesExpectedError(actualError, expectedError: error) {
matches = true

if let closure = closure {
let assertions = gatherFailingExpectations {
closure(actualError)
}
let messages = assertions.map { $0.message }
if messages.count > 0 {
if !messages.isEmpty {
matches = false
}
}
}
return matches

return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage())
}
}

Expand All @@ -88,22 +88,23 @@ 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> {
return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in

return Predicate { actualExpression in
var actualError: Error?
do {
_ = try actualExpression.evaluate()
} catch let catchedError {
actualError = catchedError
} catch {
actualError = error
}

let failureMessage = FailureMessage()
setFailureMessageForError(
failureMessage,
actualError: actualError,
error: error,
errorType: nil,
closure: closure
)

var matches = false
if let actualError = actualError as? T, error == actualError {
matches = true
Expand All @@ -113,12 +114,13 @@ public func throwError<T: Error & Equatable>(_ error: T, closure: ((T) -> Void)?
closure(actualError)
}
let messages = assertions.map { $0.message }
if messages.count > 0 {
if !messages.isEmpty {
matches = false
}
}
}
return matches

return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage())
}
}

Expand All @@ -136,32 +138,34 @@ public func throwError<T: Error & Equatable>(_ error: T, closure: ((T) -> Void)?
public func throwError<T: Error>(
errorType: T.Type,
closure: ((T) -> Void)? = nil) -> Predicate<Any> {
return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in

return Predicate { actualExpression in
var actualError: Error?
do {
_ = try actualExpression.evaluate()
} catch let catchedError {
actualError = catchedError
} catch {
actualError = error
}

let failureMessage = FailureMessage()
setFailureMessageForError(
failureMessage,
actualError: actualError,
error: nil,
errorType: errorType,
closure: closure
)

var matches = false
if let actualError = actualError {
matches = true

if let actualError = actualError as? T {
if let closure = closure {
let assertions = gatherFailingExpectations {
closure(actualError)
}
let messages = assertions.map { $0.message }
if messages.count > 0 {
if !messages.isEmpty {
matches = false
}
}
Expand All @@ -176,14 +180,14 @@ public func throwError<T: Error>(
}
}
let messages = assertions.map { $0.message }
if messages.count > 0 {
if !messages.isEmpty {
matches = false
}
}
}
}

return matches
return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage())
}
}

Expand All @@ -195,15 +199,15 @@ public func throwError<T: Error>(
///
/// The closure only gets called when an error was thrown.
public func throwError(closure: @escaping ((Error) -> Void)) -> Predicate<Any> {
return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in

return Predicate { actualExpression in
var actualError: Error?
do {
_ = try actualExpression.evaluate()
} catch let catchedError {
actualError = catchedError
} catch {
actualError = error
}

let failureMessage = FailureMessage()
setFailureMessageForError(failureMessage, actualError: actualError, closure: closure)

var matches = false
Expand All @@ -214,11 +218,12 @@ public func throwError(closure: @escaping ((Error) -> Void)) -> Predicate<Any> {
closure(actualError)
}
let messages = assertions.map { $0.message }
if messages.count > 0 {
if !messages.isEmpty {
matches = false
}
}
return matches

return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage())
}
}

Expand All @@ -230,15 +235,15 @@ public func throwError(closure: @escaping ((Error) -> Void)) -> Predicate<Any> {
///
/// The closure only gets called when an error was thrown.
public func throwError<T: Error>(closure: @escaping ((T) -> Void)) -> Predicate<Any> {
return Predicate.fromDeprecatedClosure { actualExpression, failureMessage in

return Predicate { actualExpression in
var actualError: Error?
do {
_ = try actualExpression.evaluate()
} catch let catchedError {
actualError = catchedError
} catch {
actualError = error
}

let failureMessage = FailureMessage()
setFailureMessageForError(failureMessage, actualError: actualError, closure: closure)

var matches = false
Expand All @@ -249,10 +254,11 @@ public func throwError<T: Error>(closure: @escaping ((T) -> Void)) -> Predicate<
closure(actualError)
}
let messages = assertions.map { $0.message }
if messages.count > 0 {
if !messages.isEmpty {
matches = false
}
}
return matches

return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage())
}
}