Add CLI options to execute different test suites
[tiger.ml.git] / compiler / src / lib / tiger / tiger_test.ml
CommitLineData
e69e4e8b
SK
1(* "exe" is for status of execution (whether any exceptions were raised)
2 * "out" is for status of output comparison (whether what was outputted is
3 * what was expected)
4 *
d3bdde4b
SK
5 * code |> pass_a_exe |> pass_a_out |> ... |> pass_z_exe |> pass_z_out
6 *
7 * pass a:
8 * exe: OK
9 * out: n/a
10 * pass b:
11 * exe: OK
12 * out: OK
13 * pass c:
14 * exe: OK
15 * out: ERROR
16 * ...
17 *
18 * name | pass a | ... | pass z
19 * ---------+--------+-----+--------
20 * exe foo | OK | ... | OK
21 * out foo | OK | ... | ERROR
22 *
23 * *)
1cf4e513 24(* TODO: Perhaps a global option whether to print non-fail info? *)
d3bdde4b
SK
25
26open Printf
27
28module List = ListLabels
29module String = StringLabels
30
31module Option : sig
32 type 'a t = 'a option
33
34 val map : 'a t -> ('a -> 'b) -> 'b t
35end = struct
36 type 'a t = 'a option
37
38 let map t f =
39 match t with
40 | None -> None
41 | Some x -> Some (f x)
42end
43
38ffcb1f 44(* TODO: ~expect:Output of 'a | Exception of (exn -> bool) *)
d3bdde4b
SK
45type t =
46 { name : string
47 ; code : string
48 ; out_lexing : (Tiger_parser.token list) option
49 ; out_parsing : Tiger_absyn.t option
89037894 50 ; is_error_expected_parsing : (Tiger_error.t -> bool) option
38ffcb1f 51 ; is_error_expected_semant : (Tiger_error.t -> bool) option
d3bdde4b
SK
52 }
53
b53d50d3
SK
54type status =
55 | Pass
56 | Fail
57 | Skip
58
59type 'a t_result =
60 { exe_stat : status
61 ; exe_msg : string
62 ; out_stat : status
63 ; out_val : 'a option
64 ; out_msg : string
65 }
66
d3bdde4b
SK
67type color =
68 | Red
b53d50d3 69 | Red_bold
b53d50d3
SK
70 | Green_bold
71 | Grey_bold
d3bdde4b
SK
72
73
74let color_to_ansi_code = function
b53d50d3
SK
75 | Grey_bold -> "\027[1;30m"
76 | Red -> "\027[0;31m"
77 | Red_bold -> "\027[1;31m"
b53d50d3
SK
78 | Green_bold -> "\027[1;32m"
79
80let color_off = "\027[0m"
d3bdde4b
SK
81
82let color color string =
83 let color_on = color_to_ansi_code color in
d3bdde4b
SK
84 sprintf "%s%s%s" color_on string color_off
85
1cf4e513 86let color_opt str = function
b53d50d3
SK
87 | Some c -> (color_to_ansi_code c) ^ str ^ color_off
88 | None -> str
89
90let status_to_color = function
91 | Pass -> Some Green_bold
92 | Fail -> Some Red_bold
93 | Skip -> Some Grey_bold
94
95let status_to_str = function
96 (* Expected to be a single character, but using string to allow unicode. *)
97 | Pass -> "✓"
98 | Fail -> "X"
99 | Skip -> "-"
100
e69e4e8b 101let case
38ffcb1f
SK
102 ?(out_lexing=None)
103 ?(out_parsing=None)
89037894 104 ?(is_error_expected_parsing=None)
38ffcb1f 105 ?(is_error_expected_semant=None)
e69e4e8b
SK
106 ~code
107 name
108 =
d3bdde4b
SK
109 { name
110 ; code
111 ; out_lexing
112 ; out_parsing
89037894 113 ; is_error_expected_parsing
5da420a8 114 ; is_error_expected_semant
d3bdde4b
SK
115 }
116
b53d50d3
SK
117let bar_horiz_minor = color Grey_bold (String.make 80 '-')
118let bar_horiz_major = color Grey_bold (String.make 80 '=')
119let bar_vert = color Grey_bold "|"
d3bdde4b 120
38ffcb1f
SK
121let lexbuf_set_filename lb filename
122: unit
123=
124 let Lexing.({lex_start_p; lex_curr_p; _}) = lb in
125 lb.Lexing.lex_start_p <- {lex_start_p with Lexing.pos_fname = filename};
126 lb.Lexing.lex_curr_p <- {lex_curr_p with Lexing.pos_fname = filename}
127
128let lexbuf_create ~filename ~code =
129 let lb = Lexing.from_string code in
130 lexbuf_set_filename lb filename;
131 lb
132
133let pass_lexing ~fake_filename ~code
134: (Tiger_parser.token list, string) result
135=
136 let lexbuf = lexbuf_create ~filename:fake_filename ~code in
d3bdde4b
SK
137 let rec tokens () =
138 let token = Tiger_lexer.token lexbuf in
139 (* Avoiding fragile pattern-matching *)
140 if token = Tiger_parser.EOF then [] else token :: tokens ()
141 in
142 match tokens () with
143 | exception e -> Error (Printexc.to_string e)
144 | tokens -> Ok tokens
145
38ffcb1f
SK
146let pass_parsing ~fake_filename ~code
147: (Tiger_absyn.t, string) result
148=
149 let lb = lexbuf_create ~filename:fake_filename ~code in
d3bdde4b
SK
150 match Tiger_parser.program Tiger_lexer.token lb with
151 | exception Parsing.Parse_error ->
152 let module L = Lexing in
153 let L.({lex_curr_p = {pos_lnum=l; pos_bol=b; pos_cnum=c; _}; _}) = lb in
154 let msg = sprintf "Syntax error around line: %d, column: %d" l (c - b) in
155 Error msg
156 | ast ->
157 Ok ast
158
217e9638 159let pass_semant (absyn : Tiger_absyn.t)
38ffcb1f
SK
160: (unit, string) result
161=
217e9638 162 Ok (Tiger_semant.transProg absyn)
ea3f5e0c 163
b53d50d3
SK
164let str_exact str exact =
165 let len = String.length str in
166 let take = if len > exact then exact else len in
8e47ed20 167 let str = String.sub str ~pos:0 ~len:take in
b53d50d3
SK
168 let pad = exact - take in
169 let pad = String.make pad ' ' in
170 str ^ pad
171
d3bdde4b
SK
172let s = sprintf
173let p = printf
174let p_ln = print_newline
d3bdde4b
SK
175
176let run tests =
b53d50d3
SK
177 Printexc.record_backtrace true;
178 let count_fail_all = ref 0 in
38ffcb1f
SK
179 let run_pass ~f ~expect_output ~is_error_expected =
180 let is_error_expected =
181 match is_error_expected with
182 | None -> (fun _ -> false)
183 | Some f -> f
184 in
38ffcb1f 185 match f () with
d3bdde4b 186 | exception e ->
b53d50d3
SK
187 let backtrace = Printexc.get_backtrace () in
188 let (exe_stat, exe_msg) =
e69e4e8b
SK
189 (match e with
190 | Tiger_error.T e when is_error_expected e ->
b53d50d3 191 (Pass, (Tiger_error.to_string e))
e69e4e8b 192 | Tiger_error.T e ->
b53d50d3
SK
193 incr count_fail_all;
194 (Fail, (Tiger_error.to_string e))
e69e4e8b 195 | e ->
b53d50d3
SK
196 incr count_fail_all;
197 (Fail, (Printexc.to_string e))
e69e4e8b
SK
198 )
199 in
b53d50d3
SK
200 { exe_stat
201 ; exe_msg = s "\n\tException: %s.\n\tBacktrace: %s" exe_msg backtrace
202 ; out_stat = Skip
203 ; out_val = None
204 ; out_msg = "" (* old "info" goes here *)
205 }
39dd0869 206 | Error info ->
b53d50d3
SK
207 incr count_fail_all;
208 { exe_stat = Fail
209 ; exe_msg = info
210 ; out_stat = Skip
211 ; out_val = None
212 ; out_msg = "" (* old "info" goes here *)
213 }
d3bdde4b 214 | Ok produced ->
b53d50d3 215 let (out_stat, out_msg) =
d3bdde4b 216 match
e69e4e8b 217 Option.map expect_output (fun expected -> expected = produced)
d3bdde4b
SK
218 with
219 | None ->
b53d50d3 220 (Skip, "expected output not provided")
d3bdde4b 221 | Some true ->
b53d50d3 222 (Pass, "")
d3bdde4b 223 | Some false ->
b53d50d3
SK
224 incr count_fail_all;
225 (* TODO pretty print expected and produced *)
226 (Fail, "unexpected output")
d3bdde4b 227 in
b53d50d3
SK
228 { exe_stat = Pass
229 ; exe_msg = "" (* old "info" goes here *)
230 ; out_stat
231 ; out_val = Some produced
232 ; out_msg
233 }
d3bdde4b 234 in
0f031bf2 235 let test_case_count = ref 0 in
b53d50d3 236 let col_1_width = 25 in
b53d50d3
SK
237 let p_stat width (exe, out) =
238 (* All this gymnastics to ignore color codes in cell width *)
239 let min = 5 in
240 let width = if width > min then width else min in
8e47ed20 241 p "%s" (String.concat ~sep:"" (List.init ~len:width ~f:(function
b53d50d3
SK
242 | 0 -> " "
243 | 1 -> bar_vert
244 | 2 -> " "
1cf4e513
SK
245 | 3 -> color_opt (status_to_str exe) (status_to_color exe)
246 | 4 -> color_opt (status_to_str out) (status_to_color out)
b53d50d3
SK
247 | _ -> " "
248 )))
249
250 in
251 p "%s" bar_horiz_major; p_ln ();
252 p "%s" (str_exact "Test case" col_1_width);
253 List.iter ~f:(fun header -> p " %s %s" bar_vert header)
254 [ "Lexing"
255 ; "Parsing"
256 ; "Semant"
257 ];
258 p_ln ();
259 p "%s" bar_horiz_major; p_ln ();
d3bdde4b 260 List.iter tests ~f:(
5da420a8
SK
261 fun
262 { name
263 ; code
264 ; out_lexing
265 ; out_parsing
89037894 266 ; is_error_expected_parsing
5da420a8
SK
267 ; is_error_expected_semant
268 }
269 ->
0f031bf2 270 incr test_case_count;
b53d50d3 271 let res_lex =
e69e4e8b 272 run_pass
38ffcb1f 273 ~f:(fun () -> pass_lexing ~fake_filename:name ~code)
e69e4e8b 274 ~expect_output:out_lexing
38ffcb1f 275 ~is_error_expected:None
e69e4e8b 276 in
b53d50d3 277 let res_pars =
e69e4e8b 278 run_pass
38ffcb1f 279 ~f:(fun () -> pass_parsing ~fake_filename:name ~code)
e69e4e8b 280 ~expect_output:out_parsing
89037894 281 ~is_error_expected:is_error_expected_parsing
e69e4e8b 282 in
b53d50d3 283 let res_sem =
217e9638
SK
284 (* TODO: Replace this hack with general test-dependency checking *)
285 match res_pars.out_val with
286 | None ->
287 { exe_stat = Skip
288 ; exe_msg = "No AST provided"
289 ; out_stat = Skip
290 ; out_val = None
291 ; out_msg = ""
292 }
293 | Some absyn ->
294 run_pass
295 ~f:(fun () -> pass_semant absyn)
296 ~expect_output:(Some ())
297 ~is_error_expected:is_error_expected_semant
ea3f5e0c 298 in
b53d50d3
SK
299 let results =
300 (* Replacing out_val for type compatibility *)
301 [ "Lexing" , {res_lex with out_val = None}
302 ; "Parsing" , {res_pars with out_val = None}
303 ; "Semant" , {res_sem with out_val = None}
304 ]
305 in
306 if !test_case_count > 1 then (p "%s" bar_horiz_minor; p_ln ());
307 p "%s" (str_exact name col_1_width);
308 List.iter results ~f:(fun (stage, {exe_stat=e; out_stat=o; _}) ->
309 p_stat ((String.length stage) + 3) (e, o)
310 );
311 p_ln ();
312 let printed_error = ref false in
313 List.iter results ~f:(
314 fun (stage, {exe_stat; exe_msg; out_stat; out_msg; _}) ->
315 (match exe_stat with
316 | Pass -> ()
317 | Skip -> ()
318 | Fail ->
319 printed_error := true;
320 p "%s: %s" (color Grey_bold stage) (color Red exe_msg);
321 p_ln ()
322 );
323 (match out_stat with
324 | Pass -> ()
325 | Skip -> ()
326 | Fail ->
327 printed_error := true;
328 p "%s: %s" (color Grey_bold stage) (color Red out_msg)
329 );
330 );
d3bdde4b 331 );
b53d50d3 332 p "%s" bar_horiz_major; p_ln ();
1cf4e513
SK
333 p "%s %d failures in %d test cases"
334 (match !count_fail_all with
335 | 0 -> color_opt (status_to_str Pass) (status_to_color Pass)
336 | _ -> color_opt (status_to_str Fail) (status_to_color Fail)
337 )
338 !count_fail_all
339 !test_case_count;
b762cacb 340 p_ln ();
b53d50d3
SK
341 p "%s" bar_horiz_major; p_ln ();
342 exit !count_fail_all
This page took 0.086291 seconds and 4 git commands to generate.