Created
April 30, 2016 20:53
-
-
Save coolya/cf8c29fa86ccc46bf76113a2bc2bb47c to your computer and use it in GitHub Desktop.
assert operators
This file contains hidden or 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
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