commandtest

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package commandtest provides a headless implementation of command.Env.

It exists so that editor commands can be tested with no terminal, no screen and no event loop: a test constructs a Fake with some buffer text, runs a command against it, and asserts on the resulting text, point, kill ring and recorded prompts. That is only possible because command.Env is a narrow interface that cannot reach the screen or the layout tree.

The buffer, window and kill ring inside a Fake are the real types, not stubs, so editing and kill-run accumulation behave exactly as they will in the editor. Only the things a terminal would supply — prompt answers, the window height, the file system — are canned.

Index

Constants

View Source
const Quit = "\x00commandtest.quit"

Quit placed in Replies makes that prompt return command.ErrQuit, which is how a test exercises a command's C-g path.

View Source
const QuitChar = rune(0)

QuitChar placed in Chars makes that ReadChar return command.ErrQuit.

Variables

View Source
var (
	// ErrNoReply reports that a command prompted more times than the test
	// supplied answers for. It is a bug in the test, not in the command, so it
	// is deliberately distinct from command.ErrQuit.
	ErrNoReply = errors.New("commandtest: prompt with no canned reply left")

	// ErrLastBuffer reports an attempt to kill the only live buffer.
	ErrLastBuffer = errors.New("commandtest: cannot kill the last buffer")
)

Functions

This section is empty.

Types

type Fake

type Fake struct {

	// ArgN and ArgExplicit are what Arg reports. New sets ArgN to 1.
	ArgN        int
	ArgExplicit bool

	// Height is what TextHeight reports. New sets it to 24.
	Height int

	// Replies are consumed in order by ReadString. The Quit sentinel makes a
	// prompt report command.ErrQuit.
	Replies []string

	// Chars are consumed in order by ReadChar. QuitChar reports ErrQuit.
	Chars []rune

	// Keys are consumed in order by ReadKey.
	Keys []keymap.Key

	// Files seeds OpenFile: opening a path returns a buffer holding
	// Files[path], or an empty buffer when the path is absent, as find-file
	// does for a new file.
	Files map[string]string

	// BindingMap is what Bindings reports and what Where searches.
	BindingMap map[string]string

	// Reg backs Run and CommandNames. New creates an empty registry.
	Reg *command.Registry

	// Echoes holds every formatted message passed to Echo.
	Echoes []string

	// Reads holds the full ReadOpts of every ReadString call, in order, so a
	// test can assert on completion candidates and on Initial pre-fill rather
	// than only on the prompt text. Without this every command test that cares
	// about completion has to embed the Fake and override ReadString.
	Reads []command.ReadOpts

	// Prompts, CharPrompts and KeyPrompts hold just the prompt strings each
	// read method was called with, in order. Prompts is redundant with Reads
	// but kept because len(f.Prompts) reads better than len(f.Reads) in the
	// many tests that only count prompts.
	Prompts     []string
	CharPrompts []string
	KeyPrompts  []string

	// Saves records every SaveBuffer call. Nothing reaches the real file
	// system: a save updates Files so that a later OpenFile of the same path
	// sees the saved content, which keeps the fake self-consistent.
	Saves []Save

	// SaveErr, when non-nil, is returned by the next SaveBuffer call and then
	// cleared, so a test can fail exactly one save.
	SaveErr error

	// Splits records the vertical flag of each SplitWindow call.
	Splits []bool

	// OtherWindowArgs records the argument of each OtherWindow call.
	OtherWindowArgs []int

	// DeleteWindowCalls and DeleteOtherWindowsCalls count those calls.
	DeleteWindowCalls       int
	DeleteOtherWindowsCalls int

	// QuitArgs records the force flag of each Quit call.
	QuitArgs []bool

	// RunNames records every command name passed to Run.
	RunNames []string

	// SplitErr, DeleteWindowErr, QuitErr and OpenFileErr, when non-nil, are
	// returned by those methods so a test can exercise failure paths.
	SplitErr        error
	DeleteWindowErr error
	QuitErr         error
	OpenFileErr     error
	// contains filtered or unexported fields
}

Fake is an in-memory command.Env.

Fields fall into two groups: knobs a test sets before running a command, and recorders a test reads afterwards.

func New

func New(lines ...string) *Fake

New returns a Fake whose active buffer holds the given lines, with point at the start and no mark set.

func (*Fake) AddBuffer

func (f *Fake) AddBuffer(name string, lines ...string) *text.Buffer

AddBuffer registers an extra live buffer under a display name.

func (*Fake) Arg

func (f *Fake) Arg() (int, bool)

func (*Fake) Bindings

func (f *Fake) Bindings() map[string]string

func (*Fake) Buf

func (f *Fake) Buf() *text.Buffer

func (*Fake) BufferByName

func (f *Fake) BufferByName(name string) (*text.Buffer, bool)

func (*Fake) BufferName

func (f *Fake) BufferName(b *text.Buffer) string

func (*Fake) Buffers

func (f *Fake) Buffers() []*text.Buffer

func (*Fake) CommandNames

func (f *Fake) CommandNames() []string

func (*Fake) DeleteOtherWindows

func (f *Fake) DeleteOtherWindows()

func (*Fake) DeleteWindow

func (f *Fake) DeleteWindow() error

func (*Fake) Echo

func (f *Fake) Echo(format string, a ...any)

func (*Fake) KillBackward

func (f *Fake) KillBackward(s string)

func (*Fake) KillBuffer

func (f *Fake) KillBuffer(b *text.Buffer) error

func (*Fake) KillForward

func (f *Fake) KillForward(s string)

func (*Fake) LastCommand

func (f *Fake) LastCommand() string

func (*Fake) NewBuffer

func (f *Fake) NewBuffer(name string) *text.Buffer

func (*Fake) OpenFile

func (f *Fake) OpenFile(path string) (*text.Buffer, error)

OpenFile returns the buffer already visiting path, or creates one from the seeded Files content. Nothing touches the real file system.

func (*Fake) OtherWindow

func (f *Fake) OtherWindow(n int)

func (*Fake) Point

func (f *Fake) Point() text.Pos

Point returns the active window's point.

func (*Fake) Quit

func (f *Fake) Quit(force bool) error

func (*Fake) ReadChar

func (f *Fake) ReadChar(prompt string, valid []rune) (rune, error)

ReadChar records the prompt and returns the next canned rune, rejecting one that is not in valid so a test cannot accidentally assert on an answer the real prompt would have refused.

func (*Fake) ReadKey

func (f *Fake) ReadKey(prompt string) (keymap.Key, error)

func (*Fake) ReadString

func (f *Fake) ReadString(opts command.ReadOpts) (string, error)

ReadString records the prompt and returns the next canned reply.

The change hooks are called once per successive prefix of the reply — "f", "fo", "foo" — rather than once with the final value, so an incremental-search command is exercised the way real typing would drive it.

func (*Fake) Ring

func (f *Fake) Ring() *command.KillRing

Ring exposes the kill ring so a test can assert on it directly.

func (*Fake) Run

func (f *Fake) Run(name string) error

func (*Fake) SaveBuffer

func (f *Fake) SaveBuffer(b *text.Buffer, path string) error

SaveBuffer records the save and emulates its effect without touching the real file system: the buffer adopts a non-empty path, is marked unmodified, and Files gains its content so a later OpenFile of that path agrees.

func (*Fake) Seq

func (f *Fake) Seq() *command.Seq

Seq returns the fake's sequencing state. The pointer is stable across calls and across dispatches, which is the whole point: a fake handing out a fresh Seq each time would make every yank-pop test pass for the wrong reason.

func (*Fake) SetLastCommand

func (f *Fake) SetLastCommand(name string)

SetLastCommand sets what LastCommand reports, for testing commands whose behaviour depends on repetition such as yank-pop and recenter-top-bottom.

func (*Fake) SetPoint

func (f *Fake) SetPoint(p text.Pos)

SetPoint moves point, clamped into the buffer.

func (*Fake) SplitWindow

func (f *Fake) SplitWindow(vertical bool) error

func (*Fake) Text

func (f *Fake) Text() string

Text returns the active buffer's entire contents.

func (*Fake) TextHeight

func (f *Fake) TextHeight() int

func (*Fake) Where

func (f *Fake) Where(command string) []string

func (*Fake) Win

func (f *Fake) Win() *view.Window

func (*Fake) Yank

func (f *Fake) Yank() (string, error)

func (*Fake) YankPop

func (f *Fake) YankPop() (string, error)

type Save

type Save struct {
	Buf     *text.Buffer
	Path    string // as passed to SaveBuffer; empty means the buffer's own path
	Content string // the buffer's text at the moment of the save
}

Save records one call to SaveBuffer: which buffer, the path requested, and the content that would have reached disk.

Jump to

Keyboard shortcuts

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