Method: ShellTest::ShellMethods::Session#parse

Defined in:
lib/shell_test/shell_methods/session.rb

#parse(script, options = {}, &block) ⇒ Object

Parses a terminal snippet into steps that a Session can run, and adds those steps to self. The snippet should utilize ps1 and ps2 as set on self. An exit command is added unless the :noexit option is set to true.

session = Session.new
session.parse %{
$ echo abc
abc
}
session.run.result   # => "$ echo abc\nabc\n$ exit\nexit\n"

Steps are registered with a callback block, if given, to recieve the expected and actual outputs during run. Normally the callback is used to validate that the run is going as planned.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/shell_test/shell_methods/session.rb', line 153

def parse(script, options={}, &block)
  args = split(script)
  args.shift # ignore script before first prompt

  if options[:noexit]
    args.pop
  else
    args.last << ps1 if args.last
    args.concat [ps1, "exit\n", nil, nil]
  end

  while !args.empty?
    prompt = args.shift
    input  = args.shift
    max_run_time = args.shift
    output = args.shift
    callback = make_callback(output, &block)

    on(prompt, input, max_run_time, &callback)
  end

  self
end