Class: Hiiro::Effects::NullExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/effects.rb

Overview

Test double — records every call, returns configured stubs. Usage:

ex = NullExecutor.new
ex.stub('new-window', '')        # stub any call containing 'new-window'
tmux = Hiiro::Tmux.new(executor: ex)
tmux.new_window('mysession', 'main')
assert ex.called?('new-window')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNullExecutor

Returns a new instance of NullExecutor.



23
24
25
26
# File 'lib/hiiro/effects.rb', line 23

def initialize
  @calls = []
  @stubs = {}
end

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



21
22
23
# File 'lib/hiiro/effects.rb', line 21

def calls
  @calls
end

Instance Method Details

#called?(pattern) ⇒ Boolean

Returns true if any recorded call contains pattern as a substring of any arg.

Returns:

  • (Boolean)


51
52
53
# File 'lib/hiiro/effects.rb', line 51

def called?(pattern)
  @calls.any? { |c| c[:args].any? { |a| a.to_s.include?(pattern.to_s) } }
end

#calls_to(method) ⇒ Object

Returns all calls to a given method (:capture, :run, :check).



56
57
58
# File 'lib/hiiro/effects.rb', line 56

def calls_to(method)
  @calls.select { |c| c[:method] == method }
end

#capture(*args) ⇒ Object



33
34
35
36
# File 'lib/hiiro/effects.rb', line 33

def capture(*args)
  record(:capture, args)
  find_stub(args) || ''
end

#check(*args) ⇒ Object



44
45
46
47
48
# File 'lib/hiiro/effects.rb', line 44

def check(*args)
  record(:check, args)
  result = find_stub(args)
  result.nil? ? true : !!result
end

#reset!Object

Clear all recorded calls (useful between test assertions).



61
62
63
# File 'lib/hiiro/effects.rb', line 61

def reset!
  @calls = []
end

#run(*args) ⇒ Object



38
39
40
41
42
# File 'lib/hiiro/effects.rb', line 38

def run(*args)
  record(:run, args)
  result = find_stub(args)
  result.nil? ? true : result
end

#stub(pattern, output = '') ⇒ Object

Register a stub. pattern is matched as a substring of the joined args string.



29
30
31
# File 'lib/hiiro/effects.rb', line 29

def stub(pattern, output = '')
  @stubs[pattern.to_s] = output
end