Class: Expect

Inherits:
Object show all
Defined in:
lib/carat/expect.rb

Overview

Expect

Given an IO object, read data from that object and expect certain data.

All expected data is expected in ‘chains’. When a message is received, it is compared to the pattern at the top of each chain. If it matches one of the patterns, then all is well, but an error message is printed otherwise.

A message can match patterns in more than one chain.

Synopsis

require 'carat/expect'

# to do

Adopted from Ruby Treasures 0.4 Copyright © 2002 Paul Brannan <[email protected]>

You may distribute this software under the same terms as Ruby (see the file COPYING that was distributed with this library).

History

$Id: expect.rb, v0.4 2004/11/30 transami Exp $

Defined Under Namespace

Classes: DefaultChain, SeparatedPattern, Timeout, UnmatchedMessage

Constant Summary collapse

VERSION =
"0.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_object, reactor) ⇒ Expect

Create a new Expect_Test object.

Parameters:

  • io_object

    an IO object to read from.

  • reactor

    a reactor to use



114
115
116
117
118
119
# File 'lib/carat/expect.rb', line 114

def initialize(io_object, reactor)
  @io_object = io_object
  @reactor = reactor
  @reactor.register_read(@io_object, method(:handle_event))
  reset()
end

Instance Attribute Details

#timeoutObject

It is highly recommended that this be an immutable object.



106
107
108
# File 'lib/carat/expect.rb', line 106

def timeout
  @timeout
end

Instance Method Details

#expect(pattern, chain = DefaultChain) ⇒ Object

Expect a message matching pattern in a certain chain.

Parameters:

  • pattern

    the pattern to expect.

  • chain (defaults to: DefaultChain)

    the chain in which to expect the message.



136
137
138
139
140
# File 'lib/carat/expect.rb', line 136

def expect(pattern, chain=DefaultChain)
  @expect_chain[chain] ||= Array.new
  @expect_chain[chain].push(pattern)
  @expects_left += 1
end

#handle_event(io_object) ⇒ Object



163
164
165
# File 'lib/carat/expect.rb', line 163

def handle_event(io_object)
  handle_line(io_object.gets())
end

#handle_line(line) ⇒ Object

Raises:



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/carat/expect.rb', line 167

def handle_line(line)
  line.chomp!()

  # Check each element in the ignore chain
  for pattern in @ignore_chain do
    return if pattern === line
  end

  # Check the first element of each expect chain to see if
  # the message matches
  match = false
  for key, chain in @expect_chain do
    next if chain.length == 0
    if chain.first() === line then
      chain.shift()
      @expects_left -= 1
      match = true
    end
  end

  throw :expect_loop_done if @expects_left == 0

  raise UnmatchedMessage.new(line) if match == false
end

#ignore(pattern) ⇒ Object

Ignore a pattern in all chains.

Parameters:

  • pattern

    the pattern to ignore.



147
148
149
# File 'lib/carat/expect.rb', line 147

def ignore(pattern)
  @ignore_chain.push(pattern)
end

#resetObject

Reset the the ignore chain and all expect chains.



124
125
126
127
128
# File 'lib/carat/expect.rb', line 124

def reset()
  @expect_chain = Hash.new
  @ignore_chain = Array.new
  @expects_left = 0
end

#run(timeout = nil) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/carat/expect.rb', line 151

def run(timeout = nil)
  return if @expects_left == 0
  catch :expect_loop_done do
    @reactor.run_event_loop(timeout)
    for key, chain in @expect_chain do
      next if chain.length == 0
      raise Timeout.new(key, chain.first)
    end
    # TODO: What does it mean if we got here?
  end
end