Documentation
¶
Overview ¶
Package snapshot provides snapshot assertions for tests.
Snapshots are stored under testdata. Set UPDATE_SNAPSHOTS=true to create or update snapshots.
snapshot.AssertEqual(t, model.View())
snapshot.RequireEqual(t, model.View())
snapshot.RequireEqual(t, model.View(), snapshot.WithSuffix("empty-state"))
Index ¶
- func AsScreenBuffer(tb testing.TB, v string, opts ...Option) *uv.ScreenBuffer
- func AssertEqual[T ~[]byte | ~string](tb testing.TB, got T, opts ...Option) bool
- func AssertJSONEqual(tb testing.TB, v string, opts ...Option) bool
- func AssertScreenEqual(tb testing.TB, screen *ScreenSnapshot, opts ...Option) bool
- func RequireEqual[T ~[]byte | ~string](tb testing.TB, got T, opts ...Option)
- func RequireJSONEqual(tb testing.TB, v string, opts ...Option)
- func RequireScreenEqual(tb testing.TB, screen *ScreenSnapshot, opts ...Option)
- func WriteBytes(tb testing.TB, got []byte, path string, opts ...Option)
- func WriteJSON(tb testing.TB, v, path string, opts ...Option)
- func WriteScreen(tb testing.TB, screen *ScreenSnapshot, path string, opts ...Option)
- type Cell
- type Color
- type Cursor
- type Link
- type Option
- func WithANSI(enable bool) Option
- func WithDir(dir string) Option
- func WithEscapeESC(enable bool) Option
- func WithIndent(indent int) Option
- func WithPrivate(enable bool) Option
- func WithSpinners(enable bool) Option
- func WithSuffix(name string) Option
- func WithTransform(fn func([]byte) []byte) Option
- func WithUpdate(update bool) Option
- type Position
- type ScreenSnapshot
- type Style
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsScreenBuffer ¶
AsScreenBuffer converts a string into a uv.ScreenBuffer.
func AssertEqual ¶
AssertEqual compares "got" against this test's generated snapshot file, reporting errors without stopping the test immediately. It returns whether the snapshot matched.
func AssertJSONEqual ¶
AssertJSONEqual compares the provided string against the JSON representation of a ScreenSnapshot, reporting errors without stopping the test immediately (in most cases).
func AssertScreenEqual ¶
func AssertScreenEqual(tb testing.TB, screen *ScreenSnapshot, opts ...Option) bool
AssertScreenEqual compares the provided screen snapshot against the stored JSON representation of that snapshot.
func RequireEqual ¶
RequireEqual compares "got" against this test's generated snapshot file.
func RequireJSONEqual ¶
RequireJSONEqual compares the provided string against the JSON representation of a ScreenSnapshot, failing the test immediately if the snapshot does not match.
func RequireScreenEqual ¶
func RequireScreenEqual(tb testing.TB, screen *ScreenSnapshot, opts ...Option)
func WriteBytes ¶
WriteBytes writes the given bytes to the given path, processing it in the same way as AssertEqual and RequireEqual. If path is empty, it will be generated using the test name and associated options.
It will fail the test if there are i/o errors.
func WriteJSON ¶
WriteJSON writes the JSON representation of the provided string to a file, processing it in the same way as AssertJSONEqual and RequireJSONEqual. If path is empty, it will be generated using the test name and associated options.
It will fail the test if there are i/o errors.
func WriteScreen ¶
func WriteScreen(tb testing.TB, screen *ScreenSnapshot, path string, opts ...Option)
WriteScreen writes the given screen snapshot to the given path, processing it in the same way as AssertScreenEqual and RequireScreenEqual. If path is empty, it will be generated using the test name and associated options.
It will fail the test if there are i/o errors.
Types ¶
type Cell ¶
type Cell struct {
// Content is the [Cell]'s content, which consists of a single grapheme
// cluster. Most of the time, this will be a single rune as well, but it
// can also be a combination of runes that form a grapheme cluster.
Content string `json:"content,omitempty" yaml:"content,omitempty"`
// The style of the cell. Nil style means no style. Zero value prints a
// reset sequence.
Style Style `json:"style,omitzero" yaml:"style,omitzero"`
// Link is the hyperlink of the cell.
Link Link `json:"link,omitzero" yaml:"link,omitzero"`
// Width is the mono-spaced width of the grapheme cluster.
Width int `json:"width,omitzero" yaml:"width,omitzero"`
}
Cell represents a single cell in the terminal screen.
type Color ¶
Color represents a terminal color, which can be one of the following:
- An ANSI 16 color (0-15) of type ansi.BasicColor.
- An ANSI 256 color (0-255) of type ansi.IndexedColor.
- Or any other 24-bit color that implements color.Color.
func (Color) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface for Color.
func (*Color) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface for Color.
type Cursor ¶
type Cursor struct {
Position Position `json:"position" yaml:"position"`
Visible bool `json:"visible" yaml:"visible"`
Color Color `json:"color,omitzero" yaml:"color,omitzero"`
Style vt.CursorStyle `json:"style" yaml:"style"`
Blink bool `json:"blink" yaml:"blink"`
}
Cursor represents the cursor state.
type Link ¶
type Link struct {
URL string `json:"url,omitempty" yaml:"url,omitempty"`
Params string `json:"params,omitempty" yaml:"params,omitempty"`
}
Link represents a hyperlink in the terminal screen.
type Option ¶
type Option func(*options)
Option configures snapshot normalization before comparison.
func WithANSI ¶
WithANSI determines whether ANSI sequences should be kept. When false, ANSI sequences are stripped, which will often make it easier to visually compare snapshots, but does mean stripping of color and similar information/styling.
func WithDir ¶
WithDir sets the directory to store snapshots in. Defaults to "testdata" (in the current working directory). Can be relative or absolute.
func WithEscapeESC ¶
WithEscapeESC toggles the escape of ESC bytes with "\x1b". When true (the default), ESC bytes are escaped with "\x1b".
func WithIndent ¶
WithIndent sets the indent level for the snapshot, depending on the snapshot format (e.g. JSON).
func WithPrivate ¶
WithPrivate determines whether private use grapheme clusters should be kept, or replaced with "?". When false, they will be stripped.
func WithSpinners ¶
WithSpinners determines whether typical spinner glyphs should be replaced with "?". When false, they will be stripped.
func WithSuffix ¶
WithSuffix appends name as a suffix to the generated test snapshot name.
func WithTransform ¶
WithTransform adds a transform to apply before comparison.
func WithUpdate ¶
WithUpdate sets whether snapshots should be updated. Defaults to the UPDATE_SNAPSHOTS environment variable (and UPDATE_SNAPS for compatibility with those migrating from github.com/gkampitakis/go-snaps).
type ScreenSnapshot ¶
type ScreenSnapshot struct {
// TODO: how to handle modes? not JSON marshalable atm.
// Modes ansi.Modes `json:"modes,omitempty" yaml:"modes,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Rows int `json:"rows" yaml:"rows"`
Cols int `json:"cols" yaml:"cols"`
AltScreen bool `json:"alt_screen,omitempty" yaml:"alt_screen,omitempty"`
Focused bool `json:"focused,omitempty" yaml:"focused,omitempty"`
Cursor Cursor `json:"cursor,omitempty" yaml:"cursor"`
BgColor Color `json:"bg_color,omitzero" yaml:"bg_color,omitzero"`
FgColor Color `json:"fg_color,omitzero" yaml:"fg_color,omitzero"`
Cells [][]*Cell `json:"cells" yaml:"cells"`
}
ScreenSnapshot represents a snapshot of the terminal state at a given moment.
func (*ScreenSnapshot) Compare ¶
func (ss *ScreenSnapshot) Compare(tb testing.TB, other *ScreenSnapshot, _ ...Option) bool
Compare compares the current snapshot with the given snapshot, using JSON marshaling and reflect.DeepEqual.
func (*ScreenSnapshot) WithScreenBuffer ¶
func (ss *ScreenSnapshot) WithScreenBuffer(tb testing.TB, screen *uv.ScreenBuffer, _ ...Option) *ScreenSnapshot
WithScreenBuffer updates the ScreenSnapshot with the contents of the given screen buffer (Rows, Cols, Cells).
type Style ¶
type Style struct {
Fg Color `json:"fg,omitzero" yaml:"fg,omitzero"`
Bg Color `json:"bg,omitzero" yaml:"bg,omitzero"`
UnderlineColor Color `json:"underline_color,omitzero" yaml:"underline_color,omitzero"`
Underline uv.Underline `json:"underline,omitempty" yaml:"underline,omitempty"`
Attrs byte `json:"attrs,omitempty" yaml:"attrs,omitempty"`
}
Style represents the Style of a cell.