Replace the UserPreference property wrapper with the new macro version.

This commit is contained in:
Doug
2026-06-26 13:46:14 +01:00
committed by Doug
parent c53e0c1466
commit f05fd41c36
17 changed files with 143 additions and 388 deletions
+20 -50
View File
@@ -6,8 +6,10 @@
// Please see LICENSE files in the repository root for full details.
//
import Combine
@testable import ElementX
import Foundation
import Macros
import Testing
struct UserPreferenceTests {
@@ -131,58 +133,29 @@ struct UserPreferenceTests {
#expect(value.codable == nil)
#expect(testDefaults.data(forKey: TestsKey.key3.rawValue) == nil)
}
@Test
func localOverRemoteValue() {
let storage = VolatileUserDefaults()
let preference = UserPreference(key: TestsKey.testKey, defaultValue: "", storage: storage)
#expect(preference.wrappedValue == "")
preference.remoteValue = "remote"
#expect(preference.wrappedValue == "remote")
preference.wrappedValue = "local"
#expect(preference.wrappedValue == "local")
}
@Test
func remoteOverLocalValue() {
let storage = VolatileUserDefaults()
let preference = UserPreference(key: TestsKey.testKey, defaultValue: "", storage: storage, mode: .remoteOverLocal)
#expect(preference.wrappedValue == "")
preference.remoteValue = "remote"
#expect(preference.wrappedValue == "remote")
preference.wrappedValue = "local"
#expect(preference.wrappedValue == "remote")
#expect(preference.isLockedToRemote)
}
}
private nonisolated struct TestPreferences {
@UserPreference
private final class TestPreferences {
let store: UserDefaultsProtocol
init(_ store: UserDefaultsProtocol) {
self.store = store
}
@UserPreference(key: TestsKey.key1.rawValue, volatile: true)
var volatileVar: String?
@UserPreference
@UserPreference(key: TestsKey.key2.rawValue)
var plist: String?
@UserPreference
@UserPreference(key: TestsKey.key3.rawValue)
var codable: CodableTestType?
@UserPreference
@UserPreference(key: TestsKey.key4.rawValue, volatile: true)
var volatileCodable: CodableTestType?
@UserPreference
@UserPreference(key: TestsKey.key5.rawValue)
var plistArray: [Int]?
init(_ storage: UserDefaultsProtocol) {
_volatileVar = UserPreference(key: TestsKey.key1, storage: VolatileUserDefaults())
_plist = UserPreference(key: TestsKey.key2, storage: storage)
_codable = UserPreference(key: TestsKey.key3, storage: storage)
_volatileCodable = UserPreference(key: TestsKey.key4, storage: VolatileUserDefaults())
_plistArray = UserPreference(key: TestsKey.key5, storage: storage)
}
}
private struct CodableTestType: Equatable, Codable {
@@ -190,13 +163,10 @@ private struct CodableTestType: Equatable, Codable {
let b: [Int]
}
private nonisolated struct TestsKey: PreferenceKeyable, RawRepresentable {
static let testKey = Self(rawValue: "testKey")
static let key1 = Self(rawValue: "foo.volatile")
static let key2 = Self(rawValue: "foo.plist")
static let key3 = Self(rawValue: "foo.codable")
static let key4 = Self(rawValue: "foo.volatile.codable")
static let key5 = Self(rawValue: "foo.plist.array")
let rawValue: String
private enum TestsKey: String {
case key1 = "foo.volatile"
case key2 = "foo.plist"
case key3 = "foo.codable"
case key4 = "foo.volatile.codable"
case key5 = "foo.plist.array"
}