helper.ml 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. open Base
  2. open OUnit2
  3. module type TESTABLE = sig
  4. type t
  5. val equal: t Equal.t
  6. val to_string: t -> string
  7. end
  8. module String_ = struct
  9. type t = string
  10. let equal = String.equal
  11. let to_string x = Printf.sprintf "%S" x
  12. end
  13. module Option_(X: TESTABLE) = struct
  14. type t = X.t option
  15. let equal = Option.equal X.equal
  16. let to_string = function
  17. | Some(x) -> "Some(" ^ X.to_string x ^ ")"
  18. | None -> "None"
  19. end
  20. module List_(X: TESTABLE) = struct
  21. type t = X.t list
  22. let equal = List.equal ~equal:X.equal
  23. let to_string l =
  24. Printf.sprintf "[%s]"
  25. (String.concat ~sep:";"
  26. (List.map ~f:X.to_string l))
  27. end
  28. module IntOption = Option_(Int)
  29. module StringOption = Option_(String_)
  30. module StringList = List_(String_)
  31. let asrt (type a) (module X: TESTABLE with type t = a) e a =
  32. assert_equal ~cmp:X.equal ~printer:X.to_string e a
  33. let t name m e a =
  34. name>::fun _ -> asrt m e a
  35. let ts m l =
  36. test_list
  37. (List.map l ~f:(fun (name, e, a) -> t name m e a))
  38. let log, t_log =
  39. let out = ref [] in
  40. let log x =
  41. out := x::!out
  42. in
  43. let t_log name expected func =
  44. out := [];
  45. func ();
  46. t name (module StringList) expected (List.rev !out)
  47. in
  48. log, t_log
  49. let logf fmt = Printf.ksprintf log fmt