From 76ec720b7d75c1cd43751ff02d23f97fd8c497d6 Mon Sep 17 00:00:00 2001 From: Sho Ikeda Date: Tue, 19 May 2020 21:57:57 +0900 Subject: [PATCH] Refactor message creation for errors and exceptions --- Sources/Nimble/Matchers/MatchError.swift | 18 ++---- Sources/Nimble/Matchers/RaisesException.swift | 60 +++++++++---------- Sources/Nimble/Matchers/ThrowError.swift | 28 ++++----- Sources/Nimble/Utils/Errors.swift | 42 +++++++------ 4 files changed, 70 insertions(+), 78 deletions(-) diff --git a/Sources/Nimble/Matchers/MatchError.swift b/Sources/Nimble/Matchers/MatchError.swift index 4ad71d4f5..02850388d 100644 --- a/Sources/Nimble/Matchers/MatchError.swift +++ b/Sources/Nimble/Matchers/MatchError.swift @@ -9,9 +9,7 @@ public func matchError(_ error: T) -> Predicate { return Predicate.define { actualExpression in let actualError = try actualExpression.evaluate() - let failureMessage = FailureMessage() - setFailureMessageForError( - failureMessage, + let message = messageForError( postfixMessageVerb: "match", actualError: actualError, error: error @@ -22,7 +20,7 @@ public func matchError(_ error: T) -> Predicate { matches = true } - return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage()) + return PredicateResult(bool: matches, message: message) } } @@ -35,9 +33,7 @@ public func matchError(_ error: T) -> Predicate { return Predicate.define { actualExpression in let actualError = try actualExpression.evaluate() - let failureMessage = FailureMessage() - setFailureMessageForError( - failureMessage, + let message = messageForError( postfixMessageVerb: "match", actualError: actualError, error: error @@ -48,7 +44,7 @@ public func matchError(_ error: T) -> Predicate { matches = true } - return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage()) + return PredicateResult(bool: matches, message: message) } } @@ -58,9 +54,7 @@ public func matchError(_ errorType: T.Type) -> Predicate { return Predicate.define { actualExpression in let actualError = try actualExpression.evaluate() - let failureMessage = FailureMessage() - setFailureMessageForError( - failureMessage, + let message = messageForError( postfixMessageVerb: "match", actualError: actualError, errorType: errorType @@ -71,6 +65,6 @@ public func matchError(_ errorType: T.Type) -> Predicate { matches = true } - return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage()) + return PredicateResult(bool: matches, message: message) } } diff --git a/Sources/Nimble/Matchers/RaisesException.swift b/Sources/Nimble/Matchers/RaisesException.swift index 2bd657494..60d4ad6de 100644 --- a/Sources/Nimble/Matchers/RaisesException.swift +++ b/Sources/Nimble/Matchers/RaisesException.swift @@ -31,9 +31,7 @@ public func raiseException( return PredicateResult(status: .fail, message: .fail("unexpected error thrown: <\(error)>")) } - let failureMessage = FailureMessage() - setFailureMessageForException( - failureMessage, + let message = messageForException( exception: exception, named: named, reason: reason, @@ -48,42 +46,44 @@ public func raiseException( 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( diff --git a/Sources/Nimble/Matchers/ThrowError.swift b/Sources/Nimble/Matchers/ThrowError.swift index da2925a6a..853a193fe 100644 --- a/Sources/Nimble/Matchers/ThrowError.swift +++ b/Sources/Nimble/Matchers/ThrowError.swift @@ -54,9 +54,7 @@ public func throwError(_ error: T, closure: ((Error) -> Void)? = actualError = error } - let failureMessage = FailureMessage() - setFailureMessageForError( - failureMessage, + let message = messageForError( actualError: actualError, error: error, errorType: nil, @@ -78,7 +76,7 @@ public func throwError(_ error: T, closure: ((Error) -> Void)? = } } - return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage()) + return PredicateResult(bool: matches, message: message) } } @@ -102,9 +100,7 @@ public func throwError(_ error: T, closure: ((T) -> V actualError = error } - let failureMessage = FailureMessage() - setFailureMessageForError( - failureMessage, + let message = messageForError( actualError: actualError, error: error, errorType: nil, @@ -126,7 +122,7 @@ public func throwError(_ error: T, closure: ((T) -> V } } - return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage()) + return PredicateResult(bool: matches, message: message) } } @@ -153,9 +149,7 @@ public func throwError( actualError = error } - let failureMessage = FailureMessage() - setFailureMessageForError( - failureMessage, + let message = messageForError( actualError: actualError, error: nil, errorType: errorType, @@ -194,7 +188,7 @@ public func throwError( } } - return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage()) + return PredicateResult(bool: matches, message: message) } } @@ -214,8 +208,7 @@ public func throwError(closure: @escaping ((Error) -> Void)) -> Predicate(closure: @escaping ((Error) -> Void)) -> Predicate(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 { @@ -266,6 +258,6 @@ public func throwError(closure: @escaping ((T) -> Void)) -> Predi } } - return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage()) + return PredicateResult(bool: matches, message: message) } } diff --git a/Sources/Nimble/Utils/Errors.swift b/Sources/Nimble/Utils/Errors.swift index 074cb2033..821d49622 100644 --- a/Sources/Nimble/Utils/Errors.swift +++ b/Sources/Nimble/Utils/Errors.swift @@ -2,32 +2,35 @@ import Foundation // Generic -internal func setFailureMessageForError( - _ failureMessage: FailureMessage, +internal func messageForError( 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( @@ -39,21 +42,24 @@ internal func errorMatchesExpectedError( // 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) }