Skip to content
Snippets Groups Projects
Commit 7b722418 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Add BackupStorage.stored function

parent c15b6a04
No related branches found
No related tags found
2 merge requests!111XXMessengerClient.BackupStorage improvements,!102Release 1.0.0
...@@ -18,6 +18,7 @@ public struct BackupStorage { ...@@ -18,6 +18,7 @@ public struct BackupStorage {
public typealias Observer = (Backup?) -> Void public typealias Observer = (Backup?) -> Void
public var stored: () -> Backup?
public var store: (Data) throws -> Void public var store: (Data) throws -> Void
public var observe: (@escaping Observer) -> Cancellable public var observe: (@escaping Observer) -> Cancellable
public var remove: () throws -> Void public var remove: () throws -> Void
...@@ -43,6 +44,9 @@ extension BackupStorage { ...@@ -43,6 +44,9 @@ extension BackupStorage {
backup = Backup(date: fileDate, data: fileData) backup = Backup(date: fileDate, data: fileData)
} }
return BackupStorage( return BackupStorage(
stored: {
backup
},
store: { data in store: { data in
let newBackup = Backup( let newBackup = Backup(
date: now(), date: now(),
...@@ -55,7 +59,6 @@ extension BackupStorage { ...@@ -55,7 +59,6 @@ extension BackupStorage {
observe: { observer in observe: { observer in
let id = UUID() let id = UUID()
observers[id] = observer observers[id] = observer
defer { observers[id]?(backup) }
return Cancellable { return Cancellable {
observers[id] = nil observers[id] = nil
} }
...@@ -71,6 +74,7 @@ extension BackupStorage { ...@@ -71,6 +74,7 @@ extension BackupStorage {
extension BackupStorage { extension BackupStorage {
public static let unimplemented = BackupStorage( public static let unimplemented = BackupStorage(
stored: XCTUnimplemented("\(Self.self).stored"),
store: XCTUnimplemented("\(Self.self).store"), store: XCTUnimplemented("\(Self.self).store"),
observe: XCTUnimplemented("\(Self.self).observe", placeholder: Cancellable {}), observe: XCTUnimplemented("\(Self.self).observe", placeholder: Cancellable {}),
remove: XCTUnimplemented("\(Self.self).remove") remove: XCTUnimplemented("\(Self.self).remove")
......
...@@ -32,6 +32,10 @@ final class BackupStorageTests: XCTestCase { ...@@ -32,6 +32,10 @@ final class BackupStorageTests: XCTestCase {
path: path path: path
) )
XCTAssertNoDifference(
storage.stored(),
BackupStorage.Backup(date: fileDate, data: fileData)
)
XCTAssertNoDifference(actions, [ XCTAssertNoDifference(actions, [
.didLoadFile(path), .didLoadFile(path),
.didGetModifiedTime(path), .didGetModifiedTime(path),
...@@ -42,43 +46,46 @@ final class BackupStorageTests: XCTestCase { ...@@ -42,43 +46,46 @@ final class BackupStorageTests: XCTestCase {
actions.append(.didObserve("A", backup)) actions.append(.didObserve("A", backup))
} }
XCTAssertNoDifference(actions, [ XCTAssertNoDifference(actions, [])
.didObserve("A", .init(date: fileDate, data: fileData)) XCTAssertNoDifference(
]) storage.stored(),
BackupStorage.Backup(date: fileDate, data: fileData)
)
actions = [] actions = []
now = .init(1) now = .init(1)
let data1 = "data-1".data(using: .utf8)! let data1 = "data-1".data(using: .utf8)!
try storage.store(data1) try storage.store(data1)
XCTAssertNoDifference(
storage.stored(),
BackupStorage.Backup(date: .init(1), data: data1)
)
XCTAssertNoDifference(actions, [ XCTAssertNoDifference(actions, [
.didObserve("A", .init(date: .init(1), data: data1)), .didObserve("A", .init(date: .init(1), data: data1)),
.didSaveFile(path, data1), .didSaveFile(path, data1),
]) ])
actions = [] actions = []
now = .init(2)
let observerB = storage.observe { backup in let observerB = storage.observe { backup in
actions.append(.didObserve("B", backup)) actions.append(.didObserve("B", backup))
} }
XCTAssertNoDifference(actions, [ XCTAssertNoDifference(actions, [])
.didObserve("B", .init(date: .init(1), data: data1))
])
actions = [] actions = []
now = .init(3) now = .init(2)
observerA.cancel() observerA.cancel()
let data2 = "data-2".data(using: .utf8)! let data2 = "data-2".data(using: .utf8)!
try storage.store(data2) try storage.store(data2)
XCTAssertNoDifference(actions, [ XCTAssertNoDifference(actions, [
.didObserve("B", .init(date: .init(3), data: data2)), .didObserve("B", .init(date: .init(2), data: data2)),
.didSaveFile(path, data2), .didSaveFile(path, data2),
]) ])
actions = [] actions = []
now = .init(4) now = .init(3)
try storage.remove() try storage.remove()
XCTAssertNoDifference(actions, [ XCTAssertNoDifference(actions, [
...@@ -87,7 +94,7 @@ final class BackupStorageTests: XCTestCase { ...@@ -87,7 +94,7 @@ final class BackupStorageTests: XCTestCase {
]) ])
actions = [] actions = []
now = .init(5) now = .init(4)
observerB.cancel() observerB.cancel()
let data3 = "data-3".data(using: .utf8)! let data3 = "data-3".data(using: .utf8)!
try storage.store(data3) try storage.store(data3)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment