Debian Codegraph @

test/test.ml

raw symbols SCIP
1 let sprintf = Printf.sprintf
2 let ksprintf = Printf.ksprintf
3
4 open Ppx_sexp_conv_lib.Conv
5
6 module Time : sig
7 type t
8
9 val now : unit -> t
10 val to_string : t -> string
11 val to_string_sec : t -> string
12 val to_string_abs : t -> string
13 end = struct
14 type t = string
15
16 let now () = "Time.now ()"
17 let to_string t = "[Time.to_string (" ^ t ^ ")]"
18 let to_string_sec t = "[Time.to_string_sec (" ^ t ^ ")]"
19 let to_string_abs t = "[Time.to_string_abs (" ^ t ^ ")]"
20 end
21
22 module Zone : sig
23 type t
24
25 val local : t
26 val to_string : t -> string
27 end = struct
28 type t = string
29
30 let local = "Zone.local"
31 let to_string t = "[Zone.to_string " ^ t ^ "]"
32 end
33
34 let%test _ =
35 sprintf !"The time is %{Time} and the timezone is %{Zone}.\n" (Time.now ()) Zone.local
36 = "The time is [Time.to_string (Time.now ())] and the timezone is [Zone.to_string \
37 Zone.local].\n"
38 ;;
39
40 (* check that custom directives with nothing in between are properly translated *)
41 let%test _ = sprintf !"%{sexp:int}%{sexp:int}%{sexp:int}%{sexp:int}" 1 2 3 4 = "1234"
42
43 (* check that things works well when the conversion function take optional arguments *)
44 let%test _ =
45 let to_string ?foo:_ x = string_of_int x in
46 sprintf !"%{to_string}\n" 42 = "42\n"
47 ;;
48
49 (* check the X#y kinds of format and that arguments are not
50 reversed somehow *)
51 let%test _ =
52 let now = Time.now () in
53 sprintf !"%{Time}, %{Time#sec}, %{Time.to_string_abs}\n%!" now now now
54 = "[Time.to_string (Time.now ())], [Time.to_string_sec (Time.now ())], \
55 [Time.to_string_abs (Time.now ())]\n"
56 ;;
57
58 (* same as above, with empty module paths *)
59 let%test _ =
60 let open Time in
61 let now = now () in
62 sprintf !"%{}, %{#sec}, %{to_string_abs}\n%!" now now now
63 = "[Time.to_string (Time.now ())], [Time.to_string_sec (Time.now ())], \
64 [Time.to_string_abs (Time.now ())]\n"
65 ;;
66
67 (* testing what happens if the expression to the left of the format string
68 is a bit complicated *)
69 let%test _ =
70 let s = ksprintf (fun s -> s ^ " foo") !"%{Time} bar" (Time.now ()) in
71 s = "[Time.to_string (Time.now ())] bar foo"
72 ;;
73
74 (* checking sexp: format *)
75 let%test "sexp conversion" =
76 sprintf !"The pair is: %{sexp:int * string}" (4, "asdf") = "The pair is: (4 asdf)"
77 ;;
78
79 (* checking sexp#mach: format *)
80 let%test "sexp#mach conversion" =
81 let module Ppx_sexp_conv_lib = struct
82 module Sexp = struct
83 include Ppx_sexp_conv_lib.Sexp
84
85 let to_string_mach sexp = to_string sexp ^ " (in machine format)"
86 end
87 end
88 in
89 sprintf !"The pair is: %{sexp#mach:int * string}" (4, "asdf")
90 = "The pair is: (4 asdf) (in machine format)"
91 ;;
92
93 (* checking tricky formats *)
94 let%test _ = sprintf !"%d %%{foo" 3 = "3 %{foo"
95
96 let%test _ =
97 sprintf !"%d %%{%{Time}" 3 (Time.now ()) = "3 %{[Time.to_string (Time.now ())]"
98 ;;
99
100 (* checking that when we eta expand, we do not change side effects *)
101 let%test _ =
102 let side_effect1_happened = ref false in
103 let side_effect2_happened = ref false in
104 let _f : Zone.t -> string =
105 (side_effect1_happened := true;
106 sprintf)
107 !"%{Time} %{Zone}"
108 (side_effect2_happened := true;
109 Time.now ())
110 in
111 !side_effect1_happened && !side_effect2_happened
112 ;;
113
114 let%test _ =
115 let to_string () = "plop" in
116 sprintf !"%{ }" () = "plop"
117 ;;
118
119 let%test_unit _ =
120 let f ~labeled_arg:() fmt = ksprintf (fun _ -> ()) fmt in
121 (* Check that it compiles with the labeled argument applied both before and after the
122 format string *)
123 f ~labeled_arg:() !"hello";
124 f !"hello" ~labeled_arg:()
125 ;;
126
127 let%test_unit _ =
128 let after1 = Some () in
129 let f ~before:() fmt = ksprintf (fun _ ?after1:_ () ~after2:() -> ()) fmt in
130 f ~before:() ?after1 !"hello" () ~after2:();
131 f ~before:() !"hello" ?after1 () ~after2:();
132 f !"hello" ~before:() ?after1 () ~after2:();
133 f !"hello" ?after1 ~before:() () ~after2:()
134 ;;
135
136 let%test_unit _ =
137 let f ~label:() fmt = ksprintf (fun _ -> ()) fmt in
138 let r = ref 0 in
139 let g = f !"%{Time}" ~label:(incr r) in
140 g (Time.now ());
141 g (Time.now ());
142 assert (!r = 1)
143 ;;
144
145 let%test "format subst" = sprintf !"%(%d%)" "[%d]" 1 = "[1]"
146 let first_class_format1 = !"u = %{sexp:int * int}"
147 let first_class_format2 = !"t = %{Time}"
148
149 let first_class_format3 =
150 first_class_format1 ^^ ", " ^^ first_class_format2 ^^ !", v = %{sexp:int}"
151 ;;
152
153 let%test _ = sprintf first_class_format1 (0, 42) = "u = (0 42)"
154
155 let%test _ =
156 sprintf first_class_format2 (Time.now ()) = "t = [Time.to_string (Time.now ())]"
157 ;;
158
159 let%test _ =
160 sprintf first_class_format3 (0, 42) (Time.now ()) 10
161 = "u = (0 42), t = [Time.to_string (Time.now ())], v = 10"
162 ;;