Files
TUIkit/Tests/TUIkitTests/BorderModifierTests.swift
T
phranck 3fb4944472 Refactor: Move runtime services from RenderContext to EnvironmentValues
- Add ServiceEnvironment.swift with 9 EnvironmentKeys for runtime services
  (stateStorage, lifecycle, keyEventDispatcher, renderCache, preferenceStorage,
  pulsePhase, cursorTimer, focusIndicatorColor, activeFocusSectionID)
- Remove tuiContext, pulsePhase, cursorTimer, focusIndicatorColor, and
  activeFocusSectionID as direct RenderContext properties
- Inject all services through EnvironmentValues in RenderLoop.buildEnvironment()
- Add convenience RenderContext init that accepts TUIContext and auto-injects
  services into the environment
- Simplify isolatedForBackground() to only swap environment values
- Migrate ~49 access sites in ~25 source files from context.tuiContext.X and
  context.pulsePhase/cursorTimer to context.environment.X
- Update 38 test files to use the new convenience init
2026-02-14 13:13:24 +01:00

151 lines
4.8 KiB
Swift

// TUIKit - Terminal UI Kit for Swift
// BorderModifierTests.swift
//
// Created by LAYERED.work
// License: MIT
import Testing
@testable import TUIkit
// MARK: - Test Helpers
/// Creates a default render context for testing.
private func testContext(width: Int = 40, height: Int = 24) -> RenderContext {
RenderContext(availableWidth: width, availableHeight: height, tuiContext: TUIContext())
}
// MARK: - BorderModifier Tests
@MainActor
@Suite("BorderModifier Tests")
struct BorderModifierTests {
@Test(".border() renders with top and bottom borders")
func borderModifierRenders() {
let view = Text("Test").border(.line)
let context = testContext()
let buffer = renderToBuffer(view, context: context)
// Top border + content + bottom border
#expect(buffer.height == 3)
#expect(buffer.lines[0].contains(""))
#expect(buffer.lines[1].contains("Test"))
#expect(buffer.lines[2].contains(""))
}
@Test(".border() with empty content returns empty")
func borderModifierEmptyContent() {
let view = EmptyView().border(.line)
let context = testContext()
let buffer = renderToBuffer(view, context: context)
#expect(buffer.isEmpty)
}
@Test(".border() with line style uses correct corner characters")
func borderModifierLineStyle() {
let view = Text("X").border(.line)
let context = testContext()
let buffer = renderToBuffer(view, context: context)
let topLine = buffer.lines[0].stripped
let bottomLine = buffer.lines[buffer.height - 1].stripped
#expect(topLine.hasPrefix(""))
#expect(topLine.hasSuffix(""))
#expect(bottomLine.hasPrefix(""))
#expect(bottomLine.hasSuffix(""))
}
@Test(".border() with doubleLine style uses correct characters")
func borderModifierDoubleLineStyle() {
let view = Text("X").border(.doubleLine)
let context = testContext()
let buffer = renderToBuffer(view, context: context)
let topLine = buffer.lines[0].stripped
let bottomLine = buffer.lines[buffer.height - 1].stripped
#expect(topLine.hasPrefix(""))
#expect(topLine.hasSuffix(""))
#expect(bottomLine.hasPrefix(""))
#expect(bottomLine.hasSuffix(""))
}
@Test(".border() with rounded style uses correct characters")
func borderModifierRoundedStyle() {
let view = Text("X").border(.rounded)
let context = testContext()
let buffer = renderToBuffer(view, context: context)
let topLine = buffer.lines[0].stripped
let bottomLine = buffer.lines[buffer.height - 1].stripped
#expect(topLine.hasPrefix(""))
#expect(topLine.hasSuffix(""))
#expect(bottomLine.hasPrefix(""))
#expect(bottomLine.hasSuffix(""))
}
@Test(".border() with heavy style uses correct characters")
func borderModifierHeavyStyle() {
let view = Text("X").border(.heavy)
let context = testContext()
let buffer = renderToBuffer(view, context: context)
let topLine = buffer.lines[0].stripped
let bottomLine = buffer.lines[buffer.height - 1].stripped
#expect(topLine.hasPrefix(""))
#expect(topLine.hasSuffix(""))
#expect(bottomLine.hasPrefix(""))
#expect(bottomLine.hasSuffix(""))
}
@Test(".border() adds 4 to content width (2 border + 2 padding)")
func borderModifierWidthOverhead() {
let view = Text("ABCDE").border(.line)
let context = testContext()
let buffer = renderToBuffer(view, context: context)
// Content "ABCDE" = 5, + 2 for padding + 2 for borders = 9
let topLine = buffer.lines[0].stripped
#expect(topLine.count == 9)
}
@Test(".border() content has 1 char padding on each side")
func borderModifierContentPadding() {
let view = Text("Hi").border(.line)
let context = testContext()
let buffer = renderToBuffer(view, context: context)
// Content line should be: Hi (with spaces around "Hi")
let contentLine = buffer.lines[1].stripped
#expect(contentLine.hasPrefix(""))
#expect(contentLine.hasSuffix(""))
#expect(contentLine.contains(" Hi "))
}
}
// MARK: - BorderStyle Tests
@MainActor
@Suite("BorderStyle Tests")
struct BorderStyleTests {
@Test("Custom border style defaults T-junctions to vertical")
func customBorderStyleDefaultTJunctions() {
let custom = BorderStyle(
topLeft: "A",
topRight: "B",
bottomLeft: "C",
bottomRight: "D",
horizontal: "E",
vertical: "F"
)
#expect(custom.leftT == "F")
#expect(custom.rightT == "F")
}
}