make_checkTest.ml 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. (** Build a parser for a specific check module *)
  2. module M (Check : Qsp_syntax.S.Analyzer) = struct
  3. module S = Qsp_syntax.S
  4. let pp_pos = Qsp_syntax.Report.pp_pos
  5. type pos = S.pos
  6. let equal_pos : pos -> pos -> bool = fun _ _ -> true
  7. type t = Qsp_syntax.Report.t = {
  8. level : Qsp_syntax.Report.level;
  9. loc : pos;
  10. message : string;
  11. }
  12. [@@deriving show, eq]
  13. let report : t list Alcotest.testable =
  14. Alcotest.list @@ Alcotest.testable Qsp_syntax.Report.pp equal
  15. let report_global : (string * t) list Alcotest.testable =
  16. Alcotest.list
  17. @@ Alcotest.pair Alcotest.string
  18. (Alcotest.testable Qsp_syntax.Report.pp equal)
  19. let parse :
  20. ?context:Check.context ->
  21. string ->
  22. (Check.Location.t Qparser.Analyzer.result, t) result =
  23. fun ?context content ->
  24. let lexing =
  25. Sedlexing.Latin1.from_string content |> Qparser.Lexbuf.from_lexbuf
  26. in
  27. let context = Option.value context ~default:(Check.initialize ()) in
  28. Qparser.Analyzer.parse (module Check) lexing context
  29. let get_report :
  30. (Check.Location.t Qparser.Analyzer.result, Qsp_syntax.Report.t) result ->
  31. Qsp_syntax.Report.t list = function
  32. | Ok v -> v.report
  33. | Error _ -> failwith "Error"
  34. let _test_instruction : string -> t list -> unit =
  35. fun literal expected ->
  36. let _location = Printf.sprintf {|# Location
  37. %s
  38. ------- |} literal in
  39. let actual = get_report @@ parse _location and msg = literal in
  40. Alcotest.(check' report ~msg ~expected ~actual)
  41. (** Run a test over the whole file.
  42. The parsing of the content shall not report any error.
  43. *)
  44. let global_check : string -> (string * t) list -> unit =
  45. fun literal expected ->
  46. let _location = Printf.sprintf {|# Location
  47. %s
  48. ------- |} literal in
  49. let context = Check.initialize () in
  50. let actual = get_report @@ parse ~context _location in
  51. let () =
  52. Alcotest.(
  53. check' report ~msg:"Error reported during parsing" ~expected:[] ~actual)
  54. in
  55. let msg = literal in
  56. let actual = Check.finalize context in
  57. Alcotest.(check' report_global ~msg ~expected ~actual)
  58. end