programtest

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package programtest drives a program without opening a terminal.

Host implements only program.Host's transport boundary: ordered events in, frames out, and an opening cell size. Optional terminal capabilities are deliberately not present. A test that needs one can embed *Host in its own type and implement exactly that capability, so absence and partial support remain testable rather than being hidden by an all-powerful fake.

Frames are terminal escape streams, not a simulated screen. Shows and Hides request a full frame and inspect its appearance-free text runs: style and hyperlink changes are transparent, while cursor movement and erasure remain boundaries. Tests that need terminal cell state use package ptytest instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.13.0

type Config struct {
	Width  int
	Height int
}

Config is the complete initial state of a test Host. Width and Height must be positive terminal-cell dimensions.

type Host

type Host struct {
	// contains filtered or unexported fields
}

Host is an in-memory program host for tests.

Every method is safe to call from the test goroutine while program.Run owns the program goroutine. New arranges cleanup; Close is also available when a test wants to settle the frame writer earlier. The zero Host is inert and refuses events. A Host must not be copied after first use: its event stream, frame writer and captured output are one test session.

func New

func New(tb testing.TB, config Config) *Host

New returns a host of the configured positive cell size.

t owns the host. Its cleanup closes the asynchronous frame writer after the test context has been cancelled, so a failed test cannot leave a goroutine behind.

func (*Host) Close

func (h *Host) Close() error

Close stops the frame writer. It is idempotent.

func (*Host) Err

func (h *Host) Err() error

Err reports a clean input end. Tests normally stop a program through their context or the program's own quit action.

func (*Host) Events

func (h *Host) Events() <-chan input.Event

Events is the ordered input channel.

func (*Host) Frame

func (h *Host) Frame() string

Frame returns the most recent frame write.

Ordinary frames are diffs. Call Repaint before an assertion that needs the whole screen, or use Shows and Hides, which do that automatically.

func (*Host) Frames

func (h *Host) Frames() string

Frames returns every byte written so far as a string.

func (*Host) Hides

func (h *Host) Hides(tb testing.TB, text string)

Hides waits until no visible text run in a full repaint contains text. It uses the same appearance-free projection as Host.Shows.

func (*Host) Input

func (h *Host) Input() program.EventSource

Input returns the host's clean-ending input stream.

func (*Host) Press

func (h *Host) Press(code input.Code) bool

Press sends a non-character key.

func (*Host) Repaint

func (h *Host) Repaint() bool

Repaint asks the program for a full frame at its current size.

func (*Host) Resize

func (h *Host) Resize(width, height int) bool

Resize changes the reported size and sends the corresponding event. Dimensions outside program.ValidateSize are rejected.

func (*Host) Send

func (h *Host) Send(event input.Event) bool

Send queues an event for the program and reports whether the host was open. It does not wait for the program to receive earlier events.

func (*Host) Shows

func (h *Host) Shows(tb testing.TB, text string)

Shows waits until one visible text run in a full repaint contains text. Appearance changes do not split a run; terminal geometry operations do. This is a content assertion rather than a raw escape-stream assertion — use Frame or Frames when exact transport bytes are the subject of the test.

func (*Host) Size

func (h *Host) Size() (width, height int, err error)

Size returns the opening size passed to New.

func (*Host) Type

func (h *Host) Type(text string) bool

Type sends text one character key at a time.

func (*Host) Until

func (h *Host) Until(tb testing.TB, what string, cond func() bool)

Until waits for output activity until cond becomes true, failing the test with the last frame after a bounded wait.

cond must inspect concurrency-safe state: Host output methods are safe, as is application-owned state protected by its own synchronization. The wait is driven by frame writes rather than a polling sleep.

func (*Host) Writer

func (h *Host) Writer() program.FrameWriter

Writer is where the program queues terminal frames.

Jump to

Keyboard shortcuts

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