Source file test_utils.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
open! Core
open Ppxlib
open Ppx_html_syntax

let get_model_as_string ?html_syntax_module s =
  let open Ppx_html_expander in
  let loc = Location.none in
  let html_syntax_module =
    Option.map html_syntax_module ~f:(fun s -> { txt = Lident s; loc })
  in
  let parsed runtime_kind =
    s
    |> Model_parser.of_string ~loc
    |> Model_code_gen.code ~loc ~html_syntax_module ~runtime_kind
  in
  let js_of_ocaml = Pprintast.string_of_expression (parsed Js_of_ocaml) in
  let kernel = Pprintast.string_of_expression (parsed Kernel) in
  match String.equal js_of_ocaml kernel with
  | true -> Or_error.return js_of_ocaml
  | false ->
    Or_error.error_string
      ("Difference between ppx_html and ppx_html_kernel\n\n"
       ^ "PPX_HTML:\n"
       ^ js_of_ocaml
       ^ "\n\nPPX_HTML_KERNEL (diff):\n"
       ^ Expect_test_patdiff.patdiff js_of_ocaml kernel)
;;

(* We're doing a string compare here. We normalize the strings so they should be
   equivalent if the ASTs are equivalent *)
let are_models_equivalent a b =
  let tester =
    let%bind.Or_error a = get_model_as_string a in
    let%bind.Or_error b = get_model_as_string b in
    match String.equal a b with
    | true -> Or_error.return "Equivalent"
    | false ->
      Or_error.error_string
        {%string|
Models are not equivalent!

a:
%{a}

b:
%{b}
|}
  in
  match tester with
  | Ok ok -> print_endline ok
  | Error error -> Error.to_string_hum error |> print_endline
;;

let test ?html_syntax_module s =
  match get_model_as_string ?html_syntax_module s with
  | Ok js_of_ocaml ->
    print_endline "same output between ppx_html and ppx_html_kernel\n";
    print_endline js_of_ocaml
  | Error error -> Error.to_string_hum error |> print_endline
;;

let test_raise s =
  match test s with
  | () -> print_endline "Expected an error but test succeeded."
  | exception exn ->
    (match Ppxlib.Location.Error.of_exn exn with
     | Some error -> print_endline (Ppxlib.Location.Error.message error)
     | None -> print_s [%sexp (exn : exn)])
;;