Skip to content

Commit ff1a95e

Browse files
committed
Get rid of Action isEqual(to: ) pain
1 parent cda441e commit ff1a95e

7 files changed

Lines changed: 60 additions & 80 deletions

File tree

Package.resolved

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/LoggingService/LoggingService.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import Foundation
18+
#if canImport(Mini)
1819
import Mini
1920

2021
public class LoggingService: Service {
@@ -28,3 +29,4 @@ public class LoggingService: Service {
2829

2930
public init() {}
3031
}
32+
#endif

Sources/Mini/Action.swift

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ import Foundation
2020
Protocol that has to be conformed by any object that can be dispatcher
2121
by a `Dispatcher` object.
2222
*/
23-
public protocol Action {
24-
/// Equality function between `Action` objects
25-
/// - Returns: If an `Action` is the same as other.
26-
func isEqual(to other: Action) -> Bool
27-
}
23+
public protocol Action { }
2824

2925
extension Action {
3026
/// String used as tag of the given Action based on his name.
@@ -44,21 +40,3 @@ extension Action {
4440
return tag.components(separatedBy: ".")[0]
4541
}
4642
}
47-
48-
extension Action {
49-
/// Equality operator between `Action` objects.
50-
/// - Returns: If the `Action`s are equal or not.
51-
public static func == (lhs: Self, rhs: Self) -> Bool {
52-
return lhs.isEqual(to: rhs)
53-
}
54-
}
55-
56-
extension Action where Self: Equatable {
57-
/// Convenience `isEqual` implementation when the `Action` object
58-
/// implements `Equatable`.
59-
/// - Returns: Whether the `Action` object is the same as other.
60-
public func isEqual(to other: Action) -> Bool {
61-
guard let action = other as? Self else { return false }
62-
return self == action
63-
}
64-
}

Sources/TestMiddleware/TestMiddleware.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515
*/
1616

1717
import Foundation
18+
#if canImport(Mini)
1819
import Mini
1920

2021
/// Action for testing purposes.
21-
public class TestOnlyAction: Action {
22-
public func isEqual(to _: Action) -> Bool {
23-
return true
24-
}
25-
}
22+
public class TestOnlyAction: Action { }
2623

2724
/// Interceptor class for testing purposes which mute all the received actions.
2825
public class TestMiddleware: Middleware {
@@ -39,26 +36,21 @@ public class TestMiddleware: Middleware {
3936

4037
public init() {}
4138

42-
/// Check if a given action have been intercepted before by the Middleware.
43-
///
44-
/// - Parameter action: action to be checked
45-
/// - Returns: returns true if an action with the same params have been intercepted before.
46-
public func contains(action: Action) -> Bool {
47-
return interceptedActions.contains(where: {
48-
action.isEqual(to: $0)
49-
})
50-
}
51-
5239
/// Check for actions of certain type being intercepted.
5340
///
5441
/// - Parameter kind: Action type to be checked against the intercepted actions.
5542
/// - Returns: Array of actions of `kind` being intercepted.
5643
public func actions<T: Action>(of _: T.Type) -> [T] {
5744
return interceptedActions.compactMap { $0 as? T }
5845
}
46+
47+
public func action<T: Action>(of _: T.Type, where params: (T) -> Bool) -> Bool {
48+
interceptedActions.compactMap { $0 as? T }.compactMap(params).first ?? false
49+
}
5950

6051
/// Clear all the intercepted actions
6152
public func clear() {
6253
interceptedActions.removeAll()
6354
}
6455
}
56+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@testable import Mini
2+
import XCTest
3+
4+
final class ActionTests: XCTestCase {
5+
func test_action_tag() {
6+
let action = SetCounterAction(counter: 1)
7+
8+
XCTAssertEqual(String(describing: type(of: action)), SetCounterAction.tag)
9+
}
10+
}

Tests/MiniSwiftTests/ChainTests.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@ import XCTest
33

44
final class ChainTests: XCTestCase {
55
func test_forwarding_chain_forwards_action() {
6-
class TestAction: Action, Equatable {
6+
class TestAction: Action {
77
var mutableProperty: Int
88

99
init(property: Int) {
1010
mutableProperty = property
1111
}
12-
13-
func isEqual(to other: Action) -> Bool {
14-
guard let action = other as? TestAction else { return false }
15-
return mutableProperty == action.mutableProperty
16-
}
1712
}
1813

1914
let forwardingChain = ForwardingChain { action in

Tests/MiniSwiftTests/RxTests/PrimitiveSequenceTypeTests.swift

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import RxTest
77
@testable import TestMiddleware
88
import XCTest
99

10-
private func equalAction<A: Action>(_ by: A) -> Predicate<A> {
10+
private func equalAction<A: Action & Equatable>(_ by: A) -> Predicate<A> {
1111
return Predicate { expression in
1212
guard let action = try expression.evaluate() else {
1313
return PredicateResult(status: .fail,
@@ -25,22 +25,21 @@ private func equalAction<A: Action>(_ by: A) -> Predicate<A> {
2525
final class PrimitiveSequenceTypeTests: XCTestCase {
2626
fileprivate enum Error: Swift.Error { case dummy }
2727

28-
class TestCompletableAction: CompletableAction {
28+
class TestCompletableAction: CompletableAction, Equatable {
2929
typealias Payload = Int
3030

3131
let counter: Promise<Payload>
3232

3333
required init(promise: Promise<Payload>) {
3434
counter = promise
3535
}
36-
37-
func isEqual(to other: Action) -> Bool {
38-
guard let action = other as? TestCompletableAction else { return false }
39-
return counter == action.counter
36+
37+
static func == (lhs: TestCompletableAction, rhs: TestCompletableAction) -> Bool {
38+
lhs.counter == rhs.counter
4039
}
4140
}
4241

43-
class TestKeyedCompletableAction: KeyedCompletableAction {
42+
class TestKeyedCompletableAction: KeyedCompletableAction, Equatable {
4443
typealias Payload = Int
4544
typealias Key = String
4645

@@ -49,23 +48,21 @@ final class PrimitiveSequenceTypeTests: XCTestCase {
4948
required init(promise: [Key: Promise<Payload>]) {
5049
counterMap = promise
5150
}
52-
53-
func isEqual(to other: Action) -> Bool {
54-
guard let action = other as? TestKeyedCompletableAction else { return false }
55-
return counterMap == action.counterMap
51+
52+
static func == (lhs: TestKeyedCompletableAction, rhs: TestKeyedCompletableAction) -> Bool {
53+
lhs.counterMap == rhs.counterMap
5654
}
5755
}
5856

59-
class TestEmptyAction: EmptyAction {
57+
class TestEmptyAction: EmptyAction, Equatable {
6058
let promise: Promise<Void>
6159

6260
required init(promise: Promise<Void>) {
6361
self.promise = promise
6462
}
65-
66-
func isEqual(to other: Action) -> Bool {
67-
guard let action = other as? TestEmptyAction else { return false }
68-
return promise == action.promise
63+
64+
static func == (lhs: TestEmptyAction, rhs: TestEmptyAction) -> Bool {
65+
true
6966
}
7067
}
7168

@@ -103,7 +100,9 @@ final class PrimitiveSequenceTypeTests: XCTestCase {
103100
self.testMiddleware.actions(of: TestCompletableAction.self).count
104101
).toEventually(be(1))
105102

106-
expect(self.testMiddleware.contains(action: TestCompletableAction(promise: .error(Error.dummy)))
103+
expect(self.testMiddleware.action(of: TestCompletableAction.self) {
104+
$0.counter == .error(Error.dummy)
105+
}
107106
).toEventually(beTrue())
108107
}
109108

@@ -115,7 +114,9 @@ final class PrimitiveSequenceTypeTests: XCTestCase {
115114

116115
expect(
117116
self.testMiddleware
118-
.contains(action: TestKeyedCompletableAction(promise: ["hello": .value(1)]))
117+
.action(of: TestKeyedCompletableAction.self) {
118+
$0.counterMap == ["hello": .value(1)]
119+
}
119120
).toEventually(beTrue())
120121
}
121122

@@ -127,7 +128,9 @@ final class PrimitiveSequenceTypeTests: XCTestCase {
127128

128129
expect(
129130
self.testMiddleware
130-
.contains(action: TestKeyedCompletableAction(promise: ["hello": .error(Error.dummy)]))
131+
.action(of: TestKeyedCompletableAction.self) {
132+
$0.counterMap == ["hello": .error(Error.dummy)]
133+
}
131134
).toEventually(beTrue())
132135
}
133136

0 commit comments

Comments
 (0)