Class: Tuile::FakeScreen

Inherits:
Screen
  • Object
show all
Defined in:
lib/tuile/fake_screen.rb,
sig/tuile.rbs

Overview

Testing only — a screen which doesn't paint anything, so the TTY running the tests is not painted over. It runs no event loop, so Screen#check_locked admits the thread that called Screen.fake: a spec mutating the UI from a spawned thread raises, exactly as an app would.

Intended for unit-testing individual components: instantiate a component, mutate it, and assert against #prints or #invalidated?. It does not run an event loop, so it is not suitable for system-testing whole apps — for that, drive the real script through a PTY (see spec/examples/).

Call Screen.fake to initialize the fake screen easily. Typical usage:

before { Screen.fake }
after  { Screen.close }

it "paints its content" do
label = Component::Label.new.tap { |l| l.text = "hi" }
Screen.instance.content = Component::Window.new("Greeting").tap { |w| w.content = label }
Screen.instance.repaint
assert_includes Screen.instance.prints.join, "hi"
end

Constant Summary collapse

EDITING_KEYS =

Returns:

  • (::Array[String])

Instance Attribute Summary collapse

Attributes inherited from Screen

#buffer, #color_scheme, #event_queue, #focused, #on_error, #pane, #size, #theme, #theme_def

Instance Method Summary collapse

Methods inherited from Screen

#active_window, #add_popup, #check_locked, #close, close, #content, #content=, #cursor_position, #cursor_sequence, #cycle_focus, #event_loop, fake, #focus_next, #focus_previous, #global_shortcut_hints, #handle_key, #handle_mouse, #has_popup?, instance, #invalidate, #layout, #needs_full_repaint, #on_color_scheme, #popups, #refresh_status_bar, #register_global_shortcut, #remove_popup, #repaint, #run_event_loop, #state, #unregister_global_shortcut

Constructor Details

#initializeFakeScreen

Returns a new instance of FakeScreen.



26
27
28
29
30
31
32
# File 'lib/tuile/fake_screen.rb', line 26

def initialize
  super
  @event_queue = FakeEventQueue.new
  @size = Size.new(160, 50)
  @buffer.resize(@size) # super sized it to the test runner's TTY
  @prints = []
end

Instance Attribute Details

#prints::Array[String] (readonly)

@return — whatever #print / #emit produced so far. Component painting lands in Screen#buffer, not here — assert on Buffer#row_text / Buffer#row_ansi / Buffer#cell for content, and on prints for cursor and housekeeping escapes.

Returns:

  • (::Array[String])


38
39
40
# File 'lib/tuile/fake_screen.rb', line 38

def prints
  @prints
end

Instance Method Details

#clearvoid

This method returns an undefined value.



41
42
43
# File 'lib/tuile/fake_screen.rb', line 41

def clear
  @prints.clear
end

#detect_schemeSymbol

No terminal probing in tests: skip TerminalBackground.detect (which would write an OSC 11 query to the test runner's TTY and steal its input) and pin the deterministic default.

Returns:

  • (Symbol)


76
# File 'lib/tuile/fake_screen.rb', line 76

def detect_scheme = :dark

#emit(str) ⇒ void

This method returns an undefined value.

Captures the assembled repaint frame instead of writing to the test runner's TTY. Lands in #prints so cursor/sync escapes can be asserted; painted content is read from Screen#buffer.

@param str

Parameters:

  • str (String)


57
58
59
# File 'lib/tuile/fake_screen.rb', line 57

def emit(str)
  @prints << str
end

#invalidated?(component) ⇒ Boolean

@param component — the component to check.

Parameters:

Returns:

  • (Boolean)


63
# File 'lib/tuile/fake_screen.rb', line 63

def invalidated?(component) = @invalidated.include?(component)

#invalidated_clearvoid

This method returns an undefined value.



66
67
68
# File 'lib/tuile/fake_screen.rb', line 66

def invalidated_clear
  @invalidated.clear
end

This method returns an undefined value.

Doesn't print anything: collects all strings in #prints.

@param args

Parameters:

  • args (String)


48
49
50
# File 'lib/tuile/fake_screen.rb', line 48

def print(*args)
  @prints += args
end