-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds matchers for Swift 5 Result type
- Loading branch information
Showing
4 changed files
with
138 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
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,33 @@ | ||
import Foundation | ||
|
||
/// | ||
/// A Nimble matcher for Result that succeeds when the actual value is success | ||
/// | ||
/// You can pass a closure to do any arbitrary custom matching | ||
/// to the value inside result. The closure only gets called when result is success. | ||
public func beSuccess<T>(test: ((T) -> Void)? = nil) -> Predicate<Result<T, Error>> { | ||
return Predicate.define("be <success>") { expression, message in | ||
guard case let .success(value)? = try expression.evaluate() | ||
else { | ||
return PredicateResult(status: .doesNotMatch, message: message) | ||
} | ||
test?(value) | ||
return PredicateResult(status: .matches, message: message) | ||
} | ||
} | ||
|
||
/// | ||
/// A Nimble matcher for Result that succeeds when the actual value is failure | ||
/// | ||
/// You can pass a closure to do custom matching for the error inside result. | ||
/// The closure only gets called when result is failure. | ||
public func beFailure<T>(test: ((Error) -> Void)? = nil) -> Predicate<Result<T, Error>> { | ||
return Predicate.define("be <failure>") { expression, message in | ||
guard case let .failure(error)? = try expression.evaluate() | ||
else { | ||
return PredicateResult(status: .doesNotMatch, message: message) | ||
} | ||
test?(error) | ||
return PredicateResult(status: .matches, message: message) | ||
} | ||
} |
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,59 @@ | ||
import XCTest | ||
import Nimble | ||
|
||
private struct StubError: Error, CustomDebugStringConvertible { | ||
let debugDescription = "StubError" | ||
} | ||
|
||
final class BeSuccessTest: XCTestCase { | ||
func testPositiveMatch() { | ||
let successfulResult: Result<Int, Error> = .success(1) | ||
expect(successfulResult).to(beSuccess()) | ||
} | ||
|
||
func testPositiveMatchWithValueTesting() { | ||
let stubValue = 1 | ||
let successfulResult: Result<Int, Error> = .success(stubValue) | ||
expect(successfulResult).to(beSuccess { value in | ||
expect(value).to(equal(stubValue)) | ||
}) | ||
} | ||
|
||
func testNegativeMatch() { | ||
let failureResult: Result<Int, Error> = .failure(StubError()) | ||
expect(failureResult).toNot(beSuccess()) | ||
} | ||
|
||
func testExpectationFailureMessage() { | ||
let failureResult: Result<Int, Error> = .failure(StubError()) | ||
failsWithErrorMessage("expected to be <success>, got <failure(StubError)>") { | ||
expect(failureResult).to(beSuccess()) | ||
} | ||
} | ||
} | ||
|
||
final class BeFailureTest: XCTestCase { | ||
func testPositiveMatch() { | ||
let failureResult: Result<Int, Error> = .failure(StubError()) | ||
expect(failureResult).to(beFailure()) | ||
} | ||
|
||
func testPositiveMatchWithValueTesting() { | ||
let failureResult: Result<Int, Error> = .failure(StubError()) | ||
expect(failureResult).to(beFailure { value in | ||
expect(value).to(matchError(StubError.self)) | ||
}) | ||
} | ||
|
||
func testNegativeMatch() { | ||
let successfulResult: Result<Int, Error> = .success(1) | ||
expect(successfulResult).toNot(beFailure()) | ||
} | ||
|
||
func testExpectationFailureMessage() { | ||
let successfulResult: Result<Int, Error> = .success(1) | ||
failsWithErrorMessage("expected to be <failure>, got <success(1)>") { | ||
expect(successfulResult).to(beFailure()) | ||
} | ||
} | ||
} |