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