forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoUnitLogger.ml
145 lines (133 loc) · 4.65 KB
/
oUnitLogger.ml
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
(*
* Logger for information and various OUnit events.
*)
open OUnitTypes
open OUnitUtils
type event_type = GlobalEvent of global_event | TestEvent of test_event
let format_event verbose event_type =
match event_type with
| GlobalEvent e ->
begin
match e with
| GStart ->
""
| GEnd ->
""
| GResults (running_time, results, test_case_count) ->
let separator1 = String.make (Format.get_margin ()) '=' in
let separator2 = String.make (Format.get_margin ()) '-' in
let buf = Buffer.create 1024 in
let bprintf fmt = Printf.bprintf buf fmt in
let print_results =
List.iter
(fun result ->
bprintf "%s\n%s: %s\n\n%s\n%s\n"
separator1
(result_flavour result)
(string_of_path (result_path result))
(result_msg result)
separator2)
in
let errors = List.filter is_error results in
let failures = List.filter is_failure results in
let skips = List.filter is_skip results in
let todos = List.filter is_todo results in
if not verbose then
bprintf "\n";
print_results errors;
print_results failures;
bprintf "Ran: %d tests in: %.2f seconds.\n"
(List.length results) running_time;
(* Print final verdict *)
if was_successful results then
begin
if skips = [] then
bprintf "OK"
else
bprintf "OK: Cases: %d Skip: %d"
test_case_count (List.length skips)
end
else
begin
bprintf
"FAILED: Cases: %d Tried: %d Errors: %d \
Failures: %d Skip:%d Todo:%d"
test_case_count (List.length results)
(List.length errors) (List.length failures)
(List.length skips) (List.length todos);
end;
bprintf "\n";
Buffer.contents buf
end
| TestEvent e ->
begin
let string_of_result =
if verbose then
function
| RSuccess _ -> "ok\n"
| RFailure (_, _) -> "FAIL\n"
| RError (_, _) -> "ERROR\n"
| RSkip (_, _) -> "SKIP\n"
| RTodo (_, _) -> "TODO\n"
else
function
| RSuccess _ -> "."
| RFailure (_, _) -> "F"
| RError (_, _) -> "E"
| RSkip (_, _) -> "S"
| RTodo (_, _) -> "T"
in
if verbose then
match e with
| EStart p ->
Printf.sprintf "%s start\n" (string_of_path p)
| EEnd p ->
Printf.sprintf "%s end\n" (string_of_path p)
| EResult result ->
string_of_result result
| ELog (lvl, str) ->
let prefix =
match lvl with
| LError -> "E"
| LWarning -> "W"
| LInfo -> "I"
in
prefix^": "^str
| ELogRaw str ->
str
else
match e with
| EStart _ | EEnd _ | ELog _ | ELogRaw _ -> ""
| EResult result -> string_of_result result
end
let file_logger fn =
let chn = open_out fn in
(fun ev ->
output_string chn (format_event true ev);
flush chn),
(fun () -> close_out chn)
let std_logger verbose =
(fun ev ->
print_string (format_event verbose ev);
flush stdout),
(fun () -> ())
let null_logger =
ignore, ignore
let create output_file_opt verbose (log,close) =
let std_log, std_close = std_logger verbose in
let file_log, file_close =
match output_file_opt with
| Some fn ->
file_logger fn
| None ->
null_logger
in
(fun ev ->
std_log ev; file_log ev; log ev),
(fun () ->
std_close (); file_close (); close ())
let printf log fmt =
Printf.ksprintf
(fun s ->
log (TestEvent (ELogRaw s)))
fmt