Debian Codegraph @

test-suite/unit-tests/printing/proof_diffs_test.ml

raw symbols SCIP
1 open OUnit
2 open Utest
3 open Pp_diff
4 open Proof_diffs
5
6 (* Needed to be able to set through goptions *)
7 let () =
8 let open Names in
9 Lib.start_compilation DirPath.dummy (ModPath.MPfile DirPath.dummy)
10
11 let tokenize_string = Proof_diffs.tokenize_string
12 let diff_pp = diff_pp ~tokenize_string
13 let diff_str = diff_str ~tokenize_string
14
15 let tests = ref []
16 let add_test name test = tests := (mk_test name (TestCase test)) :: !tests
17
18 let log_out_ch = open_log_out_ch __FILE__
19 let cfprintf oc = Printf.(kfprintf (fun oc -> fprintf oc "") oc)
20 let cprintf s = cfprintf log_out_ch s
21 let _ = Proof_diffs.log_out_ch := log_out_ch
22
23 let string_of_string s : string = "\"" ^ s ^ "\""
24
25 (* todo: OCaml: why can't the body of the test function be given in the add_test line? *)
26
27 let t () =
28 let expected : diff_list = [] in
29 let diffs = diff_str "" " " in
30
31 assert_equal ~msg:"empty" ~printer:string_of_diffs expected diffs;
32 let (has_added, has_removed) = has_changes diffs in
33 assert_equal ~msg:"has `Added" ~printer:string_of_bool false has_added;
34 assert_equal ~msg:"has `Removed" ~printer:string_of_bool false has_removed
35 let _ = add_test "diff_str empty" t
36
37
38 let t () =
39 let expected : diff_list =
40 [ `Common (0, 0, "a"); `Common (1, 1, "b"); `Common (2, 2, "c")] in
41 let diffs = diff_str "a b c" " a b\t c\n" in
42
43 assert_equal ~msg:"white space" ~printer:string_of_diffs expected diffs;
44 let (has_added, has_removed) = has_changes diffs in
45 assert_equal ~msg:"no `Added" ~printer:string_of_bool false has_added;
46 assert_equal ~msg:"no `Removed" ~printer:string_of_bool false has_removed
47 let _ = add_test "diff_str white space" t
48
49 let t () =
50 let expected : diff_list = [ `Removed (0, "a"); `Added (0, "b")] in
51 let diffs = diff_str "a" "b" in
52
53 assert_equal ~msg:"add/remove" ~printer:string_of_diffs expected diffs;
54 let (has_added, has_removed) = has_changes diffs in
55 assert_equal ~msg:"has `Added" ~printer:string_of_bool true has_added;
56 assert_equal ~msg:"has `Removed" ~printer:string_of_bool true has_removed
57 let _ = add_test "diff_str add/remove" t
58
59 (* lexer tweaks:
60 comments are lexed as multiple tokens
61 strings tokens include begin/end quotes and embedded ""
62 single multibyte characters returned even if they're not keywords
63
64 inputs that give a lexer failure (but no use case needs them yet):
65 ".12"
66 unterminated string
67 invalid UTF-8 sequences
68 *)
69 let t () =
70 let str = "(* comment.field *) ?id () \"str\"\"ing\" \\ := Ж > ∃ 'c' xx" in
71 let toks = tokenize_string str in
72 (*List.iter (fun x -> cprintf "'%s' " x) toks;*)
73 (*cprintf "\n";*)
74 let str_no_white = String.concat "" (String.split_on_char ' ' str) in
75 assert_equal ~printer:(fun x -> x) str_no_white (String.concat "" toks);
76 List.iter (fun s ->
77 assert_equal ~msg:("'" ^ s ^ "' is a single token") ~printer:string_of_bool true (List.mem s toks))
78 [ "(*"; "()"; ":="]
79
80 let _ = add_test "tokenize_string/diff_mode in lexer" t
81
82 open Pp
83
84 let write_diffs_option s =
85 Goptions.set_string_option_value Proof_diffs.opt_name s
86
87 (* example that was failing from #8922 *)
88 let t () =
89 write_diffs_option "removed";
90 ignore (diff_str "X : ?Goal" "X : forall x : ?Goal0, ?Goal1");
91 write_diffs_option "on"
92 let _ = add_test "shorten_diff_span failure from #8922" t
93
94 (* note pp_to_string concatenates adjacent strings, could become one token,
95 e.g. str " a" ++ str "b " will give a token "ab" *)
96 (* checks background is present and correct *)
97 let t () =
98 let o_pp = str "a" ++ str "!" ++ str "c" in
99 let n_pp = str "a" ++ str "?" ++ str "c" in
100 let (o_exp, n_exp) = (wrap_in_bg "diff.removed" (str "a" ++ (tag "diff.removed" (str "!")) ++ str "c"),
101 wrap_in_bg "diff.added" (str "a" ++ (tag "diff.added" (str "?")) ++ str "c")) in
102 let (o_diff, n_diff) = diff_pp o_pp n_pp in
103
104 assert_equal ~msg:"removed" ~printer:db_string_of_pp o_exp o_diff;
105 assert_equal ~msg:"added" ~printer:db_string_of_pp n_exp n_diff
106 let _ = add_test "diff_pp/add_diff_tags add/remove" t
107
108 let t () =
109 (*Printf.printf "%s\n" (string_of_diffs (diff_str "a d" "a b c d"));*)
110 let o_pp = str "a" ++ str " d" in
111 let n_pp = str "a" ++ str " b " ++ str " c " ++ str "d" ++ str " e " in
112 let n_exp = flatten (wrap_in_bg "diff.added" (seq [
113 str "a";
114 str " "; (tag "start.diff.added" (str "b "));
115 (tag "end.diff.added" (str " c")); str " ";
116 (str "d");
117 str " "; (tag "diff.added" (str "e")); str " "
118 ])) in
119 let (_, n_diff) = diff_pp o_pp n_pp in
120
121 assert_equal ~msg:"added" ~printer:db_string_of_pp n_exp (flatten n_diff)
122 let _ = add_test "diff_pp/add_diff_tags a span with spaces" t
123
124
125 let t () =
126 let o_pp = str " " in
127 let n_pp = tag "sometag" (str "a") in
128 let n_exp = flatten (wrap_in_bg "diff.added" (tag "diff.added" (tag "sometag" (str "a")))) in
129 let (_, n_diff) = diff_pp o_pp n_pp in
130
131 assert_equal ~msg:"added" ~printer:db_string_of_pp n_exp (flatten n_diff)
132 let _ = add_test "diff_pp/add_diff_tags diff tags outside existing tags" t
133
134 let t () =
135 let o_pp = str " " in
136 let n_pp = seq [(tag "sometag" (str " a ")); str "b"] in
137 let n_exp = flatten (wrap_in_bg "diff.added"
138 (seq [tag "sometag" (str " "); (tag "start.diff.added" (tag "sometag" (str "a ")));
139 (tag "end.diff.added" (str "b"))]) ) in
140 let (_, n_diff) = diff_pp o_pp n_pp in
141
142 assert_equal ~msg:"added" ~printer:db_string_of_pp n_exp (flatten n_diff)
143 let _ = add_test "diff_pp/add_diff_tags existing tagged values with spaces" t
144
145 let t () =
146 let o_pp = str " " in
147 let n_pp = str " a b " in
148 let n_exp = flatten (wrap_in_bg "diff.added"
149 (seq [str " "; tag "diff.added" (str "a b"); str " "])) in
150 let (_, n_diff) = diff_pp o_pp n_pp in
151
152 assert_equal ~msg:"added" ~printer:db_string_of_pp n_exp (flatten n_diff)
153 let _ = add_test "diff_pp/add_diff_tags multiple tokens in pp" t
154
155 let t () =
156 let o_pp = str "a d" in
157 let n_pp = seq [str "a b"; str "c d"] in
158 let n_exp = flatten (wrap_in_bg "diff.added"
159 (seq [str "a "; tag "start.diff.added" (str "b");
160 tag "end.diff.added" (str "c"); str " d"])) in
161 let (_, n_diff) = diff_pp o_pp n_pp in
162
163 assert_equal ~msg:"added" ~printer:db_string_of_pp n_exp (flatten n_diff)
164 let _ = add_test "diff_pp/add_diff_tags token spanning multiple Ppcmd_strs" t
165
166 let t () =
167 let o_pp = seq [str ""; str "a"] in
168 let n_pp = seq [str ""; str "a b"] in
169 let n_exp = flatten (wrap_in_bg "diff.added"
170 (seq [str ""; str "a "; tag "diff.added" (str "b")])) in
171 let (_, n_diff) = diff_pp o_pp n_pp in
172
173 assert_equal ~msg:"added" ~printer:db_string_of_pp n_exp (flatten n_diff)
174 let _ = add_test "diff_pp/add_diff_tags empty string preserved" t
175
176 (* todo: awaiting a change in the lexer to return the quotes of the string token *)
177 let t () =
178 let s = "\"a b\"" in
179 let o_pp = seq [str s] in
180 let n_pp = seq [str "\"a b\" "] in
181 cprintf "ppcmds: %s\n" (string_of_ppcmds n_pp);
182 let n_exp = flatten (wrap_in_bg "diff.added"
183 (seq [str ""; str "a "; tag "diff.added" (str "b")])) in
184 let (_, n_diff) = diff_pp o_pp n_pp in
185
186 assert_equal ~msg:"string" ~printer:string_of_string "a b" (List.hd (tokenize_string s));
187 assert_equal ~msg:"added" ~printer:db_string_of_pp n_exp (flatten n_diff)
188 let _ = if false then add_test "diff_pp/add_diff_tags token containing white space" t
189
190 let add_entries map idents rhs_pp =
191 let make_entry() = { idents; rhs_pp } in
192 List.iter (fun ident -> map := CString.Map.add ident (make_entry ()) !map) idents
193
194 let print_list hyps = List.iter (fun x -> cprintf "%s\n" (string_of_ppcmds (flatten x))) hyps
195 let db_print_list hyps = List.iter (fun x -> cprintf "%s\n" (db_string_of_pp (flatten x))) hyps
196
197
198 (* a : uint
199 b : int car ->
200 b : car
201 a : uint int
202 DIFFS
203 b : car (remove int)
204 b : car (added bg only)
205 a: uint int (add int)
206 *)
207 let t () =
208 write_diffs_option "removed"; (* turn on "removed" option *)
209 let o_line_idents = [ ["a"]; ["b"]] in
210 let o_hyp_map = ref CString.Map.empty in
211 add_entries o_hyp_map ["a"] (str " : uint");
212 add_entries o_hyp_map ["b"] (str " : int car");
213 let n_line_idents = [ ["b"]; ["a"]] in
214 let n_hyp_map = ref CString.Map.empty in
215 add_entries n_hyp_map ["b"] (str " : car");
216 add_entries n_hyp_map ["a"] (str " : uint int");
217 let expected = [flatten (wrap_in_bg "diff.removed" (seq [str "b"; str " : ";
218 (tag "diff.removed" (str "int")); str " car" ]));
219 flatten (wrap_in_bg "diff.added" (seq [str "b"; str " : car"]));
220 flatten (wrap_in_bg "diff.added" (seq [str "a";
221 str " : uint "; (tag "diff.added" (str "int")) ]))
222 ] in
223
224 let hyps_diff_list = diff_hyps o_line_idents !o_hyp_map n_line_idents !n_hyp_map in
225
226 (*print_list hyps_diff_list;*)
227 (*db_print_list hyps_diff_list;*)
228
229 List.iter2 (fun exp act ->
230 assert_equal ~msg:"added" ~printer:db_string_of_pp exp (flatten act))
231 expected hyps_diff_list
232 let _ = add_test "diff_hyps simple diffs" t
233
234 (* a : nat
235 c, d : int ->
236 a, b : nat
237 d : int
238 DIFFS
239 c, d : int (remove c,)
240 a, b : nat (add ,b)
241 d : int *)
242 let t () =
243 write_diffs_option "removed"; (* turn on "removed" option *)
244 let o_line_idents = [ ["a"]; ["c"; "d"]] in
245 let o_hyp_map = ref CString.Map.empty in
246 add_entries o_hyp_map ["a"] (str " : nat");
247 add_entries o_hyp_map ["c"; "d"] (str " : int");
248 let n_line_idents = [ ["a"; "b"]; ["d"]] in
249 let n_hyp_map = ref CString.Map.empty in
250 add_entries n_hyp_map ["a"; "b"] (str " : nat");
251 add_entries n_hyp_map ["d"] (str " : int");
252 let expected = [flatten (wrap_in_bg "diff.added" (seq [str "a"; (tag "start.diff.added"
253 (str ", ")); (tag "end.diff.added" (str "b")); str " : nat" ]));
254 flatten (wrap_in_bg "diff.removed" (seq [(tag "start.diff.removed"
255 (str "c")); (tag "end.diff.removed" (str ",")); str " "; str "d"; str " : int" ]));
256 flatten (seq [str "d"; str " : int" ])
257 ] in
258
259 let hyps_diff_list = diff_hyps o_line_idents !o_hyp_map n_line_idents !n_hyp_map in
260
261 (*print_list hyps_diff_list;*)
262 (*print_list expected;*)
263
264 (*db_print_list hyps_diff_list;*)
265 (*db_print_list expected;*)
266
267 List.iter2 (fun exp act ->
268 assert_equal ~msg:"added" ~printer:db_string_of_pp exp (flatten act))
269 expected hyps_diff_list
270 let _ = add_test "diff_hyps compacted" t
271
272 (* a : uint
273 b : int
274 c : nat ->
275 b, a, c : nat
276 DIFFS
277 a : uint (remove)
278 b : int (remove)
279 b, a, c : nat (add b, a,)
280 is this a realistic use case?
281 *)
282 let t () =
283 write_diffs_option "removed"; (* turn on "removed" option *)
284 let o_line_idents = [ ["a"]; ["b"]; ["c"]] in
285 let o_hyp_map = ref CString.Map.empty in
286 add_entries o_hyp_map ["a"] (str " : uint");
287 add_entries o_hyp_map ["b"] (str " : int");
288 add_entries o_hyp_map ["c"] (str " : nat");
289 let n_line_idents = [ ["b"; "a"; "c"] ] in
290 let n_hyp_map = ref CString.Map.empty in
291 add_entries n_hyp_map ["b"; "a"; "c"] (str " : nat");
292 let expected = [flatten (wrap_in_bg "diff.removed" (seq [str "a"; str " : ";
293 (tag "diff.removed" (str "uint"))]));
294 flatten (wrap_in_bg "diff.removed" (seq [str "b"; str " : ";
295 (tag "diff.removed" (str "int"))]));
296 flatten (wrap_in_bg "diff.added" (seq [(tag "start.diff.added"
297 (str "b")); str ", "; str "a"; (tag "end.diff.added" (str ","));
298 str " "; str "c"; str " : nat"]))
299 ] in
300
301 let hyps_diff_list = diff_hyps o_line_idents !o_hyp_map n_line_idents !n_hyp_map in
302
303 (*print_list hyps_diff_list;*)
304 (*db_print_list hyps_diff_list;*)
305
306 List.iter2 (fun exp act ->
307 assert_equal ~msg:"added" ~printer:db_string_of_pp exp (flatten act))
308 expected hyps_diff_list
309 let _ = add_test "diff_hyps compacted with join" t
310
311 (* b, a, c : nat ->
312 a : uint
313 b : int
314 c : nat
315 DIFFS
316 b, a, c : nat (remove b,a,)
317 a : uint (add uint)
318 b : int (add int)
319 c : nat
320 is this a realistic use case? *)
321 let t () =
322 write_diffs_option "removed"; (* turn on "removed" option *)
323 let o_line_idents = [ ["b"; "a"; "c"] ] in
324 let o_hyp_map = ref CString.Map.empty in
325 add_entries o_hyp_map ["b"; "a"; "c"] (str " : nat");
326 let n_line_idents = [ ["a"]; ["b"]; ["c"]] in
327 let n_hyp_map = ref CString.Map.empty in
328 add_entries n_hyp_map ["a"] (str " : uint");
329 add_entries n_hyp_map ["b"] (str " : int");
330 add_entries n_hyp_map ["c"] (str " : nat");
331 let expected = [flatten (wrap_in_bg "diff.removed" (seq [(tag "start.diff.removed"
332 (str "b")); str ", "; str "a"; (tag "end.diff.removed" (str ","));
333 str " "; str "c"; str " : nat"]));
334 flatten (wrap_in_bg "diff.added" (seq [str "a"; str " : "; (tag "diff.added" (str "uint"))]));
335 flatten (wrap_in_bg "diff.added" (seq [str "b"; str " : "; (tag "diff.added" (str "int"))]));
336 flatten (seq [str "c"; str " : nat"])
337 ] in
338
339 let hyps_diff_list = diff_hyps o_line_idents !o_hyp_map n_line_idents !n_hyp_map in
340
341 (*print_list hyps_diff_list;*)
342 (*db_print_list hyps_diff_list;*)
343
344 List.iter2 (fun exp act ->
345 assert_equal ~msg:"added" ~printer:db_string_of_pp exp (flatten act))
346 expected hyps_diff_list
347 let _ = add_test "diff_hyps compacted with split" t
348
349 (* i : nat
350 b : bool
351 j : nat ->
352 i, j : nat
353 DIFFS
354 b : bool (removed)
355 i, j : nat
356 *)
357 let t () =
358 write_diffs_option "removed"; (* turn on "removed" option *)
359 let o_line_idents = [ ["i"]; ["b"]; ["j"] ] in
360 let o_hyp_map = ref CString.Map.empty in
361 add_entries o_hyp_map ["i"] (str " : nat");
362 add_entries o_hyp_map ["b"] (str " : bool");
363 add_entries o_hyp_map ["j"] (str " : nat");
364 let n_line_idents = [ ["i"; "j"]] in
365 let n_hyp_map = ref CString.Map.empty in
366 add_entries n_hyp_map ["i"; "j"] (str " : nat");
367 let expected = [flatten (wrap_in_bg "diff.removed"
368 (seq [tag "start.diff.removed" (str "b"); tag "end.diff.removed" (str " : bool")]));
369 flatten (seq [str "i"; str ", "; str "j"; str " : nat"])
370 ] in
371
372 let hyps_diff_list = diff_hyps o_line_idents !o_hyp_map n_line_idents !n_hyp_map in
373
374 (* print_list hyps_diff_list; *)
375 (* db_print_list hyps_diff_list; *)
376
377 List.iter2 (fun exp act ->
378 assert_equal ~msg:"added" ~printer:db_string_of_pp exp (flatten act))
379 expected hyps_diff_list
380 let _ = add_test "diff_hyps removal causes compaction from #14577" t
381
382 (* other potential tests
383 coqtop/terminal formatting BLOCKED: CAN'T GET TAGS IN FORMATTER
384 white space at end of line
385 spanning diffs
386 shorten_diff_span
387
388 MAYBE NOT WORTH IT
389 diff_pp/add_diff_tags
390 add/remove - show it preserves, recurs and processes:
391 nested in boxes
392 breaks, etc. preserved
393 diff_pp_combined with/without removed
394 *)
395
396
397 let _ = run_tests __FILE__ log_out_ch (List.rev !tests)