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

Match with keypath #38

Merged
merged 3 commits into from
Oct 16, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## Unrelease
[Compare](https://github.com/leoture/MockSwift/compare/v0.2.0...HEAD)
#### Added
- Add Predicate .match(description:keyPath:) [#38](https://github.com/leoture/MockSwift/pull/38)
- Add willThrow [#37](https://github.com/leoture/MockSwift/pull/37)
- Add willReturn with a List [#36](https://github.com/leoture/MockSwift/pull/36)
- Add Predicates with Comparables [#28](https://github.com/leoture/MockSwift/pull/28)
Expand Down
9 changes: 9 additions & 0 deletions Sources/MockSwift/Predicates/Predicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public class Predicate<Input> {
}
}

/// Creates a `Predicate<Input>`.
/// - Parameter description: The description of the Predicate.
/// - Parameter keyPath: The keyPath that will be used to verify that the entry statisfies the Predicate.
/// - Returns: A new `Predicate<Input>`.
public class func match(description: String = "KeyPath matcher",
_ keyPath: KeyPath<Input, Bool>) -> Predicate<Input> {
.match(description: description) { $0[keyPath: keyPath] }
}

/// Creates a `Predicate<Input>` able to match any value of type `Input`.
public static func any() -> Predicate<Input> {
.match(description: "any") { _ in true }
Expand Down
2 changes: 1 addition & 1 deletion Tests/MockSwiftTests/Mocks/MockGivenIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class MockGivenIntegrationTests: XCTestCase {

func test_function_shouldReturnValueFromWillCompletion() {
// Given
given(custom).function(identifier: .not(.match { $0.isEmpty }))
given(custom).function(identifier: .not(.match(\.isEmpty)))
.disambiguate(with: String.self)
.will { parameters in (parameters[0] as? String ?? "") + "1" }

Expand Down
18 changes: 18 additions & 0 deletions Tests/MockSwiftTests/Predicates/PredicateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ class PredicateTests: XCTestCase {
XCTAssertEqual("\(predicate)", "description")
}

func test_match_shouldReturnTrueIfKeyPathReturnTrue() {
let predicate: AnyPredicate = Predicate<String>.match(\.isEmpty)

XCTAssertTrue(predicate.satisfy(by: ""))
}

func test_match_shouldReturnFalseIfKeyPathReturnFalse() {
let predicate: AnyPredicate = Predicate<String>.match(\.isEmpty)

XCTAssertFalse(predicate.satisfy(by: "not Empty"))
}

func test_match_KeyPathDescription() {
let predicate: AnyPredicate = Predicate<String>.match(description: "description", \.isEmpty)

XCTAssertEqual("\(predicate)", "description")
}

func test_any_shouldReturnFalse() {
let predicate: Predicate<String> = .any()
XCTAssertFalse(predicate.satisfy(by: 1))
Expand Down
3 changes: 3 additions & 0 deletions Tests/MockSwiftTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,15 @@ extension PredicateTests {
("test_any_shouldReturnFalse", test_any_shouldReturnFalse),
("test_any_shouldReturnTrue", test_any_shouldReturnTrue),
("test_match_description", test_match_description),
("test_match_KeyPathDescription", test_match_KeyPathDescription),
("test_match_shouldReturnFalseIfInputNotMatched", test_match_shouldReturnFalseIfInputNotMatched),
("test_match_shouldReturnFalseIfInputNotMatchedByAnyPredicate", test_match_shouldReturnFalseIfInputNotMatchedByAnyPredicate),
("test_match_shouldReturnFalseIfInputNotSameReference", test_match_shouldReturnFalseIfInputNotSameReference),
("test_match_shouldReturnFalseIfKeyPathReturnFalse", test_match_shouldReturnFalseIfKeyPathReturnFalse),
("test_match_shouldReturnTrueIfInputMatched", test_match_shouldReturnTrueIfInputMatched),
("test_match_shouldReturnTrueIfInputMatchedByAnyPredicate", test_match_shouldReturnTrueIfInputMatchedByAnyPredicate),
("test_match_shouldReturnTrueIfInputSameReference", test_match_shouldReturnTrueIfInputSameReference),
("test_match_shouldReturnTrueIfKeyPathReturnTrue", test_match_shouldReturnTrueIfKeyPathReturnTrue),
("test_match_withMatchShouldReturnFalseIfInputIsNotTheSameType", test_match_withMatchShouldReturnFalseIfInputIsNotTheSameType),
("test_not_description", test_not_description),
("test_not_shouldReturnFalse", test_not_shouldReturnFalse),
Expand Down