snapshot

package
v0.0.0-...-47e0271 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 19 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsScreenBuffer

func AsScreenBuffer(tb testing.TB, v string, opts ...Option) *uv.ScreenBuffer

AsScreenBuffer converts a string into a uv.ScreenBuffer.

func AssertEqual

func AssertEqual[T ~[]byte | ~string](tb testing.TB, got T, opts ...Option) bool

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

func AssertJSONEqual(tb testing.TB, v string, opts ...Option) bool

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

func RequireEqual[T ~[]byte | ~string](tb testing.TB, got T, opts ...Option)

RequireEqual compares "got" against this test's generated snapshot file.

func RequireJSONEqual

func RequireJSONEqual(tb testing.TB, v string, opts ...Option)

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

func WriteBytes(tb testing.TB, got []byte, path string, opts ...Option)

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

func WriteJSON(tb testing.TB, v, path string, opts ...Option)

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.

func (*Cell) FromUV

func (c *Cell) FromUV(cell *uv.Cell) *Cell

FromUV converts a uv.Cell into a Cell.

type Color

type Color struct {
	Color color.Color `json:"color,omitempty" yaml:"color,omitempty"`
}

Color represents a terminal color, which can be one of the following:

func (Color) MarshalText

func (c Color) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for Color.

func (*Color) UnmarshalText

func (c *Color) UnmarshalText(text []byte) error

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 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

func WithANSI(enable bool) Option

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

func WithDir(dir string) Option

WithDir sets the directory to store snapshots in. Defaults to "testdata" (in the current working directory). Can be relative or absolute.

func WithEscapeESC

func WithEscapeESC(enable bool) Option

WithEscapeESC toggles the escape of ESC bytes with "\x1b". When true (the default), ESC bytes are escaped with "\x1b".

func WithIndent

func WithIndent(indent int) Option

WithIndent sets the indent level for the snapshot, depending on the snapshot format (e.g. JSON).

func WithPrivate

func WithPrivate(enable bool) Option

WithPrivate determines whether private use grapheme clusters should be kept, or replaced with "?". When false, they will be stripped.

func WithSpinners

func WithSpinners(enable bool) Option

WithSpinners determines whether typical spinner glyphs should be replaced with "?". When false, they will be stripped.

func WithSuffix

func WithSuffix(name string) Option

WithSuffix appends name as a suffix to the generated test snapshot name.

func WithTransform

func WithTransform(fn func([]byte) []byte) Option

WithTransform adds a transform to apply before comparison.

func WithUpdate

func WithUpdate(update bool) Option

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 Position

type Position struct {
	X int `json:"x" yaml:"x"`
	Y int `json:"y" yaml:"y"`
}

Position represents a position in the terminal.

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.

Jump to

Keyboard shortcuts

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