Hi,
I am self-learning the CS3110 (OCaml) course and need some help, because my head is getting tired/exploding after reading and playing with Fmt and Alcotest sources. Pretty different beasts compared to most Xunit frameworks outside FP.
I have 2 questions
- How can I make an Alcotest testable of CharMap defined below (using existing combinators)?
- More general question: Am I testing ‘not equals’ in an idiomatic way (the last check) below? Also are the Alcotest API always checking a testable (a “sig” and/or module type" with equal and pp function) such that other kind of assertions (greater than, not equal, text starts with etc…) are using this testable equal abstraction. Seems impossible…so are there other “check-abstractions” in Alcotest?
module CharMap = Map.Make(Char)
let test_bindings() =
(* TODO: How to create testable for stdlib CharMap above *)
(* let charStringMap = Alcotest.testable (Fmt.hashtbl (Fmt.pair Fmt.char Fmt.string)) (CharMap.equal Char.compare) in *)
(* testables *)
let eq = Alcotest.(list (pair char int)) in
let not_eq = Alcotest.neg eq in
(* values to test *)
let b1 = CharMap.(empty |> add 'x' 0 |> add 'y' 1 |> bindings) in
let b2 = CharMap.(empty |> add 'y' 1 |> add 'x' 0 |> bindings) in
let cm3 = CharMap.(empty |> add 'x' 2 |> add 'y' 1 |> remove 'x' |> add 'x' 0) in
let b3 = CharMap.(cm3 |> bindings) in
let b4 = CharMap.(cm3 |> add 'z' 5 |> bindings) in
(* asserts/checks for eq and not_eq *)
Alcotest.(check eq "b1 = b2" b1 b2);
Alcotest.(check eq "bi = b3" b1 b3);
Alcotest.(check eq "b2 = b3" b2 b3);
Alcotest.(check not_eq "b3 <> b4" b3 b4)