Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Driver ¶
type Driver interface { // Close stops the model at the end. Close(t TB) // ApplyTextCommand applies the given textual command to the model. // It may return an extra tea.Cmd to process by the test. ApplyTextCommand(t TB, cmd string, args ...string) tea.Cmd // Observe observes the given component of the model. // Supported values: // - view: call View() // - gostruct: print with %#v // - debug: call Debug() Observe(t TB, what string) string // RunOneTest runs one step of a test file. // // The following directives are supported: // // - run: apply some state changes and view the result. // // Supported directive options: // - trace: produce a log of the intermediate test steps. // - observe: what to observe after the state changes. // // Supported values for observe: // - view: the result of calling View(). // - gostruct: the result of printing the model with %#v. // - debug: the result of calling the Debug() method (it needs to be defined) // - msgs/cmds: print the residual tea.Cmd / tea.Msg input. // // Supported input commands under "run": // - type: enter some runes as tea.Key // - key: enter a special key or combination as a tea.Key RunOneTest(t TB, d *datadriven.TestData) string }
Driver is the externally-visible interface for a test driver.
type KeyMapApplier ¶
KeyMapApplier is the type of a function which applies the changeKeyMap callback on a KeyMap struct inside the model, then returns the resulting model.
Example implementation:
func(m tea.Model, changeKeyMap func(interface{}) error) (tea.Model, err) { myModel := m.(mymodel) if err := changeKeyMap(&myModel.KeyMap); err != nil { return m, err } return myModel, nil }
func SimpleKeyMapApplier ¶
func SimpleKeyMapApplier(keymap interface{}) KeyMapApplier
SimpleKeyMapApplier is a helper to simplify the definition of the function argument to KeyMapUpdater, in the case the model is implemented by reference -- i.e. the address of the KeyMap does not change from one call to Update to the next.
type Observer ¶
Observer is an optional function added to RunModel(), which can extract information from the model to serve as expected output in tests.
type Option ¶
type Option func(*driver)
Option is the type of an option which can be specified with RunModel or NewDriver.
func WithAutoInitDisabled ¶
func WithAutoInitDisabled() Option
WithAutoInitDisabled tells the test driver to not automatically initialize the model (via the Init method) upon first use.
func WithObserver ¶
WithObserver tells the test driver to support an additional observer with the given function.
For example, after WithObserver("hello", myObserver) The function myObserver() will be called every time a test specifies `observe=hello` in the run directive.
func WithUpdater ¶
WithUpdater adds the specified model updater to the test. It is possible to use multiple WithUpdater options, which will chain them automatically (using ChainUpdaters).
func WithWindowSize ¶
WithWindowSize tells the test driver to issue a tea.WindowSizeMsg as the first event after initialization.
type StylesApplier ¶
StylesApplier is the type of a function which applies the changeStyles callback on a struct inside the model, then returns the resulting model.
Example implementation:
func(m tea.Model, changeStyles func(interface{}) error) (tea.Model, err) { myModel := m.(mymodel) if err := changeKeyMap(&myModel); err != nil { return m, err } return myModel, nil }
func SimpleStylesApplier ¶
func SimpleStylesApplier(styledStruct interface{}) StylesApplier
SimpleSyylesApplier is a helper to simplify the definition of the function argument to StylesUpdater, in the case the model is implemented by reference -- i.e. the address of the styles does not change from one call to Update to the next.
type TB ¶
type TB interface { Fatal(...interface{}) Fatalf(string, ...interface{}) Logf(string, ...interface{}) }
TB is a shim interface for testing.T / testing.B.
type Updater ¶
type Updater func(m tea.Model, testCmd string, args ...string) (supported bool, newModel tea.Model, teaCmd tea.Cmd, err error)
Updater is an optional function added to RunModel(), which can apply state change commands as input to a test.
It should return false in the first return value to indicate that the command is not supported.
It can return an error e.g. to indicate that the command is supported but its arguments use invalid syntax, or that the model is in an invalid state.
func ChainUpdaters ¶
ChainUpdaters chains the specified updaters into a resulting updater that supports all the commands in the chain. Test input commands are passed to each updater in turn until the first updater that supports it.
For example: - upd1 supports command "print" - upd2 supports command "get" - ChainUpdaters(upd1, upd2) will support both commands "print" and "get.
func KeyMapUpdater ¶
func KeyMapUpdater(prefix string, apply KeyMapApplier) Updater
KeyMapUpdater defines an updater which supports the "keybind" and "keyhelp" commands to change a KeyMap struct. You can add this to a test using WithUpdater(). It is possible to add multiple keymap updaters to the same test.
A KeyMap struct is any go struct containing exported fields of type key.Binding. For example, using:
KeyMapUpdater("mymodel", func(m tea.Model, changeKeyMap func(interface{})) (tea.Model, err) { myModel := m.(mymodel) if err := changeKeyMap(&myModel.KeyMap); err != nil { return m, err } return myModel, nil })
and mymodel.KeyMap containing a CursorUp binding, it becomes possible to use "keybind mymodel.CursorUp ctrl+c" to define a new keybinding during a test.
If your model implements tea.Model by reference (i.e. its address does not change through Update calls), you can simplify the call as follows:
KeyMapUpdater("...", SimpleKeyMapApplier(&yourmodel.KeyMap)).
func StylesUpdater ¶
func StylesUpdater(prefix string, apply StylesApplier) Updater
StylesUpdater defines an updater which supports the "restyle" command to change a struct containing lipgloss.Style fields. You can add this to a test using WithUpdater(). It is possible to add multiple styles updaters to the same test.
For example, using:
StylesUpdater("mymodel", func(m tea.Model, changeStyles func(interface{})) (tea.Model, err) { myModel := m.(mymodel) if err := changeStyles(&myModel); err != nil { return m, err } return myModel, nil })
and mymodel containing a CursorStyle field, it becomes possible to use "restyle mymodel.CursorStyle foreground: 11" to define a new style during a test.
If your model implements tea.Model by reference (i.e. its address does not change through Update calls), you can simplify the call as follows:
StylesUpdater("...", SimpleStylesApplier(&yourmodel)).