Class: RubyExpect::Procedure

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_expect/procedure.rb

Overview

A procedure is a set of patterns to match and blocks to be called upon matching patterns. This is useful for building blocks of expected sequences of input data. An example of this could be logging into a system using SSH

Example

retval = 0
while (retval != 2)
  retval = any do
    expect /Are you sure you want to continue connecting \(yes\/no\)\?/ do
      send 'yes'
    end

    expect /password:\s*$/ do
      send password
    end

    expect /\$\s*$/ do
      send 'uptime'
    end
  end
end

# Expect each of the following
each do
  expect /load\s+average:\s+\d+\.\d+,\s+\d+\.\d+,\s+\d+\.\d+/ do # expect the output of uptime
    puts last_match.to_s
  end

  expect /\$\s+$/ do # shell prompt
    send 'exit'
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(exp_object, &block) ⇒ Procedure

Create a new procedure to be executed by the expect object

exp_object

The expect object that will execute this procedure

block

The block to be called that defined the procedure


150
151
152
153
154
155
# File 'lib/ruby_expect/procedure.rb', line 150

def initialize exp_object, &block
  raise "First argument must be a RubyExpect::Expect object" unless (exp_object.is_a?(RubyExpect::Expect))
  @exp = exp_object
  @steps = []
  instance_eval(&block) unless block.nil? 
end

Instance Method Details

#any(&block) ⇒ Object

Add an 'any' block to the Procedure. The block will be evaluated using a new AnyMatch instance

block

The block the specifies the patterns to expect


164
165
166
# File 'lib/ruby_expect/procedure.rb', line 164

def any &block
  RubyExpect::AnyMatch.new(@exp, &block).run
end

#each(&block) ⇒ Object

Add an 'each' block to the Procedure. The block will be evaluated using a new EachMatch instance

block

The block that specifies the patterns to expect


175
176
177
# File 'lib/ruby_expect/procedure.rb', line 175

def each &block
  RubyExpect::EachMatch.new(@exp, &block).run
end