129 lines
4.1 KiB
Swift
129 lines
4.1 KiB
Swift
//
|
|
// PreferenceStoreTests.swift
|
|
// TuskerPreferencesTests
|
|
//
|
|
// Created by Shadowfacts on 4/12/24.
|
|
//
|
|
|
|
import XCTest
|
|
@testable import TuskerPreferences
|
|
import Combine
|
|
|
|
final class PreferenceStoreTests: XCTestCase {
|
|
|
|
struct TestKey: PreferenceKey {
|
|
static let defaultValue = false
|
|
}
|
|
|
|
final class TestStore<Key: PreferenceKey>: Codable, ObservableObject {
|
|
private var _test = Preference<Key>()
|
|
|
|
// the acutal subscript expects the enclosingInstance to be a PreferenceStore, so do it manually
|
|
var test: Key.Value {
|
|
get {
|
|
Preference.get(enclosingInstance: self, storage: \._test)
|
|
}
|
|
set {
|
|
Preference.set(enclosingInstance: self, storage: \._test, newValue: newValue)
|
|
}
|
|
}
|
|
|
|
var testPublisher: some Publisher<Key.Value, Never> {
|
|
_test.projectedValue
|
|
}
|
|
|
|
init() {
|
|
}
|
|
|
|
init(from decoder: any Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
self._test = try container.decode(Preference<Key>.self, forKey: .test)
|
|
}
|
|
|
|
enum CodingKeys: CodingKey {
|
|
case test
|
|
}
|
|
|
|
func encode(to encoder: any Encoder) throws {
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
try container.encode(self._test, forKey: .test)
|
|
}
|
|
}
|
|
|
|
func testDecoding() throws {
|
|
let decoder = JSONDecoder()
|
|
let present = try decoder.decode(PreferenceCoding<TestStore<TestKey>>.self, from: Data("""
|
|
{"test": true}
|
|
""".utf8)).wrapped
|
|
XCTAssertEqual(present.test, true)
|
|
let absent = try decoder.decode(PreferenceCoding<TestStore<TestKey>>.self, from: Data("""
|
|
{}
|
|
""".utf8)).wrapped
|
|
XCTAssertEqual(absent.test, false)
|
|
}
|
|
|
|
func testEncoding() throws {
|
|
let store = TestStore<TestKey>()
|
|
let encoder = JSONEncoder()
|
|
XCTAssertEqual(String(data: try encoder.encode(PreferenceCoding(wrapped: store)), encoding: .utf8)!, """
|
|
{}
|
|
""")
|
|
store.test = true
|
|
XCTAssertEqual(String(data: try encoder.encode(PreferenceCoding(wrapped: store)), encoding: .utf8)!, """
|
|
{"test":true}
|
|
""")
|
|
store.test = false
|
|
XCTAssertEqual(String(data: try encoder.encode(PreferenceCoding(wrapped: store)), encoding: .utf8)!, """
|
|
{"test":false}
|
|
""")
|
|
}
|
|
|
|
func testPublisher() {
|
|
let topLevel = expectation(description: "top level publisher")
|
|
let specificPref = expectation(description: "preference publisher")
|
|
// initial and on change
|
|
specificPref.expectedFulfillmentCount = 2
|
|
let store = TestStore<TestKey>()
|
|
var cancellables = Set<AnyCancellable>()
|
|
store.objectWillChange.sink {
|
|
topLevel.fulfill()
|
|
// fires on will change
|
|
XCTAssertEqual(store.test, false)
|
|
}.store(in: &cancellables)
|
|
store.testPublisher.sink { _ in
|
|
specificPref.fulfill()
|
|
}.store(in: &cancellables)
|
|
store.test = true
|
|
wait(for: [topLevel, specificPref])
|
|
}
|
|
|
|
func testCustomCodable() throws {
|
|
struct Key: CustomCodablePreferenceKey {
|
|
static let defaultValue = 1
|
|
static func encode(value: Int, to encoder: any Encoder) throws {
|
|
var container = encoder.singleValueContainer()
|
|
try container.encode(2)
|
|
}
|
|
static func decode(from decoder: any Decoder) throws -> Int? {
|
|
3
|
|
}
|
|
}
|
|
let store = TestStore<Key>()
|
|
store.test = 123
|
|
let encoder = JSONEncoder()
|
|
XCTAssertEqual(String(data: try encoder.encode(PreferenceCoding(wrapped: store)), encoding: .utf8)!, """
|
|
{"test":2}
|
|
""")
|
|
let decoder = JSONDecoder()
|
|
let present = try decoder.decode(PreferenceCoding<TestStore<Key>>.self, from: Data("""
|
|
{"test":2}
|
|
""".utf8)).wrapped
|
|
XCTAssertEqual(present.test, 3)
|
|
let absent = try decoder.decode(PreferenceCoding<TestStore<Key>>.self, from: Data("""
|
|
{}
|
|
""".utf8)).wrapped
|
|
XCTAssertEqual(absent.test, 1)
|
|
}
|
|
|
|
}
|