Skip to content

Commit

Permalink
Merge pull request #769 from Quick/refactor-message-creation-for-erro…
Browse files Browse the repository at this point in the history
…rs-and-exceptions

Refactor message creation for errors and exceptions
  • Loading branch information
ikesyo authored May 19, 2020
2 parents 495ebed + 76ec720 commit ca652f9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 78 deletions.
18 changes: 6 additions & 12 deletions Sources/Nimble/Matchers/MatchError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ public func matchError<T: Error>(_ error: T) -> Predicate<Error> {
return Predicate.define { actualExpression in
let actualError = try actualExpression.evaluate()

let failureMessage = FailureMessage()
setFailureMessageForError(
failureMessage,
let message = messageForError(
postfixMessageVerb: "match",
actualError: actualError,
error: error
Expand All @@ -22,7 +20,7 @@ public func matchError<T: Error>(_ error: T) -> Predicate<Error> {
matches = true
}

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

Expand All @@ -35,9 +33,7 @@ public func matchError<T: Error & Equatable>(_ error: T) -> Predicate<Error> {
return Predicate.define { actualExpression in
let actualError = try actualExpression.evaluate()

let failureMessage = FailureMessage()
setFailureMessageForError(
failureMessage,
let message = messageForError(
postfixMessageVerb: "match",
actualError: actualError,
error: error
Expand All @@ -48,7 +44,7 @@ public func matchError<T: Error & Equatable>(_ error: T) -> Predicate<Error> {
matches = true
}

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

Expand All @@ -58,9 +54,7 @@ public func matchError<T: Error>(_ errorType: T.Type) -> Predicate<Error> {
return Predicate.define { actualExpression in
let actualError = try actualExpression.evaluate()

let failureMessage = FailureMessage()
setFailureMessageForError(
failureMessage,
let message = messageForError(
postfixMessageVerb: "match",
actualError: actualError,
errorType: errorType
Expand All @@ -71,6 +65,6 @@ public func matchError<T: Error>(_ errorType: T.Type) -> Predicate<Error> {
matches = true
}

return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage())
return PredicateResult(bool: matches, message: message)
}
}
60 changes: 30 additions & 30 deletions Sources/Nimble/Matchers/RaisesException.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ public func raiseException<Out>(
return PredicateResult(status: .fail, message: .fail("unexpected error thrown: <\(error)>"))
}

let failureMessage = FailureMessage()
setFailureMessageForException(
failureMessage,
let message = messageForException(
exception: exception,
named: named,
reason: reason,
Expand All @@ -48,42 +46,44 @@ public func raiseException<Out>(
userInfo: userInfo,
closure: closure
)
return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage())
return PredicateResult(bool: matches, message: message)
}
}

// swiftlint:disable:next function_parameter_count
internal func setFailureMessageForException(
_ failureMessage: FailureMessage,
internal func messageForException(
exception: NSException?,
named: String?,
reason: String?,
userInfo: NSDictionary?,
closure: ((NSException) -> Void)?) {
failureMessage.postfixMessage = "raise exception"
closure: ((NSException) -> Void)?
) -> ExpectationMessage {
var rawMessage: String = "raise exception"

if let named = named {
failureMessage.postfixMessage += " with name <\(named)>"
}
if let reason = reason {
failureMessage.postfixMessage += " with reason <\(reason)>"
}
if let userInfo = userInfo {
failureMessage.postfixMessage += " with userInfo <\(userInfo)>"
}
if closure != nil {
failureMessage.postfixMessage += " that satisfies block"
}
if named == nil && reason == nil && userInfo == nil && closure == nil {
failureMessage.postfixMessage = "raise any exception"
}
if let named = named {
rawMessage += " with name <\(named)>"
}
if let reason = reason {
rawMessage += " with reason <\(reason)>"
}
if let userInfo = userInfo {
rawMessage += " with userInfo <\(userInfo)>"
}
if closure != nil {
rawMessage += " that satisfies block"
}
if named == nil && reason == nil && userInfo == nil && closure == nil {
rawMessage = "raise any exception"
}

if let exception = exception {
// swiftlint:disable:next line_length
failureMessage.actualValue = "\(String(describing: type(of: exception))) { name=\(exception.name), reason='\(stringify(exception.reason))', userInfo=\(stringify(exception.userInfo)) }"
} else {
failureMessage.actualValue = "no exception"
}
let actual: String
if let exception = exception {
// swiftlint:disable:next line_length
actual = "\(String(describing: type(of: exception))) { name=\(exception.name), reason='\(stringify(exception.reason))', userInfo=\(stringify(exception.userInfo)) }"
} else {
actual = "no exception"
}

return .expectedCustomValueTo(rawMessage, actual: actual)
}

internal func exceptionMatchesNonNilFieldsOrClosure(
Expand Down
28 changes: 10 additions & 18 deletions Sources/Nimble/Matchers/ThrowError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public func throwError<T: Error, Out>(_ error: T, closure: ((Error) -> Void)? =
actualError = error
}

let failureMessage = FailureMessage()
setFailureMessageForError(
failureMessage,
let message = messageForError(
actualError: actualError,
error: error,
errorType: nil,
Expand All @@ -78,7 +76,7 @@ public func throwError<T: Error, Out>(_ error: T, closure: ((Error) -> Void)? =
}
}

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

Expand All @@ -102,9 +100,7 @@ public func throwError<T: Error & Equatable, Out>(_ error: T, closure: ((T) -> V
actualError = error
}

let failureMessage = FailureMessage()
setFailureMessageForError(
failureMessage,
let message = messageForError(
actualError: actualError,
error: error,
errorType: nil,
Expand All @@ -126,7 +122,7 @@ public func throwError<T: Error & Equatable, Out>(_ error: T, closure: ((T) -> V
}
}

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

Expand All @@ -153,9 +149,7 @@ public func throwError<T: Error, Out>(
actualError = error
}

let failureMessage = FailureMessage()
setFailureMessageForError(
failureMessage,
let message = messageForError(
actualError: actualError,
error: nil,
errorType: errorType,
Expand Down Expand Up @@ -194,7 +188,7 @@ public func throwError<T: Error, Out>(
}
}

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

Expand All @@ -214,8 +208,7 @@ public func throwError<Out>(closure: @escaping ((Error) -> Void)) -> Predicate<O
actualError = error
}

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

var matches = false
if let actualError = actualError {
Expand All @@ -230,7 +223,7 @@ public func throwError<Out>(closure: @escaping ((Error) -> Void)) -> Predicate<O
}
}

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

Expand All @@ -250,8 +243,7 @@ public func throwError<T: Error, Out>(closure: @escaping ((T) -> Void)) -> Predi
actualError = error
}

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

var matches = false
if let actualError = actualError as? T {
Expand All @@ -266,6 +258,6 @@ public func throwError<T: Error, Out>(closure: @escaping ((T) -> Void)) -> Predi
}
}

return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage())
return PredicateResult(bool: matches, message: message)
}
}
42 changes: 24 additions & 18 deletions Sources/Nimble/Utils/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@ import Foundation

// Generic

internal func setFailureMessageForError<T: Error>(
_ failureMessage: FailureMessage,
internal func messageForError<T: Error>(
postfixMessageVerb: String = "throw",
actualError: Error?,
error: T? = nil,
errorType: T.Type? = nil,
closure: ((T) -> Void)? = nil) {
failureMessage.postfixMessage = "\(postfixMessageVerb) error"
closure: ((T) -> Void)? = nil
) -> ExpectationMessage {
var rawMessage = "\(postfixMessageVerb) error"

if let error = error {
failureMessage.postfixMessage += " <\(error)>"
rawMessage += " <\(error)>"
} else if errorType != nil || closure != nil {
failureMessage.postfixMessage += " from type <\(T.self)>"
rawMessage += " from type <\(T.self)>"
}
if closure != nil {
failureMessage.postfixMessage += " that satisfies block"
rawMessage += " that satisfies block"
}
if error == nil && errorType == nil && closure == nil {
failureMessage.postfixMessage = "\(postfixMessageVerb) any error"
rawMessage = "\(postfixMessageVerb) any error"
}

let actual: String
if let actualError = actualError {
failureMessage.actualValue = "<\(actualError)>"
actual = "<\(actualError)>"
} else {
failureMessage.actualValue = "no error"
actual = "no error"
}

return .expectedCustomValueTo(rawMessage, actual: actual)
}

internal func errorMatchesExpectedError<T: Error>(
Expand All @@ -39,21 +42,24 @@ internal func errorMatchesExpectedError<T: Error>(

// Non-generic

internal func setFailureMessageForError(
_ failureMessage: FailureMessage,
internal func messageForError(
actualError: Error?,
closure: ((Error) -> Void)?) {
failureMessage.postfixMessage = "throw error"
closure: ((Error) -> Void)?
) -> ExpectationMessage {
var rawMessage = "throw error"

if closure != nil {
failureMessage.postfixMessage += " that satisfies block"
rawMessage += " that satisfies block"
} else {
failureMessage.postfixMessage = "throw any error"
rawMessage = "throw any error"
}

let actual: String
if let actualError = actualError {
failureMessage.actualValue = "<\(actualError)>"
actual = "<\(actualError)>"
} else {
failureMessage.actualValue = "no error"
actual = "no error"
}

return .expectedCustomValueTo(rawMessage, actual: actual)
}

0 comments on commit ca652f9

Please sign in to comment.