Module Expect

module Expect: sig .. end
Expect module for testing interactive program.

This is a simple implementation of expect to help building unitary testing of interactive program. Since this is an OCaml library, only specific part of expect has been implemented. Other function can be replaced by standard OCaml functions (exit...).

The use of this library is built around 4 functions:

Output of the program is processed line by line.

Regular expression is implemented through the library Str. You will need to build a regexp using this module. The regexp should only match a substring of the line. If you need to match something at the beginning or at the end, use "^" and "$". To use a regexp

Additional match functions can be build using a standard function. This function is passed the entire line and should return if it match or not.

There is two additional event to match:

Both of this action, if not matched will use the default_action provided.

Here is an example program, that look for string "." in the output:

open Expect

let (), exit_code =
  with_spawn "ls" [|"-alh"|]
  (fun t () ->
    if expect t [`Exact ".", true] false then
      prerr_endline "'.' found"
    else
      prerr_endline "'.' not found")
  ()
in
  match exit_code with
  | Unix.WEXITED 0 ->
      print_endline "Exit normal"
  | _ ->
      print_endline "Problem when exiting"

See Expect manual
Author(s): Sylvain Le Gall


type t 
A process under the monitoring of Expect.
type expect_match = [ `Contains of string
| `Eof
| `Exact of string
| `Fun of string -> bool
| `Prefix of string
| `Suffix of 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:


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.