2021-05-16 20:21:17 +08:00
|
|
|
import Foundation
|
|
|
|
import CoreData
|
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
|
|
|
|
2022-01-25 13:34:27 +08:00
|
|
|
@objc(ExamplePersistentHistory)
|
2021-05-16 20:21:17 +08:00
|
|
|
private final class ExamplePersistentHistory: NSPersistentHistoryToken, Defaults.Serializable {
|
|
|
|
let value: String
|
|
|
|
|
|
|
|
init(value: String) {
|
|
|
|
self.value = value
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
self.value = coder.decodeObject(forKey: "value") as! String
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func encode(with coder: NSCoder) {
|
|
|
|
coder.encode(value, forKey: "value")
|
|
|
|
}
|
|
|
|
|
2024-01-18 17:58:06 +08:00
|
|
|
override class var supportsSecureCoding: Bool { true } // swiftlint:disable:this non_overridable_class_declaration
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NSSecureCoding
|
2021-10-12 14:45:07 +08:00
|
|
|
private let persistentHistoryValue = ExamplePersistentHistory(value: "ExampleToken")
|
2021-05-16 20:21:17 +08:00
|
|
|
|
|
|
|
extension Defaults.Keys {
|
2024-09-26 05:17:26 +08:00
|
|
|
fileprivate static let persistentHistory = Key<ExamplePersistentHistory>("persistentHistory", default: persistentHistoryValue, suite: suite_)
|
|
|
|
fileprivate static let persistentHistoryArray = Key<[ExamplePersistentHistory]>("array_persistentHistory", default: [persistentHistoryValue], suite: suite_)
|
|
|
|
fileprivate static let persistentHistoryDictionary = Key<[String: ExamplePersistentHistory]>("dictionary_persistentHistory", default: ["0": persistentHistoryValue], suite: suite_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Suite(.serialized)
|
|
|
|
final class DefaultsNSSecureCodingTests {
|
|
|
|
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<ExamplePersistentHistory>("independentNSSecureCodingKey", default: persistentHistoryValue, suite: suite_)
|
|
|
|
#expect(Defaults[key].value == persistentHistoryValue.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
|
|
|
|
Defaults[key] = newPersistentHistory
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key].value == newPersistentHistory.value)
|
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<ExamplePersistentHistory?>("independentNSSecureCodingOptionalKey", suite: suite_)
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = persistentHistoryValue
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?.value == persistentHistoryValue.value)
|
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 newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
|
|
|
|
Defaults[key] = newPersistentHistory
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?.value == newPersistentHistory.value)
|
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<[ExamplePersistentHistory]>("independentNSSecureCodingArrayKey", default: [persistentHistoryValue], suite: suite_)
|
|
|
|
#expect(Defaults[key][0].value == persistentHistoryValue.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
|
|
|
|
Defaults[key].append(newPersistentHistory1)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][1].value == newPersistentHistory1.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newPersistentHistory2 = ExamplePersistentHistory(value: "NewValue2")
|
|
|
|
Defaults[key][1] = newPersistentHistory2
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][1].value == newPersistentHistory2.value)
|
|
|
|
#expect(Defaults[key][0].value == persistentHistoryValue.value)
|
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 testArrayOptionalKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[ExamplePersistentHistory]?>("independentNSSecureCodingArrayOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = [persistentHistoryValue]
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?[0].value == persistentHistoryValue.value)
|
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
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-05-16 20:21:17 +08:00
|
|
|
func testNestedArrayKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[[ExamplePersistentHistory]]>("independentNSSecureCodingNestedArrayKey", default: [[persistentHistoryValue]], suite: suite_)
|
|
|
|
#expect(Defaults[key][0][0].value == persistentHistoryValue.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
|
|
|
|
Defaults[key][0].append(newPersistentHistory1)
|
|
|
|
let newPersistentHistory2 = ExamplePersistentHistory(value: "NewValue2")
|
|
|
|
Defaults[key].append([newPersistentHistory2])
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][0][1].value == newPersistentHistory1.value)
|
|
|
|
#expect(Defaults[key][1][0].value == newPersistentHistory2.value)
|
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 testArrayDictionaryKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[[String: ExamplePersistentHistory]]>("independentNSSecureCodingArrayDictionaryKey", default: [["0": persistentHistoryValue]], suite: suite_)
|
|
|
|
#expect(Defaults[key][0]["0"]?.value == persistentHistoryValue.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
|
|
|
|
Defaults[key][0]["1"] = newPersistentHistory1
|
|
|
|
let newPersistentHistory2 = ExamplePersistentHistory(value: "NewValue2")
|
|
|
|
Defaults[key].append(["0": newPersistentHistory2])
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][0]["1"]?.value == newPersistentHistory1.value)
|
|
|
|
#expect(Defaults[key][1]["0"]?.value == newPersistentHistory2.value)
|
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() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: ExamplePersistentHistory]>("independentNSSecureCodingDictionaryKey", default: ["0": persistentHistoryValue], suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"]?.value == persistentHistoryValue.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
|
|
|
|
Defaults[key]["1"] = newPersistentHistory1
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]["1"]?.value == newPersistentHistory1.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newPersistentHistory2 = ExamplePersistentHistory(value: "NewValue2")
|
|
|
|
Defaults[key]["1"] = newPersistentHistory2
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]["1"]?.value == newPersistentHistory2.value)
|
|
|
|
#expect(Defaults[key]["0"]?.value == persistentHistoryValue.value)
|
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 testDictionaryOptionalKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: ExamplePersistentHistory]?>("independentNSSecureCodingDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = ["0": persistentHistoryValue]
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?["0"]?.value == persistentHistoryValue.value)
|
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 testDictionaryArrayKey() {
|
2024-09-26 05:17:26 +08:00
|
|
|
let key = Defaults.Key<[String: [ExamplePersistentHistory]]>("independentNSSecureCodingDictionaryArrayKey", default: ["0": [persistentHistoryValue]], suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"]?[0].value == persistentHistoryValue.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
|
|
|
|
Defaults[key]["0"]?.append(newPersistentHistory1)
|
|
|
|
let newPersistentHistory2 = ExamplePersistentHistory(value: "NewValue2")
|
|
|
|
Defaults[key]["1"] = [newPersistentHistory2]
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]["0"]?[1].value == newPersistentHistory1.value)
|
|
|
|
#expect(Defaults[key]["1"]?[0].value == newPersistentHistory2.value)
|
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[.persistentHistory].value == persistentHistoryValue.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
|
|
|
|
Defaults[.persistentHistory] = newPersistentHistory
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.persistentHistory].value == newPersistentHistory.value)
|
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 testArrayType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.persistentHistoryArray][0].value == persistentHistoryValue.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
|
|
|
|
Defaults[.persistentHistoryArray][0] = newPersistentHistory
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.persistentHistoryArray][0].value == newPersistentHistory.value)
|
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 testDictionaryType() {
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.persistentHistoryDictionary]["0"]?.value == persistentHistoryValue.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
|
|
|
|
Defaults[.persistentHistoryDictionary]["0"] = newPersistentHistory
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[.persistentHistoryDictionary]["0"]?.value == newPersistentHistory.value)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
}
|