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 struct Unicorn: Codable, Defaults.Serializable {
|
|
|
|
var isUnicorn: Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
private let fixtureCodable = Unicorn(isUnicorn: true)
|
|
|
|
|
2021-09-11 11:49:06 +08:00
|
|
|
@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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 15:03:34 +08:00
|
|
|
@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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 20:21:17 +08:00
|
|
|
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_)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
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_)
|
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<Unicorn>("independentCodableKey", default: fixtureCodable, suite: suite_)
|
|
|
|
#expect(Defaults[key].isUnicorn)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key].isUnicorn = false
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(!Defaults[key].isUnicorn)
|
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<Unicorn?>("independentCodableOptionalKey", suite: suite_)
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key] = Unicorn(isUnicorn: true)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key]?.isUnicorn ?? false)
|
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<[Unicorn]>("independentCodableArrayKey", default: [fixtureCodable], suite: suite_)
|
|
|
|
#expect(Defaults[key][0].isUnicorn)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[key].append(Unicorn(isUnicorn: false))
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(Defaults[key][0].isUnicorn)
|
|
|
|
#expect(!Defaults[key][1].isUnicorn)
|
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<[Unicorn]?>("independentCodableArrayOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
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))
|
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<[[Unicorn]]>("independentCodableNestedArrayKey", default: [[fixtureCodable]], suite: suite_)
|
|
|
|
#expect(Defaults[key][0][0].isUnicorn)
|
2021-05-16 20:21:17 +08:00
|
|
|
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)
|
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: Unicorn]]>("independentCodableArrayDictionaryKey", default: [["0": fixtureCodable]], suite: suite_)
|
|
|
|
#expect(Defaults[key][0]["0"]?.isUnicorn ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
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))
|
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: Unicorn]>("independentCodableDictionaryKey", default: ["0": fixtureCodable], suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"]?.isUnicorn ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
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))
|
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: Unicorn]?>("independentCodableDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
|
|
|
|
#expect(Defaults[key] == nil)
|
2021-05-16 20:21:17 +08:00
|
|
|
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))
|
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: [Unicorn]]>("independentCodableDictionaryArrayKey", default: ["0": [fixtureCodable]], suite: suite_)
|
|
|
|
#expect(Defaults[key]["0"]?[0].isUnicorn ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
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))
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2023-10-04 18:58:57 +08:00
|
|
|
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_)
|
2023-10-04 18:58:57 +08:00
|
|
|
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)""#)
|
2023-10-04 18:58:57 +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[.codable].isUnicorn)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.codable] = Unicorn(isUnicorn: false)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(!Defaults[.codable].isUnicorn)
|
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[.codableArray][0].isUnicorn)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.codableArray][0] = Unicorn(isUnicorn: false)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(!Defaults[.codableArray][0].isUnicorn)
|
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[.codableDictionary]["0"]?.isUnicorn ?? false)
|
2021-05-16 20:21:17 +08:00
|
|
|
Defaults[.codableDictionary]["0"] = Unicorn(isUnicorn: false)
|
2024-09-26 05:17:26 +08:00
|
|
|
#expect(!(Defaults[.codableDictionary]["0"]?.isUnicorn ?? true))
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-09-11 11:49:06 +08:00
|
|
|
func testCodableAndNSSecureCoding() {
|
|
|
|
let fixture = UnicornCodableAndNSSecureCoding()
|
2021-10-12 15:03:34 +08:00
|
|
|
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)
|
2021-10-12 15:03:34 +08:00
|
|
|
}
|
|
|
|
|
2024-09-26 05:17:26 +08:00
|
|
|
@Test
|
2021-10-12 15:03:34 +08:00
|
|
|
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)
|
2021-05-16 20:21:17 +08:00
|
|
|
}
|
|
|
|
}
|