Skip to content

Commit

Permalink
Greatly simplify Assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
jmp committed Jul 1, 2021
1 parent 22235be commit 395030f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Sources/AssertThat/AssertThat.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
public func assertThat<T>(_ expression: @escaping @autoclosure () throws -> T) -> Assertion<T> {
Assertion(expression: expression)
public func assertThat<Subject>(_ subject: Subject) -> Assertion<Subject> {
Assertion(subject: subject)
}
6 changes: 1 addition & 5 deletions Sources/AssertThat/Assertion.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
public struct Assertion<Subject> {
public let expression: () throws -> Subject

public var subject: Subject {
try! expression() // swiftlint:disable:this force_try
}
public let subject: Subject
}
12 changes: 6 additions & 6 deletions Sources/AssertThat/Extensions/Closure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -52,7 +52,7 @@ public extension Assertion where Subject == () throws -> Any {
@discardableResult
func `throws`<T: Error>(_ 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)
Expand All @@ -63,7 +63,7 @@ public extension Assertion where Subject == () throws -> Any {
@discardableResult
func doesNotThrow<T: Error>(_ 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)
}
Expand Down

0 comments on commit 395030f

Please sign in to comment.