Module: WIP::Runner::Spec::Helpers::UIHelpers

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

Defined Under Namespace

Classes: CustomLine, CustomUI, Simulator

Instance Method Summary collapse

Instance Method Details

#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
42
43
44
45
46
47
# File 'lib/wip/runner/spec/helpers/ui_helpers.rb', line 8

def simulate(pairs = nil)
  unless pairs.nil?
    @simulated = pairs.inject(@simulated || []) do |memo, pair|
      memo << pair ; memo
    end
  end

  if block_given?
    highline = ui.err
    original = highline.instance_variable_get(:@input)

    begin
      if @simulated
        keys      = @simulated.map { |pair| pair[0] }
        values    = @simulated.map { |pair| pair[1] }
        simulator = Simulator.new(values, (@simulated[0][0] == '*'))
        highline.instance_variable_set(:@input, simulator)

        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(highline).to receive(:ask)
              .with(*question)
              .and_call_original
          else
            question = question.sub(/:\s\|.*\Z/, ': ')
            expect(highline).to receive(:ask)
              .with(question)
              .and_call_original
          end unless question == '*'
        end
      end

      yield
    ensure
      highline.instance_variable_set(:@input, original)
    end
  end
end

#uiObject



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

def ui
  @ui ||= CustomUI.new($stdin, StringIO.new, StringIO.new)
end