Class: HighLine::Simulate

Inherits:
Object show all
Defined in:
lib/highline/simulate.rb

Overview

Simulates Highline input for use in tests.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strings) ⇒ Simulate

Creates a simulator with an array of Strings as a script

Parameters:

  • strings (Array<String>)

    preloaded string to be used as input buffer when simulating.



19
20
21
# File 'lib/highline/simulate.rb', line 19

def initialize(strings)
  @strings = strings
end

Class Method Details

.with(*strings) ⇒ Object

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

Parameters:

  • strings (String)

    preloaded string buffer that will feed the input operations when simulating.



51
52
53
54
55
56
57
# File 'lib/highline/simulate.rb', line 51

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

Instance Method Details

#eof?Boolean

The simulator handles its own EOF

Returns:

  • (Boolean)


40
41
42
# File 'lib/highline/simulate.rb', line 40

def eof?
  false
end

#getbyteObject

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



30
31
32
33
34
35
36
37
# File 'lib/highline/simulate.rb', line 30

def getbyte
  line = gets
  return if line.empty?

  char = line.slice! 0
  @strings.unshift line
  char
end

#getsObject

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



24
25
26
# File 'lib/highline/simulate.rb', line 24

def gets
  @strings.shift
end