Defaults/Tests/DefaultsTests/DefaultsAnySeriliazableTest...

456 lines
15 KiB
Swift
Raw Normal View History

2024-09-26 05:17:26 +08:00
import SwiftUI
import Testing
2021-07-12 20:10:43 +08:00
import Defaults
2024-09-26 05:17:26 +08:00
private let suite_ = createSuite()
2021-07-12 20:10:43 +08:00
private enum mime: String, Defaults.Serializable {
case JSON = "application/json"
case STREAM = "application/octet-stream"
}
private struct CodableUnicorn: Defaults.Serializable, Codable {
let is_missing: Bool
}
private struct Unicorn: Defaults.Serializable, Hashable {
static let bridge = UnicornBridge()
let is_missing: Bool
}
private struct UnicornBridge: Defaults.Bridge {
typealias Value = Unicorn
typealias Serializable = Bool
func serialize(_ value: Value?) -> Serializable? {
value?.is_missing
}
func deserialize(_ object: Serializable?) -> Value? {
Value(is_missing: object!)
}
}
extension Defaults.Keys {
2024-09-26 05:17:26 +08:00
fileprivate static let magic = Key<[String: Defaults.AnySerializable]>("magic", default: [:], suite: suite_)
fileprivate static let anyKey = Key<Defaults.AnySerializable>("anyKey", default: "🦄", suite: suite_)
fileprivate static let anyArrayKey = Key<[Defaults.AnySerializable]>("anyArrayKey", default: ["No.1 🦄", "No.2 🦄"], suite: suite_)
fileprivate static let anyDictionaryKey = Key<[String: Defaults.AnySerializable]>("anyDictionaryKey", default: ["unicorn": "🦄"], suite: suite_)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Suite(.serialized)
final class DefaultsAnySerializableTests {
init() {
Defaults.removeAll(suite: suite_)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
deinit {
Defaults.removeAll(suite: suite_)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testReadMeExample() {
2024-09-26 05:17:26 +08:00
let any = Defaults.Key<Defaults.AnySerializable>("anyKey", default: Defaults.AnySerializable(mime.JSON), suite: suite_)
2021-07-12 20:10:43 +08:00
if let mimeType: mime = Defaults[any].get() {
2024-09-26 05:17:26 +08:00
#expect(mimeType == mime.JSON)
2021-07-12 20:10:43 +08:00
}
Defaults[any].set(mime.STREAM)
if let mimeType: mime = Defaults[any].get() {
2024-09-26 05:17:26 +08:00
#expect(mimeType == mime.STREAM)
2021-07-12 20:10:43 +08:00
}
Defaults[any].set(mime.JSON)
if let mimeType: mime = Defaults[any].get() {
2024-09-26 05:17:26 +08:00
#expect(mimeType == mime.JSON)
2021-07-12 20:10:43 +08:00
}
Defaults[.magic]["unicorn"] = "🦄"
Defaults[.magic]["number"] = 3
Defaults[.magic]["boolean"] = true
Defaults[.magic]["enum"] = Defaults.AnySerializable(mime.JSON)
2024-09-26 05:17:26 +08:00
#expect(Defaults[.magic]["unicorn"] == "🦄")
#expect(Defaults[.magic]["number"] == 3)
2021-07-12 20:10:43 +08:00
if let bool: Bool = Defaults[.magic]["unicorn"]?.get() {
2024-09-26 05:17:26 +08:00
#expect(bool)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
#expect(Defaults[.magic]["enum"]?.get() == mime.JSON)
2021-07-12 20:10:43 +08:00
Defaults[.magic]["enum"]?.set(mime.STREAM)
if let value: String = Defaults[.magic]["unicorn"]?.get() {
2024-09-26 05:17:26 +08:00
#expect(value == "🦄")
2021-07-12 20:10:43 +08:00
}
if let mimeType: mime = Defaults[.magic]["enum"]?.get() {
2024-09-26 05:17:26 +08:00
#expect(mimeType == mime.STREAM)
2021-07-12 20:10:43 +08:00
}
Defaults[any].set(mime.JSON)
if let mimeType: mime = Defaults[any].get() {
2024-09-26 05:17:26 +08:00
#expect(mime.JSON == mimeType)
2021-07-12 20:10:43 +08:00
}
Defaults[any].set(mime.STREAM)
if let mimeType: mime = Defaults[any].get() {
2024-09-26 05:17:26 +08:00
#expect(mime.STREAM == mimeType)
2021-07-12 20:10:43 +08:00
}
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testKey() {
// Test Int
2024-09-26 05:17:26 +08:00
let any = Defaults.Key<Defaults.AnySerializable>("independentAnyKey", default: 121_314, suite: suite_)
#expect(Defaults[any] == 121_314)
2021-07-12 20:10:43 +08:00
// Test Int8
let int8 = Int8.max
Defaults[any].set(int8)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get() == int8)
2021-07-12 20:10:43 +08:00
// Test Int16
let int16 = Int16.max
Defaults[any].set(int16)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get() == int16)
2021-07-12 20:10:43 +08:00
// Test Int32
let int32 = Int32.max
Defaults[any].set(int32)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get() == int32)
2021-07-12 20:10:43 +08:00
// Test Int64
let int64 = Int64.max
Defaults[any].set(int64)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get() == int64)
2021-07-12 20:10:43 +08:00
// Test UInt
let uint = UInt.max
Defaults[any].set(uint)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get() == uint)
2021-07-12 20:10:43 +08:00
// Test UInt8
let uint8 = UInt8.max
Defaults[any].set(uint8)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get() == uint8)
2021-07-12 20:10:43 +08:00
// Test UInt16
let uint16 = UInt16.max
Defaults[any].set(uint16)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get() == uint16)
2021-07-12 20:10:43 +08:00
// Test UInt32
let uint32 = UInt32.max
Defaults[any].set(uint32)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get() == uint32)
2021-07-12 20:10:43 +08:00
// Test UInt64
let uint64 = UInt64.max
Defaults[any].set(uint64)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get() == uint64)
2021-07-12 20:10:43 +08:00
// Test Double
Defaults[any] = 12_131.4
2024-09-26 05:17:26 +08:00
#expect(Defaults[any] == 12_131.4)
2021-07-12 20:10:43 +08:00
// Test Bool
Defaults[any] = true
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get(Bool.self)!)
2021-07-12 20:10:43 +08:00
// Test String
Defaults[any] = "121314"
2024-09-26 05:17:26 +08:00
#expect(Defaults[any] == "121314")
2021-07-12 20:10:43 +08:00
// Test Float
Defaults[any].set(12_131.456, type: Float.self)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get(Float.self) == 12_131.456)
2021-07-12 20:10:43 +08:00
// Test Date
let date = Date()
Defaults[any].set(date)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get(Date.self) == date)
2021-07-12 20:10:43 +08:00
// Test Data
let data = "121314".data(using: .utf8)
Defaults[any].set(data)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get(Data.self) == data)
2021-07-12 20:10:43 +08:00
// Test Array
Defaults[any] = [1, 2, 3]
if let array: [Int] = Defaults[any].get() {
2024-09-26 05:17:26 +08:00
#expect(array[0] == 1)
#expect(array[1] == 2)
#expect(array[2] == 3)
2021-07-12 20:10:43 +08:00
}
// Test Dictionary
Defaults[any] = ["unicorn": "🦄", "boolean": true, "number": 3]
if let dictionary = Defaults[any].get([String: Defaults.AnySerializable].self) {
2024-09-26 05:17:26 +08:00
#expect(dictionary["unicorn"] == "🦄")
#expect(dictionary["boolean"]?.get(Bool.self) == true)
#expect(dictionary["number"] == 3)
2021-07-12 20:10:43 +08:00
}
// Test Set
Defaults[any].set(Set([1]))
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get(Set<Int>.self)?.first == 1)
2021-07-12 20:10:43 +08:00
// Test URL
Defaults[any].set(URL(string: "https://example.com")!)
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get()! == URL(string: "https://example.com")!)
2021-07-12 20:10:43 +08:00
#if os(macOS)
// Test NSColor
2024-01-18 17:58:06 +08:00
Defaults[any].set(NSColor(red: Double(103) / Double(0xFF), green: Double(132) / Double(0xFF), blue: Double(255) / Double(0xFF), alpha: 0.987))
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get(NSColor.self)?.alphaComponent == 0.987)
2021-07-12 20:10:43 +08:00
#else
// Test UIColor
2024-01-18 17:58:06 +08:00
Defaults[any].set(UIColor(red: Double(103) / Double(0xFF), green: Double(132) / Double(0xFF), blue: Double(255) / Double(0xFF), alpha: 0.654))
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get(UIColor.self)?.cgColor.alpha == 0.654)
2021-07-12 20:10:43 +08:00
#endif
// Test Codable type
Defaults[any].set(CodableUnicorn(is_missing: false))
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get(CodableUnicorn.self)!.is_missing == false)
2021-07-12 20:10:43 +08:00
// Test Custom type
Defaults[any].set(Unicorn(is_missing: true))
2024-09-26 05:17:26 +08:00
#expect(Defaults[any].get(Unicorn.self)!.is_missing)
2021-07-12 20:10:43 +08:00
// Test nil
Defaults[any] = nil
2024-09-26 05:17:26 +08:00
#expect(Defaults[any] == 121_314)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<Defaults.AnySerializable?>("independentOptionalAnyKey", suite: suite_)
#expect(Defaults[key] == nil)
2021-07-12 20:10:43 +08:00
Defaults[key] = 12_131.4
2024-09-26 05:17:26 +08:00
#expect(Defaults[key] == 12_131.4)
2021-07-12 20:10:43 +08:00
Defaults[key]?.set(mime.JSON)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?.get(mime.self) == mime.JSON)
2021-07-12 20:10:43 +08:00
Defaults[key] = nil
2024-09-26 05:17:26 +08:00
#expect(Defaults[key] == nil)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[Defaults.AnySerializable]>("independentArrayAnyKey", default: [123, 456], suite: suite_)
#expect(Defaults[key][0] == 123)
#expect(Defaults[key][1] == 456)
2021-07-12 20:10:43 +08:00
Defaults[key][0] = 12_131.4
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0] == 12_131.4)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testSetKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<Set<Defaults.AnySerializable>>("independentArrayAnyKey", default: [123], suite: suite_)
#expect(Defaults[key].first == 123)
2021-07-12 20:10:43 +08:00
Defaults[key].insert(12_131.4)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(12_131.4))
2021-07-12 20:10:43 +08:00
let date = Defaults.AnySerializable(Date())
Defaults[key].insert(date)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(date))
2021-07-12 20:10:43 +08:00
let data = Defaults.AnySerializable("Hello World!".data(using: .utf8))
Defaults[key].insert(data)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(data))
2021-07-12 20:10:43 +08:00
let int = Defaults.AnySerializable(Int.max)
Defaults[key].insert(int)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(int))
2021-07-12 20:10:43 +08:00
let int8 = Defaults.AnySerializable(Int8.max)
Defaults[key].insert(int8)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(int8))
2021-07-12 20:10:43 +08:00
let int16 = Defaults.AnySerializable(Int16.max)
Defaults[key].insert(int16)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(int16))
2021-07-12 20:10:43 +08:00
let int32 = Defaults.AnySerializable(Int32.max)
Defaults[key].insert(int32)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(int32))
2021-07-12 20:10:43 +08:00
let int64 = Defaults.AnySerializable(Int64.max)
Defaults[key].insert(int64)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(int64))
2021-07-12 20:10:43 +08:00
let uint = Defaults.AnySerializable(UInt.max)
Defaults[key].insert(uint)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(uint))
2021-07-12 20:10:43 +08:00
let uint8 = Defaults.AnySerializable(UInt8.max)
Defaults[key].insert(uint8)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(uint8))
2021-07-12 20:10:43 +08:00
let uint16 = Defaults.AnySerializable(UInt16.max)
Defaults[key].insert(uint16)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(uint16))
2021-07-12 20:10:43 +08:00
let uint32 = Defaults.AnySerializable(UInt32.max)
Defaults[key].insert(uint32)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(uint32))
2021-07-12 20:10:43 +08:00
let uint64 = Defaults.AnySerializable(UInt64.max)
Defaults[key].insert(uint64)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(uint64))
2021-07-12 20:10:43 +08:00
let bool: Defaults.AnySerializable = false
Defaults[key].insert(bool)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(bool))
2021-07-12 20:10:43 +08:00
let float = Defaults.AnySerializable(Float(1213.14))
Defaults[key].insert(float)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(float))
2021-07-12 20:10:43 +08:00
2024-01-18 17:58:06 +08:00
let cgFloat = Defaults.AnySerializable(CGFloat(12_131.415)) // swiftlint:disable:this no_cgfloat2
2021-07-12 20:10:43 +08:00
Defaults[key].insert(cgFloat)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(cgFloat))
2021-07-12 20:10:43 +08:00
let string = Defaults.AnySerializable("Hello World!")
Defaults[key].insert(string)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(string))
2021-07-12 20:10:43 +08:00
let array: Defaults.AnySerializable = [1, 2, 3, 4]
Defaults[key].insert(array)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(array))
2021-07-12 20:10:43 +08:00
let dictionary: Defaults.AnySerializable = ["Hello": "World!"]
Defaults[key].insert(dictionary)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(dictionary))
2021-07-12 20:10:43 +08:00
let unicorn = Defaults.AnySerializable(Unicorn(is_missing: true))
Defaults[key].insert(unicorn)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key].contains(unicorn))
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testArrayOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[Defaults.AnySerializable]?>("testArrayOptionalAnyKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
2021-07-12 20:10:43 +08:00
Defaults[key] = [123]
Defaults[key]?.append(456)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]![0] == 123)
#expect(Defaults[key]![1] == 456)
2021-07-12 20:10:43 +08:00
Defaults[key]![0] = 12_131.4
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]![0] == 12_131.4)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testNestedArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[[Defaults.AnySerializable]]>("testNestedArrayAnyKey", default: [[123]], suite: suite_)
2021-07-12 20:10:43 +08:00
Defaults[key][0].append(456)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][0][0] == 123)
#expect(Defaults[key][0][1] == 456)
2021-07-12 20:10:43 +08:00
Defaults[key].append([12_131.4])
2024-09-26 05:17:26 +08:00
#expect(Defaults[key][1][0] == 12_131.4)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testDictionaryKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: Defaults.AnySerializable]>("independentDictionaryAnyKey", default: ["unicorn": ""], suite: suite_)
2024-09-30 18:19:33 +08:00
#expect(Defaults[key]["unicorn"] == "") // swiftlint:disable:this empty_string
2021-07-12 20:10:43 +08:00
Defaults[key]["unicorn"] = "🦄"
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["unicorn"] == "🦄")
2021-07-12 20:10:43 +08:00
Defaults[key]["number"] = 3
Defaults[key]["boolean"] = true
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["number"] == 3)
2021-07-12 20:10:43 +08:00
if let bool: Bool = Defaults[.magic]["unicorn"]?.get() {
2024-09-26 05:17:26 +08:00
#expect(bool)
2021-07-12 20:10:43 +08:00
}
Defaults[key]["set"] = Defaults.AnySerializable(Set([1]))
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["set"]!.get(Set<Int>.self)!.first == 1)
2021-07-12 20:10:43 +08:00
Defaults[key]["nil"] = nil
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["nil"] == nil)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testDictionaryOptionalKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: Defaults.AnySerializable]?>("independentDictionaryOptionalAnyKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
2021-07-12 20:10:43 +08:00
Defaults[key] = ["unicorn": "🦄"]
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?["unicorn"] == "🦄")
2021-07-12 20:10:43 +08:00
Defaults[key]?["number"] = 3
Defaults[key]?["boolean"] = true
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]?["number"] == 3)
#expect(Defaults[key]?["boolean"] == true)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testDictionaryArrayKey() {
2024-09-26 05:17:26 +08:00
let key = Defaults.Key<[String: [Defaults.AnySerializable]]>("independentDictionaryArrayAnyKey", default: ["number": [1]], suite: suite_)
#expect(Defaults[key]["number"]?[0] == 1)
2021-07-12 20:10:43 +08:00
Defaults[key]["number"]?.append(2)
Defaults[key]["unicorn"] = ["No.1 🦄"]
Defaults[key]["unicorn"]?.append("No.2 🦄")
Defaults[key]["unicorn"]?.append("No.3 🦄")
Defaults[key]["boolean"] = [true]
Defaults[key]["boolean"]?.append(false)
2024-09-26 05:17:26 +08:00
#expect(Defaults[key]["number"]?[1] == 2)
#expect(Defaults[key]["unicorn"]?[0] == "No.1 🦄")
#expect(Defaults[key]["unicorn"]?[1] == "No.2 🦄")
#expect(Defaults[key]["unicorn"]?[2] == "No.3 🦄")
// #expect(#require(Defaults[key]["boolean"]?[0].get(Bool.self)) == true)
#expect(Defaults[key]["boolean"]?[1].get(Bool.self) == false)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.anyKey] == "🦄")
2021-07-12 20:10:43 +08:00
Defaults[.anyKey] = 123
2024-09-26 05:17:26 +08:00
#expect(Defaults[.anyKey] == 123)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testArrayType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.anyArrayKey][0] == "No.1 🦄")
#expect(Defaults[.anyArrayKey][1] == "No.2 🦄")
2021-07-12 20:10:43 +08:00
Defaults[.anyArrayKey].append(123)
2024-09-26 05:17:26 +08:00
#expect(Defaults[.anyArrayKey][2] == 123)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
@Test
2021-07-12 20:10:43 +08:00
func testDictionaryType() {
2024-09-26 05:17:26 +08:00
#expect(Defaults[.anyDictionaryKey]["unicorn"] == "🦄")
2021-07-12 20:10:43 +08:00
Defaults[.anyDictionaryKey]["number"] = 3
2024-09-26 05:17:26 +08:00
#expect(Defaults[.anyDictionaryKey]["number"] == 3)
2021-07-12 20:10:43 +08:00
Defaults[.anyDictionaryKey]["boolean"] = true
2024-09-26 05:17:26 +08:00
#expect(Defaults[.anyDictionaryKey]["boolean"]!.get(Bool.self)!)
2021-07-12 20:10:43 +08:00
Defaults[.anyDictionaryKey]["array"] = [1, 2]
if let array = Defaults[.anyDictionaryKey]["array"]?.get([Int].self) {
2024-09-26 05:17:26 +08:00
#expect(array[0] == 1)
#expect(array[1] == 2)
2021-07-12 20:10:43 +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<Defaults.AnySerializable>("observeAnyKeyCombine", default: 123, suite: suite_)
2021-07-12 20:10:43 +08:00
let publisher = Defaults
.publisher(key, options: [])
.map { ($0.oldValue, $0.newValue) }
.collect(2)
let expectedValue: [(Defaults.AnySerializable, Defaults.AnySerializable)] = [(123, "🦄"), ("🦄", 123)]
2024-09-26 05:17:26 +08:00
Task {
try? await Task.sleep(for: .seconds(0.1))
Defaults[key] = "🦄"
Defaults.reset(key)
}
for await tuples in publisher.values {
2021-07-12 20:10:43 +08:00
for (index, expected) in expectedValue.enumerated() {
2024-09-26 05:17:26 +08:00
#expect(expected.0 == tuples[index].0)
#expect(expected.1 == tuples[index].1)
2021-07-12 20:10:43 +08:00
}
2024-09-26 05:17:26 +08:00
break
2021-07-12 20:10:43 +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<Defaults.AnySerializable?>("observeAnyOptionalKeyCombine", suite: suite_)
2021-07-12 20:10:43 +08:00
let publisher = Defaults
.publisher(key, options: [])
.map { ($0.oldValue, $0.newValue) }
.collect(3)
let expectedValue: [(Defaults.AnySerializable?, Defaults.AnySerializable?)] = [(nil, 123), (123, "🦄"), ("🦄", nil)]
2024-09-26 05:17:26 +08:00
Task {
try? await Task.sleep(for: .seconds(0.1))
Defaults[key] = 123
Defaults[key] = "🦄"
Defaults.reset(key)
}
for await tuples in publisher.values {
2021-07-12 20:10:43 +08:00
for (index, expected) in expectedValue.enumerated() {
if tuples[index].0?.get(Int.self) != nil {
2024-09-26 05:17:26 +08:00
#expect(expected.0 == tuples[index].0)
#expect(expected.1 == tuples[index].1)
2021-07-12 20:10:43 +08:00
} else if tuples[index].0?.get(String.self) != nil {
2024-09-26 05:17:26 +08:00
#expect(expected.0 == tuples[index].0)
#expect(tuples[index].1 == nil)
2021-07-12 20:10:43 +08:00
} else {
2024-09-26 05:17:26 +08:00
#expect(tuples[index].0 == nil)
#expect(expected.1 == tuples[index].1)
2021-07-12 20:10:43 +08:00
}
}
2024-09-26 05:17:26 +08:00
break
2021-07-12 20:10:43 +08:00
}
}
2023-09-03 14:39:57 +08:00
2024-09-26 05:17:26 +08:00
@Test
func testWrongCast() {
let value = Defaults.AnySerializable(false)
2024-09-26 05:17:26 +08:00
#expect(value.get(Bool.self) == false)
#expect(value.get(String.self) == nil)
}
2021-07-12 20:10:43 +08:00
}