Defaults/Tests/DefaultsTests/DefaultsDictionaryTests.swift

83 lines
2.4 KiB
Swift
Raw Normal View History

import Foundation
2024-09-26 05:17:26 +08:00
import Testing
import Defaults
2024-09-26 05:17:26 +08:00
private let suite_ = createSuite()
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_)
}
2024-09-26 05:17:26 +08:00
@Suite(.serialized)
final class DefaultsDictionaryTests {
init() {
Defaults.removeAll(suite: suite_)
}
2024-09-26 05:17:26 +08:00
deinit {
Defaults.removeAll(suite: suite_)
}
2024-09-26 05:17:26 +08:00
@Test
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"])
let newValue = "John"
Defaults[key]["0"] = newValue
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["0"] == newValue)
}
2024-09-26 05:17:26 +08:00
@Test
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)
Defaults[key] = fixtureDictionary
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?["0"] == fixtureDictionary["0"])
Defaults[key] = nil
2024-09-26 05:17:26 +08:00
#expect(Defaults[key] == nil)
let newValue = ["0": "Chen"]
Defaults[key] = newValue
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?["0"] == newValue["0"])
}
2024-09-26 05:17:26 +08:00
@Test
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")
let newName = "Chen"
Defaults[key]["0"]?["0"] = newName
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["0"]?["0"] == newName)
}
2024-09-26 05:17:26 +08:00
@Test
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)
let newName = "Chen"
Defaults[key]["0"]?[0] = newName
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["0"] == [newName, fixtureArray[1]])
}
2024-09-26 05:17:26 +08:00
@Test
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])
let newValue = "John"
Defaults[key][1] = newValue
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][1] == newValue)
}
2024-09-26 05:17:26 +08:00
@Test
func testType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.dictionary]["0"] == fixtureDictionary["0"])
let newName = "Chen"
Defaults[.dictionary]["0"] = newName
2024-09-26 05:17:26 +08:00
#expect(Defaults[.dictionary]["0"] == newName)
}
}