From 1d70dc549b6f2b82a87594804c54fb4bc2b95e1f Mon Sep 17 00:00:00 2001 From: jordhan leoture Date: Wed, 16 Oct 2019 17:57:01 +0200 Subject: [PATCH 1/3] Add Predicate .match(description:keyPath:) --- CHANGELOG.md | 1 + Sources/MockSwift/Predicates/Predicate.swift | 9 +++++++++ .../Mocks/MockGivenIntegrationTests.swift | 2 +- .../Predicates/PredicateTests.swift | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72d3f19..779c9b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ## Unrelease [Compare](https://github.com/leoture/MockSwift/compare/v0.2.0...HEAD) #### Added +- Add Predicate .match(description:keyPath:) - 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) diff --git a/Sources/MockSwift/Predicates/Predicate.swift b/Sources/MockSwift/Predicates/Predicate.swift index 1f4d17d..018fadb 100644 --- a/Sources/MockSwift/Predicates/Predicate.swift +++ b/Sources/MockSwift/Predicates/Predicate.swift @@ -72,6 +72,15 @@ public class Predicate { } } + /// Creates a `Predicate`. + /// - 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`. + public class func match(description: String = "KeyPath matcher", + _ keyPath: KeyPath) -> Predicate { + .match(description: description) { $0[keyPath: keyPath] } + } + /// Creates a `Predicate` able to match any value of type `Input`. public static func any() -> Predicate { .match(description: "any") { _ in true } diff --git a/Tests/MockSwiftTests/Mocks/MockGivenIntegrationTests.swift b/Tests/MockSwiftTests/Mocks/MockGivenIntegrationTests.swift index a61629c..3aac9e2 100644 --- a/Tests/MockSwiftTests/Mocks/MockGivenIntegrationTests.swift +++ b/Tests/MockSwiftTests/Mocks/MockGivenIntegrationTests.swift @@ -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" } diff --git a/Tests/MockSwiftTests/Predicates/PredicateTests.swift b/Tests/MockSwiftTests/Predicates/PredicateTests.swift index b835f1a..30f3e81 100644 --- a/Tests/MockSwiftTests/Predicates/PredicateTests.swift +++ b/Tests/MockSwiftTests/Predicates/PredicateTests.swift @@ -92,6 +92,24 @@ class PredicateTests: XCTestCase { XCTAssertEqual("\(predicate)", "description") } + func test_match_shouldReturnTrueIfKeyPathReturnTrue() { + let predicate: AnyPredicate = Predicate.match(\.isEmpty) + + XCTAssertTrue(predicate.satisfy(by: "")) + } + + func test_match_shouldReturnFalseIfKeyPathReturnFalse() { + let predicate: AnyPredicate = Predicate.match(\.isEmpty) + + XCTAssertFalse(predicate.satisfy(by: "not Empty")) + } + + func test_match_KeyPathDescription() { + let predicate: AnyPredicate = Predicate.match(description: "description", \.isEmpty) + + XCTAssertEqual("\(predicate)", "description") + } + func test_any_shouldReturnFalse() { let predicate: Predicate = .any() XCTAssertFalse(predicate.satisfy(by: 1)) From bd8d0ecb15e679f062b7c541951b635fd7c8dcc7 Mon Sep 17 00:00:00 2001 From: jordhan leoture Date: Wed, 16 Oct 2019 18:12:06 +0200 Subject: [PATCH 2/3] add linux tests --- Tests/MockSwiftTests/XCTestManifests.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/MockSwiftTests/XCTestManifests.swift b/Tests/MockSwiftTests/XCTestManifests.swift index cc1264d..dd115a9 100644 --- a/Tests/MockSwiftTests/XCTestManifests.swift +++ b/Tests/MockSwiftTests/XCTestManifests.swift @@ -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), From 597a743b5c1b4b332a1fe502462955df33615df7 Mon Sep 17 00:00:00 2001 From: Jordhan Leoture Date: Wed, 16 Oct 2019 18:17:38 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 779c9b3..8c796c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ ## Unrelease [Compare](https://github.com/leoture/MockSwift/compare/v0.2.0...HEAD) #### Added -- Add Predicate .match(description:keyPath:) +- 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)