test(clerk): add minimal unit testing structure

This commit is contained in:
Emile Rolley 2022-02-08 12:45:52 +01:00
parent c640a3eeab
commit f79c652f82
4 changed files with 39 additions and 2 deletions

View File

@ -11,7 +11,7 @@ module Expr = struct
let rec to_string = function
| Lit s -> s
| Var s -> "$" ^ s
| Seq ls -> List.fold_left (fun acc s -> acc ^ " " ^ to_string s) "" ls
| Seq ls -> List.fold_left (fun acc s -> acc ^ " " ^ to_string s) " " ls
let list_to_string ?(sep = " ") ls = ls |> List.map to_string |> String.concat sep
end
@ -46,13 +46,15 @@ module Build = struct
let make_with_inputs ~outputs ~rule ~inputs =
{ outputs; rule; inputs = Option.some inputs; vars = [] }
let empty = make ~outputs:[ Expr.Lit "empty" ] ~rule:"phony"
(** [unpath path] replaces all '/' occurences with '-' in [path] to avoid ninja writing the
corresponding file. *)
let unpath path = Re.Pcre.(substitute ~rex:(regexp "/") ~subst:(fun _ -> "-")) path
let to_string build =
Printf.sprintf "build %s: %s" (Expr.list_to_string build.outputs) build.rule
^ (build.inputs |> Option.fold ~some:(fun ls -> Expr.list_to_string ls) ~none:"")
^ (build.inputs |> Option.fold ~some:(fun ls -> " " ^ Expr.list_to_string ls) ~none:"")
^ "\n"
^ List.fold_left
(fun acc (name, exp) -> acc ^ Printf.sprintf " %s = %s\n" name (Expr.to_string exp))

View File

@ -36,6 +36,9 @@ module Build : sig
val make_with_inputs : outputs:Expr.t list -> rule:string -> inputs:Expr.t list -> t
val empty : t
(** [empty] is the minimal ninja build. *)
val unpath : string -> string
(** [unpath path] replaces all '/' occurences with '-' in [path] to avoid ninja writing the
corresponding file. *)

5
build_system/test/dune Normal file
View File

@ -0,0 +1,5 @@
(dirs ..)
(tests
(names ninja_utils)
(libraries alcotest ninja_utils))

View File

@ -0,0 +1,27 @@
open Alcotest
open Ninja_utils
module B = Build
module E = Expr
module To_test = struct
let build_to_string = B.to_string
end
let test_empty_build () =
check string "Must be the same string" "build empty: phony\n" (To_test.build_to_string B.empty)
let test_empty_build_w_inputs () =
check string "Must be the same string" "build output: rule in1 in2\n"
(To_test.build_to_string
(B.make_with_inputs ~outputs:[ E.Lit "output" ] ~rule:"rule"
~inputs:[ E.Lit "in1"; E.Lit "in2" ]))
let () =
run "Ninja builds"
[
( "to_string",
[
test_case "Empty build" `Quick test_empty_build;
test_case "Empty build \\w inputs" `Quick test_empty_build_w_inputs;
] );
]