From 78de7be311d7b56787e181168d2d2e347764b329 Mon Sep 17 00:00:00 2001 From: Sho Ikeda Date: Fri, 8 May 2020 21:36:31 +0900 Subject: [PATCH] Implement assertion chaining for Objective-C API as well --- Sources/Nimble/Adapters/NMBExpectation.swift | 16 ++++++++++------ Tests/NimbleTests/SynchronousTest.swift | 4 ++-- Tests/NimbleTests/objc/ObjCSyncTest.m | 12 ++++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Sources/Nimble/Adapters/NMBExpectation.swift b/Sources/Nimble/Adapters/NMBExpectation.swift index 15d8ce35d..36693051c 100644 --- a/Sources/Nimble/Adapters/NMBExpectation.swift +++ b/Sources/Nimble/Adapters/NMBExpectation.swift @@ -60,49 +60,53 @@ public class NMBExpectation: NSObject { } } - @objc public var to: (NMBMatcher) -> Void { + @objc public var to: (NMBMatcher) -> NMBExpectation { return { matcher in if let pred = matcher as? NMBPredicate { self.expectValue.to(from(objcPredicate: pred)) } else { self.expectValue.to(ObjCMatcherWrapper(matcher: matcher)) } + return self } } - @objc public var toWithDescription: (NMBMatcher, String) -> Void { + @objc public var toWithDescription: (NMBMatcher, String) -> NMBExpectation { return { matcher, description in if let pred = matcher as? NMBPredicate { self.expectValue.to(from(objcPredicate: pred), description: description) } else { self.expectValue.to(ObjCMatcherWrapper(matcher: matcher), description: description) } + return self } } - @objc public var toNot: (NMBMatcher) -> Void { + @objc public var toNot: (NMBMatcher) -> NMBExpectation { return { matcher in if let pred = matcher as? NMBPredicate { self.expectValue.toNot(from(objcPredicate: pred)) } else { self.expectValue.toNot(ObjCMatcherWrapper(matcher: matcher)) } + return self } } - @objc public var toNotWithDescription: (NMBMatcher, String) -> Void { + @objc public var toNotWithDescription: (NMBMatcher, String) -> NMBExpectation { return { matcher, description in if let pred = matcher as? NMBPredicate { self.expectValue.toNot(from(objcPredicate: pred), description: description) } else { self.expectValue.toNot(ObjCMatcherWrapper(matcher: matcher), description: description) } + return self } } - @objc public var notTo: (NMBMatcher) -> Void { return toNot } + @objc public var notTo: (NMBMatcher) -> NMBExpectation { return toNot } - @objc public var notToWithDescription: (NMBMatcher, String) -> Void { return toNotWithDescription } + @objc public var notToWithDescription: (NMBMatcher, String) -> NMBExpectation { return toNotWithDescription } @objc public var toEventually: (NMBMatcher) -> Void { return { matcher in diff --git a/Tests/NimbleTests/SynchronousTest.swift b/Tests/NimbleTests/SynchronousTest.swift index 8d5bb2738..cab4e5287 100644 --- a/Tests/NimbleTests/SynchronousTest.swift +++ b/Tests/NimbleTests/SynchronousTest.swift @@ -120,8 +120,8 @@ final class SynchronousTest: XCTestCase { expect(2).toNot(equal(1)).to(equal(2)).notTo(equal(3)) } - func testChainFailOnFirstError() { - failsWithErrorMessage("expected to not equal <2>, got <2>") { + func testChainFail() { + failsWithErrorMessage(["expected to not equal <2>, got <2>", "expected to equal <3>, got <2>"]) { expect(2).toNot(equal(1)).toNot(equal(2)).to(equal(3)) } } diff --git a/Tests/NimbleTests/objc/ObjCSyncTest.m b/Tests/NimbleTests/objc/ObjCSyncTest.m index 2aae8168c..64b6a0585 100644 --- a/Tests/NimbleTests/objc/ObjCSyncTest.m +++ b/Tests/NimbleTests/objc/ObjCSyncTest.m @@ -18,4 +18,16 @@ - (void)testFailureExpectation { }); } +#pragma mark - Assertion chaining + +- (void)testChain { + expect(@2).toNot(equal(@1)).to(equal(@2)).notTo(equal(@3)); +} + +- (void)testChainFail { + expectFailureMessages((@[@"expected to not equal <2>, got <2>", @"expected to equal <3>, got <2>"]), ^{ + expect(@2).toNot(equal(@1)).toNot(equal(@2)).to(equal(@3)); + }); +} + @end