-
-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a matcher to negate a passed in matcher (#1151)
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/// A matcher that negates the passed in matcher | ||
/// | ||
/// - Note: If the passed-in matcher unconditionally fails, then `not` also unconditionally fails. | ||
public func not<T>(_ matcher: Matcher<T>) -> Matcher<T> { | ||
Matcher { actualExpression in | ||
negateMatcherResult( | ||
try matcher.satisfies(actualExpression) | ||
) | ||
} | ||
} | ||
|
||
/// A matcher that negates the passed in matcher | ||
/// | ||
/// - Note: If the passed-in matcher unconditionally fails, then `not` also unconditionally fails. | ||
public func not<T>(_ matcher: AsyncMatcher<T>) -> AsyncMatcher<T> { | ||
AsyncMatcher { actualExpression in | ||
negateMatcherResult( | ||
try await matcher.satisfies(actualExpression) | ||
) | ||
} | ||
} | ||
|
||
private func negateMatcherResult(_ matcherResult: MatcherResult) -> MatcherResult { | ||
let status: MatcherStatus | ||
switch matcherResult.status { | ||
case .matches: | ||
status = .doesNotMatch | ||
case .doesNotMatch: | ||
status = .matches | ||
case .fail: | ||
status = .fail | ||
} | ||
return MatcherResult( | ||
status: status, | ||
message: matcherResult.message.prepended(expectation: "not ") | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import XCTest | ||
import Nimble | ||
#if SWIFT_PACKAGE | ||
import NimbleSharedTestHelpers | ||
#endif | ||
|
||
final class NegationTest: XCTestCase { | ||
func testSyncNil() { | ||
expect(nil as Int?).toNot(not(beNil())) | ||
|
||
failsWithErrorMessage("expected to not be nil, got <nil>") { | ||
expect(nil as Int?).to(not(beNil())) | ||
} | ||
} | ||
|
||
func testSyncNonNil() { | ||
expect(1).to(not(equal(2))) | ||
|
||
failsWithErrorMessage("expected to not equal <2>, got <2>") { | ||
expect(2).to(not(equal(2))) | ||
} | ||
} | ||
|
||
func testAsyncNil() async { | ||
@Sendable func nilFunc() async -> Int? { | ||
nil | ||
} | ||
|
||
await expect(nilFunc).toNot(not(beNil())) | ||
|
||
await failsWithErrorMessage("expected to not be nil, got <nil>") { | ||
await expect(nilFunc).to(not(beNil())) | ||
} | ||
} | ||
|
||
func testAsyncNonNil() async { | ||
await expect(1).to(not(asyncEqual(2))) | ||
|
||
await failsWithErrorMessage("expected to not equal <2>, got <2>") { | ||
await expect(2).to(not(asyncEqual(2))) | ||
} | ||
} | ||
} |