Skip to content

Instantly share code, notes, and snippets.

@atetlaw
Last active September 11, 2015 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atetlaw/43fd8bad4a3b51e8f2b3 to your computer and use it in GitHub Desktop.
Save atetlaw/43fd8bad4a3b51e8f2b3 to your computer and use it in GitHub Desktop.
Simple `AssertThrows` and `AssertNoThrow` for unit testing using XCTest in Swift
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)
}
@atetlaw
Copy link
Author

atetlaw commented Aug 27, 2015

For example:

//Test that I can save this CoreData entity
AssertNoThrow(try entity.managedObjectContext.save(), "Save failed for \(entity)")

@atetlaw
Copy link
Author

atetlaw commented Aug 28, 2015

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