TermUI.Test.TestRenderer (TermUI v0.2.0)
View SourceTest 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
@type t() :: %TermUI.Test.TestRenderer{ buffer: TermUI.Renderer.Buffer.t(), cols: pos_integer(), rows: pos_integer() }
Functions
@spec clear(t()) :: :ok
Clears the entire buffer.
@spec destroy(t()) :: :ok
Destroys the test renderer and frees resources.
@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.
@spec dimensions(t()) :: {pos_integer(), pos_integer()}
Gets buffer dimensions.
@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}]
@spec get_cell(t(), pos_integer(), pos_integer()) :: TermUI.Renderer.Cell.t()
Gets the cell at the given position.
@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! "
@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])}
@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"
@spec in_bounds?(t(), pos_integer(), pos_integer()) :: boolean()
Checks if position is within buffer bounds.
Checks if current buffer matches a snapshot.
Examples
snapshot = TestRenderer.snapshot(renderer)
# ... modify renderer ...
TestRenderer.matches_snapshot?(renderer, snapshot)
# => false
@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)
@spec set_cell(t(), pos_integer(), pos_integer(), TermUI.Renderer.Cell.t()) :: :ok | {:error, :out_of_bounds}
Sets the cell at the given position.
@spec set_cells(t(), [{pos_integer(), pos_integer(), TermUI.Renderer.Cell.t()}]) :: :ok
Sets multiple cells at once.
Creates a snapshot of the current buffer state.
Snapshots can be compared for equality or saved for regression testing.
Examples
snapshot = TestRenderer.snapshot(renderer)
Converts snapshot to a printable string representation.
Useful for test failure output.
@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
@spec text_contains?(t(), pos_integer(), pos_integer(), pos_integer(), String.t()) :: boolean()
Checks if text contains expected substring at a position.
Converts current buffer to a printable string.
@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.