2021-05-16 20:21:17 +08:00
|
|
|
import Foundation
|
2024-09-26 05:17:26 +08:00
|
|
|
import Testing
|
2021-05-16 20:21:17 +08:00
|
|
|
import Defaults
|
2024-09-26 05:17:26 +08:00
|
|
|
|
|
|
|
private let suite_ = createSuite()
|
2021-05-16 20:21:17 +08:00
|
|
|
|
|
|
|
private let fixtureDictionary = ["0": "Hank"]
|
|
|
|
|
|
|
|
private let fixtureArray = ["Hank", "Chen"]
|
|
|
|
|
|
|
|
extension Defaults.Keys {
|
2024-09-26 05:17:26 +08:00
|
|
|
fileprivate static let dictionary = Key<[String: String]>("dictionary", default: fixtureDictionary, suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Suite(.serialized)
|
|
|
|
final class DefaultsDictionaryTests {
|
|
|
|
init() {
|
|
|
|
Defaults.removeAll(suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
deinit {
|
|
|
|
Defaults.removeAll(suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: String]>("independentDictionaryStringKey", default: fixtureDictionary, suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"] == fixtureDictionary["0"])
|
2021-05-16 20:21:17 +08:00
|
|
|
let newValue = "John"
|
|
|
|
Defaults[key]["0"] = newValue
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]["0"] == newValue)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testOptionalKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: String]?>("independentDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = fixtureDictionary
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?["0"] == fixtureDictionary["0"])
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = nil
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newValue = ["0": "Chen"]
|
|
|
|
Defaults[key] = newValue
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?["0"] == newValue["0"])
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testNestedKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: [String: String]]>("independentDictionaryNestedKey", default: ["0": fixtureDictionary], suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"]?["0"] == "Hank")
|
2021-05-16 20:21:17 +08:00
|
|
|
let newName = "Chen"
|
|
|
|
Defaults[key]["0"]?["0"] = newName
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]["0"]?["0"] == newName)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testArrayKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: [String]]>("independentDictionaryArrayKey", default: ["0": fixtureArray], suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"] == fixtureArray)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newName = "Chen"
|
|
|
|
Defaults[key]["0"]?[0] = newName
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]["0"] == [newName, fixtureArray[1]])
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2023-09-14 21:42:48 +08:00
|
|
|
func testIntKey() {
|
|
|
|
let fixture = [1: "x"]
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[Int: String]>("independentDictionaryIntKey", default: fixture, suite: suite_)
|
|
|
|
#expect(Defaults[key][1] == fixture[1])
|
2023-09-14 21:42:48 +08:00
|
|
|
let newValue = "John"
|
|
|
|
Defaults[key][1] = newValue
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][1] == newValue)
|
2023-09-14 21:42:48 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.dictionary]["0"] == fixtureDictionary["0"])
|
2021-05-16 20:21:17 +08:00
|
|
|
let newName = "Chen"
|
|
|
|
Defaults[.dictionary]["0"] = newName
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.dictionary]["0"] == newName)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
}
|