Files
TUIkit/Tests/TUIkitTests/PreferenceKeyTests.swift
T
phranck 82544e0e02 Refactor: Add Swift 6 @MainActor to View hierarchy
Add @MainActor to View, ViewModifier, Renderable, App, Scene protocols and all builders (ViewBuilder, SceneBuilder, ChildInfoProvider). Update renderToBuffer(), makeChildInfo(), resolveChildInfos() for main-actor isolation. Mark generic Equatable conformances with @preconcurrency. Update all 45 test suites with @MainActor annotation.
2026-02-07 02:50:41 +01:00

62 lines
1.7 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 🖥 TUIKit Terminal UI Kit for Swift
// PreferenceKeyTests.swift
//
// Created by LAYERED.work
// License: MIT
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())
}
}
@MainActor
@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"])
}
}