Files
TUIkit/Tests/TUIkitTests/PreferenceKeyTests.swift
T
phranck f880208a05 Test: Add 140 tests for Core systems (H.8 Phase 2)
- PredefinedPaletteTests: Green, Amber, White, Red, NCurses palettes (12)
- PaletteDefaultTests: Protocol default implementations (12)
- GeneratedPaletteTests: Hue-based palette generation (7)
- PaletteRegistryTests: Registry lookup and cycling order (6)
- ThemeManagerCyclingTests: cycleNext/Previous, setCurrent, typed accessors (12)
- KeyEventTests: Key enum, KeyEvent creation, parse() for all input types (38)
- KeyEventDispatcherTests: Handler registration, reverse dispatch, cleanup (6)
- AppStateTests: Render flag, observer notification (6)
- BindingTests: Get/set, projectedValue, constant (6)
- StatePropertyTests: Initial value, mutation, render trigger, binding (8)
- PreferenceKeyTests: Default and custom reduce implementations (3)
- PreferenceValuesTests: Subscript access, merge behavior (6)
- PreferenceStorageTests: Stack push/pop, setValue, callbacks, reset (12)
- NavigationTitleKeyTests: Built-in preference key (3)

Each suite in its own file (14 new files). One suite per file convention.
Total: 288 → 428 tests (+140 new)
2026-01-30 22:18:26 +01:00

62 lines
1.7 KiB
Swift

//
// PreferenceKeyTests.swift
// TUIkit
//
// Tests for PreferenceKey protocol: default reduce and custom reduce implementations.
//
import Testing
@testable import TUIkit
/// A simple string preference key for testing (default reduce = last value).
private struct TestStringKey: PreferenceKey {
static let defaultValue: String = "default"
}
/// A counter preference key with additive reduce.
private struct TestCounterKey: PreferenceKey {
static let defaultValue: Int = 0
static func reduce(value: inout Int, nextValue: () -> Int) {
value += nextValue()
}
}
/// An array preference key with append reduce.
private struct TestArrayKey: PreferenceKey {
static let defaultValue: [String] = []
static func reduce(value: inout [String], nextValue: () -> [String]) {
value.append(contentsOf: nextValue())
}
}
@Suite("PreferenceKey Tests")
struct PreferenceKeyTests {
@Test("Default reduce uses last value")
func defaultReduce() {
var value = TestStringKey.defaultValue
TestStringKey.reduce(value: &value) { "first" }
TestStringKey.reduce(value: &value) { "second" }
#expect(value == "second")
}
@Test("Custom additive reduce accumulates")
func customAdditiveReduce() {
var value = TestCounterKey.defaultValue
TestCounterKey.reduce(value: &value) { 5 }
TestCounterKey.reduce(value: &value) { 3 }
#expect(value == 8)
}
@Test("Custom array reduce appends")
func customArrayReduce() {
var value = TestArrayKey.defaultValue
TestArrayKey.reduce(value: &value) { ["a"] }
TestArrayKey.reduce(value: &value) { ["b", "c"] }
#expect(value == ["a", "b", "c"])
}
}