From a2645bd9b9b06c45efc52c284b73b34fa58ccfe9 Mon Sep 17 00:00:00 2001 From: Joe Masilotti Date: Sun, 27 Dec 2015 14:06:26 -0500 Subject: [PATCH] Show failures on same line as caller --- src/CatchingFire.swift | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/CatchingFire.swift b/src/CatchingFire.swift index 0ced4f1..4846ead 100644 --- a/src/CatchingFire.swift +++ b/src/CatchingFire.swift @@ -37,19 +37,20 @@ import XCTest /// /// If the expression fails, your test fails. /// -public func AssertNoThrow(@autoclosure closure: () throws -> R) -> R? { +public func AssertNoThrow(@autoclosure closure: () throws -> R, file: String = __FILE__, line: UInt = __LINE__) -> R? { var result: R? - AssertNoThrow() { + AssertNoThrow(file: file, line: line) { result = try closure() } return result } -public func AssertNoThrow(@noescape closure: () throws -> ()) { +public func AssertNoThrow(file file: String = __FILE__, line: UInt = __LINE__, @noescape closure: () throws -> ()) { do { try closure() } catch let error { - XCTFail("Caught unexpected error <\(error)>.") + XCTFail("Caught unexpected error <\(error)>.", + file: file, line: line) } } @@ -77,41 +78,46 @@ public func AssertNoThrow(@noescape closure: () throws -> ()) { /// /// If the expression or closure doesn't throw the expected error, your test fails. /// -public func AssertThrows(expectedError: E, @autoclosure _ closure: () throws -> R) -> () { - AssertThrows(expectedError) { try closure() } +public func AssertThrows(expectedError: E, file: String = __FILE__, line: UInt = __LINE__, @autoclosure _ closure: () throws -> R) -> () { + AssertThrows(expectedError, file: file, line: line) { try closure() } } -public func AssertThrows(expectedError: E, @noescape _ closure: () throws -> ()) -> () { +public func AssertThrows(expectedError: E, file: String = __FILE__, line: UInt = __LINE__, @noescape _ closure: () throws -> ()) -> () { do { try closure() XCTFail("Expected to catch <\(expectedError)>, " - + "but no error was thrown.") + + "but no error was thrown.", + file: file, line: line) } catch expectedError { return // that's what we expected } catch { XCTFail("Caught error <\(error)>, " + "but not of the expected type and value " - + "<\(expectedError)>.") + + "<\(expectedError)>.", + file: file, line: line) } } -public func AssertThrows(expectedError: E, @autoclosure _ closure: () throws -> R) -> () { - AssertThrows(expectedError) { try closure() } +public func AssertThrows(expectedError: E, @autoclosure _ closure: () throws -> R, file: String = __FILE__, line: UInt = __LINE__) -> () { + AssertThrows(expectedError, file: file, line: line) { try closure() } } -public func AssertThrows(expectedError: E, @noescape _ closure: () throws -> ()) -> () { +public func AssertThrows(expectedError: E, file: String = __FILE__, line: UInt = __LINE__, @noescape _ closure: () throws -> ()) -> () { do { try closure() XCTFail("Expected to catch <\(expectedError)>, " - + "but no error was thrown.") + + "but no error was thrown.", + file: file, line: line) } catch let error as E { XCTAssertEqual(error, expectedError, "Caught error <\(error)> is of the expected type <\(E.self)>, " - + "but not the expected case <\(expectedError)>.") + + "but not the expected case <\(expectedError)>.", + file: file, line: line) } catch { XCTFail("Caught error <\(error)>, " + "but not of the expected type and value " - + "<\(expectedError)>.") + + "<\(expectedError)>.", + file: file, line: line) } }