Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement assertion chaining for Objective-C API as well #750

Merged
merged 1 commit into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Sources/Nimble/Adapters/NMBExpectation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Tests/NimbleTests/SynchronousTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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>"]) {
Comment on lines +123 to +124
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#742 (comment)

Actually, the chain does not stop on first error; all chained assertions are executed. The behavior itself should be okay but the test should be tweaked a bit.

expect(2).toNot(equal(1)).toNot(equal(2)).to(equal(3))
}
}
Expand Down
12 changes: 12 additions & 0 deletions Tests/NimbleTests/objc/ObjCSyncTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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