Commit 8f8f17d0 authored by Drew's avatar Drew

Support assertions for Equatable / SequenceType

parent ec2f56fa
......@@ -33,4 +33,54 @@ extension CarolineTest {
}
}
#endif
}
\ No newline at end of file
//Equatable and SequenceType<Equatable>
#if swift(>=3.0)
public final func assert<T: Equatable>(_ a: T, equals b: T, _ message: @autoclosure () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
if a != b {
var state = CarolineState(test: self)
state.outcome = .Failed
state.commit()
print("Test failed at \(file):\(line) - \(message()); \(a) != \(b)")
}
}
public final func assert<T:Sequence where T.Iterator.Element: Equatable>(_ a: T, equals b: T, _ message: @autoclosure () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
var iteratorB = b.makeIterator()
for itemA in a {
let itemB = iteratorB.next()
if itemB != itemA {
var state = CarolineState(test: self)
state.outcome = .Failed
state.commit()
print("Test failed at \(file):\(line) - \(message()); \(a) != \(b)")
return
}
}
}
#else
public final func assert<T: Equatable>(_ a: T, equals b: T, @autoclosure _ message: () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
if a != b {
var state = CarolineState(test: self)
state.outcome = .Failed
state.commit()
print("Test failed at \(file):\(line) - \(message()); \(a) != \(b)")
}
}
public final func assert<T:SequenceType where T.Generator.Element: Equatable>(_ a: T, equals b: T, @autoclosure _ message: () -> String = "Assertion failed", file: String = #file, line: Int = #line) {
var iteratorB = b.generate()
for itemA in a {
let itemB = iteratorB.next()
if itemB != itemA {
var state = CarolineState(test: self)
state.outcome = .Failed
state.commit()
print("Test failed at \(file):\(line) - \(message()); \(a) != \(b)")
return
}
}
}
#endif
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment