Documentation
¶
Overview ¶
Package vtermtest provides snapshot testing for interactive TUIs/REPLs using a real PTY and libvterm.
Index ¶
- type Emulator
- func (e *Emulator) AssertLineEqual(t TestingT, row int, want string)
- func (e *Emulator) AssertScreenContains(t TestingT, substr string)
- func (e *Emulator) AssertScreenEqual(t TestingT, want string)
- func (e *Emulator) Close() error
- func (e *Emulator) Command(path string, args ...string) *Emulator
- func (e *Emulator) Dir(dir string) *Emulator
- func (e *Emulator) EnableRawBytesCollection() *Emulator
- func (e *Emulator) Env(env ...string) *Emulator
- func (e *Emulator) GetCursorPosition() (row, col int, err error)
- func (e *Emulator) GetLine(row int) (string, error)
- func (e *Emulator) GetRawBytes() []byte
- func (e *Emulator) GetScreenText() (string, error)
- func (e *Emulator) KeyPress(keys ...[]byte) error
- func (e *Emulator) KeyPressString(dsl string) error
- func (e *Emulator) KeyPressStringWithOptions(dsl string, opts keys.ParseOptions) error
- func (e *Emulator) Resize(rows, cols uint16) error
- func (e *Emulator) Start(ctx context.Context) error
- func (e *Emulator) WaitFor(text string, timeout time.Duration) error
- func (e *Emulator) WaitStable(quiet, timeout time.Duration) bool
- func (e *Emulator) WithAssertBackoffFactor(f float64) *Emulator
- func (e *Emulator) WithAssertInitialDelay(d time.Duration) *Emulator
- func (e *Emulator) WithAssertMaxAttempts(n int) *Emulator
- type TestingT
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Emulator ¶
type Emulator struct {
// contains filtered or unexported fields
}
Emulator represents a terminal emulator for testing interactive programs. It creates a PTY, launches a process, and uses libvterm to emulate terminal behavior.
func New ¶
New creates a new Emulator with the specified terminal dimensions. rows and cols specify the terminal size in characters.
func (*Emulator) AssertLineEqual ¶
AssertLineEqual asserts that a specific line equals the expected string. It retries with exponential backoff until the assertion passes or max attempts is reached.
func (*Emulator) AssertScreenContains ¶
AssertScreenContains asserts that the screen contains the given substring.
func (*Emulator) AssertScreenEqual ¶
AssertScreenEqual asserts that the entire screen matches the expected string. Leading/trailing whitespace in want is trimmed, and empty lines at the start are ignored.
func (*Emulator) Close ¶
Close terminates the process and cleans up resources. It closes the PTY, kills the process if still running, and waits for cleanup.
func (*Emulator) Dir ¶
Dir sets the working directory for the command. Returns self for method chaining.
func (*Emulator) EnableRawBytesCollection ¶
EnableRawBytesCollection enables collection of raw bytes from PTY. When enabled, all bytes read from PTY are stored and can be retrieved with GetRawBytes().
func (*Emulator) Env ¶
Env adds environment variables. Multiple calls append variables. Format: "KEY=value". Returns self for method chaining.
func (*Emulator) GetCursorPosition ¶
GetCursorPosition returns the current cursor position from libvterm's internal state. Returns the 1-based row and column position.
func (*Emulator) GetLine ¶
GetLine returns a specific line from the terminal screen. Row index starts at 0. Trailing spaces are trimmed.
func (*Emulator) GetRawBytes ¶
GetRawBytes returns the raw bytes collected from PTY. Raw bytes collection must be enabled with EnableRawBytesCollection(). Returns a copy of the collected bytes.
func (*Emulator) GetScreenText ¶
GetScreenText returns the entire terminal screen as a string. Lines are trimmed of trailing spaces and joined with newlines.
func (*Emulator) KeyPress ¶
KeyPress sends keystrokes to the terminal. Use the keys package for special keys (e.g., keys.Tab, keys.Enter).
func (*Emulator) KeyPressString ¶
KeyPressString sends keystrokes using DSL notation. Example: "hello<Tab>world<C-c>" sends "hello", Tab key, "world", then Ctrl-C. Special DSL: <WaitStable> waits for screen to stabilize. See keys.Parse for supported notation.
func (*Emulator) KeyPressStringWithOptions ¶
func (e *Emulator) KeyPressStringWithOptions(dsl string, opts keys.ParseOptions) error
KeyPressStringWithOptions sends keystrokes using DSL notation with custom tag delimiters. Example with options {TagStart: '[', TagEnd: ']'}: "hello[Tab]world[C-c]"
func (*Emulator) Resize ¶
Resize changes the terminal size dynamically. Both PTY and libvterm are resized to match the new dimensions.
func (*Emulator) Start ¶
Start launches the command in a PTY and begins terminal emulation. The context can be used to control the lifetime of the process.
func (*Emulator) WaitFor ¶
WaitFor waits until the specified text appears on the screen. Returns error if text doesn't appear within timeout. timeout: maximum time to wait for the text to appear
func (*Emulator) WaitStable ¶
WaitStable waits until the screen output is stable (no changes for 'quiet' duration). Returns true if stable within timeout, false if timeout exceeded. quiet: duration of inactivity to consider stable timeout: maximum time to wait
func (*Emulator) WithAssertBackoffFactor ¶
WithAssertBackoffFactor sets the backoff multiplier for retry delays
func (*Emulator) WithAssertInitialDelay ¶
WithAssertInitialDelay sets the initial delay between retry attempts
func (*Emulator) WithAssertMaxAttempts ¶
WithAssertMaxAttempts sets the maximum number of retry attempts for assertions