Skip to content

Commit cb85b99

Browse files
committed
Add unit tests for ObservationToken and StorageObservationRegistry
1 parent 0ad8bbe commit cb85b99

File tree

6 files changed

+98
-11
lines changed

6 files changed

+98
-11
lines changed

Cache.xcodeproj/project.pbxproj

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@
128128
D2D4CC251FA3426B00E4A2D5 /* JSONArrayWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2D4CC231FA3426B00E4A2D5 /* JSONArrayWrapper.swift */; };
129129
D2D4CC261FA3426B00E4A2D5 /* JSONArrayWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2D4CC231FA3426B00E4A2D5 /* JSONArrayWrapper.swift */; };
130130
D2D4CC281FA342CA00E4A2D5 /* JSONWrapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2D4CC271FA342CA00E4A2D5 /* JSONWrapperTests.swift */; };
131+
D511464B2114775100197DCE /* StorageObservationRegistryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D511464A2114775100197DCE /* StorageObservationRegistryTests.swift */; };
132+
D511464D2114775100197DCE /* StorageObservationRegistryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D511464A2114775100197DCE /* StorageObservationRegistryTests.swift */; };
133+
D511464F21147B7C00197DCE /* ObservationTokenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D511464E21147B7C00197DCE /* ObservationTokenTests.swift */; };
134+
D511465121147B7C00197DCE /* ObservationTokenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D511464E21147B7C00197DCE /* ObservationTokenTests.swift */; };
131135
D5291D1D1C2837DB00B702C9 /* Cache.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5DC59E01C20593E003BD79B /* Cache.framework */; };
132136
D5291D6A1C283B5400B702C9 /* Cache.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5291D601C283B5300B702C9 /* Cache.framework */; };
133137
D5291D851C283C7C00B702C9 /* TestHelper+OSX.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291D811C283C7000B702C9 /* TestHelper+OSX.swift */; };
@@ -141,9 +145,9 @@
141145
D5A9D1BF21134776005DBD3F /* StoreChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5A9D1BE21134776005DBD3F /* StoreChange.swift */; };
142146
D5A9D1C021134776005DBD3F /* StoreChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5A9D1BE21134776005DBD3F /* StoreChange.swift */; };
143147
D5A9D1C121134776005DBD3F /* StoreChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5A9D1BE21134776005DBD3F /* StoreChange.swift */; };
144-
D5A9D1C321144B65005DBD3F /* StoreObservable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5A9D1C221144B65005DBD3F /* StoreObservable.swift */; };
145-
D5A9D1C421144B65005DBD3F /* StoreObservable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5A9D1C221144B65005DBD3F /* StoreObservable.swift */; };
146-
D5A9D1C521144B65005DBD3F /* StoreObservable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5A9D1C221144B65005DBD3F /* StoreObservable.swift */; };
148+
D5A9D1C321144B65005DBD3F /* StorageObservationRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5A9D1C221144B65005DBD3F /* StorageObservationRegistry.swift */; };
149+
D5A9D1C421144B65005DBD3F /* StorageObservationRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5A9D1C221144B65005DBD3F /* StorageObservationRegistry.swift */; };
150+
D5A9D1C521144B65005DBD3F /* StorageObservationRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5A9D1C221144B65005DBD3F /* StorageObservationRegistry.swift */; };
147151
/* End PBXBuildFile section */
148152

149153
/* Begin PBXContainerItemProxy section */
@@ -219,6 +223,8 @@
219223
D2D4CC1F1FA3411300E4A2D5 /* JSONDictionaryWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONDictionaryWrapper.swift; sourceTree = "<group>"; };
220224
D2D4CC231FA3426B00E4A2D5 /* JSONArrayWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONArrayWrapper.swift; sourceTree = "<group>"; };
221225
D2D4CC271FA342CA00E4A2D5 /* JSONWrapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONWrapperTests.swift; sourceTree = "<group>"; };
226+
D511464A2114775100197DCE /* StorageObservationRegistryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageObservationRegistryTests.swift; sourceTree = "<group>"; };
227+
D511464E21147B7C00197DCE /* ObservationTokenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservationTokenTests.swift; sourceTree = "<group>"; };
222228
D5291CDF1C28374800B702C9 /* TestHelper+iOS.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "TestHelper+iOS.swift"; sourceTree = "<group>"; };
223229
D5291D181C2837DB00B702C9 /* Cache-iOS-Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Cache-iOS-Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
224230
D5291D231C28380100B702C9 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -233,7 +239,7 @@
233239
D5A138C31EB29C2100881A20 /* NSImage+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSImage+Extensions.swift"; sourceTree = "<group>"; };
234240
D5A9D1B621134547005DBD3F /* ObservationToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservationToken.swift; sourceTree = "<group>"; };
235241
D5A9D1BE21134776005DBD3F /* StoreChange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreChange.swift; sourceTree = "<group>"; };
236-
D5A9D1C221144B65005DBD3F /* StoreObservable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreObservable.swift; sourceTree = "<group>"; };
242+
D5A9D1C221144B65005DBD3F /* StorageObservationRegistry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageObservationRegistry.swift; sourceTree = "<group>"; };
237243
D5DC59E01C20593E003BD79B /* Cache.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Cache.framework; sourceTree = BUILT_PRODUCTS_DIR; };
238244
EBAACA991FBC369300FA206E /* SimpleStorage.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = SimpleStorage.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
239245
/* End PBXFileReference section */
@@ -362,7 +368,7 @@
362368
D270148320D10E76003B45C7 /* AsyncStorage.swift */,
363369
D270148720D11040003B45C7 /* Storage+Transform.swift */,
364370
D5A9D1BE21134776005DBD3F /* StoreChange.swift */,
365-
D5A9D1C221144B65005DBD3F /* StoreObservable.swift */,
371+
D5A9D1C221144B65005DBD3F /* StorageObservationRegistry.swift */,
366372
);
367373
path = Storage;
368374
sourceTree = "<group>";
@@ -395,6 +401,7 @@
395401
D285143E1F6FFE1F00C674D1 /* ObjectConverterTests.swift */,
396402
D2D4CC191FA3166900E4A2D5 /* MD5Tests.swift */,
397403
D2D4CC271FA342CA00E4A2D5 /* JSONWrapperTests.swift */,
404+
D511464E21147B7C00197DCE /* ObservationTokenTests.swift */,
398405
);
399406
path = Library;
400407
sourceTree = "<group>";
@@ -409,6 +416,7 @@
409416
D292DB001F6AA06B0060F614 /* SyncStorageTests.swift */,
410417
D292DB031F6AA0730060F614 /* AsyncStorageTests.swift */,
411418
D236F3191F6BEF73004EE01F /* StorageTests.swift */,
419+
D511464A2114775100197DCE /* StorageObservationRegistryTests.swift */,
412420
);
413421
path = Storage;
414422
sourceTree = "<group>";
@@ -809,7 +817,7 @@
809817
files = (
810818
D221E5C620D00DDB00BC940E /* DiskStorage.swift in Sources */,
811819
D21B669D1F6A724600125DE1 /* DiskConfig.swift in Sources */,
812-
D5A9D1C521144B65005DBD3F /* StoreObservable.swift in Sources */,
820+
D5A9D1C521144B65005DBD3F /* StorageObservationRegistry.swift in Sources */,
813821
D21B66871F6A723C00125DE1 /* ExpirationMode.swift in Sources */,
814822
D21B66881F6A723C00125DE1 /* Expiry.swift in Sources */,
815823
D270147620D101F3003B45C7 /* StorageAware.swift in Sources */,
@@ -853,8 +861,10 @@
853861
D27014A020D12870003B45C7 /* MemoryStorageTests.swift in Sources */,
854862
D2CF98261F69427C00CE8F68 /* User.swift in Sources */,
855863
D27014AE20D12D83003B45C7 /* AsyncStorageTests.swift in Sources */,
864+
D511465121147B7C00197DCE /* ObservationTokenTests.swift in Sources */,
856865
D28A1D241F6FFEF60030DF81 /* ObjectConverterTests.swift in Sources */,
857866
D27014B120D12E38003B45C7 /* StorageSupportTests.swift in Sources */,
867+
D511464D2114775100197DCE /* StorageObservationRegistryTests.swift in Sources */,
858868
D27014AD20D12CC3003B45C7 /* SyncStorageTests.swift in Sources */,
859869
D27014AA20D12BA4003B45C7 /* HybridStorageTests.swift in Sources */,
860870
);
@@ -870,9 +880,11 @@
870880
D2CF987C1F69513800CE8F68 /* Date+ExtensionsTests.swift in Sources */,
871881
D28C9BAC1F67ECD400C180C1 /* TestHelper+iOS.swift in Sources */,
872882
D2CF98211F69427C00CE8F68 /* TestHelper.swift in Sources */,
883+
D511464F21147B7C00197DCE /* ObservationTokenTests.swift in Sources */,
873884
D2CF987F1F69513800CE8F68 /* ImageWrapperTests.swift in Sources */,
874885
D2D4CC1A1FA3166900E4A2D5 /* MD5Tests.swift in Sources */,
875886
D2D4CC281FA342CA00E4A2D5 /* JSONWrapperTests.swift in Sources */,
887+
D511464B2114775100197DCE /* StorageObservationRegistryTests.swift in Sources */,
876888
D27014B320D13E2C003B45C7 /* StorageTests.swift in Sources */,
877889
D28C9BAF1F67EF8300C180C1 /* UIImage+ExtensionsTests.swift in Sources */,
878890
D2CF987D1F69513800CE8F68 /* MemoryCapsuleTests.swift in Sources */,
@@ -893,7 +905,7 @@
893905
files = (
894906
D221E5C520D00DDB00BC940E /* DiskStorage.swift in Sources */,
895907
D21B669B1F6A724600125DE1 /* DiskConfig.swift in Sources */,
896-
D5A9D1C421144B65005DBD3F /* StoreObservable.swift in Sources */,
908+
D5A9D1C421144B65005DBD3F /* StorageObservationRegistry.swift in Sources */,
897909
D21B667E1F6A723C00125DE1 /* ExpirationMode.swift in Sources */,
898910
D21B667F1F6A723C00125DE1 /* Expiry.swift in Sources */,
899911
D270147520D101F3003B45C7 /* StorageAware.swift in Sources */,
@@ -946,7 +958,7 @@
946958
files = (
947959
D221E5C420D00DDB00BC940E /* DiskStorage.swift in Sources */,
948960
D2CF98681F694FFA00CE8F68 /* ImageWrapper.swift in Sources */,
949-
D5A9D1C321144B65005DBD3F /* StoreObservable.swift in Sources */,
961+
D5A9D1C321144B65005DBD3F /* StorageObservationRegistry.swift in Sources */,
950962
D2CF98871F695B8F00CE8F68 /* Types.swift in Sources */,
951963
D2CF98621F694FFA00CE8F68 /* Date+Extensions.swift in Sources */,
952964
D2CF98641F694FFA00CE8F68 /* DataSerializer.swift in Sources */,

Source/Shared/Storage/HybridStorage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44
public final class HybridStorage<T> {
55
public let memoryStorage: MemoryStorage<T>
66
public let diskStorage: DiskStorage<T>
7-
public let registry = StorageObservationRegister<HybridStorage>()
7+
public let registry = StorageObservationRegistry<HybridStorage>()
88

99
public init(memoryStorage: MemoryStorage<T>, diskStorage: DiskStorage<T>) {
1010
self.memoryStorage = memoryStorage

Source/Shared/Storage/Storage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public final class Storage<T> {
88
let syncStorage: SyncStorage<T>
99
let asyncStorage: AsyncStorage<T>
1010

11-
public let registry = StorageObservationRegister<Storage>()
11+
public let registry = StorageObservationRegistry<Storage>()
1212

1313
/// Initialize storage with configuration options.
1414
///

Source/Shared/Storage/StoreObservable.swift renamed to Source/Shared/Storage/StorageObservationRegistry.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
public final class StorageObservationRegister<T: StorageAware> {
3+
public final class StorageObservationRegistry<T: StorageAware> {
44
public typealias Observation = (T, StorageChange) -> Void
55
private(set) var observations = [UUID: Observation]()
66

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import XCTest
2+
@testable import Cache
3+
4+
final class ObservationTokenTests: XCTestCase {
5+
func testCancel() {
6+
var cancelled = false
7+
8+
let token = ObservationToken {
9+
cancelled = true
10+
}
11+
12+
token.cancel()
13+
XCTAssertTrue(cancelled)
14+
}
15+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import XCTest
2+
@testable import Cache
3+
4+
final class StorageObservationRegistryTests: XCTestCase {
5+
private var registry: StorageObservationRegistry<Storage<User>>!
6+
private var storage: Storage<User>!
7+
8+
override func setUp() {
9+
super.setUp()
10+
registry = StorageObservationRegistry()
11+
storage = try! Storage<User>(
12+
diskConfig: DiskConfig(name: "Thor"),
13+
memoryConfig: MemoryConfig(),
14+
transformer: TransformerFactory.forCodable(ofType: User.self)
15+
)
16+
}
17+
18+
func testRegister() {
19+
registry.register { _, _ in }
20+
XCTAssertEqual(registry.observations.count, 1)
21+
22+
registry.register { _, _ in }
23+
XCTAssertEqual(registry.observations.count, 2)
24+
}
25+
26+
func testDeregister() {
27+
let token = registry.register { _, _ in }
28+
XCTAssertEqual(registry.observations.count, 1)
29+
30+
registry.deregister(token: token)
31+
XCTAssertTrue(registry.observations.isEmpty)
32+
}
33+
34+
func testDeregisterAll() {
35+
registry.register { _, _ in }
36+
registry.register { _, _ in }
37+
XCTAssertEqual(registry.observations.count, 2)
38+
39+
registry.deregisterAll()
40+
XCTAssertTrue(registry.observations.isEmpty)
41+
}
42+
43+
func testNotifyObservers() {
44+
var change1: StorageChange?
45+
var change2: StorageChange?
46+
47+
registry.register { _, change in
48+
change1 = change
49+
}
50+
51+
registry.register { _, change in
52+
change2 = change
53+
}
54+
55+
registry.notifyObservers(about: .addition, in: storage)
56+
57+
XCTAssertEqual(change1, .addition)
58+
XCTAssertEqual(change2, .addition)
59+
}
60+
}

0 commit comments

Comments
 (0)