Class: Chamomile::Testing::Harness

Inherits:
Object
  • Object
show all
Defined in:
lib/chamomile/testing.rb

Overview

Runs a Chamomile model without a real terminal for testing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, width: 80, height: 24) ⇒ Harness

Returns a new instance of Harness.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#commands_runObject (readonly)

Returns the value of attribute commands_run.



8
9
10
# File 'lib/chamomile/testing.rb', line 8

def commands_run
  @commands_run
end

#messagesObject (readonly)

Returns the value of attribute messages.



8
9
10
# File 'lib/chamomile/testing.rb', line 8

def messages
  @messages
end

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/chamomile/testing.rb', line 8

def model
  @model
end

Instance Method Details

#assert_snapshot(name, &normalize) ⇒ Object

Assert snapshot — normalizes view through optional block



59
60
61
62
63
64
65
66
67
68
# 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

#quit?Boolean

Returns true if the model has quit

Returns:

  • (Boolean)


71
72
73
# File 'lib/chamomile/testing.rb', line 71

def quit?
  @quit
end

#resize(width, height) ⇒ Object

Simulate a terminal resize



47
48
49
50
51
# File 'lib/chamomile/testing.rb', line 47

def resize(width, height)
  @width = width
  @height = height
  deliver(Chamomile::ResizeEvent.new(width: width, height: height))
end

#send_key(key, mod: []) ⇒ Object

Send a key event to the model



32
33
34
# File 'lib/chamomile/testing.rb', line 32

def send_key(key, mod: [])
  deliver(Chamomile::KeyEvent.new(key: key, mod: Array(mod)))
end

#send_mouse(x:, y:, button: :left, action: :press, mod: []) ⇒ Object

Send a mouse event



37
38
39
# 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_msg(msg) ⇒ Object

Send any message directly



42
43
44
# File 'lib/chamomile/testing.rb', line 42

def send_msg(msg)
  deliver(msg)
end

#viewObject

Get current rendered view



54
55
56
# File 'lib/chamomile/testing.rb', line 54

def view
  @model.view
end