-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update: move PictureCache to another file * update: add couple of tests for PictureCache * update: remove playground * resolve: review threads
- Loading branch information
1 parent
06531d5
commit 85e0d9b
Showing
3 changed files
with
50 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import SwiftUI | ||
|
||
public class PictureCache { | ||
private var cache: [URL: Image] | ||
private var lock: NSLock | ||
|
||
public init() { | ||
cache = [:] | ||
lock = NSLock() | ||
} | ||
|
||
public func `get`(url: URL) -> Image? { | ||
lock.lock(); defer { lock.unlock() } | ||
|
||
return cache[url] | ||
} | ||
|
||
public func `set`(value: Image?, for url: URL) { | ||
lock.lock(); defer { lock.unlock() } | ||
|
||
cache[url] = value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import XCTest | ||
import SwiftUI | ||
@testable import Picture | ||
|
||
final class PictureCacheTests: XCTestCase { | ||
private let faviconURL: URL = .init(string: "https://openbytes.com/favicon.png")! | ||
|
||
internal func testNotFoundImageForSpecifiedURL() { | ||
let cache = PictureCache() | ||
|
||
let image = cache.get(url: faviconURL) | ||
|
||
XCTAssertNil(image) | ||
} | ||
|
||
internal func testFoundImageForSpecifiedURL() { | ||
let cache = PictureCache() | ||
|
||
let expectedImage = Image("house") | ||
|
||
cache.set(value: expectedImage, for: faviconURL) | ||
|
||
let actualImage = cache.get(url: faviconURL) | ||
|
||
XCTAssertEqual(actualImage, expectedImage) | ||
} | ||
} |