Skip to content

Commit

Permalink
Add IdentityEquatable and IdentityHashable
Browse files Browse the repository at this point in the history
  • Loading branch information
devxoul committed Nov 19, 2019
1 parent 2770c46 commit 30edd8d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Sources/ReactorKit/IdentityEquatable.swift
@@ -0,0 +1,15 @@
//
// IdentityEquatable.swift
// ReactorKit
//
// Created by Suyeol Jeon on 2019/10/17.
//

public protocol IdentityEquatable: class, Equatable {
}

public extension IdentityEquatable {
static func == (lhs: Self, rhs: Self) -> Bool {
return lhs === rhs
}
}
15 changes: 15 additions & 0 deletions Sources/ReactorKit/IdentityHashable.swift
@@ -0,0 +1,15 @@
//
// IdentityHashable.swift
// ReactorKit
//
// Created by Suyeol Jeon on 2019/10/17.
//

public protocol IdentityHashable: Hashable, IdentityEquatable {
}

public extension IdentityHashable {
func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self).hashValue)
}
}
27 changes: 27 additions & 0 deletions Tests/ReactorKitTests/IdentityEquatableTests.swift
@@ -0,0 +1,27 @@
//
// IdentityEquatableTests.swift
// ReactorKitTests
//
// Created by Suyeol Jeon on 2019/10/17.
//

import XCTest
import ReactorKit
import RxSwift

final class IdentityEquatableTests: XCTestCase {
func testReactorEqual_whenCurrentStatesAreEqual() {
let reactorA = SimpleReactor()
let reactorB = reactorA
XCTAssertEqual(reactorA, reactorB)
}
}

private final class SimpleReactor: Reactor, IdentityEquatable {
typealias Action = Never
typealias Mutation = Never
struct State {
}

let initialState = State()
}
28 changes: 28 additions & 0 deletions Tests/ReactorKitTests/IdentityHashableTests.swift
@@ -0,0 +1,28 @@
//
// IdentityHashableTests.swift
// ReactorKitTests
//
// Created by Suyeol Jeon on 2019/10/17.
//

import XCTest
import ReactorKit
import RxSwift

final class IdentityHashableTests: XCTestCase {
func testReactorHashValue() {
let reactorA = SimpleReactor()
let reactorB = reactorA
XCTAssertEqual(reactorA.hashValue, reactorB.hashValue)
XCTAssertEqual(reactorA, reactorB)
}
}

private final class SimpleReactor: Reactor, IdentityHashable {
typealias Action = Never
typealias Mutation = Never
struct State {
}

let initialState = State()
}

0 comments on commit 30edd8d

Please sign in to comment.