summaryrefslogtreecommitdiff
path: root/lib/highline/simulate.rb
blob: 0f1049d0ddcaa83277388b37a60a1155032541d3 (plain)
1
2
3
4
5
6
7
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
48
49
50
51
52
53
# coding: utf-8

#--
# simulate.rb
#
#  Created by Andy Rossmeissl on 2012-04-29.
#  Copyright 2005 Gray Productions. All rights reserved.
#
#  This is Free Software.  See LICENSE and COPYING for details.
#
# adapted from https://gist.github.com/194554


class HighLine

  # Simulates Highline input for use in tests.
  class Simulate

    # Creates a simulator with an array of Strings as a script
    def initialize(strings)
      @strings = strings
    end

    # Simulate StringIO#gets by shifting a string off of the script
    def gets
      @strings.shift
    end

    # Simulate StringIO#getbyte by shifting a single character off of the next line of the script
    def getbyte
      line = gets
      if line.length > 0
        char = line.slice! 0
        @strings.unshift line
        char
      end
    end

    # The simulator handles its own EOF
    def eof?
      false
    end

    # A wrapper method that temporarily replaces the Highline instance in $terminal with an instance of this object for the duration of the block
    def self.with(*strings)
      @input = $terminal.instance_variable_get :@input
      $terminal.instance_variable_set :@input, new(strings)
      yield
    ensure
      $terminal.instance_variable_set :@input, @input
    end
  end
end