class Chamomile::Testing::Harness
Runs a Chamomile model without a real terminal for testing.
Attributes
Public Class Methods
Source
# File lib/chamomile/testing.rb, line 10 def initialize(model, width: 80, height: 24) @model = model @width = width @height = height @messages = [] @commands_run = [] @snapshots = {} @quit = false # Deliver initial window size deliver(Chamomile::ResizeEvent.new(width: width, height: height)) # Run start command if any if model.respond_to?(:on_start, true) && model.method(:on_start).owner != Chamomile::Model start_cmd = model.send(:on_start) run_cmd_sync(start_cmd) if start_cmd elsif (start_cmd = model.start) run_cmd_sync(start_cmd) end end
Public Instance Methods
Source
# File lib/chamomile/testing.rb, line 59 def assert_snapshot(name, &normalize) rendered = view rendered = normalize.call(rendered.lines).join if normalize if @snapshots.key?(name) raise SnapshotMismatch.new(name, @snapshots[name], rendered) unless @snapshots[name] == rendered else @snapshots[name] = rendered end rendered end
Assert snapshot — normalizes view through optional block
Source
# File lib/chamomile/testing.rb, line 71 def quit? @quit end
Returns true if the model has quit
Source
# File lib/chamomile/testing.rb, line 47 def resize(width, height) @width = width @height = height deliver(Chamomile::ResizeEvent.new(width: width, height: height)) end
Simulate a terminal resize
Source
# File lib/chamomile/testing.rb, line 32 def send_key(key, mod: []) deliver(Chamomile::KeyEvent.new(key: key, mod: Array(mod))) end
Send a key event to the model
Source
# File lib/chamomile/testing.rb, line 37 def send_mouse(x:, y:, button: :left, action: :press, mod: []) deliver(Chamomile::MouseEvent.new(x: x, y: y, button: button, action: action, mod: mod)) end
Send a mouse event
Source
# File lib/chamomile/testing.rb, line 42 def send_msg(msg) deliver(msg) end
Send any message directly
Source
# File lib/chamomile/testing.rb, line 54 def view @model.view end
Get current rendered view