-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
DefaultsUIColorTests.swift
142 lines (124 loc) · 5.25 KB
/
DefaultsUIColorTests.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#if !os(macOS)
import Foundation
import UIKit
import Testing
import Defaults
private let suite_ = createSuite()
private let fixtureColor = UIColor(red: Double(103) / Double(0xFF), green: Double(132) / Double(0xFF), blue: Double(255) / Double(0xFF), alpha: 1)
private let fixtureColor1 = UIColor(red: Double(255) / Double(0xFF), green: Double(241) / Double(0xFF), blue: Double(180) / Double(0xFF), alpha: 1)
private let fixtureColor2 = UIColor(red: Double(255) / Double(0xFF), green: Double(180) / Double(0xFF), blue: Double(194) / Double(0xFF), alpha: 1)
extension Defaults.Keys {
fileprivate static let color = Defaults.Key<UIColor>("NSColor", default: fixtureColor, suite: suite_)
fileprivate static let colorArray = Defaults.Key<[UIColor]>("NSColorArray", default: [fixtureColor], suite: suite_)
fileprivate static let colorDictionary = Defaults.Key<[String: UIColor]>("NSColorArray", default: ["0": fixtureColor], suite: suite_)
}
@Suite(.serialized)
final class DefaultsNSColorTests {
init() {
Defaults.removeAll(suite: suite_)
}
deinit {
Defaults.removeAll(suite: suite_)
}
@Test
func testKey() {
let key = Defaults.Key<UIColor>("independentNSColorKey", default: fixtureColor, suite: suite_)
#expect(Defaults[key].isEqual(fixtureColor))
Defaults[key] = fixtureColor1
#expect(Defaults[key].isEqual(fixtureColor1))
}
@Test
func testPreservesColorSpace() {
let fixture = UIColor(displayP3Red: 1, green: 0.3, blue: 0.7, alpha: 1)
let key = Defaults.Key<UIColor?>("independentNSColorPreservesColorSpaceKey", suite: suite_)
Defaults[key] = fixture
#expect(Defaults[key] == fixture)
#expect(Defaults[key]?.cgColor.colorSpace == fixture.cgColor.colorSpace)
#expect(Defaults[key]?.cgColor == fixture.cgColor)
}
@Test
func testOptionalKey() {
let key = Defaults.Key<UIColor?>("independentNSColorOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = fixtureColor
#expect(Defaults[key]?.isEqual(fixtureColor) ?? false)
}
@Test
func testArrayKey() {
let key = Defaults.Key<[UIColor]>("independentNSColorArrayKey", default: [fixtureColor], suite: suite_)
#expect(Defaults[key][0].isEqual(fixtureColor))
Defaults[key].append(fixtureColor1)
#expect(Defaults[key][1].isEqual(fixtureColor1))
}
@Test
func testArrayOptionalKey() {
let key = Defaults.Key<[UIColor]?>("independentNSColorOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = [fixtureColor]
Defaults[key]?.append(fixtureColor1)
#expect(Defaults[key]?[0].isEqual(fixtureColor) ?? false)
#expect(Defaults[key]?[1].isEqual(fixtureColor1) ?? false)
}
@Test
func testNestedArrayKey() {
let key = Defaults.Key<[[UIColor]]>("independentNSColorNestedArrayKey", default: [[fixtureColor]], suite: suite_)
#expect(Defaults[key][0][0].isEqual(fixtureColor))
Defaults[key][0].append(fixtureColor1)
Defaults[key].append([fixtureColor2])
#expect(Defaults[key][0][1].isEqual(fixtureColor1))
#expect(Defaults[key][1][0].isEqual(fixtureColor2))
}
@Test
func testArrayDictionaryKey() {
let key = Defaults.Key<[[String: UIColor]]>("independentNSColorArrayDictionaryKey", default: [["0": fixtureColor]], suite: suite_)
#expect(Defaults[key][0]["0"]?.isEqual(fixtureColor) ?? false)
Defaults[key][0]["1"] = fixtureColor1
Defaults[key].append(["0": fixtureColor2])
#expect(Defaults[key][0]["1"]?.isEqual(fixtureColor1) ?? false)
#expect(Defaults[key][1]["0"]?.isEqual(fixtureColor2) ?? false)
}
@Test
func testDictionaryKey() {
let key = Defaults.Key<[String: UIColor]>("independentNSColorDictionaryKey", default: ["0": fixtureColor], suite: suite_)
#expect(Defaults[key]["0"]?.isEqual(fixtureColor) ?? false)
Defaults[key]["1"] = fixtureColor1
#expect(Defaults[key]["1"]?.isEqual(fixtureColor1) ?? false)
}
@Test
func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: UIColor]?>("independentNSColorDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = ["0": fixtureColor]
Defaults[key]?["1"] = fixtureColor1
#expect(Defaults[key]?["0"]?.isEqual(fixtureColor) ?? false)
#expect(Defaults[key]?["1"]?.isEqual(fixtureColor1) ?? false)
}
@Test
func testDictionaryArrayKey() {
let key = Defaults.Key<[String: [UIColor]]>("independentNSColorDictionaryArrayKey", default: ["0": [fixtureColor]], suite: suite_)
#expect(Defaults[key]["0"]?[0].isEqual(fixtureColor) ?? false)
Defaults[key]["0"]?.append(fixtureColor1)
Defaults[key]["1"] = [fixtureColor2]
#expect(Defaults[key]["0"]?[1].isEqual(fixtureColor1) ?? false)
#expect(Defaults[key]["1"]?[0].isEqual(fixtureColor2) ?? false)
}
@Test
func testType() {
#expect(Defaults[.color].isEqual(fixtureColor))
Defaults[.color] = fixtureColor1
#expect(Defaults[.color].isEqual(fixtureColor1))
}
@Test
func testArrayType() {
#expect(Defaults[.colorArray][0].isEqual(fixtureColor))
Defaults[.colorArray][0] = fixtureColor1
#expect(Defaults[.colorArray][0].isEqual(fixtureColor1))
}
@Test
func testDictionaryType() {
#expect(Defaults[.colorDictionary]["0"]?.isEqual(fixtureColor) ?? false)
Defaults[.colorDictionary]["0"] = fixtureColor1
#expect(Defaults[.colorDictionary]["0"]?.isEqual(fixtureColor1) ?? false)
}
}
#endif