(* Copyright (c) 2024-2026 Thomas Gazagnaire Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *) let test_stats_empty () = let s = Git.Diff.stats [] in Alcotest.(check int) "additions" 0 s.additions; Alcotest.(check int) "deletions" 0 s.deletions; Alcotest.(check int) "modifications" 0 s.modifications let test_stats_mixed () = let h = Git.Hash.of_hex (String.make 40 'a') in let changes = [ Git.Diff.Added { path = "a.txt"; hash = h; perm = `Normal }; Git.Diff.Added { path = "b.txt"; hash = h; perm = `Normal }; Git.Diff.Removed { path = "c.txt"; hash = h; perm = `Normal }; Git.Diff.Modified { path = "d.txt"; old_hash = h; new_hash = h; old_perm = `Normal; new_perm = `Normal; }; ] in let s = Git.Diff.stats changes in Alcotest.(check int) "additions" 2 s.additions; Alcotest.(check int) "deletions" 1 s.deletions; Alcotest.(check int) "modifications" 1 s.modifications let test_filter_by_path_empty () = let result = Git.Diff.filter_by_path ~prefix:"src" [] in Alcotest.(check int) "empty list" 0 (List.length result) let test_filter_by_path () = let h = Git.Hash.of_hex (String.make 40 'b') in let changes = [ Git.Diff.Added { path = "src/a.ml"; hash = h; perm = `Normal }; Git.Diff.Added { path = "test/b.ml"; hash = h; perm = `Normal }; Git.Diff.Added { path = "src/lib/c.ml"; hash = h; perm = `Normal }; ] in let filtered = Git.Diff.filter_by_path ~prefix:"src" changes in Alcotest.(check int) "filtered count" 2 (List.length filtered) let test_pp_change () = let h = Git.Hash.of_hex (String.make 40 'c') in let change = Git.Diff.Added { path = "foo.ml"; hash = h; perm = `Normal } in let s = Fmt.to_to_string Git.Diff.pp_change change in Alcotest.(check bool) "contains path" true (String.length s > 0) let test_pp_stats () = let s = { Git.Diff.additions = 1; deletions = 2; modifications = 3 } in let out = Fmt.to_to_string Git.Diff.pp_stats s in Alcotest.(check bool) "non-empty" true (String.length out > 0) let tests = [ Alcotest.test_case "stats_empty" `Quick test_stats_empty; Alcotest.test_case "stats_mixed" `Quick test_stats_mixed; Alcotest.test_case "filter_by_path_empty" `Quick test_filter_by_path_empty; Alcotest.test_case "filter_by_path" `Quick test_filter_by_path; Alcotest.test_case "pp_change" `Quick test_pp_change; Alcotest.test_case "pp_stats" `Quick test_pp_stats; ] let suite = ("diff", tests)