Class: HighLine::Simulate
Overview
Simulates Highline input for use in tests.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#eof? ⇒ Boolean
The simulator handles its own EOF.
-
#getbyte ⇒ Object
Simulate StringIO#getbyte by shifting a single character off of the next line of the script.
-
#gets ⇒ Object
Simulate StringIO#gets by shifting a string off of the script.
-
#initialize(strings) ⇒ Simulate
constructor
Creates a simulator with an array of Strings as a script.
Constructor Details
#initialize(strings) ⇒ Simulate
Creates a simulator with an array of Strings as a script
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
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
40 41 42 |
# File 'lib/highline/simulate.rb', line 40 def eof? false end |
#getbyte ⇒ Object
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 |
#gets ⇒ Object
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 |