class HighLine::Simulate

Simulates Highline input for use in tests.

Public Class Methods

new(strings) click to toggle source

Creates a simulator with an array of Strings as a script @param strings [Array<String>] preloaded string to be used

as input buffer when simulating.
# File lib/highline/simulate.rb, line 18
def initialize(strings)
  @strings = strings
end
with(*strings) { || ... } click to toggle source

A wrapper method that temporarily replaces the Highline instance in HighLine.default_instance with an instance of this object for the duration of the block

@param strings [String] preloaded string buffer that

will feed the input operations when simulating.
# File lib/highline/simulate.rb, line 50
def self.with(*strings)
  @input = HighLine.default_instance.instance_variable_get :@input
  HighLine.default_instance.instance_variable_set :@input, new(strings)
  yield
ensure
  HighLine.default_instance.instance_variable_set :@input, @input
end

Public Instance Methods

eof?() click to toggle source

The simulator handles its own EOF

# File lib/highline/simulate.rb, line 39
def eof?
  false
end
getbyte() click to toggle source

Simulate StringIO#getbyte by shifting a single character off of the next line of the script

# File lib/highline/simulate.rb, line 29
def getbyte
  line = gets
  return if line.empty?

  char = line.slice! 0
  @strings.unshift line
  char
end
gets() click to toggle source

Simulate StringIO#gets by shifting a string off of the script

# File lib/highline/simulate.rb, line 23
def gets
  @strings.shift
end