1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
open OUnitUtils
let get_test_context,
set_test_context,
reset_test_context =
let context_opt = ref None in
(fun () ->
match !context_opt with
| Some ctxt -> ctxt
| None -> failwith "Function need to be called from inside a test."),
(fun ctxt ->
context_opt := Some ctxt),
(fun _ ->
context_opt := None)
type node = ListItem of int | Label of string
let node1_of_node =
function
| OUnitTest.ListItem i -> ListItem i
| OUnitTest.Label s -> Label s
let node_of_node1 =
function
| ListItem i -> OUnitTest.ListItem i
| Label s -> OUnitTest.Label s
type path = node list
let path1_of_path pth =
List.map node1_of_node pth
type test_fun = unit -> unit
type test =
TestCase of test_fun
| TestList of test list
| TestLabel of string * test
let rec test1_of_test =
function
| OUnitTest.TestCase (_, f) -> TestCase (fun () -> f (get_test_context ()))
| OUnitTest.TestList lst -> TestList (List.map test1_of_test lst)
| OUnitTest.TestLabel (str, tst) -> TestLabel (str, test1_of_test tst)
let rec test_of_test1 =
function
| TestCase f ->
OUnitTest.TestCase
(OUnitTest.Short,
fun ctxt ->
set_test_context ctxt;
f ();
reset_test_context ())
| TestList lst -> OUnitTest.TestList (List.map test_of_test1 lst)
| TestLabel (str, tst) -> OUnitTest.TestLabel (str, test_of_test1 tst)
let rec ounit2_of_ounit1 =
function
| TestCase f ->
OUnit2.test_case
(fun ctxt ->
set_test_context ctxt;
f ();
reset_test_context ())
| TestList lst ->
OUnit2.test_list (List.map ounit2_of_ounit1 lst)
| TestLabel (lbl, test) ->
OUnit2.( >: ) lbl (ounit2_of_ounit1 test)
type test_result =
RSuccess of path
| RFailure of path * string
| RError of path * string
| RSkip of path * string
| RTodo of path * string
let test_result1_of_test_result path rslt =
let path1 =
path1_of_path path
in
let rslt1 =
match rslt with
| OUnitTest.RSuccess ->
RSuccess path1
| OUnitTest.RFailure (str, _, _) ->
RFailure (path1, str)
| OUnitTest.RError (str, _) ->
RError (path1, str)
| OUnitTest.RSkip str ->
RSkip (path1, str)
| OUnitTest.RTodo str ->
RTodo (path1, str)
| OUnitTest.RTimeout test_length ->
RError (path1,
(Printf.sprintf
"timeout after %.1fs."
(OUnitTest.delay_of_length test_length)))
in
rslt1
type test_event =
EStart of path
| EEnd of path
| EResult of test_result
type test_results = test_result list
let list_result1_of_list_result =
List.map
(fun (pth, rslt, _) ->
test_result1_of_test_result pth rslt)
let assert_failure =
OUnitAssert.assert_failure
let assert_bool =
OUnitAssert.assert_bool
let ( @? ) =
OUnitAssert.assert_bool
let assert_string =
OUnitAssert.assert_string
let assert_command
?exit_code ?sinput ?foutput ?use_stderr ?env ?(verbose=false) prg args =
let ctxt =
let ctxt = get_test_context () in
let conf' = Hashtbl.copy ctxt.OUnitTest.conf in
OUnitConf.set ~origin:"OUnit.assert_command" conf'
"verbose" (string_of_bool verbose);
{
ctxt with
OUnitTest.test_logger =
OUnitLogger.Test.create
(OUnitLoggerStd.std_logger conf' OUnitLogger.shard_default)
ctxt.OUnitTest.path;
}
in
OUnitAssert.assert_command
?exit_code ?sinput ?foutput ?use_stderr ?env ~ctxt
prg args
let assert_equal ?cmp ?printer ?pp_diff ?msg a b =
OUnitAssert.assert_equal ?cmp ?printer ?pp_diff ?msg a b
let assert_raises ?msg exc f =
OUnitAssert.assert_raises ?msg exc f
let skip_if =
OUnitAssert.skip_if
let todo =
OUnitAssert.todo
let cmp_float ?epsilon f1 f2 =
OUnitUtils.cmp_float ?epsilon f1 f2
let bracket pre f post () =
OUnitTest.section_ctxt (get_test_context ())
(fun ctxt ->
let fixture =
OUnitBracket.create
(fun _ -> pre ())
(fun fixture _ -> post fixture)
ctxt
in
let () = f fixture in
())
let bracket_tmpfile ?prefix ?suffix ?mode gen () =
OUnitTest.section_ctxt (get_test_context ())
(fun ctxt ->
let fixture =
OUnitBracket.bracket_tmpfile ?prefix ?suffix ?mode ctxt
in
gen fixture)
let (>:) a b =
test1_of_test (OUnitTest.(>:) a (test_of_test1 b))
let (>::) a b =
test1_of_test (OUnitTest.(>::) a (fun _ -> b ()))
let (>:::) a b =
test1_of_test (OUnitTest.(>:::) a (List.map test_of_test1 b))
let test_decorate g tst =
test1_of_test
(OUnitTest.test_decorate
(fun f ->
let f1 = (fun () -> f (get_test_context ())) in
let f1' = g f1 in
(fun ctxt ->
set_test_context ctxt;
f1' ();
reset_test_context ()))
(test_of_test1 tst))
let test_filter ?skip lst test =
let res =
OUnitTest.test_filter ?skip lst (test_of_test1 test)
in
match res with
| Some tst -> Some (test1_of_test tst)
| None -> None
let test_case_count tst =
OUnitTest.test_case_count (test_of_test1 tst)
let string_of_node nd =
OUnitTest.string_of_node (node_of_node1 nd)
let string_of_path pth =
OUnitTest.string_of_path (List.map node_of_node1 pth)
let test_case_paths tst =
let lst =
OUnitTest.test_case_paths (test_of_test1 tst)
in
List.map
(List.map node1_of_node)
lst
let default_v1_conf ?(verbose=false) () =
OUnitConf.default
~preset:
[
"chooser", "simple";
"runner", "sequential";
"results_style_1_X", "true";
"verbose", (string_of_bool verbose);
"output_file", "none";
]
()
let perform_test logger1 tst =
let logger =
OUnitLogger.fun_logger
(function
| {OUnitLogger.event = OUnitLogger.GlobalEvent _; _} ->
()
| {OUnitLogger.event = OUnitLogger.TestEvent (path, test_event); _} ->
begin
let path1 =
path1_of_path path
in
match test_event with
| OUnitLogger.EStart ->
logger1 (EStart path1)
| OUnitLogger.EEnd ->
logger1 (EEnd path1)
| OUnitLogger.EResult rslt ->
logger1 (EResult (test_result1_of_test_result path rslt))
| OUnitLogger.ELog _ | OUnitLogger.ELogRaw _ ->
()
end)
ignore
in
let conf = default_v1_conf () in
list_result1_of_list_result
(OUnitCore.perform_test
conf
logger
(snd (OUnitRunner.choice conf))
(snd (OUnitChooser.choice conf))
(test_of_test1 tst))
let run_test_tt ?verbose test =
let conf = default_v1_conf ?verbose () in
list_result1_of_list_result
(OUnitCore.run_test_tt
conf
(OUnitLoggerStd.create conf OUnitLogger.shard_default)
(snd (OUnitRunner.choice conf))
(snd (OUnitChooser.choice conf))
(test_of_test1 test))
let run_test_tt_main ?(arg_specs=[]) ?(set_verbose=ignore) suite =
let suite = test_of_test1 suite in
let only_test = ref [] in
let list_test = ref false in
let verbose = ref false in
let specs =
[
"-verbose",
Arg.Set verbose,
" Rather than displaying dots while running the test, be more verbose.";
"-only-test",
Arg.String (fun str -> only_test := str :: !only_test),
"path Run only the selected tests.";
"-list-test",
Arg.Set list_test,
" List tests";
] @ arg_specs
in
let () =
Arg.parse
(Arg.align specs)
(fun x -> raise (Arg.Bad ("Bad argument : " ^ x)))
("usage: " ^ Sys.argv.(0) ^ " [options] [-only-test path]*")
in
let conf = default_v1_conf ~verbose:!verbose () in
set_verbose (OUnitLoggerStd.verbose conf);
if !list_test then
begin
List.iter
(fun pth -> print_endline (OUnitTest.string_of_path pth))
(OUnitTest.test_case_paths suite);
[]
end
else
begin
let nsuite =
if !only_test = [] then
suite
else
begin
match OUnitTest.test_filter ~skip:true !only_test suite with
| Some test ->
test
| None ->
failwithf
"Filtering test %s lead to no tests."
(String.concat ", " !only_test)
end
in
let test_results =
OUnitCore.run_test_tt
conf
(OUnitLoggerStd.std_logger conf OUnitLogger.shard_default)
(snd (OUnitRunner.choice conf))
(snd (OUnitChooser.choice conf))
nsuite
in
if not (OUnitResultSummary.was_successful test_results) then
exit 1
else
list_result1_of_list_result test_results;
end