Skip to content

Commit

Permalink
Add support for throwing closures as input.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZevEisenberg committed May 17, 2023
1 parent ac31246 commit 2b949fa
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ assertCustom(
],
tests: { pair, file, line in
myCustomAssertion(
pair.left, pair.right,
try pair.left, try pair.right,
message: pair.message,
file: file, line: line // <-- ⚠️ this is important!
)
Expand Down
108 changes: 58 additions & 50 deletions Sources/TestCleaner/TestCleaner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public extension XCTestCase {
struct TestPair<Left, Right> {

/// The left-hand value in the pair. Might represent the observed value in a test, or the left-hand side of a comparison expression.
let leftClosure: () -> Left
let leftClosure: () throws -> Left

/// The right-hand value in the pair. Might represent the expected value in a test, or the right-hand side of a comparison expression.
let rightClosure: () -> Right
let rightClosure: () throws -> Right

/// An optional description of the failure.
let messageClosure: () -> String
Expand All @@ -36,12 +36,16 @@ public extension XCTestCase {

/// The left-hand value in the pair. Might represent the observed value in a test, or the left-hand side of a comparison expression.
public var left: Left {
leftClosure()
get throws {
try leftClosure()
}
}

/// The right-hand value in the pair. Might represent the expected value in a test, or the right-hand side of a comparison expression.
public var right: Right {
rightClosure()
get throws {
try rightClosure()
}
}

/// An optional description of the failure.
Expand All @@ -58,13 +62,13 @@ public extension XCTestCase {
/// - file: the file in which the `TestPair` was initialized.
/// - line: the line in which the `TestPair` was initialized.
init(
_ left: @escaping () -> Left,
_ right: @escaping () -> Right,
_ left: @escaping () throws -> Left,
_ right: @escaping () throws -> Right,
involvement: involvement?,
message: @escaping () -> String,
file: StaticString = #filePath,
line: UInt = #line
) {
) rethrows {
self.leftClosure = left
self.rightClosure = right
self.involvement = involvement
Expand All @@ -82,35 +86,35 @@ public extension XCTestCase {

/// Creates a test pair that is evaluated in the enclosing test, unless it appears alongside a focused pair as denoted by `fPair`.
func Pair<Left, Right>(
_ left: @autoclosure @escaping () -> Left,
_ right: @autoclosure @escaping () -> Right,
_ left: @autoclosure @escaping () throws -> Left,
_ right: @autoclosure @escaping () throws -> Right,
_ message: @autoclosure @escaping () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) -> TestPair<Left, Right> {
TestPair(left, right, involvement: nil, message: message, file: file, line: line)
) rethrows -> TestPair<Left, Right> {
try TestPair(left, right, involvement: nil, message: message, file: file, line: line)
}

/// Creates a test pair that is skipped when running the enclosing test.
func xPair<Left, Right>(
_ left: @autoclosure @escaping () -> Left,
_ right: @autoclosure @escaping () -> Right,
_ left: @autoclosure @escaping () throws -> Left,
_ right: @autoclosure @escaping () throws -> Right,
_ message: @autoclosure @escaping () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) -> TestPair<Left, Right> {
TestPair(left, right, involvement: .excluded, message: message, file: file, line: line)
) rethrows -> TestPair<Left, Right> {
try TestPair(left, right, involvement: .excluded, message: message, file: file, line: line)
}

/// Creates a test pair that is always run when the enclosing test is run. Causes any non-focused pairs to be skipped. If a test contains multiple focused pairs, they will all be run.
func fPair<Left, Right>(
_ left: @autoclosure @escaping () -> Left,
_ right: @autoclosure @escaping () -> Right,
_ left: @autoclosure @escaping () throws -> Left,
_ right: @autoclosure @escaping () throws -> Right,
_ message: @autoclosure @escaping () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) -> TestPair<Left, Right> {
TestPair(left, right, involvement: .focused, message: message, file: file, line: line)
) rethrows -> TestPair<Left, Right> {
try TestPair(left, right, involvement: .focused, message: message, file: file, line: line)
}

}
Expand All @@ -120,22 +124,26 @@ public extension XCTestCase {
/// Assert that a given set of input booleans (e.g. the result of some transformation) matches a given set of output booleans
/// - Parameter testCases: the cases to test, with the test value on the left and the expected value on the right.
func assertBoolean(testCases: [TestPair<Bool, Bool>]) {
for testCase in testCases.pairsToTest {
if testCase.rightClosure() {
XCTAssertTrue(
testCase.leftClosure(),
testCase.message,
file: testCase.file,
line: testCase.line
)
} else {
XCTAssertFalse(
testCase.leftClosure(),
testCase.message,
file: testCase.file,
line: testCase.line
)
do {
for testCase in testCases.pairsToTest {
if try testCase.rightClosure() {
XCTAssertTrue(
try testCase.leftClosure(),
testCase.message,
file: testCase.file,
line: testCase.line
)
} else {
XCTAssertFalse(
try testCase.leftClosure(),
testCase.message,
file: testCase.file,
line: testCase.line
)
}
}
} catch {
XCTFail("Caught error while running assertBoolean: \(error)")
}
}

Expand All @@ -144,8 +152,8 @@ public extension XCTestCase {
func assertLessThan<T: Comparable>(testCases: [TestPair<T, T>]) {
for testCase in testCases.pairsToTest {
XCTAssertLessThan(
testCase.leftClosure(),
testCase.rightClosure(),
try testCase.leftClosure(),
try testCase.rightClosure(),
testCase.message,
file: testCase.file,
line: testCase.line
Expand All @@ -158,8 +166,8 @@ public extension XCTestCase {
func assertGreaterThan<T: Comparable>(testCases: [TestPair<T, T>]) {
for testCase in testCases.pairsToTest {
XCTAssertGreaterThan(
testCase.leftClosure(),
testCase.rightClosure(),
try testCase.leftClosure(),
try testCase.rightClosure(),
testCase.message,
file: testCase.file,
line: testCase.line
Expand All @@ -172,8 +180,8 @@ public extension XCTestCase {
func assertLessThanOrEqual<T: Comparable>(testCases: [TestPair<T, T>]) {
for testCase in testCases.pairsToTest {
XCTAssertLessThanOrEqual(
testCase.leftClosure(),
testCase.rightClosure(),
try testCase.leftClosure(),
try testCase.rightClosure(),
testCase.message,
file: testCase.file,
line: testCase.line
Expand All @@ -186,8 +194,8 @@ public extension XCTestCase {
func assertGreaterThanOrEqual<T: Comparable>(testCases: [TestPair<T, T>]) {
for testCase in testCases.pairsToTest {
XCTAssertGreaterThanOrEqual(
testCase.leftClosure(),
testCase.rightClosure(),
try testCase.leftClosure(),
try testCase.rightClosure(),
testCase.message,
file: testCase.file,
line: testCase.line
Expand All @@ -200,8 +208,8 @@ public extension XCTestCase {
func assertEqual<T: Equatable>(testCases: [TestPair<T, T>]) {
for testCase in testCases.pairsToTest {
XCTAssertEqual(
testCase.leftClosure(),
testCase.rightClosure(),
try testCase.leftClosure(),
try testCase.rightClosure(),
testCase.message,
file: testCase.file,
line: testCase.line
Expand All @@ -215,8 +223,8 @@ public extension XCTestCase {
func assertEqual<T: FloatingPoint>(testCases: [TestPair<T, T>], accuracy: T) {
for testCase in testCases.pairsToTest {
XCTAssertEqual(
testCase.leftClosure(),
testCase.rightClosure(),
try testCase.leftClosure(),
try testCase.rightClosure(),
accuracy: accuracy,
testCase.message,
file: testCase.file,
Expand All @@ -230,8 +238,8 @@ public extension XCTestCase {
func assertNotEqual<T: Equatable>(testCases: [TestPair<T, T>]) {
for testCase in testCases.pairsToTest {
XCTAssertNotEqual(
testCase.leftClosure(),
testCase.rightClosure(),
try testCase.leftClosure(),
try testCase.rightClosure(),
testCase.message,
file: testCase.file,
line: testCase.line
Expand All @@ -245,8 +253,8 @@ public extension XCTestCase {
func assertNotEqual<T: FloatingPoint>(testCases: [TestPair<T, T>], accuracy: T) {
for testCase in testCases.pairsToTest {
XCTAssertNotEqual(
testCase.leftClosure(),
testCase.rightClosure(),
try testCase.leftClosure(),
try testCase.rightClosure(),
accuracy: accuracy,
testCase.message,
file: testCase.file,
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestCleanerTests/TestCleanerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ final class TestCleanerTests: XCTestCase {
Pair("One", "Two"),
xPair("Three", "Four"),
], tests: { pair, file, line in
XCTAssertEqual(pair.left.count, pair.right.count, file: file, line: line)
XCTAssertEqual(try pair.left.count, try pair.right.count, file: file, line: line)
})

func testLazyEvaluationForExclusion() {
Expand Down

0 comments on commit 2b949fa

Please sign in to comment.