Debian Codegraph @

test/lib/conv_test.ml

raw symbols SCIP
1 open Ppx_sexp_conv_lib
2 open Conv
3
4 let%test_module "Exceptions" =
5 (module struct
6 let check_sexp exn string =
7 match sexp_of_exn_opt exn with
8 | None -> raise exn
9 | Some sexp ->
10 let sexp_as_string = Ppx_sexp_conv_lib.Sexp.to_string sexp in
11 if sexp_as_string <> string then failwith sexp_as_string
12 ;;
13
14 (* first global exceptions, checking different arities since they
15 don't have the same representation *)
16 exception Arg0 [@@deriving sexp]
17 exception Arg1 of int [@@deriving sexp]
18 exception Arg2 of int * int [@@deriving sexp]
19
20 let%test_unit _ = check_sexp Arg0 "conv_test.ml.Arg0"
21 let%test_unit _ = check_sexp (Arg1 1) "(conv_test.ml.Arg1 1)"
22 let%test_unit _ = check_sexp (Arg2 (2, 3)) "(conv_test.ml.Arg2 2 3)"
23
24 (* now local exceptions *)
25 let exn (type a) a sexp_of_a =
26 let module M = struct
27 exception E of a [@@deriving sexp]
28 end
29 in
30 M.E a
31 ;;
32
33 let%test_unit "incompatible exceptions with the same name" =
34 let e_int = exn 1 sexp_of_int in
35 let e_string = exn "a" sexp_of_string in
36 check_sexp e_int "(conv_test.ml.E 1)";
37 check_sexp e_string "(conv_test.ml.E a)"
38 ;;
39
40 let%test_unit "sexp converters are finalized properly for local exceptions" =
41 Gc.compact ();
42 Gc.compact ();
43 let size_before =
44 Ppx_sexp_conv_lib.Conv.Exn_converter.For_unit_tests_only.size ()
45 in
46 let e = exn 2.5 sexp_of_float in
47 let size_after_local_exn =
48 Ppx_sexp_conv_lib.Conv.Exn_converter.For_unit_tests_only.size ()
49 in
50 let e_finalized = ref false in
51 Gc.finalise (fun _ -> e_finalized := true) e;
52 check_sexp e "(conv_test.ml.E 2.5)";
53 Gc.compact ();
54 Gc.compact ();
55 assert !e_finalized;
56 let size_after_gc =
57 Ppx_sexp_conv_lib.Conv.Exn_converter.For_unit_tests_only.size ()
58 in
59 assert (size_before + 1 = size_after_local_exn);
60 assert (size_before = size_after_gc)
61 ;;
62 end)
63 ;;