module Expect:sig
..end
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:
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:
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
typeexpect_match =
[ `Contains of string
| `Eof
| `Exact of string
| `Fun of string -> bool
| `Prefix of string
| `Suffix of string
| `Timeout ]
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 expectval set_timeout : t -> float option -> t
val send : t -> string -> unit
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
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