From 364d2ecc046ed6ab3d16b82ff785b3ae6c0d6c50 Mon Sep 17 00:00:00 2001 From: Vadym Markov Date: Fri, 3 Aug 2018 15:21:56 +0200 Subject: [PATCH] Use associated enum for the key information --- Source/Shared/Storage/HybridStorage.swift | 8 +++---- Source/Shared/Storage/StoreChange.swift | 21 ++++++++++++++----- .../Tests/Storage/HybridStorageTests.swift | 10 ++++++++- .../StorageObservationRegistryTests.swift | 6 +++--- Tests/iOS/Tests/Storage/StorageTests.swift | 10 ++++++++- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/Source/Shared/Storage/HybridStorage.swift b/Source/Shared/Storage/HybridStorage.swift index 941c5fda..16d497b6 100644 --- a/Source/Shared/Storage/HybridStorage.swift +++ b/Source/Shared/Storage/HybridStorage.swift @@ -27,25 +27,25 @@ extension HybridStorage: StorageAware { public func removeObject(forKey key: String) throws { memoryStorage.removeObject(forKey: key) try diskStorage.removeObject(forKey: key) - storageObservationRegistry.notifyObservers(about: .singleDeletion, in: self) + storageObservationRegistry.notifyObservers(about: .remove(key: key), in: self) } public func setObject(_ object: T, forKey key: String, expiry: Expiry? = nil) throws { memoryStorage.setObject(object, forKey: key, expiry: expiry) try diskStorage.setObject(object, forKey: key, expiry: expiry) - storageObservationRegistry.notifyObservers(about: .addition, in: self) + storageObservationRegistry.notifyObservers(about: .add(key: key), in: self) } public func removeAll() throws { memoryStorage.removeAll() try diskStorage.removeAll() - storageObservationRegistry.notifyObservers(about: .allDeletion, in: self) + storageObservationRegistry.notifyObservers(about: .removeAll, in: self) } public func removeExpiredObjects() throws { memoryStorage.removeExpiredObjects() try diskStorage.removeExpiredObjects() - storageObservationRegistry.notifyObservers(about: .expiredDeletion, in: self) + storageObservationRegistry.notifyObservers(about: .removeExpired, in: self) } } diff --git a/Source/Shared/Storage/StoreChange.swift b/Source/Shared/Storage/StoreChange.swift index b68c31dc..cf1e05ec 100644 --- a/Source/Shared/Storage/StoreChange.swift +++ b/Source/Shared/Storage/StoreChange.swift @@ -1,6 +1,17 @@ -public enum StorageChange { - case addition - case singleDeletion - case allDeletion - case expiredDeletion +public enum StorageChange: Equatable { + case add(key: String) + case remove(key: String) + case removeAll + case removeExpired +} + +public func == (lhs: StorageChange, rhs: StorageChange) -> Bool { + switch (lhs, rhs) { + case (.add(let key1), .add(let key2)), (.remove(let key1), .remove(let key2)): + return key1 == key2 + case (.removeAll, .removeAll), (.removeExpired, .removeExpired): + return true + default: + return false + } } diff --git a/Tests/iOS/Tests/Storage/HybridStorageTests.swift b/Tests/iOS/Tests/Storage/HybridStorageTests.swift index 68581f30..bd2e9aae 100644 --- a/Tests/iOS/Tests/Storage/HybridStorageTests.swift +++ b/Tests/iOS/Tests/Storage/HybridStorageTests.swift @@ -170,6 +170,14 @@ final class HybridStorageTests: XCTestCase { try storage.removeExpiredObjects() try storage.removeAll() - XCTAssertEqual(changes, [.addition, .addition, .singleDeletion, .expiredDeletion, .allDeletion]) + let expectedChanges: [StorageChange] = [ + .add(key: "user1"), + .add(key: "user2"), + .remove(key: "user1"), + .removeExpired, + .removeAll + ] + + XCTAssertEqual(changes, expectedChanges) } } diff --git a/Tests/iOS/Tests/Storage/StorageObservationRegistryTests.swift b/Tests/iOS/Tests/Storage/StorageObservationRegistryTests.swift index 507dd97c..a66044fc 100644 --- a/Tests/iOS/Tests/Storage/StorageObservationRegistryTests.swift +++ b/Tests/iOS/Tests/Storage/StorageObservationRegistryTests.swift @@ -52,9 +52,9 @@ final class StorageObservationRegistryTests: XCTestCase { change2 = change } - registry.notifyObservers(about: .addition, in: storage) + registry.notifyObservers(about: .add(key: "key"), in: storage) - XCTAssertEqual(change1, .addition) - XCTAssertEqual(change2, .addition) + XCTAssertEqual(change1, StorageChange.add(key: "key")) + XCTAssertEqual(change2, StorageChange.add(key: "key")) } } diff --git a/Tests/iOS/Tests/Storage/StorageTests.swift b/Tests/iOS/Tests/Storage/StorageTests.swift index 91e8ff77..87436cab 100644 --- a/Tests/iOS/Tests/Storage/StorageTests.swift +++ b/Tests/iOS/Tests/Storage/StorageTests.swift @@ -110,6 +110,14 @@ final class StorageTests: XCTestCase { try storage.removeExpiredObjects() try storage.removeAll() - XCTAssertEqual(changes, [.addition, .addition, .singleDeletion, .expiredDeletion, .allDeletion]) + let expectedChanges: [StorageChange] = [ + .add(key: "user1"), + .add(key: "user2"), + .remove(key: "user1"), + .removeExpired, + .removeAll + ] + + XCTAssertEqual(changes, expectedChanges) } }