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
- Variables
- type Fake
- func (f *Fake) AddBuffer(name string, lines ...string) *text.Buffer
- func (f *Fake) Arg() (int, bool)
- func (f *Fake) Bindings() map[string]string
- func (f *Fake) Buf() *text.Buffer
- func (f *Fake) BufferByName(name string) (*text.Buffer, bool)
- func (f *Fake) BufferName(b *text.Buffer) string
- func (f *Fake) Buffers() []*text.Buffer
- func (f *Fake) CommandNames() []string
- func (f *Fake) DeleteOtherWindows()
- func (f *Fake) DeleteWindow() error
- func (f *Fake) Echo(format string, a ...any)
- func (f *Fake) KillBackward(s string)
- func (f *Fake) KillBuffer(b *text.Buffer) error
- func (f *Fake) KillForward(s string)
- func (f *Fake) LastCommand() string
- func (f *Fake) NewBuffer(name string) *text.Buffer
- func (f *Fake) OpenFile(path string) (*text.Buffer, error)
- func (f *Fake) OtherWindow(n int)
- func (f *Fake) Point() text.Pos
- func (f *Fake) Quit(force bool) error
- func (f *Fake) ReadChar(prompt string, valid []rune) (rune, error)
- func (f *Fake) ReadKey(prompt string) (keymap.Key, error)
- func (f *Fake) ReadString(opts command.ReadOpts) (string, error)
- func (f *Fake) Ring() *command.KillRing
- func (f *Fake) Run(name string) error
- func (f *Fake) SaveBuffer(b *text.Buffer, path string) error
- func (f *Fake) Seq() *command.Seq
- func (f *Fake) SetLastCommand(name string)
- func (f *Fake) SetPoint(p text.Pos)
- func (f *Fake) SplitWindow(vertical bool) error
- func (f *Fake) Text() string
- func (f *Fake) TextHeight() int
- func (f *Fake) Where(command string) []string
- func (f *Fake) Win() *view.Window
- func (f *Fake) WrapWidth() int
- func (f *Fake) Yank() (string, error)
- func (f *Fake) YankPop() (string, error)
- type Save
Constants ¶
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.
const QuitChar = rune(0)
QuitChar placed in Chars makes that ReadChar return command.ErrQuit.
Variables ¶
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
// Wrap is what WrapWidth reports: 0, lines not wrapped, unless a test
// wraps them.
Wrap 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 ¶
New returns a Fake whose active buffer holds the given lines, with point at the start and no mark set.
func (*Fake) CommandNames ¶
func (*Fake) DeleteOtherWindows ¶
func (f *Fake) DeleteOtherWindows()
func (*Fake) DeleteWindow ¶
func (*Fake) KillBackward ¶
func (*Fake) KillForward ¶
func (*Fake) LastCommand ¶
func (*Fake) OpenFile ¶
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 (*Fake) ReadChar ¶
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) ReadString ¶
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) SaveBuffer ¶
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 ¶
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 ¶
SetLastCommand sets what LastCommand reports, for testing commands whose behaviour depends on repetition such as yank-pop and recenter-top-bottom.
func (*Fake) SplitWindow ¶
func (*Fake) TextHeight ¶
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.