Last active
September 11, 2015 03:34
-
-
Save atetlaw/43fd8bad4a3b51e8f2b3 to your computer and use it in GitHub Desktop.
Simple `AssertThrows` and `AssertNoThrow` for unit testing using XCTest in Swift
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
public func AssertThrowsSpecific<E:ErrorType>(@autoclosure expression: () throws -> Void, expectedError: E, _ message: String = "", file: String = __FILE__, line: UInt = __LINE__) { | |
do { | |
try expression() | |
FailAssertion(__FUNCTION__, message, file, line) | |
} catch let caughtError { | |
guard caughtError is E else { | |
FailAssertion(__FUNCTION__, message, file, line) | |
return | |
} | |
} | |
} | |
public func AssertThrowsSpecific<E where E: ErrorType, E: Equatable>(@autoclosure expression: () throws -> Void, expectedError: E, _ message: String = "", file: String = __FILE__, line: UInt = __LINE__) { | |
do { | |
try expression() | |
FailAssertion(__FUNCTION__, message, file, line) | |
} catch let caughtError as E { | |
if caughtError != expectedError { | |
FailAssertion(__FUNCTION__, message, file, line) | |
} | |
} catch { | |
FailAssertion(__FUNCTION__, message, file, line) | |
} | |
} | |
public func AssertThrowsSpecific<E: NSError>(@autoclosure expression: () throws -> Void, expectedError: E, _ message: String = "", file: String = __FILE__, line: UInt = __LINE__) { | |
do { | |
try expression() | |
FailAssertion(__FUNCTION__, message, file, line) | |
} catch let caughtError as E { | |
if !caughtError.isEqual(expectedError) { | |
FailAssertion(__FUNCTION__, message, file, line) | |
} | |
} catch { | |
FailAssertion(__FUNCTION__, message, file, line) | |
} | |
} | |
public func AssertNoThrowSpecific<E:ErrorType>(@autoclosure expression: () throws -> Void, expectedError: E, _ message: String = "", file: String = __FILE__, line: UInt = __LINE__) { | |
do { | |
try expression() | |
FailAssertion(__FUNCTION__, message, file, line) | |
} catch let caughtError { | |
guard caughtError is E else { | |
FailAssertion(__FUNCTION__, message, file, line) | |
return | |
} | |
} | |
} | |
public func AssertNoThrowSpecific<E where E: ErrorType, E: Equatable>(@autoclosure expression: () throws -> Void, expectedError: E, _ message: String = "", file: String = __FILE__, line: UInt = __LINE__) { | |
do { | |
try expression() | |
} catch let caughtError as E { | |
if caughtError == expectedError { | |
FailAssertion(__FUNCTION__, message, file, line) | |
} | |
} catch {} | |
} | |
public func AssertNoThrowSpecific<E: NSError>(@autoclosure expression: () throws -> Void, expectedError: E, _ message: String = "", file: String = __FILE__, line: UInt = __LINE__) { | |
do { | |
try expression() | |
} catch let caughtError as E { | |
if caughtError.isEqual(expectedError) { | |
FailAssertion(__FUNCTION__, message, file, line) | |
} | |
} catch {} | |
} | |
public func AssertThrows(@autoclosure expression: () throws -> Void, _ message: String = "", file: String = __FILE__, line: UInt = __LINE__) { | |
if let _ = try? expression() { | |
FailAssertion(__FUNCTION__, message, file, line) | |
} | |
} | |
public func AssertNoThrow(@autoclosure expression: () throws -> Void, _ message: String = "", file: String = __FILE__, line: UInt = __LINE__) { | |
guard let _ = try? expression() else { | |
FailAssertion(__FUNCTION__, message, file, line) | |
return | |
} | |
} | |
internal func FailAssertion(function: String, _ message: String = "", _ file: String = __FILE__, _ line: UInt = __LINE__) { | |
XCTFail("\(function) failed: \(message)", file: file, line: line) | |
} |
Also added some extras for specific errors. Had to make 3 versions, one for just matching the specific ErrorType
, one for matching the ErrorType
if it implements Equatable
and one specifically for NSError
because it's a bastard.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example: