Created
February 26, 2016 01:13
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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