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

Feature/predicates #33

Merged
merged 4 commits into from
Oct 8, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
## Unrelease
[Compare](https://github.com/leoture/MockSwift/compare/v0.2.0...HEAD)
#### Added
- Add Predicates with Comparables [#28](https://github.com/leoture/MockSwift/pull/28)
- Add Predicates .isTrue() & .isFalse() [#33](https://github.com/leoture/MockSwift/pull/33)
- Add Predicate .not() [#33](https://github.com/leoture/MockSwift/pull/33)
- Add Predicate .isNil() [#33](https://github.com/leoture/MockSwift/pull/33)

#### Changed
- mocked() with optionals [#32](https://github.com/leoture/MockSwift/pull/32)

#### Deprecated

Expand Down
16 changes: 8 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
algoliasearch (1.27.0)
algoliasearch (1.27.1)
httpclient (~> 2.8, >= 2.8.3)
json (>= 1.5.1)
atomos (0.1.3)
claide (1.0.3)
cocoapods (1.8.0)
cocoapods (1.8.3)
activesupport (>= 4.0.2, < 5)
claide (>= 1.0.2, < 2.0)
cocoapods-core (= 1.8.0)
cocoapods-core (= 1.8.3)
cocoapods-deintegrate (>= 1.0.3, < 2.0)
cocoapods-downloader (>= 1.2.2, < 2.0)
cocoapods-plugins (>= 1.0.0, < 2.0)
Expand All @@ -31,10 +31,10 @@ GEM
nap (~> 1.0)
ruby-macho (~> 1.4)
xcodeproj (>= 1.11.1, < 2.0)
cocoapods-core (1.8.0)
cocoapods-core (1.8.3)
activesupport (>= 4.0.2, < 6)
algoliasearch (~> 1.0)
concurrent-ruby (~> 1.0)
concurrent-ruby (~> 1.1)
fuzzy_match (~> 2.0.4)
nap (~> 1.0)
cocoapods-deintegrate (1.0.4)
Expand All @@ -43,7 +43,7 @@ GEM
nap
cocoapods-search (1.0.0)
cocoapods-stats (1.1.0)
cocoapods-trunk (1.4.0)
cocoapods-trunk (1.4.1)
nap (>= 0.8, < 2.0)
netrc (~> 0.11)
cocoapods-try (1.1.0)
Expand All @@ -68,15 +68,15 @@ GEM
xcinvoke (~> 0.3.0)
json (2.2.0)
liferaft (0.0.6)
minitest (5.12.0)
minitest (5.12.2)
molinillo (0.6.6)
mustache (1.1.0)
nanaimo (0.2.6)
nap (1.1.0)
netrc (0.11.0)
open4 (1.3.4)
redcarpet (3.5.0)
rouge (3.11.0)
rouge (3.11.1)
ruby-macho (1.4.0)
sassc (2.2.1)
ffi (~> 1.9)
Expand Down
16 changes: 16 additions & 0 deletions MockSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
9EA5D1612302FCB7006F0A28 /* FunctionCallRegister.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA5D1602302FCB7006F0A28 /* FunctionCallRegister.swift */; };
9EA5D1632303F130006F0A28 /* Predicate+Comparable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA5D1622303F130006F0A28 /* Predicate+Comparable.swift */; };
9EAC378D234378DD00281F7B /* ParameterType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EAC378C234378DD00281F7B /* ParameterType.swift */; };
9EAC3791234A25B000281F7B /* Predicate+Bool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EAC3790234A25B000281F7B /* Predicate+Bool.swift */; };
9EAC3792234A25B800281F7B /* PredicateBoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EAC378E234A248D00281F7B /* PredicateBoolTests.swift */; };
9EAC3794234BA23F00281F7B /* Predicate+Optional.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EAC3793234BA23F00281F7B /* Predicate+Optional.swift */; };
9EAC3796234BA25A00281F7B /* PredicateOptionalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EAC3795234BA25A00281F7B /* PredicateOptionalTests.swift */; };
9EB5542422FDDDEB00449662 /* MockDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB5542322FDDDEB00449662 /* MockDefaults.swift */; };
9EB5542622FDDF2C00449662 /* MockDefaultExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB5542522FDDF2C00449662 /* MockDefaultExtensionTests.swift */; };
9EB5542B22FDE44300449662 /* MockDefaultIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB5542A22FDE44300449662 /* MockDefaultIntegrationTests.swift */; };
Expand Down Expand Up @@ -114,6 +118,10 @@
9EA77EF4230EE7C900DAE694 /* CONTRIBUTING.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CONTRIBUTING.md; sourceTree = "<group>"; };
9EA77EF5230EE7C900DAE694 /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
9EAC378C234378DD00281F7B /* ParameterType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParameterType.swift; sourceTree = "<group>"; };
9EAC378E234A248D00281F7B /* PredicateBoolTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PredicateBoolTests.swift; sourceTree = "<group>"; };
9EAC3790234A25B000281F7B /* Predicate+Bool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Predicate+Bool.swift"; sourceTree = "<group>"; };
9EAC3793234BA23F00281F7B /* Predicate+Optional.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Predicate+Optional.swift"; sourceTree = "<group>"; };
9EAC3795234BA25A00281F7B /* PredicateOptionalTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PredicateOptionalTests.swift; sourceTree = "<group>"; };
9EB5542322FDDDEB00449662 /* MockDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockDefaults.swift; sourceTree = "<group>"; };
9EB5542522FDDF2C00449662 /* MockDefaultExtensionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockDefaultExtensionTests.swift; sourceTree = "<group>"; };
9EB5542A22FDE44300449662 /* MockDefaultIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockDefaultIntegrationTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -251,6 +259,8 @@
9EA5D1522301DABB006F0A28 /* PredicateTests.swift */,
9EA5D1562301DE6F006F0A28 /* AnyPredicateEquatableTests.swift */,
9E0071592304007700F040F4 /* PredicateComparableTests.swift */,
9EAC378E234A248D00281F7B /* PredicateBoolTests.swift */,
9EAC3795234BA25A00281F7B /* PredicateOptionalTests.swift */,
);
path = Predicates;
sourceTree = "<group>";
Expand All @@ -262,6 +272,8 @@
9EA5D14F2301DA6E006F0A28 /* Predicate.swift */,
9EA5D1542301DE3F006F0A28 /* AnyPredicate+Equatable.swift */,
9EA5D1622303F130006F0A28 /* Predicate+Comparable.swift */,
9EAC3790234A25B000281F7B /* Predicate+Bool.swift */,
9EAC3793234BA23F00281F7B /* Predicate+Optional.swift */,
);
path = Predicates;
sourceTree = "<group>";
Expand Down Expand Up @@ -504,7 +516,9 @@
OBJ_41 /* Mock.swift in Sources */,
9EA5D15D2302EE7B006F0A28 /* Verifiable.swift in Sources */,
9EB5542422FDDDEB00449662 /* MockDefaults.swift in Sources */,
9EAC3794234BA23F00281F7B /* Predicate+Optional.swift in Sources */,
9E2F052422FB32B200E04F34 /* Behaviour.swift in Sources */,
9EAC3791234A25B000281F7B /* Predicate+Bool.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -525,6 +539,7 @@
9E00715F230406B300F040F4 /* FunctionCallRegisterTests.swift in Sources */,
OBJ_59 /* MockTests.swift in Sources */,
9EB5542622FDDF2C00449662 /* MockDefaultExtensionTests.swift in Sources */,
9EAC3792234A25B800281F7B /* PredicateBoolTests.swift in Sources */,
9EA5D1592301E43B006F0A28 /* MockGivenIntegrationTests.swift in Sources */,
9EA5D1532301DABB006F0A28 /* PredicateTests.swift in Sources */,
9EB5542B22FDE44300449662 /* MockDefaultIntegrationTests.swift in Sources */,
Expand All @@ -533,6 +548,7 @@
9E2F052822FB357F00E04F34 /* DefaultFunctionBehaviourTests.swift in Sources */,
9E2F051D22FAF1B900E04F34 /* FunctionBehaviourRegisterTests.swift in Sources */,
9E2F052022FAF54E00E04F34 /* FunctionIdentifierStub.swift in Sources */,
9EAC3796234BA25A00281F7B /* PredicateOptionalTests.swift in Sources */,
9EA5D1572301DE6F006F0A28 /* AnyPredicateEquatableTests.swift in Sources */,
OBJ_60 /* BehaviourRegisterMock.swift in Sources */,
9E2F052622FB354C00E04F34 /* FunctionBehaviourTests.swift in Sources */,
Expand Down
10 changes: 5 additions & 5 deletions MockSwiftExample/MockSwiftExampleTests/BasicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class MockSwiftExampleTests: XCTestCase {
$0.doSomething().disambiguate(with: Void.self).will { _ in blockDone = true }
$0.doSomething().willReturn(0)
$0.doSomething().willReturn(nil)
$0.doSomething(arg: .any).willReturn("2")
$0.doSomething(arg1: =="3", arg2: ==nil).willReturn("3")
$0.doSomething(arg: .any()).willReturn("2")
$0.doSomething(arg1: =="3", arg2: .isNil()).willReturn("3")
$0.doSomething(with: =="4").willReturn("4")
$0.doSomething(with: =="5", and: ==true).willReturn("5")
$0.doSomething(with: =="5", and: .isTrue()).willReturn("5")
}

// When
Expand All @@ -65,9 +65,9 @@ class MockSwiftExampleTests: XCTestCase {
$0.doSomething().disambiguate(with: Int.self).called(times: 1)
$0.doSomething().disambiguate(with: String?.self).called(times: 1)
$0.doSomething(arg: =="2").called(times: 1)
$0.doSomething(arg1: =="3", arg2: ==nil).called(times: 1)
$0.doSomething(arg1: =="3", arg2: .isNil()).called(times: 1)
$0.doSomething(with: =="4").called(times: 1)
$0.doSomething(with: =="5", and: ==true).called(times: 1)
$0.doSomething(with: =="5", and: .isTrue()).called(times: 1)
}
}

Expand Down
4 changes: 2 additions & 2 deletions MockSwiftPlayground/Playground.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ class MyTests: XCTestCase {
let expectedUser = User(identifier: "id", name: "John")

given(service)
.fetch(identifier: .any)
.fetch(identifier: .any())
.willReturn(expectedUser)

// When
let user: User = service.fetch(identifier: "id")

// Then
then(service)
.fetch(identifier: .any)
.fetch(identifier: .any())
.disambiguate(with: User.self)
.called()
XCTAssertEqual(user, expectedUser)
Expand Down
35 changes: 35 additions & 0 deletions Sources/MockSwift/Predicates/Predicate+Bool.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//Predicate+Bool.swift
/*
MIT License

Copyright (c) 2019 Jordhan Leoture

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import Foundation

public extension Predicate where Input == Bool {

/// Creates a `Predicate<Bool>` that can match any `Bool` equal to `true`
static func isTrue() -> Predicate<Bool> { .match(description: "True") { $0 == true } }

/// Creates a `Predicate<Bool>` that can match any `Bool` equal to `false`
static func isFalse() -> Predicate<Bool> { .match(description: "False") { $0 == false } }
}
34 changes: 34 additions & 0 deletions Sources/MockSwift/Predicates/Predicate+Optional.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//Predicate+Optional.swift
/*
MIT License

Copyright (c) 2019 Jordhan Leoture

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import Foundation

public extension Predicate {

/// Creates a `Predicate<Input?>` able to match `nil`.
static func isNil<Input>() -> Predicate<Input?> {
.match(description: "nil") { $0 == nil }
}
}
50 changes: 30 additions & 20 deletions Sources/MockSwift/Predicates/Predicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,28 @@ import Foundation
/// Predicate represents a condition on an `Input`.
public class Predicate<Input> {

// MARK: - Properties

/// Description of the Predicate.
public var description: String

private let predicate: (Input) -> Bool

// MARK: - Init

private init(description: String, predicate: @escaping (Input) -> Bool) {
self.description = description
self.predicate = predicate
}

// MARK: - Public Methods

/// Creates a `Predicate<Input>`.
/// - Parameter description: The description of the Predicate.
/// - Parameter predicate: The block that will be used to verify that the entry statisfies the Predicate.
/// - Returns: A new `Predicate<Input>`.
public class func match(description: String = "custom matcher",
_ predicate: @escaping (Input) -> Bool) -> Predicate<Input> {
public static func match(description: String = "custom matcher",
_ predicate: @escaping (Input) -> Bool) -> Predicate<Input> {
Predicate(description: description, predicate: predicate)
}

Expand All @@ -43,34 +59,28 @@ public class Predicate<Input> {
/// - Parameter line: The line where the method is called.
/// - Returns: A `AnyPredicate` able to match `value`.
/// - Important: If value cannot be cast to `AnyPredicate` or to `AnyObject` a `fatalError` will be raised.
public class func match(_ value: Input,
file: StaticString = #file,
line: UInt = #line) -> AnyPredicate {
public static func match(_ value: Input,
file: StaticString = #file,
line: UInt = #line) -> AnyPredicate {
switch value {
case let value as AnyPredicate: return value
case let value as AnyObject: return Predicate<AnyObject>.match(description: "\(value)") { $0 === value }
default: fatalError("\(value) cannot be cast to \(AnyPredicate.self) or \(AnyObject.self)", file: file, line: line)
}
}

/// Predicate matches any value of type `Input`.
public class var any: Predicate<Input> {
Self.match(description: "any") { _ in true }
/// Creates a `Predicate<Input>` able to match any value of type `Input`.
public static func any() -> Predicate<Input> {
.match(description: "any") { _ in true }
}

// MARK: - Properties

/// Description of the Predicate.
public var description: String

private let predicate: (Input) -> Bool

// MARK: - Init

private init(description: String, predicate: @escaping (Input) -> Bool) {
self.description = description
self.predicate = predicate
/// Creates a `Predicate<Input>` able to match any value of type `Input` not matched by an other predicate.
/// - Parameter predicate: The predicate to not match.
/// - Returns: A new `Predicate<Input>`.
public static func not(_ predicate: Predicate<Input>) -> Predicate<Input> {
.match(description: "not \(predicate)") { !predicate.satisfy(by: $0) }
}

}

// MARK: - AnyPredicate
Expand Down
61 changes: 61 additions & 0 deletions Tests/MockSwiftTests/Predicates/PredicateBoolTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//PredicateBoolTests.swift
/*
MIT License

Copyright (c) 2019 Jordhan Leoture

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import Foundation
import MockSwift
import XCTest

class PredicatBoolTests: XCTestCase {

func test_isTrue_shouldReturnTrue() {
let predicate: Predicate<Bool> = .isTrue()
XCTAssertTrue(predicate.satisfy(by: true))
}

func test_isTrue_shouldReturnFalse() {
let predicate: Predicate<Bool> = .isTrue()
XCTAssertFalse(predicate.satisfy(by: false))
}

func test_isTrue_description() {
let predicate: Predicate<Bool> = .isTrue()
XCTAssertEqual(predicate.description, "True")
}

func test_isFalse_shouldReturnTrue() {
let predicate: Predicate<Bool> = .isFalse()
XCTAssertTrue(predicate.satisfy(by: false))
}

func test_isFalse_shouldReturnFalse() {
let predicate: Predicate<Bool> = .isFalse()
XCTAssertFalse(predicate.satisfy(by: true))
}

func test_isFalse_description() {
let predicate: Predicate<Bool> = .isFalse()
XCTAssertEqual(predicate.description, "False")
}
}
Loading