Module: WIP::Runner::Spec::Helpers::IOHelpers

Defined in:
lib/wip/runner/spec/helpers/io_helpers.rb

Defined Under Namespace

Classes: CustomHighLine, Simulator

Instance Method Summary collapse

Instance Method Details

#ioObject



4
5
6
# File 'lib/wip/runner/spec/helpers/io_helpers.rb', line 4

def io
  @io ||= CustomHighLine.new
end

#simulate(pairs = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wip/runner/spec/helpers/io_helpers.rb', line 8

def simulate(pairs = nil)
  unless pairs.nil?
    @simulated = pairs.inject(@simulated || {}) do |memo, (q, a)|
      memo[q] = a ; memo
    end
  end

  if block_given?
    begin
      originput = io.instance_variable_get(:@input)
      simulator = Simulator.new(@simulated.values, (@simulated.keys == ['*']))
      io.instance_variable_set(:@input, simulator)

      @simulated.keys.each do |question|
        # NOTE: the "|default|" is stripped because that is added
        # later by the Question instance, in time for a call to #say.
        if question.is_a?(Array)
          expect(io).to receive(:ask)
            .with(*question)
            .and_call_original
        else
          question = question.sub(/:\s\|.*\Z/, ': ')
          expect(io).to receive(:ask)
            .with(question)
            .and_call_original
        end unless question == '*'
      end

      yield
    ensure
      io.instance_variable_set(:@input, originput)
    end
  end
end