Disable info for non-fail status
[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
38ffcb1f 49 ; is_error_expected_semant : (Tiger_error.t -> bool) option
d3bdde4b
SK
50 }
51
52type color =
53 | Red
54 | Yellow
55 | Green
56
57
58let color_to_ansi_code = function
59 | Red -> "\027[0;31m"
60 | Yellow -> "\027[0;33m"
61 | Green -> "\027[0;32m"
62
63let color color string =
64 let color_on = color_to_ansi_code color in
65 let color_off = "\027[0m" in
66 sprintf "%s%s%s" color_on string color_off
67
39dd0869
SK
68let status indicator info =
69 match info with
70 | "" -> indicator
71 | _ -> sprintf "%s: %s" indicator info
72
fca3442c 73(* TODO: Perhaps a global option whether to print non-fail info? *)
9949f15b
SK
74let status_pass ?(info="") () =
75 status (color Green "Pass") info
39dd0869 76
9949f15b
SK
77let status_fail ?(info="") () =
78 status (color Red "Fail") info
39dd0869 79
9949f15b
SK
80let status_skip ?(info="") () =
81 status (color Yellow "Skip") info
39dd0869 82
e69e4e8b 83let case
38ffcb1f
SK
84 ?(out_lexing=None)
85 ?(out_parsing=None)
86 ?(is_error_expected_semant=None)
e69e4e8b
SK
87 ~code
88 name
89 =
d3bdde4b
SK
90 { name
91 ; code
92 ; out_lexing
93 ; out_parsing
5da420a8 94 ; is_error_expected_semant
d3bdde4b
SK
95 }
96
97let bar_sep = String.make 80 '-'
98let bar_end = String.make 80 '='
99
100let indent =
101 let unit_spaces = 2 in
102 fun n ->
103 String.make (n * unit_spaces) ' '
104
38ffcb1f
SK
105let lexbuf_set_filename lb filename
106: unit
107=
108 let Lexing.({lex_start_p; lex_curr_p; _}) = lb in
109 lb.Lexing.lex_start_p <- {lex_start_p with Lexing.pos_fname = filename};
110 lb.Lexing.lex_curr_p <- {lex_curr_p with Lexing.pos_fname = filename}
111
112let lexbuf_create ~filename ~code =
113 let lb = Lexing.from_string code in
114 lexbuf_set_filename lb filename;
115 lb
116
117let pass_lexing ~fake_filename ~code
118: (Tiger_parser.token list, string) result
119=
120 let lexbuf = lexbuf_create ~filename:fake_filename ~code in
d3bdde4b
SK
121 let rec tokens () =
122 let token = Tiger_lexer.token lexbuf in
123 (* Avoiding fragile pattern-matching *)
124 if token = Tiger_parser.EOF then [] else token :: tokens ()
125 in
126 match tokens () with
127 | exception e -> Error (Printexc.to_string e)
128 | tokens -> Ok tokens
129
38ffcb1f
SK
130let pass_parsing ~fake_filename ~code
131: (Tiger_absyn.t, string) result
132=
133 let lb = lexbuf_create ~filename:fake_filename ~code in
d3bdde4b
SK
134 match Tiger_parser.program Tiger_lexer.token lb with
135 | exception Parsing.Parse_error ->
136 let module L = Lexing in
137 let L.({lex_curr_p = {pos_lnum=l; pos_bol=b; pos_cnum=c; _}; _}) = lb in
138 let msg = sprintf "Syntax error around line: %d, column: %d" l (c - b) in
139 Error msg
140 | ast ->
141 Ok ast
142
38ffcb1f
SK
143let pass_semant (absyn_opt : Tiger_absyn.t option)
144: (unit, string) result
145=
ea3f5e0c
SK
146 match absyn_opt with
147 | None ->
148 Error "AST not provided"
149 | Some absyn ->
150 Ok (Tiger_semant.transProg absyn)
151
d3bdde4b
SK
152let s = sprintf
153let p = printf
154let p_ln = print_newline
155let p_indent n = p "%s" (indent n)
156
157let run tests =
b762cacb 158 let failure_count = ref 0 in
38ffcb1f
SK
159 let run_pass ~f ~expect_output ~is_error_expected =
160 let is_error_expected =
161 match is_error_expected with
162 | None -> (fun _ -> false)
163 | Some f -> f
164 in
e69e4e8b
SK
165 let output_status = "n/a" in
166 let output_value = None in
38ffcb1f 167 match f () with
d3bdde4b 168 | exception e ->
39dd0869 169 let execution_status =
e69e4e8b
SK
170 (match e with
171 | Tiger_error.T e when is_error_expected e ->
fca3442c 172 status_pass () (*~info:(Tiger_error.to_string e)*)
e69e4e8b 173 | Tiger_error.T e ->
b762cacb 174 incr failure_count;
9949f15b 175 status_fail () ~info:(Tiger_error.to_string e)
e69e4e8b 176 | e ->
b762cacb 177 incr failure_count;
9949f15b 178 status_fail () ~info:(Printexc.to_string e)
e69e4e8b
SK
179 )
180 in
39dd0869 181 ( execution_status
e69e4e8b
SK
182 , output_status
183 , output_value
d3bdde4b 184 )
39dd0869 185 | Error info ->
b762cacb 186 incr failure_count;
9949f15b 187 ( status_fail ~info ()
e69e4e8b
SK
188 , output_status
189 , output_value
d3bdde4b
SK
190 )
191 | Ok produced ->
9949f15b 192 let execution_status = status_pass () in
e69e4e8b 193 let output_status =
d3bdde4b 194 match
e69e4e8b 195 Option.map expect_output (fun expected -> expected = produced)
d3bdde4b
SK
196 with
197 | None ->
fca3442c 198 status_skip () (*~info:"expected output not provided"*)
d3bdde4b 199 | Some true ->
9949f15b 200 status_pass ()
d3bdde4b 201 | Some false ->
b762cacb 202 incr failure_count;
9949f15b 203 status_fail ()
d3bdde4b 204 in
e69e4e8b
SK
205 let output_value = Some produced in
206 (execution_status, output_status, output_value)
d3bdde4b 207 in
0f031bf2 208 let test_case_count = ref 0 in
d3bdde4b 209 List.iter tests ~f:(
5da420a8
SK
210 fun
211 { name
212 ; code
213 ; out_lexing
214 ; out_parsing
215 ; is_error_expected_semant
216 }
217 ->
0f031bf2 218 incr test_case_count;
e69e4e8b
SK
219 let (stat_lex_exe, stat_lex_out_cmp, _) =
220 run_pass
38ffcb1f 221 ~f:(fun () -> pass_lexing ~fake_filename:name ~code)
e69e4e8b 222 ~expect_output:out_lexing
38ffcb1f 223 ~is_error_expected:None
e69e4e8b 224 in
ea3f5e0c 225 let (stat_pars_exe, stat_pars_out_cmp, absyn_opt) =
e69e4e8b 226 run_pass
38ffcb1f 227 ~f:(fun () -> pass_parsing ~fake_filename:name ~code)
e69e4e8b 228 ~expect_output:out_parsing
38ffcb1f 229 ~is_error_expected:None
e69e4e8b 230 in
ea3f5e0c
SK
231 let (stat_semant_exe, stat_semant_out_cmp, _) =
232 run_pass
38ffcb1f 233 ~f:(fun () -> pass_semant absyn_opt)
ea3f5e0c 234 ~expect_output:(Some ())
5da420a8 235 ~is_error_expected:is_error_expected_semant
ea3f5e0c 236 in
d3bdde4b
SK
237 p "%s" bar_sep; p_ln ();
238 p "Test: %S" name; p_ln ();
239 p_indent 1; p "Lexing:"; p_ln ();
e69e4e8b
SK
240 p_indent 2; p "exe: %s" stat_lex_exe ; p_ln ();
241 p_indent 2; p "out: %s" stat_lex_out_cmp; p_ln ();
d3bdde4b 242 p_indent 1; p "Parsing:"; p_ln ();
e69e4e8b
SK
243 p_indent 2; p "exe: %s" stat_pars_exe ; p_ln ();
244 p_indent 2; p "out: %s" stat_pars_out_cmp; p_ln ();
ea3f5e0c
SK
245 p_indent 1; p "Semantic Analysis:"; p_ln ();
246 p_indent 2; p "exe: %s" stat_semant_exe ; p_ln ();
247 p_indent 2; p "out: %s" stat_semant_out_cmp; p_ln ();
d3bdde4b
SK
248 );
249 p "%s" bar_end; p_ln ();
b762cacb 250 p "%s"
0f031bf2
SK
251 ( let info =
252 s "%d failures in %d test cases" !failure_count !test_case_count
253 in
254 match !failure_count with
255 | 0 -> status_pass () ~info
256 | _ -> status_fail () ~info
b762cacb
SK
257 );
258 p_ln ();
d3bdde4b 259 p "%s" bar_end; p_ln ();
b762cacb 260 exit !failure_count
This page took 0.044217 seconds and 4 git commands to generate.