widgettest

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package widgettest provides test harness utilities for FluffyUI widgets.

Index

Constants

View Source
const DefaultTimeout = 100 * time.Millisecond

DefaultTimeout is the default timeout for wait operations.

Variables

View Source
var ErrTimeout = errors.New("timeout waiting for condition")

ErrTimeout is returned when a wait operation times out.

View Source
var UpdateSnapshots = false

UpdateSnapshots controls whether Snapshot overwrites golden files instead of comparing against them. Set to true (e.g. via TestMain or an init function that reads a flag/env var) to regenerate snapshots.

Functions

func AssertBindable

func AssertBindable(t testing.TB, w runtime.Widget)

AssertBindable checks that a widget implements runtime.Bindable.

func AssertFocusable

func AssertFocusable(t testing.TB, w runtime.Widget)

AssertFocusable checks that a widget implements runtime.Focusable and reports CanFocus() == true.

func AssertLabel

func AssertLabel(t testing.TB, w runtime.Widget, expected string)

AssertLabel checks that a widget's accessibility label matches expected. The widget must implement accessibility.Accessible.

func AssertMeasure

func AssertMeasure(t testing.TB, w runtime.Widget, minWidth, minHeight int)

AssertMeasure checks that a widget measures to at least the given minimum width and height. The widget is measured with unconstrained limits.

func AssertRenders

func AssertRenders(t testing.TB, w runtime.Widget, width, height int)

AssertRenders checks that a widget renders without panicking. It performs a full measure-layout-render cycle at the given dimensions.

func AssertRole

func AssertRole(t testing.TB, w runtime.Widget, expected string)

AssertRole checks that a widget has the expected ARIA role. The widget must implement accessibility.Accessible.

func AssertState

func AssertState(t testing.TB, w runtime.Widget, check func(accessibility.StateSet) bool, desc string)

AssertState checks that a specific accessibility state condition holds. The check function receives the widget's StateSet and should return true if the condition is met. The desc parameter describes the expected state for the error message (e.g. "Disabled == true").

func AssertValue

func AssertValue(t testing.TB, w runtime.Widget, expected string)

AssertValue checks that a widget's accessibility value text matches expected. The widget must implement accessibility.Accessible and have a non-nil value.

func Snapshot

func Snapshot(t testing.TB, name string, widget runtime.Widget, width, height int)

Snapshot captures the rendered output of a widget and compares it against a golden file. On first run (or when UpdateSnapshots is true), creates the golden file. On subsequent runs, compares against the golden file and fails the test if the output differs.

Golden files are stored in testdata/snapshots/<name>.snap relative to the caller's test directory. The name should be a simple identifier without path separators (e.g. "label_hello", "button_disabled").

func SnapshotString

func SnapshotString(widget runtime.Widget, width, height int) string

SnapshotString captures the rendered output of a widget and returns it as a string without comparing against a golden file. Useful for debugging.

The widget goes through a full measure-layout-render cycle at the given dimensions. The buffer is then serialized cell-by-cell, row-by-row.

Types

type Harness

type Harness struct {
	App     *runtime.App
	Backend *sim.Backend
	// contains filtered or unexported fields
}

Harness runs a widget inside a live app with a simulation backend. It provides deterministic synchronization for testing widget behavior.

func New

func New(t *testing.T, root runtime.Widget, width, height int) *Harness

New starts a background app with the provided root widget.

func (*Harness) AssertContains

func (h *Harness) AssertContains(text string)

AssertContains fails the test if screen doesn't contain the text.

func (*Harness) AssertNotContains

func (h *Harness) AssertNotContains(text string)

AssertNotContains fails the test if screen contains the text.

func (*Harness) Capture

func (h *Harness) Capture() string

Capture returns the full screen buffer as a string.

func (*Harness) CaptureRegion

func (h *Harness) CaptureRegion(x, y, width, height int) string

CaptureRegion returns a region of the screen buffer.

func (*Harness) Close

func (h *Harness) Close()

Close stops the app and releases backend resources.

func (*Harness) ContainsText

func (h *Harness) ContainsText(text string) bool

ContainsText checks if the screen contains the given text.

func (*Harness) DrainRenders

func (h *Harness) DrainRenders()

DrainRenders clears any pending render notifications.

func (*Harness) InjectKey

func (h *Harness) InjectKey(key terminal.Key, r rune)

InjectKey injects a key event into the backend.

func (*Harness) InjectKeyAndWait

func (h *Harness) InjectKeyAndWait(key terminal.Key, r rune, timeout time.Duration) bool

InjectKeyAndWait injects a key and waits for the resulting render.

func (*Harness) InjectKeyString

func (h *Harness) InjectKeyString(str string)

InjectKeyString injects a string as key events.

func (*Harness) InjectMouse

func (h *Harness) InjectMouse(x, y int, button terminal.MouseButton)

InjectMouse injects a mouse event into the backend.

func (*Harness) InjectMouseAndWait

func (h *Harness) InjectMouseAndWait(x, y int, button terminal.MouseButton, timeout time.Duration) bool

InjectMouseAndWait injects a mouse event and waits for render.

func (*Harness) InvalidateAndWait

func (h *Harness) InvalidateAndWait(timeout time.Duration) bool

InvalidateAndWait triggers an invalidation and waits for render.

func (*Harness) Resize

func (h *Harness) Resize(width, height int)

Resize updates the backend size and emits a resize event.

func (*Harness) ResizeAndWait

func (h *Harness) ResizeAndWait(width, height int, timeout time.Duration) bool

ResizeAndWait resizes and waits for the resulting render.

func (*Harness) SendKey

func (h *Harness) SendKey(key terminal.Key, r rune) bool

SendKey posts a key message through the app and waits for render.

func (*Harness) SendKeyWithTimeout

func (h *Harness) SendKeyWithTimeout(key terminal.Key, r rune, timeout time.Duration) bool

SendKeyWithTimeout posts a key message and waits with custom timeout.

func (*Harness) TypeAndWait

func (h *Harness) TypeAndWait(text string, timeout time.Duration) bool

TypeAndWait types a string and waits for renders after each character.

func (*Harness) Wait

func (h *Harness) Wait(d time.Duration)

Wait is deprecated. Use WaitForRender or WaitForCondition instead. Kept for backwards compatibility.

func (*Harness) WaitForCondition

func (h *Harness) WaitForCondition(fn func() bool, timeout time.Duration) error

WaitForCondition waits for a condition function to return true.

func (*Harness) WaitForFocus

func (h *Harness) WaitForFocus(widget runtime.Widget, timeout time.Duration) error

WaitForFocus waits for a widget to receive focus.

func (*Harness) WaitForRender

func (h *Harness) WaitForRender(timeout time.Duration) bool

WaitForRender waits for a render to complete or times out.

func (*Harness) WaitForRenders

func (h *Harness) WaitForRenders(n int, timeout time.Duration) bool

WaitForRenders waits for at least n renders to complete.

func (*Harness) WaitForText

func (h *Harness) WaitForText(widget runtime.Widget, text string, timeout time.Duration) error

WaitForText waits for a widget to contain specific text.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL