Defaults/Tests/DefaultsTests/DefaultsNSSecureCodingTests...

174 lines
7.3 KiB
Swift
Raw Normal View History

import Foundation
import CoreData
2024-09-26 05:17:26 +08:00
import Testing
import Defaults
2024-09-26 05:17:26 +08:00
private let suite_ = createSuite()
2022-01-25 13:34:27 +08:00
@objc(ExamplePersistentHistory)
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
}
// NSSecureCoding
private let persistentHistoryValue = ExamplePersistentHistory(value: "ExampleToken")
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_)
}
2024-09-26 05:17:26 +08:00
@Suite(.serialized)
final class DefaultsNSSecureCodingTests {
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<ExamplePersistentHistory>("independentNSSecureCodingKey", default: persistentHistoryValue, suite: suite_)
#expect(Defaults[key].value == persistentHistoryValue.value)
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
Defaults[key] = newPersistentHistory
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].value == newPersistentHistory.value)
}
2024-09-26 05:17:26 +08:00
@Test
func testOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<ExamplePersistentHistory?>("independentNSSecureCodingOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = persistentHistoryValue
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?.value == persistentHistoryValue.value)
Defaults[key] = nil
2024-09-26 05:17:26 +08:00
#expect(Defaults[key] == nil)
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
Defaults[key] = newPersistentHistory
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?.value == newPersistentHistory.value)
}
2024-09-26 05:17:26 +08:00
@Test
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)
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
Defaults[key].append(newPersistentHistory1)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][1].value == newPersistentHistory1.value)
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)
}
2024-09-26 05:17:26 +08:00
@Test
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)
Defaults[key] = [persistentHistoryValue]
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?[0].value == persistentHistoryValue.value)
Defaults[key] = nil
2024-09-26 05:17:26 +08:00
#expect(Defaults[key] == nil)
}
2024-09-26 05:17:26 +08:00
@Test
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)
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)
}
2024-09-26 05:17:26 +08:00
@Test
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)
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)
}
2024-09-26 05:17:26 +08:00
@Test
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)
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
Defaults[key]["1"] = newPersistentHistory1
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["1"]?.value == newPersistentHistory1.value)
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)
}
2024-09-26 05:17:26 +08:00
@Test
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)
Defaults[key] = ["0": persistentHistoryValue]
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?["0"]?.value == persistentHistoryValue.value)
}
2024-09-26 05:17:26 +08:00
@Test
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)
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)
}
2024-09-26 05:17:26 +08:00
@Test
func testType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.persistentHistory].value == persistentHistoryValue.value)
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
Defaults[.persistentHistory] = newPersistentHistory
2024-09-26 05:17:26 +08:00
#expect(Defaults[.persistentHistory].value == newPersistentHistory.value)
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.persistentHistoryArray][0].value == persistentHistoryValue.value)
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
Defaults[.persistentHistoryArray][0] = newPersistentHistory
2024-09-26 05:17:26 +08:00
#expect(Defaults[.persistentHistoryArray][0].value == newPersistentHistory.value)
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.persistentHistoryDictionary]["0"]?.value == persistentHistoryValue.value)
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
Defaults[.persistentHistoryDictionary]["0"] = newPersistentHistory
2024-09-26 05:17:26 +08:00
#expect(Defaults[.persistentHistoryDictionary]["0"]?.value == newPersistentHistory.value)
}
}