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 Err = Tiger_error
32 module Opt = Tiger_opt
34 (* TODO: ~expect:Output of 'a | Exception of (exn -> bool) *)
38 ; out_lexing : (Tiger_parser.token list) option
39 ; out_parsing : Tiger_absyn.t option
40 ; is_error_expected_parsing : (Tiger_error.t -> bool) option
41 ; is_error_expected_semant : (Tiger_error.t -> bool) option
64 let color_to_ansi_code = function
65 | Grey_bold -> "\027[1;30m"
67 | Red_bold -> "\027[1;31m"
68 | Green_bold -> "\027[1;32m"
70 let color_off = "\027[0m"
72 let color color string =
73 let color_on = color_to_ansi_code color in
74 sprintf "%s%s%s" color_on string color_off
76 let color_opt str = function
77 | Some c -> (color_to_ansi_code c) ^ str ^ color_off
80 let status_to_color = function
81 | Pass -> Some Green_bold
82 | Fail -> Some Red_bold
83 | Skip -> Some Grey_bold
85 let status_to_str = function
86 (* Expected to be a single character, but using string to allow unicode. *)
94 ?(is_error_expected_parsing=None)
95 ?(is_error_expected_semant=None)
103 ; is_error_expected_parsing
104 ; is_error_expected_semant
107 let bar_horiz_minor = color Grey_bold (String.make 80 '-')
108 let bar_horiz_major = color Grey_bold (String.make 80 '=')
109 let bar_vert = color Grey_bold "|"
111 let lexbuf_set_filename lb filename
114 let Lexing.({lex_start_p; lex_curr_p; _}) = lb in
115 lb.Lexing.lex_start_p <- {lex_start_p with Lexing.pos_fname = filename};
116 lb.Lexing.lex_curr_p <- {lex_curr_p with Lexing.pos_fname = filename}
118 let lexbuf_create ~filename ~code =
119 let lb = Lexing.from_string code in
120 lexbuf_set_filename lb filename;
123 let pass_lexing ~fake_filename ~code
124 : Tiger_parser.token list
126 let lexbuf = lexbuf_create ~filename:fake_filename ~code in
128 let token = Tiger_lexer.token lexbuf in
129 (* Avoiding fragile pattern-matching *)
130 if token = Tiger_parser.EOF then [] else token :: tokens ()
134 let pass_parsing ~fake_filename ~code
139 (lexbuf_create ~filename:fake_filename ~code)
141 let pass_semant (absyn : Tiger_absyn.t)
144 Tiger_semant.transProg absyn
146 let str_exact str exact =
147 let len = String.length str in
148 let take = if len > exact then exact else len in
149 let str = String.sub str ~pos:0 ~len:take in
150 let pad = exact - take in
151 let pad = String.make pad ' ' in
154 let exn_to_string = function
155 | Tiger_error.T e -> Tiger_error.to_string e
156 | e -> Printexc.to_string e
160 let p_ln = print_newline
163 Printexc.record_backtrace true;
164 let fail, fail_count =
165 let count_fail_all = ref 0 in
166 ( (fun () -> incr count_fail_all; Fail)
167 , (fun () -> !count_fail_all)
170 let run_pass ~f ~expect_output ~is_error_expected =
171 let execution = match f () with exception e -> `Exn e | o -> `Out o in
172 (match execution, is_error_expected with
173 | `Exn (Err.T e), Some is_error_expected when is_error_expected e ->
182 let b = Printexc.get_backtrace () in
183 let e = exn_to_string e in
185 ; exe_msg = s "\n\tException: %s.\n\tBacktrace: %s" e b
190 | `Out output, Some _ ->
192 ; exe_msg = "Expected exception, but got output."
194 ; out_val = Some output (* TODO: Do we really want to keep going? *)
195 ; out_msg = "Expected exception, but got output."
197 | `Out output, None ->
198 let (out_stat, out_msg) =
200 Opt.map expect_output (fun expected -> expected = output)
203 (Skip, "expected output not provided")
207 (* TODO pretty print expected and output *)
208 (fail (), "unexpected output")
211 ; exe_msg = "" (* old "info" goes here *)
213 ; out_val = Some output
218 let test_case_count = ref 0 in
219 let col_1_width = 30 in
220 let p_stat width (exe, out) =
221 (* All this gymnastics to ignore color codes in cell width *)
223 let width = if width > min then width else min in
224 p "%s" (String.concat ~sep:"" (List.init ~len:width ~f:(function
228 | 3 -> color_opt (status_to_str exe) (status_to_color exe)
229 | 4 -> color_opt (status_to_str out) (status_to_color out)
234 p "%s" bar_horiz_major; p_ln ();
235 p "%s" (str_exact "Test case" col_1_width);
236 List.iter ~f:(fun header -> p " %s %s" bar_vert header)
242 p "%s" bar_horiz_major; p_ln ();
249 ; is_error_expected_parsing
250 ; is_error_expected_semant
253 incr test_case_count;
256 ~f:(fun () -> pass_lexing ~fake_filename:name ~code)
257 ~expect_output:out_lexing
258 ~is_error_expected:None
262 ~f:(fun () -> pass_parsing ~fake_filename:name ~code)
263 ~expect_output:out_parsing
264 ~is_error_expected:is_error_expected_parsing
267 (* TODO: Replace this hack with general test-dependency checking *)
268 match res_pars.out_val with
271 ; exe_msg = "No AST provided"
278 ~f:(fun () -> pass_semant absyn)
279 ~expect_output:(Some ())
280 ~is_error_expected:is_error_expected_semant
283 (* Replacing out_val for type compatibility *)
284 [ "Lexing" , {res_lex with out_val = None}
285 ; "Parsing" , {res_pars with out_val = None}
286 ; "Semant" , {res_sem with out_val = None}
289 if !test_case_count > 1 then (p "%s" bar_horiz_minor; p_ln ());
290 p "%s" (str_exact name col_1_width);
291 List.iter results ~f:(fun (stage, {exe_stat=e; out_stat=o; _}) ->
292 p_stat ((String.length stage) + 3) (e, o)
295 let printed_error = ref false in
296 List.iter results ~f:(
297 fun (stage, {exe_stat; exe_msg; out_stat; out_msg; _}) ->
302 printed_error := true;
303 p "%s: %s" (color Grey_bold stage) (color Red exe_msg);
310 printed_error := true;
311 p "%s: %s" (color Grey_bold stage) (color Red out_msg)
315 p "%s" bar_horiz_major; p_ln ();
316 p "%s %d failures in %d test cases"
317 (match fail_count () with
318 | 0 -> color_opt (status_to_str Pass) (status_to_color Pass)
319 | _ -> color_opt (status_to_str Fail) (status_to_color Fail)
324 p "%s" bar_horiz_major; p_ln ();