feat(ios): algorithm to determines UserDefaults delta

This commit is contained in:
jahorton 2020-02-18 15:22:13 +07:00
parent 891a22ab90
commit 6fa31dbbe5

View file

@ -23,6 +23,29 @@ class KeymanEngineTests: XCTestCase {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func getAssignedDictionary(for userDefaults: UserDefaults) -> [String: Any] {
var currentDictionary = userDefaults.dictionaryRepresentation()
clearSettings(of: userDefaults)
let baseDict = userDefaults.dictionaryRepresentation()
baseDict.forEach({ pair in
currentDictionary.removeValue(forKey: pair.key)
})
// The return value is now properly constructed. Now to put the defaults back in place before we leave.
//userDefaults.register(defaults: currentDictionary) // adds them in an unclearable way!
currentDictionary.forEach({ pair in
userDefaults.set(pair.value, forKey: pair.key)
})
return currentDictionary
}
func clearSettings(of userDefaults: UserDefaults) {
let domain = Bundle.main.bundleIdentifier!
userDefaults.synchronize()
userDefaults.removePersistentDomain(forName: domain)
}
func testStorageAccess() {
// Xcode unit tests cannot use capabilities, and thus we cannot access a true app group.
// So, we'll use a sandboxed analogue for this instead.
@ -31,8 +54,10 @@ class KeymanEngineTests: XCTestCase {
let storage = Storage.active!
log.info(storage.baseDir)
// Diff-magic for determining the dictionary to be utilized for a test.
let userDefaults = storage.userDefaults
let dictionary = userDefaults.dictionaryRepresentation()
userDefaults.set(4, forKey: "testEntry")
let dictCheck = getAssignedDictionary(for: userDefaults)
Storage.active.erase()
}