lib/expectr.rb in expectr-1.0.1 vs lib/expectr.rb in expectr-1.0.2
- old
+ new
@@ -28,16 +28,18 @@
# # Do other stuff
# end
class Expectr
# Public: Gets/sets the number of seconds a call to Expectr#expect may last
attr_accessor :timeout
+ # Public: Gets/sets whether to flush program output to STDOUT
+ attr_accessor :flush_buffer
# Public: Gets/sets the number of bytes to use for the internal buffer
attr_accessor :buffer_size
# Public: Gets/sets whether to constrain the buffer to the buffer size
attr_accessor :constrain
- # Public: Gets/sets whether to flush program output to STDOUT
- attr_accessor :flush_buffer
+ # Public: Whether to always attempt to match once on calls to Expectr#expect.
+ attr_accessor :force_match
# Public: Returns the PID of the running process
attr_reader :pid
# Public: Returns the active buffer to match against
attr_reader :buffer
# Public: Returns the buffer discarded by the latest call to Expectr#expect
@@ -56,10 +58,16 @@
# at a time. If :constrain is true, this will be the
# maximum size of the internal buffer as well.
# (default: 8192)
# :constrain - Whether to constrain the internal buffer from the
# sub-process to :buffer_size (default: false)
+ # :force_match - Whether to always attempt to match against the
+ # internal buffer on a call to Expectr#expect. This
+ # is relevant following a failed call to
+ # Expectr#expect, which will leave the update status
+ # set to false, preventing further matches until more
+ # output is generated otherwise. (default: false)
def initialize(cmd, args={})
unless cmd.kind_of? String or cmd.kind_of? File
raise ArgumentError, "String or File expected"
end
@@ -70,10 +78,11 @@
@timeout = args[:timeout] || 30
@flush_buffer = args[:flush_buffer].nil? ? true : args[:flush_buffer]
@buffer_size = args[:buffer_size] || 8192
@constrain = args[:constrain] || false
+ @force_match = args[:force_match] || false
@out_mutex = Mutex.new
@out_update = false
@interact = false
@@ -230,9 +239,10 @@
# Yields the MatchData object representing the match
# Raises TypeError if something other than a String or Regexp is given
# Raises Timeout::Error if a match isn't found in time, unless recoverable
def expect(pattern, recoverable = false)
match = nil
+ @out_update = true if @force_match
case pattern
when String
pattern = Regexp.new(Regexp.quote(pattern))
when Regexp