Class: Expectr::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/expectr/interpreter.rb,
lib/expectr/error.rb,
lib/expectr/errstr.rb

Overview

Internal: Provide an interface to Expectr in a manner designed to mimic Expect’s original functionality.

Defined Under Namespace

Modules: Errstr Classes: NotRunningError

Constant Summary collapse

DEFAULT_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Interpreter

Public: Initialize a new Expectr Interpreter interface.

source - String containing the source to be executed.



15
16
17
18
19
# File 'lib/expectr/interpreter.rb', line 15

def initialize(source)
  @source = source
  @variables = { timeout: DEFAULT_TIMEOUT }
  @expect = nil
end

Instance Attribute Details

#filenameObject

Public: Filename of currently executing script.



10
11
12
# File 'lib/expectr/interpreter.rb', line 10

def filename
  @filename
end

Instance Method Details

#closeObject

Public: Terminate any process associated with the Interpreter.

Returns nothing.



94
95
96
97
98
99
# File 'lib/expectr/interpreter.rb', line 94

def close
  @expect.kill!(:KILL) if @expect.respond_to?(:kill!)
rescue Expectr::ProcessError
ensure
  @expect = nil
end

#expect(*args) ⇒ Object

Public: Provide an interface to Expectr#expect.

args - Arguments to be passed through to the Expectr object.

Returns per Expectr#expect.



73
74
75
# File 'lib/expectr/interpreter.rb', line 73

def expect(*args)
  @expect.expect(*args)
end

#log_user(enable) ⇒ Object

Public: Set whether output from the active Expectr object should be echoed to the user.

enable - Boolean value denoting whether output to the screen should be

enabled.
In order to keep compatibility with Expect, a Fixnum can be
passed, such that any number greater than 0 is evaluated as
true, and 0 or less is false.

Returns nothing. Raises TypeError if enable is not a boolean.



48
49
50
51
52
53
54
55
56
57
# File 'lib/expectr/interpreter.rb', line 48

def log_user(enable)
  if enable.is_a?(Numeric)
    enable = (enable > 0)
  end
  unless enable.is_a?(TrueClass) || enable.is_a?(FalseClass)
    raise(TypeError, Errstr::BOOLEAN_OR_FIXNUM % enable.class.name)
  end

  set(:flush_buffer, enable)
end

#runObject

Public: Run the source associated with the Interface object.

Returns nothing.



24
25
26
# File 'lib/expectr/interpreter.rb', line 24

def run
  eval(@source, binding, (@filename || "(expectr)"), 0)
end

#send(str) ⇒ Object

Public: Send a String to the process referenced by the active Expectr object.

str - String to send to the process.

Returns nothing. Raises NotRunningError if no Expectr object is active.



84
85
86
87
88
89
# File 'lib/expectr/interpreter.rb', line 84

def send(str)
  if @expect.nil?
    raise(NotRunningError, Errstr::PROCESS_NOT_RUNNING)
  end
  @expect.send(str)
end

#send_user(*str) ⇒ Object

Public: Print one or more messages to $stdout.

str - String or Array of Strings to print to $stdout.

Returns nothing.



33
34
35
# File 'lib/expectr/interpreter.rb', line 33

def send_user(*str)
  str.each { |line| $stdout.print line }
end

#spawn(cmd) ⇒ Object

Public: Spawn an instance of a command via Expectr.

cmd - String referencing the application to spawn.

Returns nothing.



64
65
66
# File 'lib/expectr/interpreter.rb', line 64

def spawn(cmd)
  @expect = Expectr.new(cmd, @variables)
end