Debian Codegraph @

test-suite/unit-tests/src/utest.ml

raw symbols SCIP
1 open OUnit
2
3 (* general case to build a test *)
4 let mk_test nm test = nm >: test
5
6 (* common cases for building tests *)
7 let mk_eq_test nm descr expected actual =
8 mk_test nm (TestCase (fun _ -> assert_equal ~msg:descr expected actual))
9
10 let mk_bool_test nm descr actual =
11 mk_test nm (TestCase (fun _ -> assert_bool descr actual))
12
13 let cfprintf oc = Printf.(kfprintf (fun oc -> fprintf oc "\n%!") oc)
14
15 (* given test result, print message, return success boolean *)
16 let logger out_ch result =
17 let cprintf s = cfprintf out_ch s in
18 match result with
19 | RSuccess path ->
20 cprintf "TEST SUCCEEDED: %s" (string_of_path path);
21 true
22 | RError (path,msg)
23 | RFailure (path,msg) ->
24 cprintf "TEST FAILED: %s (%s)" (string_of_path path) msg;
25 false
26 | RSkip (path,msg)
27 | RTodo (path,msg) ->
28 cprintf "TEST DID NOT SUCCEED: %s (%s)" (string_of_path path) msg;
29 false
30
31 (* run one OUnit test case, return successes, no. of tests *)
32 (* notionally one test, which might be a TestList *)
33 let run_one logit test =
34 let rec process_results rs =
35 match rs with
36 [] -> (0,0)
37 | (r::rest) ->
38 let succ = if logit r then 1 else 0 in
39 let succ_results,tot_results = process_results rest in
40 (succ + succ_results,tot_results + 1)
41 in
42 let results = perform_test (fun _ -> ()) test in
43 process_results results
44
45 let open_log_out_ch ml_fn =
46 let log_fn = ml_fn ^ ".log" in
47 open_out log_fn
48
49 (* run list of OUnit test cases, log results *)
50 let run_tests ml_fn out_ch tests =
51 let cprintf s = cfprintf out_ch s in
52 let ceprintf s = cfprintf stderr s in
53 let logit = logger out_ch in
54 let rec run_some tests succ tot =
55 match tests with
56 [] -> (succ,tot)
57 | (t::ts) ->
58 let succ_one,tot_one = run_one logit t in
59 run_some ts (succ + succ_one) (tot + tot_one)
60 in
61 (* format for test-suite summary to find status
62 success if all tests succeeded, else failure
63 *)
64 let succ,tot = run_some tests 0 0 in
65 cprintf
66 "*** Ran %d tests, with %d successes and %d failures ***"
67 tot succ (tot - succ);
68 if succ = tot then
69 cprintf
70 "==========> SUCCESS <==========\n %s ... Ok" ml_fn
71 else begin
72 cprintf
73 "==========> FAILURE <==========\n %s ... Error!" ml_fn;
74 ceprintf "FAILED %s.log" ml_fn
75 end;
76 close_out out_ch