From 91247fae81435e250992ec7a7fc5c2e7b1eb8663 Mon Sep 17 00:00:00 2001 From: Drew Crawford Date: Tue, 18 Oct 2016 23:30:34 -0500 Subject: [PATCH] Support for throws-by-default --- CarolineCore/CarolineCore.swift | 8 ++++- CarolineCore/CarolineCoreTest.swift | 2 +- CarolineCore/Functions.swift | 45 ++++++++++++++++++++++++++--- tests/CoreTests/Errors.swift | 7 +++++ tests/CoreTests/main.swift | 3 +- 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/CarolineCore/CarolineCore.swift b/CarolineCore/CarolineCore.swift index 546b6ab..6c19a18 100644 --- a/CarolineCore/CarolineCore.swift +++ b/CarolineCore/CarolineCore.swift @@ -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() } diff --git a/CarolineCore/CarolineCoreTest.swift b/CarolineCore/CarolineCoreTest.swift index 2dc2ae0..0931166 100644 --- a/CarolineCore/CarolineCoreTest.swift +++ b/CarolineCore/CarolineCoreTest.swift @@ -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 } diff --git a/CarolineCore/Functions.swift b/CarolineCore/Functions.swift index 50c2318..62b99fb 100644 --- a/CarolineCore/Functions.swift +++ b/CarolineCore/Functions.swift @@ -35,20 +35,57 @@ private func compareDict (_ 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 diff --git a/tests/CoreTests/Errors.swift b/tests/CoreTests/Errors.swift index 799292b..54b8360 100644 --- a/tests/CoreTests/Errors.swift +++ b/tests/CoreTests/Errors.swift @@ -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 } +} + diff --git a/tests/CoreTests/main.swift b/tests/CoreTests/main.swift index fa73306..306276f 100644 --- a/tests/CoreTests/main.swift +++ b/tests/CoreTests/main.swift @@ -43,7 +43,8 @@ let allTests: [CarolineTest] = [ SimpleEqual(), SimpleEqualInverse(), SimpleNotEqual(), - SimpleNotEqualInverse() + SimpleNotEqualInverse(), + ThrowsByDefault() ] let engine = CarolineCoreEngine() if !engine.testAll(allTests) { -- 2.22.0