Skip to content

Instantly share code, notes, and snippets.

@kylehughes
Created April 29, 2024 04:52
Show Gist options
  • Save kylehughes/47281ad98ac2c84d17da43e88b14c75b to your computer and use it in GitHub Desktop.
Save kylehughes/47281ad98ac2c84d17da43e88b14c75b to your computer and use it in GitHub Desktop.
Subclass of NSUbiquitousKeyValueStore suitable for use in unit tests.
// Copyright 2024 Kyle Hughes
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import Foundation
public class MockUbiquitousKeyValueStore: NSUbiquitousKeyValueStore {
public var storage: [String: Any] = [:]
public var synchronizeReturnValue: Bool = true
// MARK: Getting the Shared Instance
public static let mockDefault = MockUbiquitousKeyValueStore()
public override class var `default`: NSUbiquitousKeyValueStore {
mockDefault
}
// MARK: Getting Values
public override func object(forKey aKey: String) -> Any? {
storage[aKey]
}
public override func string(forKey aKey: String) -> String? {
storage[aKey] as? String
}
public override func array(forKey aKey: String) -> [Any]? {
storage[aKey] as? [Any]
}
public override func dictionary(forKey aKey: String) -> [String: Any]? {
storage[aKey] as? [String: Any]
}
public override func data(forKey aKey: String) -> Data? {
storage[aKey] as? Data
}
public override func longLong(forKey aKey: String) -> Int64 {
storage[aKey] as? Int64 ?? 0
}
public override func double(forKey aKey: String) -> Double {
storage[aKey] as? Double ?? 0.0
}
public override func bool(forKey aKey: String) -> Bool {
storage[aKey] as? Bool ?? false
}
// MARK: Setting Values
public override func set(_ anObject: Any?, forKey aKey: String) {
storage[aKey] = anObject
}
public override func set(_ aString: String?, forKey aKey: String) {
storage[aKey] = aString
}
public override func set(_ aData: Data?, forKey aKey: String) {
storage[aKey] = aData
}
public override func set(_ anArray: [Any]?, forKey aKey: String) {
storage[aKey] = anArray
}
public override func set(_ aDictionary: [String : Any]?, forKey aKey: String) {
storage[aKey] = aDictionary
}
public override func set(_ value: Int64, forKey aKey: String) {
storage[aKey] = value
}
public override func set(_ value: Double, forKey aKey: String) {
storage[aKey] = value
}
public override func set(_ value: Bool, forKey aKey: String) {
storage[aKey] = value
}
// MARK: Explicitly Synchronizing In-Memory Key-Value Data to Disk
public override func synchronize() -> Bool {
synchronizeReturnValue
}
// MARK: Removing Keys
public override func removeObject(forKey aKey: String) {
storage.removeValue(forKey: aKey)
}
// MARK: Retrieving the Current Keys and Values
public override var dictionaryRepresentation: [String: Any] {
storage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment