Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick&dirty Nimble matcher for testing ErrorType equality. Requires no casting or Equatable conformance!
@testable import Nimble
public func matchError<T: ErrorType>(expectedValue: T?) -> NonNilMatcherFunc<ErrorType> {
return NonNilMatcherFunc { actualExpression, failureMessage in
defer { failureMessage.postfixMessage = "match <\(T.self).\(stringify(expectedValue))>" }
if try actualExpression.evaluate() is T {
return try equal(expectedValue as? NSError).matches(
actualExpression.cast({ $0 as? NSError }),
failureMessage: failureMessage
)
}
return false
}
}
//////
enum Error: ErrorType {
case MyError
case OtherError
}
enum AnotherError: ErrorType {
case Foo
}
let errType: ErrorType = Error.MyError
expect(errType).to(matchError(Error.MyError))
expect(errType).to(matchError(Error.OtherError))
expect(errType).to(matchError(AnotherError.Foo))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment