Skip to content

Instantly share code, notes, and snippets.

@briancroom
Created February 26, 2016 01:13
Show Gist options
  • Save briancroom/8093da273b5ea1f16ba9 to your computer and use it in GitHub Desktop.
Save briancroom/8093da273b5ea1f16ba9 to your computer and use it in GitHub Desktop.
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