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


15
16
17
# File 'lib/highline/simulate.rb', line 15

def initialize(strings)
  @strings = strings
end

Class Method Details

.with(*strings) ⇒ Object

A wrapper method that temporarily replaces the Highline instance in $terminal with an instance of this object for the duration of the block


40
41
42
43
44
45
46
# File 'lib/highline/simulate.rb', line 40

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

Instance Method Details

#eof?Boolean

The simulator handles its own EOF

Returns:

  • (Boolean)

35
36
37
# File 'lib/highline/simulate.rb', line 35

def eof?
  false
end

#getbyteObject

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


25
26
27
28
29
30
31
32
# File 'lib/highline/simulate.rb', line 25

def getbyte
  line = gets
  if line.length > 0
    char = line.slice! 0
    @strings.unshift line
    char
  end
end

#getsObject

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


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

def gets
  @strings.shift
end