Class: Expect
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
Legal
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
-
#timeout ⇒ Object
It is highly recommended that this be an immutable object.
Instance Method Summary collapse
-
#expect(pattern, chain = DefaultChain) ⇒ Object
Expect a message matching pattern in a certain chain.
- #handle_event(io_object) ⇒ Object
- #handle_line(line) ⇒ Object
-
#ignore(pattern) ⇒ Object
Ignore a pattern in all chains.
-
#initialize(io_object, reactor) ⇒ Expect
constructor
Create a new Expect_Test object.
-
#reset ⇒ Object
Reset the the ignore chain and all expect chains.
- #run(timeout = nil) ⇒ Object
Constructor Details
#initialize(io_object, reactor) ⇒ Expect
Create a new Expect_Test object.
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
#timeout ⇒ Object
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.
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
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.
147 148 149 |
# File 'lib/carat/expect.rb', line 147 def ignore(pattern) @ignore_chain.push(pattern) end |
#reset ⇒ Object
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 |