Skip to content

Instantly share code, notes, and snippets.

@jarsen
Created April 14, 2015 20:49
Show Gist options
  • Save jarsen/fd25c3c00b1f484b80ef to your computer and use it in GitHub Desktop.
Save jarsen/fd25c3c00b1f484b80ef to your computer and use it in GitHub Desktop.
cool swift unit test expectation syntax
func expect<T: Equatable>(value: T, file: String = __FILE__, line: Int = __LINE__) -> (T, String, Int) {
return (value, file, line)
}
func ==<T: Equatable>(lhs: (T, String, Int), rhs: T) {
if lhs.0 != rhs {
XCTFail("Expected \(rhs), but got \(lhs.0) instead", file: lhs.1, line: UInt(lhs.2))
}
}
func !=<T: Equatable>(lhs: (T, String, Int), rhs: T) {
if lhs.0 == rhs {
XCTFail("Expected \(rhs), but got \(lhs.0) instead", file: lhs.1, line: UInt(lhs.2))
}
}
@jarsen
Copy link
Author

jarsen commented Apr 14, 2015

example test:

expect(foo.bar) == 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment