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
module Suite = struct
exception TestFailure of string
type result =
| Error of exn
| Fail of string
| Pass
type outcome = {
label: string;
result: result;
time_s: float;
}
let string_of_result = function
| Error e -> Printf.sprintf "Error: %s" (Printexc.to_string e)
| Fail msg -> Printf.sprintf "Fail: %s" msg
| Pass -> "Pass"
let finally f cleanup =
let result =
try f ()
with e ->
cleanup ();
raise e
in
cleanup ();
result
module Sync = struct
type test_fun = unit -> unit
let bracket setup test teardown () =
let state = setup () in
finally
(fun () -> test state)
(fun () -> teardown state)
end
module Async = struct
type callback = unit -> unit
let noop () = ()
type wrapper = callback -> unit
type test_fun = wrapper -> unit
let bracket setup test teardown =
(fun wrapper ->
let state = setup () in
let wrapper' f =
wrapper (fun () -> finally f (fun () -> teardown state)) in
try
test state wrapper'
with e ->
teardown state;
raise e)
let run_one label test log handle_outcome =
let handled = ref false in
let start_time = ref 0.0 in
let handle_result_once result =
if not !handled
then begin
handled := true;
log "End";
log (string_of_result result);
handle_outcome {label; result; time_s = Sys.time () -. !start_time}
end
in
let catch_all f =
try f ()
with
| TestFailure msg -> handle_result_once (Fail msg)
| e -> handle_result_once (Error e)
in
catch_all (fun () ->
log "Start";
start_time := Sys.time ();
test
(fun callback ->
catch_all (fun () ->
callback ();
handle_result_once Pass)))
let of_sync test wrapper =
test ();
wrapper noop
end
type t =
| TestCase of string * Async.test_fun
| TestList of string * t list
let (>::) label test_fun = TestCase (label, Async.of_sync test_fun)
let (>:~) label test_fun = TestCase (label, test_fun)
let (>:::) label tests = TestList (label, tests)
let string_of_opt = function
| Some value -> Printf.sprintf " (%s)" value
| None -> ""
let assert_true ?label value =
if not value then begin
let msg = Printf.sprintf "test value was false%s" (string_of_opt label) in
raise (TestFailure msg)
end
let assert_equal ?(equal = (=)) ?label ?printer a b =
if not (equal a b)
then begin
let values = match printer with
| Some printer -> Printf.sprintf ": %s %s" (printer a) (printer b)
| None -> ""
in
let msg = Printf.sprintf "not equal%s%s" (string_of_opt label) values in
raise (TestFailure msg)
end
let assert_raises ?label expected_exn task =
match
try task (); None
with raised_exn -> Some raised_exn
with
| None ->
let msg =
Printf.sprintf
"expected exception not raised%s" (string_of_opt label)
in
raise (TestFailure msg)
| Some raised_exn when raised_exn = expected_exn -> ()
| Some raised_exn ->
let msg =
Printf.sprintf
"unexpected exception raised%s: %s"
(string_of_opt label)
(Printexc.to_string raised_exn)
in
raise (TestFailure msg)
let assert_raises_string ?label expected_exn_string task =
match
try task (); None
with raised_exn -> Some raised_exn
with
| None ->
let msg =
Printf.sprintf
"expected exception not raised%s" (string_of_opt label)
in
raise (TestFailure msg)
| Some raised_exn
when (Printexc.to_string raised_exn) = expected_exn_string -> ()
| Some raised_exn ->
let msg =
Printf.sprintf
"unexpected exception raised%s: %s"
(string_of_opt label)
(Printexc.to_string raised_exn)
in
raise (TestFailure msg)
end
module Zipper = struct
type crumb = {
left: Suite.t list;
label: string;
right: Suite.t list;
}
type t = {
crumbs: crumb list;
location: Suite.t;
}
let of_suite suite = {
crumbs = [];
location = suite;
}
let to_suite {location; _} = location
let move_up {crumbs; location} =
match crumbs with
| [] -> None
| {left; label; right} :: other_crumbs -> Some {
crumbs = other_crumbs;
location =
Suite.TestList (label, List.rev_append (location :: left) right);
}
let move_down {crumbs; location} =
match location with
| Suite.TestCase _ -> None
| Suite.TestList (_, []) -> None
| Suite.TestList (label, first_child :: other_children) -> Some {
crumbs = {
left = [];
label;
right = other_children;
} :: crumbs;
location = first_child;
}
let move_right {crumbs; location} =
match crumbs with
| [] -> None
| {right = []; _} :: _ -> None
| {left; label; right = first_right :: other_right} :: other_crumbs -> Some {
crumbs = {
left = location :: left;
label;
right = other_right;
} :: other_crumbs;
location = first_right;
}
let rec next_sibling zipper =
match move_right zipper with
| (Some _) as result -> result
| None -> begin
match move_up zipper with
| Some zipper' -> next_sibling zipper'
| None -> None
end
let next_location zipper =
match move_down zipper with
| Some _ as result -> result
| None -> next_sibling zipper
let get_labels {crumbs; location} =
let location_label = match location with
| Suite.TestCase (label, _) -> label
| Suite.TestList (label, _) -> label
in
location_label :: (List.map (fun crumb -> crumb.label) crumbs) |> List.rev
end
module Utils = struct
type output = {
log: string list;
outcomes: Suite.outcome list;
}
type raw_summary = {
total: int;
errors: int;
failures: int;
passes: int;
passed: bool
}
type summary = {
report: string;
passed: bool;
}
let run suite callback =
let log = ref [] in
let log_with_prefix prefix msg =
let line = Printf.sprintf "%s:%s" prefix msg in
log := (line :: !log)
in
let zipper = Zipper.of_suite suite in
let rec run' ({Zipper.location; _} as zipper) outcomes =
let continue zipper outcomes' =
match Zipper.next_location zipper with
| Some zipper' -> run' zipper' outcomes'
| None ->
callback {
log = List.rev !log;
outcomes = List.rev outcomes'
}
in
match location with
| Suite.TestCase (_, test_fun) ->
let prefix = Zipper.get_labels zipper |> String.concat ":" in
let log = log_with_prefix prefix in
Suite.Async.run_one
prefix test_fun log
(fun outcome -> continue zipper (outcome :: outcomes))
| Suite.TestList (_, _) -> continue zipper outcomes
in
run' zipper []
let summarise_raw outcomes =
let total, errors, failures, passes =
List.fold_left
(fun (total, errors, failures, passes) outcome ->
let open Suite in
match outcome.result with
| Error _ -> total + 1, errors + 1, failures, passes
| Fail _ -> total + 1, errors, failures + 1, passes
| Pass -> total + 1, errors, failures, passes + 1)
(0, 0, 0, 0) outcomes
in
{
total; errors; failures; passes;
passed = (total = passes)
}
let summary_of_raw raw =
let report =
String.concat "\n"
[
Printf.sprintf "%d tests run" raw.total;
Printf.sprintf "%d errors" raw.errors;
Printf.sprintf "%d failures" raw.failures;
Printf.sprintf "%d passes" raw.passes;
]
in
{
report;
passed = raw.passed;
}
let summarise outcomes =
summarise_raw outcomes
|> summary_of_raw
end