diff --git a/Sources/AssertThat/AssertThat.swift b/Sources/AssertThat/AssertThat.swift index 7ea6ec7..bdf71bb 100644 --- a/Sources/AssertThat/AssertThat.swift +++ b/Sources/AssertThat/AssertThat.swift @@ -1,3 +1,3 @@ -public func assertThat(_ expression: @escaping @autoclosure () throws -> T) -> Assertion { - Assertion(expression: expression) +public func assertThat(_ subject: Subject) -> Assertion { + Assertion(subject: subject) } diff --git a/Sources/AssertThat/Assertion.swift b/Sources/AssertThat/Assertion.swift index 2ab4871..61ca2d6 100644 --- a/Sources/AssertThat/Assertion.swift +++ b/Sources/AssertThat/Assertion.swift @@ -1,7 +1,3 @@ public struct Assertion { - public let expression: () throws -> Subject - - public var subject: Subject { - try! expression() // swiftlint:disable:this force_try - } + public let subject: Subject } diff --git a/Sources/AssertThat/Extensions/Closure.swift b/Sources/AssertThat/Extensions/Closure.swift index 1eb9f26..a27b9c0 100644 --- a/Sources/AssertThat/Extensions/Closure.swift +++ b/Sources/AssertThat/Extensions/Closure.swift @@ -4,7 +4,7 @@ public extension Assertion where Subject == () throws -> Any { @discardableResult func throwsAnError(file: StaticString = #filePath, line: UInt = #line) -> Self { do { - _ = try expression()() + _ = try subject() XCTFail("no error was thrown", file: file, line: line) } catch {} return self @@ -13,7 +13,7 @@ public extension Assertion where Subject == () throws -> Any { @discardableResult func doesNotThrowAnError(file: StaticString = #filePath, line: UInt = #line) -> Self { do { - _ = try expression()() + _ = try subject() } catch { XCTFail("\(error) was thrown", file: file, line: line) } @@ -27,7 +27,7 @@ public extension Assertion where Subject == () throws -> Any { line: UInt = #line ) -> Self { do { - _ = try expression()() + _ = try subject() XCTFail("\(expectedError) was not thrown", file: file, line: line) } catch { XCTAssertTrue(error as? T == expectedError, "\(error) is not \(expectedError)", file: file, line: line) @@ -42,7 +42,7 @@ public extension Assertion where Subject == () throws -> Any { line: UInt = #line ) -> Self { do { - _ = try expression()() + _ = try subject() } catch { XCTAssertFalse(error as? T == expectedError, "\(expectedError) was thrown", file: file, line: line) } @@ -52,7 +52,7 @@ public extension Assertion where Subject == () throws -> Any { @discardableResult func `throws`(_ expectedError: T.Type, file: StaticString = #filePath, line: UInt = #line) -> Self { do { - _ = try expression()() + _ = try subject() XCTFail("\(expectedError) was not thrown", file: file, line: line) } catch { XCTAssertTrue(type(of: error) == T.self, "\(error) is not \(expectedError)", file: file, line: line) @@ -63,7 +63,7 @@ public extension Assertion where Subject == () throws -> Any { @discardableResult func doesNotThrow(_ expectedError: T.Type, file: StaticString = #filePath, line: UInt = #line) -> Self { do { - _ = try expression()() + _ = try subject() } catch { XCTAssertFalse(type(of: error) == T.self, "\(expectedError) was thrown", file: file, line: line) }