-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
DefaultsDictionaryTests.swift
82 lines (70 loc) · 2.35 KB
/
DefaultsDictionaryTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import Foundation
import Testing
import Defaults
private let suite_ = createSuite()
private let fixtureDictionary = ["0": "Hank"]
private let fixtureArray = ["Hank", "Chen"]
extension Defaults.Keys {
fileprivate static let dictionary = Key<[String: String]>("dictionary", default: fixtureDictionary, suite: suite_)
}
@Suite(.serialized)
final class DefaultsDictionaryTests {
init() {
Defaults.removeAll(suite: suite_)
}
deinit {
Defaults.removeAll(suite: suite_)
}
@Test
func testKey() {
let key = Defaults.Key<[String: String]>("independentDictionaryStringKey", default: fixtureDictionary, suite: suite_)
#expect(Defaults[key]["0"] == fixtureDictionary["0"])
let newValue = "John"
Defaults[key]["0"] = newValue
#expect(Defaults[key]["0"] == newValue)
}
@Test
func testOptionalKey() {
let key = Defaults.Key<[String: String]?>("independentDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = fixtureDictionary
#expect(Defaults[key]?["0"] == fixtureDictionary["0"])
Defaults[key] = nil
#expect(Defaults[key] == nil)
let newValue = ["0": "Chen"]
Defaults[key] = newValue
#expect(Defaults[key]?["0"] == newValue["0"])
}
@Test
func testNestedKey() {
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
#expect(Defaults[key]["0"]?["0"] == newName)
}
@Test
func testArrayKey() {
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
#expect(Defaults[key]["0"] == [newName, fixtureArray[1]])
}
@Test
func testIntKey() {
let fixture = [1: "x"]
let key = Defaults.Key<[Int: String]>("independentDictionaryIntKey", default: fixture, suite: suite_)
#expect(Defaults[key][1] == fixture[1])
let newValue = "John"
Defaults[key][1] = newValue
#expect(Defaults[key][1] == newValue)
}
@Test
func testType() {
#expect(Defaults[.dictionary]["0"] == fixtureDictionary["0"])
let newName = "Chen"
Defaults[.dictionary]["0"] = newName
#expect(Defaults[.dictionary]["0"] == newName)
}
}