|
| 1 | +#if !os(Windows) |
| 2 | +import XCTest |
| 3 | +@testable import Cache |
| 4 | + |
| 5 | +final class ComposableCacheTests: XCTestCase { |
| 6 | + func testComplexComposableCache() { |
| 7 | + enum Key { |
| 8 | + case a |
| 9 | + case b |
| 10 | + case c |
| 11 | + case d |
| 12 | + } |
| 13 | + |
| 14 | + let lruCache: LRUCache<Key, Any> = LRUCache(capacity: 3) |
| 15 | + let expiringCache: ExpiringCache<Key, Any> = ExpiringCache(duration: .seconds(1)) |
| 16 | + |
| 17 | + let cache: ComposableCache = ComposableCache<Key>( |
| 18 | + caches: [ |
| 19 | + lruCache, // First Cache |
| 20 | + // ... // Other Caches |
| 21 | + expiringCache // Last Cache |
| 22 | + ] |
| 23 | + ) |
| 24 | + |
| 25 | + XCTAssertNil(cache.get(.a)) |
| 26 | + |
| 27 | + cache.set(value: "Hello, A!", forKey: .a) |
| 28 | + |
| 29 | + XCTAssertNotNil(cache.get(.a)) |
| 30 | + XCTAssertNotNil(lruCache.get(.a)) |
| 31 | + XCTAssertNotNil(expiringCache.get(.a)) |
| 32 | + |
| 33 | + cache.set(value: "Hello, B!", forKey: .b) |
| 34 | + cache.set(value: "Hello, C!", forKey: .c) |
| 35 | + cache.set(value: "Hello, D!", forKey: .d) |
| 36 | + |
| 37 | + XCTAssertNil(lruCache.get(.a)) |
| 38 | + XCTAssertNotNil(cache.get(.a)) |
| 39 | + XCTAssertNotNil(expiringCache.get(.a)) |
| 40 | + |
| 41 | + XCTAssertNotNil(cache.get(.b)) |
| 42 | + XCTAssertNotNil(cache.get(.c)) |
| 43 | + XCTAssertNotNil(cache.get(.d)) |
| 44 | + |
| 45 | + sleep(1) |
| 46 | + |
| 47 | + // Check ComposableCache |
| 48 | + |
| 49 | + XCTAssertNil(cache.get(.a)) |
| 50 | + XCTAssertNotNil(cache.get(.b)) |
| 51 | + XCTAssertNotNil(cache.get(.c)) |
| 52 | + XCTAssertNotNil(cache.get(.d)) |
| 53 | + |
| 54 | + // Check LRUCache |
| 55 | + |
| 56 | + XCTAssertNil(lruCache.get(.a)) |
| 57 | + XCTAssertNotNil(lruCache.get(.b)) |
| 58 | + XCTAssertNotNil(lruCache.get(.c)) |
| 59 | + XCTAssertNotNil(lruCache.get(.d)) |
| 60 | + |
| 61 | + // Check ExpiringCache |
| 62 | + |
| 63 | + XCTAssertNil(expiringCache.get(.a)) |
| 64 | + XCTAssertNil(expiringCache.get(.b)) |
| 65 | + XCTAssertNil(expiringCache.get(.c)) |
| 66 | + XCTAssertNil(expiringCache.get(.d)) |
| 67 | + } |
| 68 | + |
| 69 | + func testAllValues() { |
| 70 | + enum Key { |
| 71 | + case text |
| 72 | + } |
| 73 | + |
| 74 | + let cache: ComposableCache = ComposableCache<Key>( |
| 75 | + initialValues: [ |
| 76 | + .text: "Hello, World!" |
| 77 | + ] |
| 78 | + ) |
| 79 | + |
| 80 | + XCTAssertEqual(cache.allValues.count, 1) |
| 81 | + } |
| 82 | + |
| 83 | + func testAllValues_None() { |
| 84 | + enum Key { |
| 85 | + case text |
| 86 | + } |
| 87 | + |
| 88 | + let cache: ComposableCache = ComposableCache<Key>( |
| 89 | + initialValues: [:] |
| 90 | + ) |
| 91 | + |
| 92 | + XCTAssertEqual(cache.allValues.count, 0) |
| 93 | + } |
| 94 | + |
| 95 | + func testGet_Success() { |
| 96 | + enum Key { |
| 97 | + case text |
| 98 | + } |
| 99 | + |
| 100 | + let cache: ComposableCache = ComposableCache<Key>( |
| 101 | + caches: [ |
| 102 | + Cache<Key, String>( |
| 103 | + initialValues: [ |
| 104 | + .text: "Hello, World!" |
| 105 | + ] |
| 106 | + ) |
| 107 | + ] |
| 108 | + ) |
| 109 | + |
| 110 | + XCTAssertEqual(cache.get(.text), "Hello, World!") |
| 111 | + } |
| 112 | + |
| 113 | + func testGet_MissingKey() { |
| 114 | + enum Key { |
| 115 | + case text |
| 116 | + case missingKey |
| 117 | + } |
| 118 | + |
| 119 | + let cache: ComposableCache = ComposableCache<Key>( |
| 120 | + caches: [ |
| 121 | + Cache<Key, String>( |
| 122 | + initialValues: [ |
| 123 | + .text: "Hello, World!" |
| 124 | + ] |
| 125 | + ) |
| 126 | + ] |
| 127 | + ) |
| 128 | + |
| 129 | + XCTAssertNil(cache.get(.missingKey)) |
| 130 | + } |
| 131 | + |
| 132 | + func testGet_InvalidType() { |
| 133 | + enum Key { |
| 134 | + case text |
| 135 | + } |
| 136 | + |
| 137 | + let cache: ComposableCache = ComposableCache<Key>( |
| 138 | + caches: [ |
| 139 | + Cache<Key, String>( |
| 140 | + initialValues: [ |
| 141 | + .text: "Hello, World!" |
| 142 | + ] |
| 143 | + ) |
| 144 | + ] |
| 145 | + ) |
| 146 | + |
| 147 | + XCTAssertNil(cache.get(.text, as: Int.self)) |
| 148 | + } |
| 149 | + |
| 150 | + func testResolve_Success() throws { |
| 151 | + enum Key { |
| 152 | + case text |
| 153 | + } |
| 154 | + |
| 155 | + let cache: ComposableCache = ComposableCache<Key>( |
| 156 | + caches: [ |
| 157 | + Cache<Key, String>( |
| 158 | + initialValues: [ |
| 159 | + .text: "Hello, World!" |
| 160 | + ] |
| 161 | + ) |
| 162 | + ] |
| 163 | + ) |
| 164 | + |
| 165 | + XCTAssertEqual(try cache.resolve(.text), "Hello, World!") |
| 166 | + } |
| 167 | + |
| 168 | + func testResolve_MissingKey() throws { |
| 169 | + enum Key { |
| 170 | + case text |
| 171 | + case missingKey |
| 172 | + } |
| 173 | + |
| 174 | + let cache: ComposableCache = ComposableCache<Key>( |
| 175 | + caches: [ |
| 176 | + Cache<Key, String>( |
| 177 | + initialValues: [ |
| 178 | + .text: "Hello, World!" |
| 179 | + ] |
| 180 | + ) |
| 181 | + ] |
| 182 | + ) |
| 183 | + |
| 184 | + XCTAssertThrowsError(try cache.resolve(.missingKey, as: Any.self)) |
| 185 | + } |
| 186 | + |
| 187 | + func testResolve_InvalidType() throws { |
| 188 | + enum Key { |
| 189 | + case text |
| 190 | + } |
| 191 | + |
| 192 | + let cache: ComposableCache = ComposableCache<Key>( |
| 193 | + caches: [ |
| 194 | + Cache<Key, String>( |
| 195 | + initialValues: [ |
| 196 | + .text: "Hello, World!" |
| 197 | + ] |
| 198 | + ) |
| 199 | + ] |
| 200 | + ) |
| 201 | + |
| 202 | + XCTAssertThrowsError(try cache.resolve(.text, as: Int.self)) |
| 203 | + } |
| 204 | + |
| 205 | + func testSet() { |
| 206 | + enum Key { |
| 207 | + case text |
| 208 | + } |
| 209 | + |
| 210 | + let cache: ComposableCache = ComposableCache<Key>( |
| 211 | + caches: [ |
| 212 | + Cache<Key, String>() |
| 213 | + ] |
| 214 | + ) |
| 215 | + |
| 216 | + cache.set(value: "Hello, World!", forKey: .text) |
| 217 | + |
| 218 | + XCTAssertEqual(cache.get(.text), "Hello, World!") |
| 219 | + } |
| 220 | + |
| 221 | + func testRemove() { |
| 222 | + enum Key { |
| 223 | + case text |
| 224 | + } |
| 225 | + |
| 226 | + let cache: ComposableCache = ComposableCache<Key>( |
| 227 | + caches: [ |
| 228 | + Cache<Key, String>( |
| 229 | + initialValues: [ |
| 230 | + .text: "Hello, World!" |
| 231 | + ] |
| 232 | + ) |
| 233 | + ] |
| 234 | + ) |
| 235 | + |
| 236 | + XCTAssertEqual(cache.get(.text), "Hello, World!") |
| 237 | + |
| 238 | + cache.remove(.text) |
| 239 | + |
| 240 | + XCTAssertNil(cache.get(.text)) |
| 241 | + } |
| 242 | + |
| 243 | + func testContains_Success() { |
| 244 | + enum Key { |
| 245 | + case text |
| 246 | + } |
| 247 | + |
| 248 | + let cache: ComposableCache = ComposableCache<Key>( |
| 249 | + caches: [ |
| 250 | + Cache<Key, String>( |
| 251 | + initialValues: [ |
| 252 | + .text: "Hello, World!" |
| 253 | + ] |
| 254 | + ) |
| 255 | + ] |
| 256 | + ) |
| 257 | + |
| 258 | + XCTAssert(cache.contains(.text)) |
| 259 | + } |
| 260 | + |
| 261 | + func testContains_Failure() { |
| 262 | + enum Key { |
| 263 | + case text |
| 264 | + } |
| 265 | + |
| 266 | + let cache: ComposableCache = ComposableCache<Key>( |
| 267 | + caches: [ |
| 268 | + Cache<Key, String>() |
| 269 | + ] |
| 270 | + ) |
| 271 | + |
| 272 | + XCTAssertFalse(cache.contains(.text)) |
| 273 | + } |
| 274 | + |
| 275 | + func testRequire_Success() throws { |
| 276 | + enum Key { |
| 277 | + case text |
| 278 | + } |
| 279 | + |
| 280 | + let cache: ComposableCache = ComposableCache<Key>( |
| 281 | + caches: [ |
| 282 | + Cache<Key, String>( |
| 283 | + initialValues: [ |
| 284 | + .text: "Hello, World!" |
| 285 | + ] |
| 286 | + ) |
| 287 | + ] |
| 288 | + ) |
| 289 | + |
| 290 | + XCTAssertNoThrow(try cache.require(.text)) |
| 291 | + } |
| 292 | + |
| 293 | + func testRequire_Missing() throws { |
| 294 | + enum Key { |
| 295 | + case text |
| 296 | + case missingKey |
| 297 | + } |
| 298 | + |
| 299 | + let cache: ComposableCache = ComposableCache<Key>( |
| 300 | + caches: [ |
| 301 | + Cache<Key, String>( |
| 302 | + initialValues: [ |
| 303 | + .text: "Hello, World!" |
| 304 | + ] |
| 305 | + ) |
| 306 | + ] |
| 307 | + ) |
| 308 | + |
| 309 | + XCTAssertThrowsError(try cache.require(.missingKey)) |
| 310 | + } |
| 311 | + |
| 312 | + func testRequireSet_Success() throws { |
| 313 | + enum Key { |
| 314 | + case text |
| 315 | + } |
| 316 | + |
| 317 | + let cache: ComposableCache = ComposableCache<Key>( |
| 318 | + caches: [ |
| 319 | + Cache<Key, String>( |
| 320 | + initialValues: [ |
| 321 | + .text: "Hello, World!" |
| 322 | + ] |
| 323 | + ) |
| 324 | + ] |
| 325 | + ) |
| 326 | + |
| 327 | + XCTAssertNoThrow(try cache.require(keys: [.text])) |
| 328 | + } |
| 329 | + |
| 330 | + func testRequireSet_Missing() throws { |
| 331 | + enum Key { |
| 332 | + case text |
| 333 | + case missingKey |
| 334 | + } |
| 335 | + |
| 336 | + let cache: ComposableCache = ComposableCache<Key>( |
| 337 | + caches: [ |
| 338 | + Cache<Key, String>( |
| 339 | + initialValues: [ |
| 340 | + .text: "Hello, World!" |
| 341 | + ] |
| 342 | + ) |
| 343 | + ] |
| 344 | + ) |
| 345 | + |
| 346 | + XCTAssertThrowsError(try cache.require(keys: [.text, .missingKey])) |
| 347 | + } |
| 348 | +} |
| 349 | +#endif |
0 commit comments