Class: RubyTUI::TestBackend

Inherits:
Object
  • Object
show all
Includes:
Backend
Defined in:
lib/rubytui/backends/test_backend.rb

Overview

In-memory Backend for tests.

Records drawn cells in a grid and tracks cursor, alternate-screen, raw-mode and mouse state as plain values; nothing is written to a terminal. #width, #height, #cursor_visible, #alternate_screen, #raw_mode, #cursor_x, #cursor_y and #draw_history expose that state for assertions.

Examples:

backend = TestBackend.new(width: 20, height: 5)
terminal = Terminal.new(backend: backend)
terminal.draw { |frame| frame.buffer.set_string(0, 0, "Hi") }
backend.row_text(0)  # => "Hi"

Instance Attribute Summary collapse

Test Assertion Helpers collapse

Instance Method Summary collapse

Methods included from Backend

#cell_pixel_size, #clear_all_images!, #query_cell_pixel_size

Constructor Details

#initialize(width: 80, height: 24) ⇒ TestBackend

Returns a new instance of TestBackend.

Parameters:

  • width (Integer) (defaults to: 80) —

    grid width in columns (default: 80)

  • height (Integer) (defaults to: 24) —

    grid height in rows (default: 24)



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubytui/backends/test_backend.rb', line 25

def initialize(width: 80, height: 24)
  @width = width
  @height = height
  @cells = Array.new(width * height) { Cell.new }
  @cursor_visible = true
  @alternate_screen = false
  @raw_mode = false
  @cursor_x = 0
  @cursor_y = 0
  @draw_history = []
end

Instance Attribute Details

#alternate_screen ⇒ Object (readonly)

Returns the value of attribute alternate_screen.



20
21
22
# File 'lib/rubytui/backends/test_backend.rb', line 20

def alternate_screen
  @alternate_screen
end

#cursor_visible ⇒ Object (readonly)

Returns the value of attribute cursor_visible.



20
21
22
# File 'lib/rubytui/backends/test_backend.rb', line 20

def cursor_visible
  @cursor_visible
end

#cursor_x ⇒ Object (readonly)

Returns the value of attribute cursor_x.



21
22
23
# File 'lib/rubytui/backends/test_backend.rb', line 21

def cursor_x
  @cursor_x
end

#cursor_y ⇒ Object (readonly)

Returns the value of attribute cursor_y.



21
22
23
# File 'lib/rubytui/backends/test_backend.rb', line 21

def cursor_y
  @cursor_y
end

#draw_history ⇒ Object (readonly)

Returns the value of attribute draw_history.



21
22
23
# File 'lib/rubytui/backends/test_backend.rb', line 21

def draw_history
  @draw_history
end

#height ⇒ Object (readonly)

Returns the value of attribute height.



20
21
22
# File 'lib/rubytui/backends/test_backend.rb', line 20

def height
  @height
end

#raw_mode ⇒ Object (readonly)

Returns the value of attribute raw_mode.



20
21
22
# File 'lib/rubytui/backends/test_backend.rb', line 20

def raw_mode
  @raw_mode
end

#width ⇒ Object (readonly)

Returns the value of attribute width.



20
21
22
# File 'lib/rubytui/backends/test_backend.rb', line 20

def width
  @width
end

Instance Method Details

#cell_at(x, y) ⇒ Cell?

Get the grid cell at (x, y).

Looks up the flat index y * width + x without per-axis bounds checks, so an out-of-range x wraps into an adjacent row and a negative index counts from the end of the grid.

Parameters:

  • x (Integer) —

    zero-based column

  • y (Integer) —

    zero-based row

Returns:

  • (Cell, nil) —

    the cell, or nil if the index is outside the grid



173
174
175
176
# File 'lib/rubytui/backends/test_backend.rb', line 173

def cell_at(x, y)
  idx = y * @width + x
  @cells[idx]
end

#clear ⇒ void

This method returns an undefined value.

Reset every grid cell to empty.



81
82
83
# File 'lib/rubytui/backends/test_backend.rb', line 81

def clear
  @cells.each(&:reset!)
end

#delete_kitty_image(key) ⇒ void

This method returns an undefined value.

Record an image deletion.

Parameters:



151
152
153
154
# File 'lib/rubytui/backends/test_backend.rb', line 151

def delete_kitty_image(key)
  @image_deletes ||= []
  @image_deletes << key
end

#disable_mouse ⇒ void

This method returns an undefined value.

Record that mouse reporting is disabled (see #mouse_enabled?).



133
134
135
# File 'lib/rubytui/backends/test_backend.rb', line 133

def disable_mouse
  @mouse_enabled = false
end

#disable_raw_mode ⇒ void

This method returns an undefined value.

Set #raw_mode to false.



109
110
111
# File 'lib/rubytui/backends/test_backend.rb', line 109

def disable_raw_mode
  @raw_mode = false
end

#draw(changes) ⇒ void

This method returns an undefined value.

Record the changes and copy each cell into the in-memory grid.

A copy of changes is appended to #draw_history. Changes whose grid index (y * width + x) falls outside the grid are skipped.

Parameters:

  • changes (Array<Array(Integer, Integer, Cell)>) —

    changed cells as [x, y, cell]



44
45
46
47
48
49
50
51
52
# File 'lib/rubytui/backends/test_backend.rb', line 44

def draw(changes)
  @draw_history << changes.dup
  changes.each do |x, y, cell|
    idx = y * @width + x
    next if idx < 0 || idx >= @cells.length

    @cells[idx] = Cell.new(cell.symbol, cell.style)
  end
end

#draw_count ⇒ Integer

Number of draw calls made

Returns:



199
# File 'lib/rubytui/backends/test_backend.rb', line 199

def draw_count = @draw_history.length

#draw_kitty_image(placement) ⇒ void

This method returns an undefined value.

Record an image placement (Kitty graphics protocol).

Parameters:



143
144
145
146
# File 'lib/rubytui/backends/test_backend.rb', line 143

def draw_kitty_image(placement)
  @image_history ||= []
  @image_history << placement.dup
end

#enable_mouse ⇒ void

This method returns an undefined value.

Record that mouse reporting is enabled (see #mouse_enabled?).



126
127
128
# File 'lib/rubytui/backends/test_backend.rb', line 126

def enable_mouse
  @mouse_enabled = true
end

#enable_raw_mode ⇒ void

This method returns an undefined value.

Set #raw_mode to true.



102
103
104
# File 'lib/rubytui/backends/test_backend.rb', line 102

def enable_raw_mode
  @raw_mode = true
end

#enter_alternate_screen ⇒ void

This method returns an undefined value.

Set #alternate_screen to true.



88
89
90
# File 'lib/rubytui/backends/test_backend.rb', line 88

def enter_alternate_screen
  @alternate_screen = true
end

#flush ⇒ void

This method returns an undefined value.

No-op; the test backend has no output stream.



119
120
121
# File 'lib/rubytui/backends/test_backend.rb', line 119

def flush
  # no-op for test backend
end

#hide_cursor ⇒ void

This method returns an undefined value.

Mark the cursor as hidden (#cursor_visible becomes false).



57
58
59
# File 'lib/rubytui/backends/test_backend.rb', line 57

def hide_cursor
  @cursor_visible = false
end

#image_deletes ⇒ Array<Hash{Symbol => Object}>

Returns history of placements passed to #delete_kitty_image.

Returns:



160
# File 'lib/rubytui/backends/test_backend.rb', line 160

def image_deletes = (@image_deletes ||= [])

#image_history ⇒ Array<Hash{Symbol => Object}>

Returns history of image placements drawn via #draw_kitty_image.

Returns:

  • (Array<Hash{Symbol => Object}>) —

    history of image placements drawn via #draw_kitty_image



157
# File 'lib/rubytui/backends/test_backend.rb', line 157

def image_history = (@image_history ||= [])

#leave_alternate_screen ⇒ void

This method returns an undefined value.

Set #alternate_screen to false.



95
96
97
# File 'lib/rubytui/backends/test_backend.rb', line 95

def leave_alternate_screen
  @alternate_screen = false
end

#mouse_enabled? ⇒ Boolean

Returns true if #enable_mouse was called more recently than #disable_mouse.

Returns:



138
# File 'lib/rubytui/backends/test_backend.rb', line 138

def mouse_enabled? = @mouse_enabled == true

#move_cursor(x, y) ⇒ void

This method returns an undefined value.

Record the cursor position in #cursor_x and #cursor_y.

Parameters:

  • x (Integer) —

    zero-based column

  • y (Integer) —

    zero-based row



73
74
75
76
# File 'lib/rubytui/backends/test_backend.rb', line 73

def move_cursor(x, y)
  @cursor_x = x
  @cursor_y = y
end

#reset! ⇒ void

This method returns an undefined value.

Reset for a fresh test

Clears the grid and #draw_history, marks the cursor visible and moves it to (0, 0). Alternate-screen, raw-mode and mouse state and the image histories are left unchanged.



213
214
215
216
217
218
219
# File 'lib/rubytui/backends/test_backend.rb', line 213

def reset!
  @cells.each(&:reset!)
  @draw_history.clear
  @cursor_visible = true
  @cursor_x = 0
  @cursor_y = 0
end

#row_text(y) ⇒ String

Read an entire row as text

Parameters:

  • y (Integer) —

    row

Returns:

  • (String) —

    the row's text with trailing whitespace removed



192
193
194
# File 'lib/rubytui/backends/test_backend.rb', line 192

def row_text(y)
  text_at(0, y, @width).rstrip
end

#show_cursor ⇒ void

This method returns an undefined value.

Mark the cursor as visible (#cursor_visible becomes true).



64
65
66
# File 'lib/rubytui/backends/test_backend.rb', line 64

def show_cursor
  @cursor_visible = true
end

#size ⇒ Array(Integer, Integer)

Returns the grid size as [width, height].

Returns:

  • (Array(Integer, Integer)) —

    the grid size as [width, height]



114
# File 'lib/rubytui/backends/test_backend.rb', line 114

def size = [@width, @height]

#text_at(x, y, length) ⇒ String

Read text from a row starting at (x, y) for the given length

Parameters:

  • x (Integer) —

    starting column

  • y (Integer) —

    row

  • length (Integer) —

    number of cells to read

Returns:

  • (String) —

    the cell symbols joined; missing cells read as a space



184
185
186
# File 'lib/rubytui/backends/test_backend.rb', line 184

def text_at(x, y, length)
  (0...length).map { |i| cell_at(x + i, y)&.symbol || " " }.join
end

#total_changes ⇒ Integer

Total cells changed across all draws

Returns:



204
# File 'lib/rubytui/backends/test_backend.rb', line 204

def total_changes = @draw_history.sum(&:length)