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 fixtureArray = ["Hank", "Chen"]
|
|
|
|
|
|
|
|
extension Defaults.Keys {
|
2024-09-26 05:17:26 +08:00
|
|
|
fileprivate static let array = Key<[String]>("array", default: fixtureArray, suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Suite(.serialized)
|
|
|
|
final class DefaultsArrayTests {
|
|
|
|
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]>("independentArrayStringKey", default: fixtureArray, suite: suite_)
|
|
|
|
#expect(Defaults[key][0] == fixtureArray[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]?>("independentArrayOptionalStringKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = fixtureArray
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?[0] == fixtureArray[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 = ["John", "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() {
|
|
|
|
let defaultValue = ["Hank", "Chen"]
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[[String]]>("independentArrayNestedKey", default: [defaultValue], suite: suite_)
|
|
|
|
#expect(Defaults[key][0][0] == "Hank")
|
2021-05-16 20:21:17 +08:00
|
|
|
let newValue = ["Sindre", "Sorhus"]
|
|
|
|
Defaults[key][0] = newValue
|
|
|
|
Defaults[key].append(defaultValue)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][0][0] == newValue[0])
|
|
|
|
#expect(Defaults[key][0][1] == newValue[1])
|
|
|
|
#expect(Defaults[key][1][0] == defaultValue[0])
|
|
|
|
#expect(Defaults[key][1][1] == defaultValue[1])
|
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 testDictionaryKey() {
|
|
|
|
let defaultValue = ["0": "HankChen"]
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[[String: String]]>("independentArrayDictionaryKey", default: [defaultValue], suite: suite_)
|
|
|
|
#expect(Defaults[key][0]["0"] == defaultValue["0"])
|
2021-05-16 20:21:17 +08:00
|
|
|
let newValue = ["0": "SindreSorhus"]
|
|
|
|
Defaults[key][0] = newValue
|
|
|
|
Defaults[key].append(defaultValue)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][0]["0"] == newValue["0"])
|
|
|
|
#expect(Defaults[key][1]["0"] == defaultValue["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 testNestedDictionaryKey() {
|
|
|
|
let defaultValue = ["0": [["0": 0]]]
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[[String: [[String: Int]]]]>("independentArrayNestedDictionaryKey", default: [defaultValue], suite: suite_)
|
|
|
|
#expect(Defaults[key][0]["0"]?[0]["0"] == 0)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newValue = 1
|
2024-09-26 05:17:26 +08:00
|
|
|
Defaults[key][0]["0"]?[0]["0"] = newValue
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key].append(defaultValue)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][1]["0"]?[0]["0"] == 0)
|
|
|
|
#expect(Defaults[key][0]["0"]?[0]["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 testType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.array][0] == fixtureArray[0])
|
2021-05-16 20:21:17 +08:00
|
|
|
let newName = "Hank121314"
|
|
|
|
Defaults[.array][0] = newName
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.array][0] == newName)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, visionOS 1.0, *)
|
|
|
|
@Test
|
|
|
|
func testObserveKeyCombine() async {
|
|
|
|
let key = Defaults.Key<[String]>("observeArrayKeyCombine", default: fixtureArray, suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newName = "Chen"
|
|
|
|
|
|
|
|
let publisher = Defaults
|
|
|
|
.publisher(key, options: [])
|
|
|
|
.map { ($0.oldValue, $0.newValue) }
|
|
|
|
.collect(2)
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
let expectedValues = [(fixtureArray[0], newName), (newName, fixtureArray[0])]
|
2021-05-16 20:21:17 +08:00
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
Task {
|
|
|
|
try? await Task.sleep(for: .seconds(0.1))
|
|
|
|
Defaults[key][0] = newName
|
|
|
|
Defaults.reset(key)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
for await tuples in publisher.values {
|
|
|
|
for (index, expected) in expectedValues.enumerated() {
|
|
|
|
#expect(expected.0 == tuples[index].0[0])
|
|
|
|
#expect(expected.1 == tuples[index].1[0])
|
|
|
|
}
|
2021-05-16 20:21:17 +08:00
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
break
|
|
|
|
}
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, visionOS 1.0, *)
|
|
|
|
@Test
|
|
|
|
func testObserveOptionalKeyCombine() async {
|
|
|
|
let key = Defaults.Key<[String]?>("observeArrayOptionalKeyCombine", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
2021-05-16 20:21:17 +08:00
|
|
|
let newName = ["Chen"]
|
|
|
|
|
|
|
|
let publisher = Defaults
|
|
|
|
.publisher(key, options: [])
|
|
|
|
.map { ($0.oldValue, $0.newValue) }
|
|
|
|
.collect(3)
|
|
|
|
|
2023-09-03 14:39:57 +08:00
|
|
|
// swiftlint:disable:next discouraged_optional_collection
|
2021-05-16 20:21:17 +08:00
|
|
|
let expectedValues: [([String]?, [String]?)] = [(nil, fixtureArray), (fixtureArray, newName), (newName, nil)]
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
Task {
|
|
|
|
try? await Task.sleep(for: .seconds(0.1))
|
|
|
|
Defaults[key] = fixtureArray
|
|
|
|
Defaults[key] = newName
|
|
|
|
Defaults.reset(key)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
for await actualValues in publisher.values {
|
|
|
|
for (expected, actual) in zip(expectedValues, actualValues) {
|
|
|
|
#expect(expected.0 == actual.0)
|
|
|
|
#expect(expected.1 == actual.1)
|
|
|
|
}
|
2021-05-16 20:21:17 +08:00
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
break
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|