Commit 91247fae authored by Drew's avatar Drew

Support for throws-by-default

parent 992256b5
Pipeline #2732 passed with stages
in 1 minute and 25 seconds
......@@ -33,7 +33,13 @@ public class CarolineCoreEngine : CarolineEngine {
continue
}
print("test \(test) started.")
test.test()
do {
try test.test()
}
catch {
test.fail(error, note: "Line info unavailable here; use `.check` to get line-level information", file: nil, line: nil)
}
print("test \(test) ended.")
var state = CarolineState(test: test)
if state.outcome == .Initial { state.outcome = .Passed; state.commit() }
......
......@@ -15,7 +15,7 @@
///All tests should conform to this protocol
public protocol CarolineTest: class {
///Implement the test behavior here
func test()
func test() throws
///Return true iff the test should be skipped.
///This method is optional.
var skip: Bool { get }
......
......@@ -35,20 +35,57 @@ private func compareDict<K: Equatable, V: Equatable> (_ a: [K:V], _ b: [K:V]) ->
}
extension CarolineTest {
///Fail the test.
///- parameter message: A reason for failure
public final func fail(_ message: String = "Test failed", file: String = #file, line: Int = #line) {
private final func setFailed() {
var state = CarolineState(test: self)
if self.expectFailure {
state.outcome = .XFailed
}
else {
state.outcome = .Failed
print("Test failed at \(file):\(line) - \(message)")
}
state.commit()
}
private final func logFailureIfNecessary(_ message: @autoclosure() -> String, note: String? = nil, file: String?, line: Int?) {
let state = CarolineState(test: self)
if state.outcome == .Failed {
if let f = file, let l = line {
print("Test failed at \(f):\(l) - \(message())")
}
else {
print("Test failed \(message())")
}
if let n = note {
print("note: \(n)")
}
}
}
///Fail the test.
///- parameter error: An error explaining the reason for failure
internal final func fail(_ error: Error, note: String?, file: String?, line: Int?) {
self.setFailed()
self.logFailureIfNecessary("\(error)", note: note, file: file, line: line)
}
///Fail the test.
///- parameter message: A reason for failure
internal final func fail(_ message: @autoclosure() -> String, note: String?, file: String?, line: Int?) {
self.setFailed()
self.logFailureIfNecessary(message, note: note, file: file, line: line)
}
///Fail the test.
///- parameter error: An error explaining the reason for failure
public final func fail(_ error: Error, file: String = #file, line: Int = #line) {
self.fail(error, note: nil, file: file, line: line)
}
public final func fail(_ message: @autoclosure() -> String = "Test failed", file: String? = #file, line: Int? = #line) {
self.fail(message, note: nil, file: file, line: line)
}
///Assert a boolean expression
///- parameter condition: A condition to assert
///- message: A reason for failure
......
......@@ -34,3 +34,10 @@ class ErrorCheck: CarolineTest {
var expectFailure: Bool { return true }
}
class ThrowsByDefault: CarolineTest {
func test() throws {
throw MyError()
}
var expectFailure: Bool { return true }
}
......@@ -43,7 +43,8 @@ let allTests: [CarolineTest] = [
SimpleEqual(),
SimpleEqualInverse(),
SimpleNotEqual(),
SimpleNotEqualInverse()
SimpleNotEqualInverse(),
ThrowsByDefault()
]
let engine = CarolineCoreEngine()
if !engine.testAll(allTests) {
......
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