From 569e846c26afdb71ebbf37fd13396b7da2749594 Mon Sep 17 00:00:00 2001 From: Sho Ikeda Date: Wed, 6 May 2020 21:10:54 +0900 Subject: [PATCH] Implement assertion chaining Co-authored-by: mockersf --- Sources/Nimble/Expectation.swift | 13 +++++++++---- Tests/NimbleTests/Matchers/ThrowAssertionTest.swift | 6 ++++++ Tests/NimbleTests/Matchers/ThrowErrorTest.swift | 6 +++++- Tests/NimbleTests/SynchronousTest.swift | 12 ++++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Sources/Nimble/Expectation.swift b/Sources/Nimble/Expectation.swift index 41625a123..80cb514a6 100644 --- a/Sources/Nimble/Expectation.swift +++ b/Sources/Nimble/Expectation.swift @@ -101,22 +101,27 @@ public struct Expectation { ////////////////// NEW API ///////////////////// /// Tests the actual value using a matcher to match. - public func to(_ predicate: Predicate, description: String? = nil) { + @discardableResult + public func to(_ predicate: Predicate, description: String? = nil) -> Self { let (pass, msg) = execute(expression, .toMatch, predicate, to: "to", description: description) verify(pass, msg) + return self } /// Tests the actual value using a matcher to not match. - public func toNot(_ predicate: Predicate, description: String? = nil) { + @discardableResult + public func toNot(_ predicate: Predicate, description: String? = nil) -> Self { let (pass, msg) = execute(expression, .toNotMatch, predicate, to: "to not", description: description) verify(pass, msg) + return self } /// Tests the actual value using a matcher to not match. /// /// Alias to toNot(). - public func notTo(_ predicate: Predicate, description: String? = nil) { - toNot(predicate, description: description) + @discardableResult + public func notTo(_ predicate: Predicate, description: String? = nil) -> Self { + return toNot(predicate, description: description) } // see: diff --git a/Tests/NimbleTests/Matchers/ThrowAssertionTest.swift b/Tests/NimbleTests/Matchers/ThrowAssertionTest.swift index 1e58c357b..f3b99695b 100644 --- a/Tests/NimbleTests/Matchers/ThrowAssertionTest.swift +++ b/Tests/NimbleTests/Matchers/ThrowAssertionTest.swift @@ -68,4 +68,10 @@ final class ThrowAssertionTest: XCTestCase { expect { () -> Int in fatalError() }.to(throwAssertion()) #endif } + + func testChainOnThrowAssertion() { + #if canImport(Darwin) + expect { () -> Int in return 5 }.toNot(throwAssertion()).to(equal(5)) + #endif + } } diff --git a/Tests/NimbleTests/Matchers/ThrowErrorTest.swift b/Tests/NimbleTests/Matchers/ThrowErrorTest.swift index 36ec3b3d3..5c1556724 100644 --- a/Tests/NimbleTests/Matchers/ThrowErrorTest.swift +++ b/Tests/NimbleTests/Matchers/ThrowErrorTest.swift @@ -125,7 +125,7 @@ final class ThrowErrorTest: XCTestCase { func testNegativeMatchesWithClosure() { let moduleName = "NimbleTests" let innerFailureMessage = "expected to equal , got <\(moduleName).NimbleError>" - let closure = { (error: Error) in + let closure = { (error: Error) -> Void in expect(error._domain).to(equal("foo")) } @@ -164,4 +164,8 @@ final class ThrowErrorTest: XCTestCase { // swiftlint:enable unused_closure_parameter } + + func testChainOnThrowError() { + expect { () throws -> Int in return 5 }.toNot(throwError()).to(equal(5)) + } } diff --git a/Tests/NimbleTests/SynchronousTest.swift b/Tests/NimbleTests/SynchronousTest.swift index bd05da37b..d0f38b041 100644 --- a/Tests/NimbleTests/SynchronousTest.swift +++ b/Tests/NimbleTests/SynchronousTest.swift @@ -139,4 +139,16 @@ final class SynchronousTest: XCTestCase { expect(1).notTo(MatcherFunc { _, _ in false }.predicate) expect(1).notTo(Predicate.simple("match") { _ in .doesNotMatch }) } + + // MARK: Assertion chaining + + func testChain() { + expect(2).toNot(equal(1)).to(equal(2)).notTo(equal(3)) + } + + func testChainFailOnFirstError() { + failsWithErrorMessage("expected to not equal <2>, got <2>") { + expect(2).toNot(equal(1)).toNot(equal(2)).to(equal(3)) + } + } }