<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <link rel="stylesheet" href="style.css" type="text/css"> <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"> <link rel="Start" href="index.html"> <link rel="next" href="ExpectPcre.html"> <link rel="Up" href="index.html"> <link title="Index of types" rel=Appendix href="index_types.html"> <link title="Index of values" rel=Appendix href="index_values.html"> <link title="Index of modules" rel=Appendix href="index_modules.html"> <link title="Expect" rel="Chapter" href="Expect.html"> <link title="ExpectPcre" rel="Chapter" href="ExpectPcre.html"> <link title="ExpectStr" rel="Chapter" href="ExpectStr.html"><title>Expect</title> </head> <body> <div class="navbar"> <a class="up" href="index.html" title="Index">Up</a> <a class="post" href="ExpectPcre.html" title="ExpectPcre">Next</a> </div> <h1>Module <a href="type_Expect.html">Expect</a></h1> <pre><span class="keyword">module</span> Expect: <code class="code">sig</code> <a href="Expect.html">..</a> <code class="code">end</code></pre><div class="info"> Expect module for testing interactive program. <p> 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...). <p> The use of this library is built around 4 functions:<ul> <li>spawn: to create a process</li> <li>send: to send a string to the process</li> <li>expect: match output of the process</li> <li>close: end the process</li> </ul> Output of the program is processed line by line. <p> 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 <p> 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. <p> There is two additional event to match:<ul> <li>eof: process close its output</li> <li>timeout: too much time has been spent waiting to match something</li> </ul> Both of this action, if not matched will use the default_action provided. <p> Here is an example program, that look for string "." in the output: <p> <pre class="codepre"><code class="code">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" </code></pre> <p> See <a href="http://directory.fsf.org/project/expect/">Expect manual</a><br> <b>Author(s):</b> Sylvain Le Gall<br> </div> <hr width="100%"> <pre><span id="TYPEt"><span class="keyword">type</span> <code class="type"></code>t</span> </pre> <div class="info"> A process under the monitoring of Expect.<br> </div> <pre><span id="TYPEexpect_match"><span class="keyword">type</span> <code class="type"></code>expect_match</span> = <code class="type">[ `Contains of string<br> | `Eof<br> | `Exact of string<br> | `Fun of string -> bool<br> | `Prefix of string<br> | `Suffix of string<br> | `Timeout ]</code> </pre> <div class="info"> Describe expectation about the output of the process. Lines includes the EOL (i.e. \n).<br> </div> <pre><span id="VALspawn"><span class="keyword">val</span> spawn</span> : <code class="type">?verbose:bool -><br> ?timeout:float option -><br> ?env:string array -> ?use_stderr:bool -> string -> string array -> <a href="Expect.html#TYPEt">t</a></code></pre><div class="info"> <code class="code">spawn prg args</code> Start a process and monitor its output. Contrary to <code class="code">Unix.create_process</code>, you don't need to repeat the program name at the beginning of args. <p> Optional parameters:<ul> <li><code class="code">~timeout</code>: define the default timeout, in seconds. None means that you can wait forever</li> <li><code class="code">~env</code>: provide environment to run the process</li> <li><code class="code">~use_stderr</code>: redirect stderr to stdout and process it through expect</li> </ul> <br> </div> <pre><span id="VALset_timeout"><span class="keyword">val</span> set_timeout</span> : <code class="type"><a href="Expect.html#TYPEt">t</a> -> float option -> <a href="Expect.html#TYPEt">t</a></code></pre><div class="info"> Define the timeout for a process.<br> </div> <pre><span id="VALsend"><span class="keyword">val</span> send</span> : <code class="type"><a href="Expect.html#TYPEt">t</a> -> string -> unit</code></pre><div class="info"> Send a string to a process.<br> </div> <pre><span id="VALexpect"><span class="keyword">val</span> expect</span> : <code class="type"><a href="Expect.html#TYPEt">t</a> -><br> ?fmatches:(string -> 'a option) list -><br> (<a href="Expect.html#TYPEexpect_match">expect_match</a> * 'a) list -> 'a -> 'a</code></pre><div class="info"> <code class="code">expect t ~fmatches matches dflt</code> Waits for output of the process and match it against expectations <code class="code">matches</code>. If no expectations match at timeout, returns <code class="code">dflt</code>. You can use <code class="code">~fmatch</code> to define while processing the output what the result is, if you find a match, return <code class="code">Some res</code> otherwise return <code class="code">None</code>. The function take into account <code class="code">matches</code> before <code class="code">~fmatch</code> and it picks the first result which is not <code class="code">None</code>.<br> </div> <pre><span id="VALclose"><span class="keyword">val</span> close</span> : <code class="type"><a href="Expect.html#TYPEt">t</a> -> Unix.process_status</code></pre><div class="info"> Close the process.<br> </div> <pre><span id="VALwith_spawn"><span class="keyword">val</span> with_spawn</span> : <code class="type">?verbose:bool -><br> ?timeout:float option -><br> ?env:string array -><br> ?use_stderr:bool -><br> string -><br> string array -> (<a href="Expect.html#TYPEt">t</a> -> 'a -> 'a) -> 'a -> 'a * Unix.process_status</code></pre><div class="info"> Take care of opening and closing the process.<br> </div> </body></html>