test(clerk): setup alcotest + two first tests

This commit is contained in:
Emile Rolley 2022-02-25 20:06:03 +01:00
parent 15bb52e37a
commit f277f272d1
11 changed files with 88 additions and 18 deletions

View File

@ -255,12 +255,11 @@ test_suite: .FORCE
test_examples: .FORCE
@$(CLERK) test examples
test_clerk:
@$(CLERK) test $(BUILD_SYSTEM_DIR)/tests
test_clerk: .FORCE
cd $(BUILD_SYSTEM_DIR) && dune test
#> tests : Run interpreter tests
tests: .FORCE
@$(CLERK) test tests examples # $(BUILD_SYSTEM_DIR)/tests
tests: test_suite test_examples test_clerk
#> tests_ocaml : Run OCaml unit tests for the Catala-generated code
tests_ocaml: run_french_law_library_ocaml_tests

View File

@ -605,7 +605,6 @@ let driver (files_or_folders : string list) (command : string) (catala_exe : str
(ninja_building_context_init (ninja_start catala_exe catala_opts))
files_or_folders
in
Printf.printf "is none: %b\n" (Option.is_none ctx.curr_ninja);
let there_is_some_fails = 0 <> List.length ctx.all_failed_names in
let ninja = match ctx.curr_ninja with Some ninja -> ninja | None -> ctx.last_valid_ninja in
if there_is_some_fails then

View File

@ -1,11 +0,0 @@
module To_test = struct
let catala_backend_to_string = Clerk_driver.catala_backend_to_string
end
let test_catala_backend_to_string () =
Alcotest.(check string) "same string" "Dcalc" (To_test.catala_backend_to_string Utils.Cli.Dcalc)
let () =
let open Alcotest in
run "Clerk"
[ ("catala_backend_to_string", [ test_case "Dcalc" `Quick test_catala_backend_to_string ]) ]

View File

@ -1,3 +1,3 @@
(tests
(names clerk_driver)
(libraries alcotest clerk.driver catala.utils))
(names test_clerk_driver)
(libraries alcotest clerk.driver ninja_utils catala.utils))

View File

@ -0,0 +1,73 @@
module D = Clerk_driver
module Al = Alcotest
module Nj = Ninja_utils
module To_test = struct
let collect_in_file = D.collect_in_file
end
(* cwd: _build/default/build_system/tests/ *)
let test_files_dir = "../../../../build_system/tests/catala_files/"
let ninja_start = D.ninja_start "catala" ""
let al_assert msg = Al.(check bool) msg true
let test_collect_in_untested_file () =
let untested_file = test_files_dir ^ "untested_file.catala_en" in
let ctx = D.ninja_building_context_init Nj.empty in
let nj_building_ctx = To_test.collect_in_file ctx untested_file Nj.empty false in
al_assert "no test cases should be found" (Option.is_none nj_building_ctx.curr_ninja);
al_assert "ninja_start should be the last valid ninja"
(Nj.empty = nj_building_ctx.last_valid_ninja)
let test_collect_in_simple_interpret_scope_file () =
let simple_interpret_scope_file = test_files_dir ^ "simple_interpret_scope_file.catala_en" in
let ctx = D.ninja_building_context_init ninja_start in
let nj_building_ctx = To_test.collect_in_file ctx simple_interpret_scope_file ninja_start false in
al_assert "a test case should be found" (Option.is_some nj_building_ctx.curr_ninja);
let expected_format =
let open Nj in
let test_file_output = "test_file_" ^ Nj.Build.unpath simple_interpret_scope_file in
let test_A_file_output = "test_A_Interpret_" ^ Nj.Build.unpath simple_interpret_scope_file in
let test_A_file =
Build.make_with_vars ~outputs:[ Expr.Lit test_A_file_output ] ~rule:"test_with_scope"
~vars:
[
("scope", Lit "A");
("catala_cmd", Lit "Interpret");
("tested_file", Lit simple_interpret_scope_file);
( "expected_output",
Lit (test_files_dir ^ "output/simple_interpret_scope_file.catala_en.A.Interpret") );
]
in
let test_file =
Build.make_with_inputs ~outputs:[ Expr.Lit test_file_output ] ~rule:"phony"
~inputs:[ Expr.Lit (" $\n " ^ test_A_file_output) ]
in
BuildMap.empty
|> BuildMap.add test_file_output test_file
|> BuildMap.add test_A_file_output test_A_file
|> Nj.BuildMap.iter (fun _ b -> Nj.Build.format Format.str_formatter b);
Buffer.contents Format.stdbuf
in
let actual_format =
let ninja = Option.get nj_building_ctx.curr_ninja in
Buffer.clear Format.stdbuf;
Nj.BuildMap.iter (fun _ b -> Nj.Build.format Format.str_formatter b) ninja.builds;
Buffer.contents Format.stdbuf
in
Al.(check string) "should be equal" expected_format actual_format
let suite_collect_in_file =
Al.
[
test_case "an untested file" `Quick test_collect_in_untested_file;
test_case "a simple Interpret scope" `Quick test_collect_in_simple_interpret_scope_file;
]
let () = Al.run "Clerk" [ ("Test collect_in_file", suite_collect_in_file) ]

View File

@ -41,6 +41,7 @@ depends: [
"z3" {>= "4.8.11"}
"cppo" {>= "1"}
"obelisk" {dev}
"alcotest" {with-test & >= "1.5.0"}
"ocamlformat" {dev & = "0.19.0"}
"odoc" {with-doc}
]

View File

@ -18,6 +18,7 @@ depends: [
"cmdliner" {= "1.0.4"}
"re" {>= "1.9.0"}
"ANSITerminal" {>= "0.8.2"}
"alcotest" {with-test & >= "1.5.0"}
"catala" {= version}
"odoc" {with-doc}
]

View File

@ -73,6 +73,10 @@
(cppo
(>= 1))
(obelisk :dev)
(alcotest
(and
:with-test
(>= 1.5.0)))
(ocamlformat
(and
:dev
@ -112,6 +116,10 @@
(>= 1.9.0))
(ANSITerminal
(>= 0.8.2))
(alcotest
(and
:with-test
(>= 1.5.0)))
(catala
(= :version))))