Defaults/Tests/DefaultsTests/DefaultsCodableTests.swift

195 lines
6.4 KiB
Swift
Raw Normal View History

import Foundation
2024-09-26 05:17:26 +08:00
import Testing
import Defaults
2024-09-26 05:17:26 +08:00
private let suite_ = createSuite()
private struct Unicorn: Codable, Defaults.Serializable {
var isUnicorn: Bool
}
private let fixtureCodable = Unicorn(isUnicorn: true)
@objc(UnicornCodableAndNSSecureCoding)
private final class UnicornCodableAndNSSecureCoding: NSObject, NSSecureCoding, Codable, Defaults.Serializable {
static let supportsSecureCoding = true
func encode(with coder: NSCoder) {}
init?(coder: NSCoder) {}
override init() {
super.init()
}
}
@objc(UnicornCodableAndPreferNSSecureCoding)
private final class UnicornCodableAndPreferNSSecureCoding: NSObject, NSSecureCoding, Codable, Defaults.Serializable, Defaults.PreferNSSecureCoding {
static let supportsSecureCoding = true
func encode(with coder: NSCoder) {}
init?(coder: NSCoder) {}
override init() {
super.init()
}
}
extension Defaults.Keys {
2024-09-26 05:17:26 +08:00
fileprivate static let codable = Key<Unicorn>("codable", default: fixtureCodable, suite: suite_)
fileprivate static let codableArray = Key<[Unicorn]>("codable", default: [fixtureCodable], suite: suite_)
fileprivate static let codableDictionary = Key<[String: Unicorn]>("codable", default: ["0": fixtureCodable], suite: suite_)
}
2024-09-26 05:17:26 +08:00
@Suite(.serialized)
final class DefaultsCodableTests {
init() {
// TODO: Convert all the keys to use a prefix and then remove based on the prefix.
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<Unicorn>("independentCodableKey", default: fixtureCodable, suite: suite_)
#expect(Defaults[key].isUnicorn)
Defaults[key].isUnicorn = false
2024-09-26 05:17:26 +08:00
#expect(!Defaults[key].isUnicorn)
}
2024-09-26 05:17:26 +08:00
@Test
func testOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<Unicorn?>("independentCodableOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = Unicorn(isUnicorn: true)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?.isUnicorn ?? false)
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[Unicorn]>("independentCodableArrayKey", default: [fixtureCodable], suite: suite_)
#expect(Defaults[key][0].isUnicorn)
Defaults[key].append(Unicorn(isUnicorn: false))
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0].isUnicorn)
#expect(!Defaults[key][1].isUnicorn)
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[Unicorn]?>("independentCodableArrayOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = [fixtureCodable]
Defaults[key]?.append(Unicorn(isUnicorn: false))
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?[0].isUnicorn ?? false)
#expect(!(Defaults[key]?[1].isUnicorn ?? false))
}
2024-09-26 05:17:26 +08:00
@Test
func testNestedArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[[Unicorn]]>("independentCodableNestedArrayKey", default: [[fixtureCodable]], suite: suite_)
#expect(Defaults[key][0][0].isUnicorn)
Defaults[key].append([fixtureCodable])
Defaults[key][0].append(Unicorn(isUnicorn: false))
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0][0].isUnicorn)
#expect(Defaults[key][1][0].isUnicorn)
#expect(!Defaults[key][0][1].isUnicorn)
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayDictionaryKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[[String: Unicorn]]>("independentCodableArrayDictionaryKey", default: [["0": fixtureCodable]], suite: suite_)
#expect(Defaults[key][0]["0"]?.isUnicorn ?? false)
Defaults[key].append(["0": fixtureCodable])
Defaults[key][0]["1"] = Unicorn(isUnicorn: false)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0]["0"]?.isUnicorn ?? false)
#expect(Defaults[key][1]["0"]?.isUnicorn ?? false)
#expect(!(Defaults[key][0]["1"]?.isUnicorn ?? true))
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: Unicorn]>("independentCodableDictionaryKey", default: ["0": fixtureCodable], suite: suite_)
#expect(Defaults[key]["0"]?.isUnicorn ?? false)
Defaults[key]["1"] = Unicorn(isUnicorn: false)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["0"]?.isUnicorn ?? false)
#expect(!(Defaults[key]["1"]?.isUnicorn ?? true))
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: Unicorn]?>("independentCodableDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = ["0": fixtureCodable]
Defaults[key]?["1"] = Unicorn(isUnicorn: false)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?["0"]?.isUnicorn ?? false)
#expect(!(Defaults[key]?["1"]?.isUnicorn ?? true))
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: [Unicorn]]>("independentCodableDictionaryArrayKey", default: ["0": [fixtureCodable]], suite: suite_)
#expect(Defaults[key]["0"]?[0].isUnicorn ?? false)
Defaults[key]["1"] = [fixtureCodable]
Defaults[key]["0"]?.append(Unicorn(isUnicorn: false))
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["1"]?[0].isUnicorn ?? false)
#expect(!(Defaults[key]["0"]?[1].isUnicorn ?? true))
}
2024-09-26 05:17:26 +08:00
@Test
func testCodableAndRawRepresentable() {
struct Unicorn: Codable, RawRepresentable, Defaults.Serializable {
var rawValue: String
}
let fixture = Unicorn(rawValue: "x")
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<Unicorn?>("independentKey_codableAndRawRepresentable", suite: suite_)
Defaults[key] = fixture
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?.rawValue == fixture.rawValue)
#expect(suite_.string(forKey: key.name) == #""\#(fixture.rawValue)""#)
}
2024-09-26 05:17:26 +08:00
@Test
func testType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.codable].isUnicorn)
Defaults[.codable] = Unicorn(isUnicorn: false)
2024-09-26 05:17:26 +08:00
#expect(!Defaults[.codable].isUnicorn)
}
2024-09-26 05:17:26 +08:00
@Test
func testArrayType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.codableArray][0].isUnicorn)
Defaults[.codableArray][0] = Unicorn(isUnicorn: false)
2024-09-26 05:17:26 +08:00
#expect(!Defaults[.codableArray][0].isUnicorn)
}
2024-09-26 05:17:26 +08:00
@Test
func testDictionaryType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.codableDictionary]["0"]?.isUnicorn ?? false)
Defaults[.codableDictionary]["0"] = Unicorn(isUnicorn: false)
2024-09-26 05:17:26 +08:00
#expect(!(Defaults[.codableDictionary]["0"]?.isUnicorn ?? true))
}
2024-09-26 05:17:26 +08:00
@Test
func testCodableAndNSSecureCoding() {
let fixture = UnicornCodableAndNSSecureCoding()
let keyName = "testCodableAndNSSecureCoding"
2024-09-26 05:17:26 +08:00
_ = Defaults.Key<UnicornCodableAndNSSecureCoding>(keyName, default: fixture, suite: suite_)
#expect(UserDefaults.standard.data(forKey: keyName) == nil)
#expect(UserDefaults.standard.string(forKey: keyName) != nil)
}
2024-09-26 05:17:26 +08:00
@Test
func testCodableAndPreferNSSecureCoding() {
let fixture = UnicornCodableAndPreferNSSecureCoding()
let keyName = "testCodableAndPreferNSSecureCoding"
2024-09-26 05:17:26 +08:00
_ = Defaults.Key<UnicornCodableAndPreferNSSecureCoding>(keyName, default: fixture, suite: suite_)
#expect(UserDefaults.standard.string(forKey: keyName) == nil)
#expect(UserDefaults.standard.data(forKey: keyName) != nil)
}
}