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
5 * code |> pass_a_exe |> pass_a_out |> ... |> pass_z_exe |> pass_z_out
18 * name | pass a | ... | pass z
19 * ---------+--------+-----+--------
20 * exe foo | OK | ... | OK
21 * out foo | OK | ... | ERROR
24 (* TODO: Perhaps a global option whether to print non-fail info? *)
28 module List = ListLabels
29 module String = StringLabels
31 module Opt = Tiger_opt
33 (* TODO: ~expect:Output of 'a | Exception of (exn -> bool) *)
37 ; out_lexing : (Tiger_parser.token list) option
38 ; out_parsing : Tiger_absyn.t option
39 ; is_error_expected_parsing : (Tiger_error.t -> bool) option
40 ; is_error_expected_semant : (Tiger_error.t -> bool) option
63 let color_to_ansi_code = function
64 | Grey_bold -> "\027[1;30m"
66 | Red_bold -> "\027[1;31m"
67 | Green_bold -> "\027[1;32m"
69 let color_off = "\027[0m"
71 let color color string =
72 let color_on = color_to_ansi_code color in
73 sprintf "%s%s%s" color_on string color_off
75 let color_opt str = function
76 | Some c -> (color_to_ansi_code c) ^ str ^ color_off
79 let status_to_color = function
80 | Pass -> Some Green_bold
81 | Fail -> Some Red_bold
82 | Skip -> Some Grey_bold
84 let status_to_str = function
85 (* Expected to be a single character, but using string to allow unicode. *)
93 ?(is_error_expected_parsing=None)
94 ?(is_error_expected_semant=None)
102 ; is_error_expected_parsing
103 ; is_error_expected_semant
106 let bar_horiz_minor = color Grey_bold (String.make 80 '-')
107 let bar_horiz_major = color Grey_bold (String.make 80 '=')
108 let bar_vert = color Grey_bold "|"
110 let lexbuf_set_filename lb filename
113 let Lexing.({lex_start_p; lex_curr_p; _}) = lb in
114 lb.Lexing.lex_start_p <- {lex_start_p with Lexing.pos_fname = filename};
115 lb.Lexing.lex_curr_p <- {lex_curr_p with Lexing.pos_fname = filename}
117 let lexbuf_create ~filename ~code =
118 let lb = Lexing.from_string code in
119 lexbuf_set_filename lb filename;
122 let pass_lexing ~fake_filename ~code
123 : (Tiger_parser.token list, string) result
125 let lexbuf = lexbuf_create ~filename:fake_filename ~code in
127 let token = Tiger_lexer.token lexbuf in
128 (* Avoiding fragile pattern-matching *)
129 if token = Tiger_parser.EOF then [] else token :: tokens ()
132 | exception e -> Error (Printexc.to_string e)
133 | tokens -> Ok tokens
135 let pass_parsing ~fake_filename ~code
136 : (Tiger_absyn.t, string) result
138 let lb = lexbuf_create ~filename:fake_filename ~code in
139 match Tiger_parser.program Tiger_lexer.token lb with
140 | exception Parsing.Parse_error ->
141 let module L = Lexing in
142 let L.({lex_curr_p = {pos_lnum=l; pos_bol=b; pos_cnum=c; _}; _}) = lb in
143 let msg = sprintf "Syntax error around line: %d, column: %d" l (c - b) in
148 let pass_semant (absyn : Tiger_absyn.t)
149 : (unit, string) result
151 Ok (Tiger_semant.transProg absyn)
153 let str_exact str exact =
154 let len = String.length str in
155 let take = if len > exact then exact else len in
156 let str = String.sub str ~pos:0 ~len:take in
157 let pad = exact - take in
158 let pad = String.make pad ' ' in
163 let p_ln = print_newline
166 Printexc.record_backtrace true;
167 let count_fail_all = ref 0 in
168 let run_pass ~f ~expect_output ~is_error_expected =
169 let is_error_expected =
170 match is_error_expected with
171 | None -> (fun _ -> false)
176 let backtrace = Printexc.get_backtrace () in
177 let (exe_stat, exe_msg) =
179 | Tiger_error.T e when is_error_expected e ->
180 (Pass, (Tiger_error.to_string e))
183 (Fail, (Tiger_error.to_string e))
186 (Fail, (Printexc.to_string e))
190 ; exe_msg = s "\n\tException: %s.\n\tBacktrace: %s" exe_msg backtrace
193 ; out_msg = "" (* old "info" goes here *)
201 ; out_msg = "" (* old "info" goes here *)
204 let (out_stat, out_msg) =
206 Opt.map expect_output (fun expected -> expected = produced)
209 (Skip, "expected output not provided")
214 (* TODO pretty print expected and produced *)
215 (Fail, "unexpected output")
218 ; exe_msg = "" (* old "info" goes here *)
220 ; out_val = Some produced
224 let test_case_count = ref 0 in
225 let col_1_width = 30 in
226 let p_stat width (exe, out) =
227 (* All this gymnastics to ignore color codes in cell width *)
229 let width = if width > min then width else min in
230 p "%s" (String.concat ~sep:"" (List.init ~len:width ~f:(function
234 | 3 -> color_opt (status_to_str exe) (status_to_color exe)
235 | 4 -> color_opt (status_to_str out) (status_to_color out)
240 p "%s" bar_horiz_major; p_ln ();
241 p "%s" (str_exact "Test case" col_1_width);
242 List.iter ~f:(fun header -> p " %s %s" bar_vert header)
248 p "%s" bar_horiz_major; p_ln ();
255 ; is_error_expected_parsing
256 ; is_error_expected_semant
259 incr test_case_count;
262 ~f:(fun () -> pass_lexing ~fake_filename:name ~code)
263 ~expect_output:out_lexing
264 ~is_error_expected:None
268 ~f:(fun () -> pass_parsing ~fake_filename:name ~code)
269 ~expect_output:out_parsing
270 ~is_error_expected:is_error_expected_parsing
273 (* TODO: Replace this hack with general test-dependency checking *)
274 match res_pars.out_val with
277 ; exe_msg = "No AST provided"
284 ~f:(fun () -> pass_semant absyn)
285 ~expect_output:(Some ())
286 ~is_error_expected:is_error_expected_semant
289 (* Replacing out_val for type compatibility *)
290 [ "Lexing" , {res_lex with out_val = None}
291 ; "Parsing" , {res_pars with out_val = None}
292 ; "Semant" , {res_sem with out_val = None}
295 if !test_case_count > 1 then (p "%s" bar_horiz_minor; p_ln ());
296 p "%s" (str_exact name col_1_width);
297 List.iter results ~f:(fun (stage, {exe_stat=e; out_stat=o; _}) ->
298 p_stat ((String.length stage) + 3) (e, o)
301 let printed_error = ref false in
302 List.iter results ~f:(
303 fun (stage, {exe_stat; exe_msg; out_stat; out_msg; _}) ->
308 printed_error := true;
309 p "%s: %s" (color Grey_bold stage) (color Red exe_msg);
316 printed_error := true;
317 p "%s: %s" (color Grey_bold stage) (color Red out_msg)
321 p "%s" bar_horiz_major; p_ln ();
322 p "%s %d failures in %d test cases"
323 (match !count_fail_all with
324 | 0 -> color_opt (status_to_str Pass) (status_to_color Pass)
325 | _ -> color_opt (status_to_str Fail) (status_to_color Fail)
330 p "%s" bar_horiz_major; p_ln ();