From b3d90af6450bf1421616e65d77481d704947c86a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Stefanesco?= Date: Fri, 24 Nov 2023 17:28:44 +0100 Subject: [PATCH] Use ppx_inline_test for unit tests --- coq-lsp.opam | 4 ++-- coq/dune | 2 ++ coq/utf8.ml | 28 ++++++++++++++++++++++++++++ flake.nix | 4 ++-- test/unit_test/dune | 4 ---- test/unit_test/utf16_pos.ml | 30 ------------------------------ 6 files changed, 34 insertions(+), 38 deletions(-) delete mode 100644 test/unit_test/dune delete mode 100644 test/unit_test/utf16_pos.ml diff --git a/coq-lsp.opam b/coq-lsp.opam index 68d437c5..15bdcf7c 100644 --- a/coq-lsp.opam +++ b/coq-lsp.opam @@ -30,8 +30,8 @@ depends: [ # waterproof parser "menhir" { >= "20220210" } - # for unit testing - "alcotest" { >= "1.6.0" & with-test } + # unit testing + "ppx_inline_test" { >= "0.14.1" } # Uncomment this for releases # "coq" { >= "8.17" < "8.18" } diff --git a/coq/dune b/coq/dune index 572ead33..5eb0195a 100644 --- a/coq/dune +++ b/coq/dune @@ -3,4 +3,6 @@ (public_name coq-lsp.coq) ; Unfortunate we have to link the STM due to the LTAC plugin ; depending on it, we should fix this upstream + (inline_tests) + (preprocess (pps ppx_inline_test)) (libraries lang coq-core.vernac coq-serapi.serlib)) diff --git a/coq/utf8.ml b/coq/utf8.ml index 5b676372..9051e717 100644 --- a/coq/utf8.ml +++ b/coq/utf8.ml @@ -189,3 +189,31 @@ let get_byte_offset_from_utf16_pos line (char : int) = () done; if !byte_idx < String.length line then Some !byte_idx else None + +let%test_unit "utf16 offsets" = + let testcases_x = + [ ("aax", 2, true) + ; (" xoo", 2, true) + ; ("0123", 4, false) + ; (" 𝒞x", 4, true) + ; (" 𝒞x𝒞", 4, true) + ; (" 𝒞∫x", 5, true) + ; (" 𝒞", 4, false) + ; ("∫x.dy", 1, true) + ] + in + List.iter + (fun (s, i, b) -> + let res = get_byte_offset_from_utf16_pos s i in + if b then + let res = Option.map (fun i -> s.[i]) res in + match res with + | Some x when x = 'x' -> () + | Some x -> + failwith + (Printf.sprintf "Didn't find x in test %s : %d, instead: %c" s i x) + | None -> + failwith (Printf.sprintf "Didn't not find x in test %s : %d" s i) + else if res != None then + failwith (Printf.sprintf "Shouldn't find x in test %s : %d" s i)) + testcases_x diff --git a/flake.nix b/flake.nix index 2bb4caca..056ac7f4 100644 --- a/flake.nix +++ b/flake.nix @@ -66,7 +66,7 @@ in l.attrValues { inherit serapi; - inherit (ocamlPackages) yojson cmdliner uri dune-build-info; + inherit (ocamlPackages) yojson cmdliner uri dune-build-info ppx_inline_test; }; }; @@ -90,7 +90,7 @@ packages = l.attrValues { inherit (config.treefmt.build) wrapper; inherit (pkgs) dune_3 nodejs dune-release; - inherit (ocamlPackages) ocaml ocaml-lsp alcotest; + inherit (ocamlPackages) ocaml ocamlformat ocaml-lsp; }; }; }; diff --git a/test/unit_test/dune b/test/unit_test/dune deleted file mode 100644 index 9bf5f92e..00000000 --- a/test/unit_test/dune +++ /dev/null @@ -1,4 +0,0 @@ -(test - (name utf16_pos) - (link_flags -linkall) - (libraries alcotest coq)) diff --git a/test/unit_test/utf16_pos.ml b/test/unit_test/utf16_pos.ml deleted file mode 100644 index a0ee5b11..00000000 --- a/test/unit_test/utf16_pos.ml +++ /dev/null @@ -1,30 +0,0 @@ -(* Build with `ocamlbuild -pkg alcotest simple.byte` *) - -open Coq - -let testcases_x = - [ ("aax", 2, true) - ; (" xoo", 2, true) - ; ("0123", 4, false) - ; (" 𝒞x", 4, true) - ; (" 𝒞x𝒞", 4, true) - ; (" 𝒞∫x", 5, true) - ; (" 𝒞", 4, false) - ; ("∫x.dy", 1, true) - ] - -(* The tests *) -let test_x () = - List.iter - (fun (s, i, b) -> - let res = Utf8.get_byte_offset_from_utf16_pos s i in - if b then - let res = Option.map (fun i -> s.[i]) res in - Alcotest.(check (option char)) "x present" (Some 'x') res - else Alcotest.(check (option int)) "x present" None res) - testcases_x - -(* Run it *) -let () = - let open Alcotest in - run "Controller" [ ("utf16", [ test_case "Find the x" `Quick test_x ]) ]