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