expect

Simple implementation of "expect" to help building unitary testing of interactive program
IN THIS PACKAGE
Module Expect
type t

A process under the monitoring of Expect.

type expect_match = [
| `Eof(*

Look for EOF

*)
| `Fun of string -> bool(*

Look for a line matching the string

*)
| `Exact of string(*

Look for a line matching exactly this string

*)
| `Suffix of string(*

Look for a line ending with this string

*)
| `Prefix of string(*

Look for a line starting with this string

*)
| `Contains of string(*

Look for a line containing this string

*)
| `Timeout
]

Describe expectation about the output of the process. Lines includes the EOL (i.e. \n).

val spawn : ?verbose:bool -> ?verbose_output:( string -> unit ) -> ?timeout:float option -> ?env:string array -> ?use_stderr:bool -> string -> string array -> t

spawn prg args Start a process and monitor its output. Contrary to Unix.create_process, you don't need to repeat the program name at the beginning of args.

Optional parameters:

  • ~timeout: define the default timeout, in seconds. None means that you can wait forever
  • ~env: provide environment to run the process
  • ~use_stderr: redirect stderr to stdout and process it through expect
val set_timeout : t -> float option -> t

Define the timeout for a process.

val send : t -> string -> unit

Send a string to a process.

val expect : t -> ?fmatches:( string -> 'a option ) list -> (expect_match * 'a) list -> 'a -> 'a

expect t ~fmatches matches dflt Waits for output of the process and match it against expectations matches. If no expectations match at timeout, returns dflt. You can use ~fmatch to define while processing the output what the result is, if you find a match, return Some res otherwise return None. The function take into account matches before ~fmatch and it picks the first result which is not None.

val close : t -> Unix.process_status

Close the process.

val with_spawn : ?verbose:bool -> ?verbose_output:( string -> unit ) -> ?timeout:float option -> ?env:string array -> ?use_stderr:bool -> string -> string array -> ( t -> 'a -> 'a ) -> 'a -> 'a * Unix.process_status

Take care of opening and closing the process.