Skip to content

Instantly share code, notes, and snippets.

@coolya
Created April 30, 2016 20:53
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 coolya/cf8c29fa86ccc46bf76113a2bc2bb47c to your computer and use it in GitHub Desktop.
Save coolya/cf8c29fa86ccc46bf76113a2bc2bb47c to your computer and use it in GitHub Desktop.
assert operators
enum AssertOp {
case Equals
case NotEquals
}
enum SimpleAssertOp {
case NotNil
case Nil
}
infix operator <=> { associativity left }
/**
Asserts that the left hand side equals the right hand side and returns the left hand side.
*/
func <=><T : Equatable> (left : T?, right : (value :T?, op: AssertOp, file: StaticString, msg: String, line : UInt)) -> T? {
switch right.op {
case .Equals:
XCTAssertEqual(left, right.value, right.msg, file: right.file, line: right.line)
case .NotEquals:
XCTAssertNotEqual(left, right.value, right.msg, file: right.file, line: right.line)
}
return left
}
func equals<T>(value : T?, message : String = "", file : StaticString = #file, line : UInt = #line) -> (T?, AssertOp, StaticString, String, UInt) {
return (value, .Equals, file, message, line)
}
func notEquals<T>(value : T?, message : String = "", file : StaticString = #file, line : UInt = #line) -> (T?, AssertOp, StaticString, String, UInt) {
return (value, .NotEquals, file, message, line)
}
func <=><T : Equatable> (left : T?, right : (op: SimpleAssertOp, file: StaticString, msg: String, line : UInt)) -> T? {
switch right.op {
case .Nil:
XCTAssertNil(left, right.msg, file: right.file, line: right.line)
case .NotNil:
XCTAssertNotNil(left, right.msg, file: right.file, line: right.line)
}
return left
}
func notNil(message : String = "", file : StaticString = #file, line : UInt = #line) -> (SimpleAssertOp, StaticString, String, UInt){
return (.NotNil, file, message, line)
}
func isNil(message : String = "", file : StaticString = #file, line : UInt = #line) -> (SimpleAssertOp, StaticString, String, UInt) {
return (.Nil, file, message, line)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment