TermUI.Test.TestRenderer (TermUI v0.2.0)

View Source

Test renderer that captures output to a buffer for inspection.

The test renderer implements a screen buffer interface without actual terminal output. Tests can inspect rendered content, styles, and positions.

Usage

{:ok, renderer} = TestRenderer.new(24, 80)
TestRenderer.set_cell(renderer, 1, 1, Cell.new("X", fg: :red))

# Inspect rendered content
text = TestRenderer.get_text_at(renderer, 1, 1, 5)
style = TestRenderer.get_style_at(renderer, 1, 1)

# Snapshot comparison
snapshot = TestRenderer.snapshot(renderer)
assert TestRenderer.matches_snapshot?(renderer, snapshot)

Buffer Coordinates

Rows and columns are 1-indexed to match terminal conventions.

Summary

Functions

Clears the entire buffer.

Destroys the test renderer and frees resources.

Compares current buffer with snapshot and returns differences.

Gets buffer dimensions.

Searches for text in the entire buffer.

Gets the cell at the given position.

Gets an entire row as text.

Gets the style at a position.

Gets text at a position with specified width.

Checks if position is within buffer bounds.

Checks if current buffer matches a snapshot.

Creates a new test renderer with given dimensions.

Sets the cell at the given position.

Sets multiple cells at once.

Creates a snapshot of the current buffer state.

Converts snapshot to a printable string representation.

Checks if text appears at a position.

Checks if text contains expected substring at a position.

Converts current buffer to a printable string.

Writes a string starting at the given position.

Types

t()

@type t() :: %TermUI.Test.TestRenderer{
  buffer: TermUI.Renderer.Buffer.t(),
  cols: pos_integer(),
  rows: pos_integer()
}

Functions

clear(test_renderer)

@spec clear(t()) :: :ok

Clears the entire buffer.

destroy(test_renderer)

@spec destroy(t()) :: :ok

Destroys the test renderer and frees resources.

diff_snapshot(renderer, snapshot)

@spec diff_snapshot(t(), map()) :: [{pos_integer(), pos_integer(), map(), map()}]

Compares current buffer with snapshot and returns differences.

Returns list of {row, col, expected, actual} tuples for differing cells.

dimensions(test_renderer)

@spec dimensions(t()) :: {pos_integer(), pos_integer()}

Gets buffer dimensions.

find_text(renderer, text)

@spec find_text(t(), String.t()) :: [{pos_integer(), pos_integer()}]

Searches for text in the entire buffer.

Returns list of {row, col} positions where text was found.

Examples

positions = TestRenderer.find_text(renderer, "Error")
# => [{5, 10}, {12, 3}]

get_cell(test_renderer, row, col)

@spec get_cell(t(), pos_integer(), pos_integer()) :: TermUI.Renderer.Cell.t()

Gets the cell at the given position.

get_row_text(renderer, row)

@spec get_row_text(t(), pos_integer()) :: String.t()

Gets an entire row as text.

Examples

row_text = TestRenderer.get_row_text(renderer, 1)
# => "Hello, World!                                              "

get_style_at(test_renderer, row, col)

@spec get_style_at(t(), pos_integer(), pos_integer()) :: map()

Gets the style at a position.

Returns a map with fg, bg, and attrs.

Examples

style = TestRenderer.get_style_at(renderer, 1, 1)
# => %{fg: :red, bg: :default, attrs: MapSet.new([:bold])}

get_text_at(test_renderer, row, col, width)

@spec get_text_at(t(), pos_integer(), pos_integer(), pos_integer()) :: String.t()

Gets text at a position with specified width.

Returns the characters in cells from (row, col) to (row, col + width - 1).

Examples

text = TestRenderer.get_text_at(renderer, 1, 1, 5)
# => "Hello"

in_bounds?(test_renderer, row, col)

@spec in_bounds?(t(), pos_integer(), pos_integer()) :: boolean()

Checks if position is within buffer bounds.

matches_snapshot?(renderer, snapshot)

@spec matches_snapshot?(t(), map()) :: boolean()

Checks if current buffer matches a snapshot.

Examples

snapshot = TestRenderer.snapshot(renderer)
# ... modify renderer ...
TestRenderer.matches_snapshot?(renderer, snapshot)
# => false

new(rows, cols)

@spec new(pos_integer(), pos_integer()) :: {:ok, t()} | {:error, term()}

Creates a new test renderer with given dimensions.

Examples

{:ok, renderer} = TestRenderer.new(24, 80)

set_cell(test_renderer, row, col, cell)

@spec set_cell(t(), pos_integer(), pos_integer(), TermUI.Renderer.Cell.t()) ::
  :ok | {:error, :out_of_bounds}

Sets the cell at the given position.

set_cells(test_renderer, cells)

@spec set_cells(t(), [{pos_integer(), pos_integer(), TermUI.Renderer.Cell.t()}]) ::
  :ok

Sets multiple cells at once.

snapshot(test_renderer)

@spec snapshot(t()) :: map()

Creates a snapshot of the current buffer state.

Snapshots can be compared for equality or saved for regression testing.

Examples

snapshot = TestRenderer.snapshot(renderer)

snapshot_to_string(snapshot)

@spec snapshot_to_string(map()) :: String.t()

Converts snapshot to a printable string representation.

Useful for test failure output.

text_at?(renderer, row, col, expected)

@spec text_at?(t(), pos_integer(), pos_integer(), String.t()) :: boolean()

Checks if text appears at a position.

Examples

TestRenderer.text_at?(renderer, 1, 1, "Hello")
# => true

text_contains?(renderer, row, col, width, expected)

@spec text_contains?(t(), pos_integer(), pos_integer(), pos_integer(), String.t()) ::
  boolean()

Checks if text contains expected substring at a position.

to_string(renderer)

@spec to_string(t()) :: String.t()

Converts current buffer to a printable string.

write_string(test_renderer, row, col, string, opts \\ [])

@spec write_string(t(), pos_integer(), pos_integer(), String.t(), keyword()) ::
  non_neg_integer()

Writes a string starting at the given position.

Returns the number of columns written.