From 107ff95dc4fb2a02d506a4d5b5e6c270507f2c5e Mon Sep 17 00:00:00 2001 From: Denis Merigoux Date: Fri, 21 Apr 2023 10:37:31 +0200 Subject: [PATCH] Remove dependency on ppx_expect and use alcotest instead --- .nix/catala.nix | 2 - catala.opam | 1 - compiler/dune | 11 +++++ compiler/lcalc/dune | 5 +- compiler/lcalc/optimizations.ml | 82 +++++++++++++------------------- compiler/lcalc/optimizations.mli | 5 ++ compiler/tests.ml | 10 ++++ 7 files changed, 62 insertions(+), 54 deletions(-) create mode 100644 compiler/tests.ml diff --git a/.nix/catala.nix b/.nix/catala.nix index 38e06779..6ecffc6f 100644 --- a/.nix/catala.nix +++ b/.nix/catala.nix @@ -25,7 +25,6 @@ , zarith_stubs_js , ocaml-crunch , cohttp-lwt-unix -, ppx_expect }: buildDunePackage { @@ -63,7 +62,6 @@ buildDunePackage { zarith zarith_stubs_js cohttp-lwt-unix - ppx_expect ]; # Currently there is no unit tests in catala and Cram tests are handled by clerk diff --git a/catala.opam b/catala.opam index e82238a8..0fac12c0 100644 --- a/catala.opam +++ b/catala.opam @@ -32,7 +32,6 @@ depends: [ "ocamlfind" {!= "1.9.5"} "ocamlgraph" {>= "1.8.8"} "ppx_yojson_conv" {>= "0.14.0"} - "ppx_expect" {>= "0.14.0"} "re" {>= "1.9.0"} "sedlex" {>= "2.4"} "uutf" {>= "1.0.3"} diff --git a/compiler/dune b/compiler/dune index 35eb2f42..ad9165e8 100644 --- a/compiler/dune +++ b/compiler/dune @@ -28,6 +28,11 @@ catala.runtime_ocaml catala.runtime_jsoo)) +(executable + (name tests) + (modules tests) + (libraries catala.driver alcotest)) + (rule (target custom_linking.sexp) (mode fallback) @@ -54,3 +59,9 @@ (alias (name catala) (deps catala.exe)) + +(rule + (alias runtest) + (package catala) + (action + (run ./tests.exe))) diff --git a/compiler/lcalc/dune b/compiler/lcalc/dune index 330473ca..b4aa9cd8 100644 --- a/compiler/lcalc/dune +++ b/compiler/lcalc/dune @@ -1,10 +1,9 @@ (library (name lcalc) (public_name catala.lcalc) - (libraries bindlib ubase dcalc scopelang catala.runtime_ocaml) - (inline_tests) + (libraries bindlib ubase dcalc scopelang catala.runtime_ocaml alcotest) (preprocess - (pps visitors.ppx ppx_expect))) + (pps visitors.ppx))) (documentation (package catala) diff --git a/compiler/lcalc/optimizations.ml b/compiler/lcalc/optimizations.ml index ffc0b342..3f82a9f4 100644 --- a/compiler/lcalc/optimizations.ml +++ b/compiler/lcalc/optimizations.ml @@ -205,23 +205,18 @@ let optimize_program (p : 'm program) : untyped program = lift_optim peephole_expr; ]) -let%expect_test _ = +let test_lcalc_optims1 () = Cli.call_unstyled (fun _ -> let x = Var.make "x" in - let enumT = EnumName.fresh ("t", Pos.no_pos) in let consA = EnumConstructor.fresh ("A", Pos.no_pos) in let consB = EnumConstructor.fresh ("B", Pos.no_pos) in let consC = EnumConstructor.fresh ("C", Pos.no_pos) in let consD = EnumConstructor.fresh ("D", Pos.no_pos) in - let nomark = Untyped { pos = Pos.no_pos } in - let injA = Expr.einj (Expr.evar x nomark) consA enumT nomark in - (* let injB = Expr.einj (Expr.evar x nomark) consB enumT nomark in *) let injC = Expr.einj (Expr.evar x nomark) consC enumT nomark in let injD = Expr.einj (Expr.evar x nomark) consD enumT nomark in - let cases : ('a, 't) boxed_gexpr EnumConstructor.Map.t = EnumConstructor.Map.of_seq @@ List.to_seq @@ -230,26 +225,20 @@ let%expect_test _ = consB, Expr.eabs (Expr.bind [| x |] injD) [TAny, Pos.no_pos] nomark; ] in - let matchA = Expr.ematch injA enumT cases nomark in - - Format.printf "before=%a\n" - (Print.expr_debug ~debug:false) - (Expr.unbox matchA); - Format.print_flush (); - Format.printf "after=%a\n" - (Print.expr_debug ~debug:false) - (Expr.unbox (iota_expr (Expr.unbox matchA))); - - [%expect - {| - before=match (A x) - with - | A → (λ (x: any) → C x) - | B → (λ (x: any) → D x) - after=C - x - |}]) + Alcotest.(check string) + "same string" + "before=match (A x)\n\ + \ with\n\ + \ | A → (λ (x: any) → C x)\n\ + \ | B → (λ (x: any) → D x)\n\ + after=C\n\ + x" + (Format.asprintf "before=%a\nafter=%a" + (Print.expr_debug ~debug:false) + (Expr.unbox matchA) + (Print.expr_debug ~debug:false) + (Expr.unbox (iota_expr (Expr.unbox matchA))))) let cases_of_list l : ('a, 't) boxed_gexpr EnumConstructor.Map.t = EnumConstructor.Map.of_seq @@ -262,7 +251,7 @@ let cases_of_list l : ('a, 't) boxed_gexpr EnumConstructor.Map.t = [TAny, Pos.no_pos] (Untyped { pos = Pos.no_pos }) )) -let%expect_test _ = +let test_lcalc_optims2 () = Cli.call_unstyled (fun _ -> let enumT = EnumName.fresh ("t", Pos.no_pos) in let consA = EnumConstructor.fresh ("A", Pos.no_pos) in @@ -297,25 +286,22 @@ let%expect_test _ = (cases_of_list [consA, injC; consB, injD]) nomark in - - Format.printf "before=@[%a@]@." - (Print.expr_debug ~debug:false) - (Expr.unbox matchA); - Format.printf "after=%a@." - (Print.expr_debug ~debug:false) - (Expr.unbox (iota2_expr (Expr.unbox matchA))); - - [%expect - {| - before=match - (match 1 - with - | A → (λ (x: any) → A 20) - | B → (λ (x: any) → B B x)) - with - | A → (λ (x: any) → C x) - | B → (λ (x: any) → D x) - after=match 1 - with - | A → (λ (x: any) → C 20) - | B → (λ (x: any) → D B x) |}]) + Alcotest.(check string) + "same string " + "before=match\n\ + \ (match 1\n\ + \ with\n\ + \ | A → (λ (x: any) → A 20)\n\ + \ | B → (λ (x: any) → B B x))\n\ + \ with\n\ + \ | A → (λ (x: any) → C x)\n\ + \ | B → (λ (x: any) → D x)\n\ + after=match 1\n\ + \ with\n\ + \ | A → (λ (x: any) → C 20)\n\ + \ | B → (λ (x: any) → D B x)\n" + (Format.asprintf "before=@[%a@]@.after=%a@." + (Print.expr_debug ~debug:false) + (Expr.unbox matchA) + (Print.expr_debug ~debug:false) + (Expr.unbox (iota2_expr (Expr.unbox matchA))))) diff --git a/compiler/lcalc/optimizations.mli b/compiler/lcalc/optimizations.mli index 17cdd8e7..b091b947 100644 --- a/compiler/lcalc/optimizations.mli +++ b/compiler/lcalc/optimizations.mli @@ -43,3 +43,8 @@ open Ast val optimize_program : 'm program -> Shared_ast.untyped program (** Warning/todo: no effort was yet made to ensure correct propagation of type annotations in the typed case *) + +(**{1 Tests}*) + +val test_lcalc_optims2 : unit -> unit +val test_lcalc_optims1 : unit -> unit diff --git a/compiler/tests.ml b/compiler/tests.ml new file mode 100644 index 00000000..e2cded43 --- /dev/null +++ b/compiler/tests.ml @@ -0,0 +1,10 @@ +let _ = + let open Alcotest in + run "Optimizations" + [ + ( "Lcalc", + [ + test_case "#1" `Quick Lcalc.Optimizations.test_lcalc_optims1; + test_case "#2" `Quick Lcalc.Optimizations.test_lcalc_optims2; + ] ); + ]