Debian Codegraph @

test/ppx_sexp_test.ml

raw symbols SCIP
1 open Ppx_sexp_conv_lib
2 open Conv
3
4 (* Module names below are used in error messages being tested. *)
5 [@@@warning "-unused-module"]
6
7 module Sum_and_polymorphic_variants = struct
8 type poly =
9 [ `No_arg
10 | `One_arg of int
11 | `One_tuple of int * string
12 | `Two_args of int * string
13 ]
14 [@@deriving sexp, sexp_grammar]
15
16 let%test_unit _ =
17 List.iter
18 (fun (value, sexp) ->
19 assert (sexp_of_poly value = sexp);
20 assert (poly_of_sexp sexp = value))
21 [ `No_arg, Sexp.Atom "No_arg"
22 ; (`One_arg 1, Sexp.(List [ Atom "One_arg"; Atom "1" ]))
23 ; ( `One_tuple (1, "a")
24 , Sexp.(List [ Atom "One_tuple"; List [ Atom "1"; Atom "a" ] ]) )
25 ; (`Two_args (1, "a"), Sexp.(List [ Atom "Two_args"; List [ Atom "1"; Atom "a" ] ]))
26 ]
27 ;;
28
29 type nominal =
30 | No_arg
31 | One_arg of int
32 | One_tuple of (int * string)
33 | Two_args of int * string
34 [@@deriving sexp, sexp_grammar]
35
36 let%test_unit _ =
37 List.iter
38 (fun (value, sexp) ->
39 assert (sexp_of_nominal value = sexp);
40 assert (nominal_of_sexp sexp = value))
41 [ No_arg, Sexp.Atom "No_arg"
42 ; (One_arg 1, Sexp.(List [ Atom "One_arg"; Atom "1" ]))
43 ; (One_tuple (1, "a"), Sexp.(List [ Atom "One_tuple"; List [ Atom "1"; Atom "a" ] ]))
44 ; (Two_args (1, "a"), Sexp.(List [ Atom "Two_args"; Atom "1"; Atom "a" ]))
45 ]
46 ;;
47 end
48
49 module Records = struct
50 type t =
51 { a : int
52 ; b : (float * string) list option
53 }
54 [@@deriving sexp, sexp_grammar]
55
56 let%test_unit _ =
57 let t = { a = 2; b = Some [ 1., "a"; 2.3, "b" ] } in
58 let sexp = Sexplib.Sexp.of_string "((a 2)(b (((1 a)(2.3 b)))))" in
59 assert (t_of_sexp sexp = t);
60 assert (sexp_of_t t = sexp)
61 ;;
62
63 let%expect_test _ =
64 let sexp = Sexplib.Sexp.of_string "((a)(b ()))" in
65 Expect_test_helpers_core.show_raise (fun () -> t_of_sexp sexp);
66 [%expect
67 {|
68 (raised (
69 Of_sexp_error
70 "ppx_sexp_test.ml.Records.t_of_sexp: record conversion: only pairs expected, their first element must be an atom"
71 (invalid_sexp ((a) (b ())))))
72 |}]
73 ;;
74
75 let%expect_test _ =
76 let sexp = Sexplib.Sexp.of_string "((a 1)(a))" in
77 Expect_test_helpers_core.show_raise (fun () -> t_of_sexp sexp);
78 [%expect
79 {|
80 (raised (
81 Of_sexp_error
82 "ppx_sexp_test.ml.Records.t_of_sexp: duplicate fields: a"
83 (invalid_sexp ((a 1) (a)))))
84 |}]
85 ;;
86
87 let%expect_test _ =
88 let sexp = Sexplib.Sexp.of_string "((a 3 4))" in
89 Expect_test_helpers_core.show_raise (fun () -> t_of_sexp sexp);
90 [%expect
91 {|
92 (raised (
93 Of_sexp_error
94 "ppx_sexp_test.ml.Records.t_of_sexp: record conversion: only pairs expected, their first element must be an atom"
95 (invalid_sexp (a 3 4))))
96 |}]
97 ;;
98
99 let%expect_test _ =
100 let sexp = Sexplib.Sexp.of_string "((c 3))" in
101 Expect_test_helpers_core.show_raise (fun () -> t_of_sexp sexp);
102 [%expect
103 {|
104 (raised (
105 Of_sexp_error
106 "ppx_sexp_test.ml.Records.t_of_sexp: extra fields: c"
107 (invalid_sexp ((c 3)))))
108 |}]
109 ;;
110 end
111
112 module Inline_records = struct
113 type t =
114 | A of
115 { a : int
116 ; b : (float * string) list option
117 }
118 | B of int
119 [@@deriving sexp, sexp_grammar]
120
121 let%test_unit _ =
122 let t = A { a = 2; b = Some [ 1., "a"; 2.3, "b" ] } in
123 let sexp = Sexplib.Sexp.of_string "(A (a 2)(b (((1 a)(2.3 b)))))" in
124 assert (t_of_sexp sexp = t);
125 assert (sexp_of_t t = sexp)
126 ;;
127 end
128
129 module User_specified_conversion = struct
130 type my_float = float
131
132 let sexp_of_my_float n = Sexp.Atom (Printf.sprintf "%.4f" n)
133 let my_float_of_sexp = float_of_sexp
134
135 let%test_unit _ =
136 let my_float : my_float = 1.2 in
137 let sexp = Sexp.Atom "1.2000" in
138 assert (my_float_of_sexp sexp = my_float);
139 assert (sexp_of_my_float my_float = sexp)
140 ;;
141 end
142
143 module Exceptions : sig
144 (* no sexp_grammars for exceptions, as they can't be parsed *)
145 exception E0 [@@deriving sexp]
146 exception E1 of string [@@deriving sexp]
147 exception E2 of string * int [@@deriving sexp]
148 exception E_tuple of (string * int) [@@deriving sexp]
149
150 exception
151 E_record of
152 { a : string
153 ; b : int
154 }
155 [@@deriving sexp]
156 end = struct
157 exception E0 [@@deriving sexp]
158 exception E1 of string [@@deriving sexp]
159 exception E2 of string * int [@@deriving sexp]
160 exception E_tuple of (string * int) [@@deriving sexp]
161
162 exception
163 E_record of
164 { a : string
165 ; b : int
166 }
167 [@@deriving sexp]
168
169 let%test_unit _ =
170 let cases =
171 [ E0, "ppx_sexp_test.ml.Exceptions.E0"
172 ; E1 "a", "(ppx_sexp_test.ml.Exceptions.E1 a)"
173 ; E2 ("b", 2), "(ppx_sexp_test.ml.Exceptions.E2 b 2)"
174 ; E_tuple ("c", 3), "(ppx_sexp_test.ml.Exceptions.E_tuple(c 3))"
175 ; E_record { a = "c"; b = 3 }, "(ppx_sexp_test.ml.Exceptions.E_record(a c)(b 3))"
176 ]
177 in
178 List.iter
179 (fun (exn, sexp_as_str) ->
180 let sexp = Sexplib.Sexp.of_string sexp_as_str in
181 assert ([%sexp_of: exn] exn = sexp))
182 cases
183 ;;
184 end
185
186 module Abstract_types_are_allowed_in_structures : sig
187 type t [@@deriving sexp, sexp_grammar]
188 end = struct
189 type t [@@deriving sexp, sexp_grammar]
190 end
191
192 module Manifest_types = struct
193 type a = { t : int }
194 type b = a = { t : int } [@@deriving sexp, sexp_grammar]
195 end
196
197 module Uses_of_exn = struct
198 type t = int * exn [@@deriving sexp_of]
199 end
200
201 module Function_types : sig
202 type t1 = int -> unit [@@deriving sexp, sexp_grammar]
203 type t2 = label:int -> ?optional:int -> unit -> unit [@@deriving sexp, sexp_grammar]
204 end = struct
205 type t1 = int -> unit [@@deriving sexp, sexp_grammar]
206 type t2 = label:int -> ?optional:int -> unit -> unit [@@deriving sexp, sexp_grammar]
207 end
208
209 module No_unused_rec = struct
210 type r = { r : int } [@@deriving sexp, sexp_grammar]
211 end
212
213 module Field_name_should_not_be_rewritten = struct
214 open No_unused_rec
215
216 type nonrec r = { r : r }
217
218 let _ = fun (r : r) -> r.r
219 end
220
221 module Polymorphic_variant_inclusion = struct
222 type sub1 =
223 [ `C1
224 | `C2
225 ]
226 [@@deriving sexp, sexp_grammar]
227
228 type 'b sub2 =
229 [ `C4
230 | `C5 of 'b
231 ]
232 [@@deriving sexp, sexp_grammar]
233
234 type ('a, 'b) t = [ sub1 | `C3 of [ `Nested of 'a ] | 'b sub2 | `C6 ] option
235 [@@deriving sexp, sexp_grammar]
236
237 let%test_unit _ =
238 let cases : ((string * string, float) t * _) list =
239 [ None, "()"
240 ; Some `C1, "(C1)"
241 ; Some `C2, "(C2)"
242 ; Some (`C3 (`Nested ("a", "b"))), "((C3 (Nested (a b))))"
243 ; Some `C4, "(C4)"
244 ; Some (`C5 1.5), "((C5 1.5))"
245 ; Some `C6, "(C6)"
246 ]
247 in
248 List.iter
249 (fun (t, sexp_as_str) ->
250 let sexp = Sexplib.Sexp.of_string sexp_as_str in
251 assert ([%of_sexp: (string * string, float) t] sexp = t);
252 assert ([%sexp_of: (string * string, float) t] t = sexp))
253 cases
254 ;;
255
256 type sub1_alias = sub1 [@@deriving sexp_poly, sexp_grammar]
257
258 type u =
259 [ `A
260 | sub1_alias
261 | `D
262 ]
263 [@@deriving sexp, sexp_grammar]
264
265 let%test_unit _ =
266 let cases : (u * _) list = [ `A, "A"; `C1, "C1"; `C2, "C2"; `D, "D" ] in
267 List.iter
268 (fun (u, sexp_as_str) ->
269 let sexp = Sexplib.Sexp.of_string sexp_as_str in
270 assert ([%of_sexp: u] sexp = u);
271 assert ([%sexp_of: u] u = sexp))
272 cases
273 ;;
274 end
275
276 module Polymorphic_record_field = struct
277 type 'x t =
278 { poly : 'a 'b. 'a list
279 ; maybe_x : 'x option
280 }
281 [@@deriving sexp]
282
283 let%test_unit _ =
284 let t x = { poly = []; maybe_x = Some x } in
285 let sexp = Sexplib.Sexp.of_string "((poly ())(maybe_x (1)))" in
286 assert (t_of_sexp int_of_sexp sexp = t 1);
287 assert (sexp_of_t sexp_of_int (t 1) = sexp)
288 ;;
289 end
290
291 module No_unused_value_warnings : sig end = struct
292 module No_warning : sig
293 type t = [ `A ] [@@deriving sexp, sexp_grammar]
294 end = struct
295 type t = [ `A ] [@@deriving sexp, sexp_grammar]
296 end
297
298 module Empty = struct end
299
300 module No_warning2 (X : sig
301 type t [@@deriving sexp, sexp_grammar]
302 end) =
303 struct end
304
305 (* this one can't be handled (what if Empty was a functor, huh?) *)
306 (* module No_warning3(X : sig type t with sexp end) = Empty *)
307 module type S = sig
308 type t = [ `A ] [@@deriving sexp, sexp_grammar]
309 end
310
311 module No_warning4 : S = struct
312 type t = [ `A ] [@@deriving sexp, sexp_grammar]
313 end
314
315 module No_warning5 : S = (
316 (
317 struct
318 type t = [ `A ] [@@deriving sexp, sexp_grammar]
319 end :
320 S) :
321 S)
322
323 module Nested_functors (M1 : sig
324 type t [@@deriving sexp, sexp_grammar]
325 end) (M2 : sig
326 type t [@@deriving sexp, sexp_grammar]
327 end) =
328 struct end
329
330 let () =
331 let module M : sig
332 type t [@@deriving sexp, sexp_grammar]
333 end = struct
334 type t [@@deriving sexp, sexp_grammar]
335 end
336 in
337 ()
338 ;;
339
340 module Include = struct
341 include (
342 struct
343 type t = int [@@deriving sexp, sexp_grammar]
344 end :
345 sig
346 type t [@@deriving sexp, sexp_grammar]
347 end
348 with type t := int)
349 end
350 end
351
352 module Default = struct
353 type t = { a : int [@default 2] } [@@deriving sexp, sexp_grammar]
354
355 let%test _ = Sexp.(List [ List [ Atom "a"; Atom "1" ] ]) = sexp_of_t { a = 1 }
356 let%test _ = Sexp.(List [ List [ Atom "a"; Atom "2" ] ]) = sexp_of_t { a = 2 }
357 let%test _ = t_of_sexp Sexp.(List [ List [ Atom "a"; Atom "1" ] ]) = { a = 1 }
358 let%test _ = t_of_sexp Sexp.(List [ List [ Atom "a"; Atom "2" ] ]) = { a = 2 }
359 let%test _ = t_of_sexp Sexp.(List []) = { a = 2 }
360 end
361
362 module Type_alias = struct
363 (* checking that the [as 'a] is supported and ignored in signatures, that it still
364 exports the sexp_of_t__ when needed *)
365 module B : sig
366 type a = [ `A ]
367 type t = [ `A ] as 'a constraint 'a = a [@@deriving sexp, sexp_grammar]
368 end = struct
369 type a = [ `A ] [@@deriving sexp, sexp_grammar]
370 type t = [ `A ] [@@deriving sexp, sexp_grammar]
371 end
372
373 let%test _ = Sexp.to_string (B.sexp_of_t `A) = "A"
374 let%test _ = `A = B.t_of_sexp (Sexplib.Sexp.of_string "A")
375
376 module B2 = struct
377 type t =
378 [ B.t
379 | `B
380 ]
381 [@@deriving sexp, sexp_grammar]
382 end
383
384 module C : sig
385 type t = int as 'a [@@deriving sexp, sexp_grammar]
386 end = struct
387 type t = int [@@deriving sexp, sexp_grammar]
388 end
389
390 module D : sig
391 type t = 'a constraint 'a = int [@@deriving sexp, sexp_grammar]
392 end = struct
393 type t = int [@@deriving sexp, sexp_grammar]
394 end
395 end
396
397 module Tricky_variants = struct
398 (* Checking that the generated code compiles (there used to be a problem with subtyping
399 constraints preventing proper generalization). *)
400 type t = [ `a ] [@@deriving sexp, sexp_grammar]
401 type 'a u = [ t | `b of 'a ] * int [@@deriving sexp, sexp_grammar]
402 end
403
404 module Drop_default = struct
405 open! Base
406 open Expect_test_helpers_core
407
408 type t = { a : int } [@@deriving equal]
409
410 let test ?cr t_of_sexp sexp_of_t =
411 let ( = ) = Sexp.( = ) in
412 require ?cr [%here] (Sexp.(List [ List [ Atom "a"; Atom "1" ] ]) = sexp_of_t { a = 1 });
413 require ?cr [%here] (Sexp.(List []) = sexp_of_t { a = 2 });
414 let ( = ) = equal in
415 require ?cr [%here] (t_of_sexp Sexp.(List [ List [ Atom "a"; Atom "1" ] ]) = { a = 1 });
416 require ?cr [%here] (t_of_sexp Sexp.(List [ List [ Atom "a"; Atom "2" ] ]) = { a = 2 });
417 require ?cr [%here] (t_of_sexp Sexp.(List []) = { a = 2 })
418 ;;
419
420 type my_int = int [@@deriving sexp, sexp_grammar]
421
422 module Poly = struct
423 type nonrec t = t = { a : my_int [@default 2] [@sexp_drop_default Poly.( = )] }
424 [@@deriving sexp, sexp_grammar]
425
426 let%test_unit _ = test t_of_sexp sexp_of_t
427 end
428
429 module Equal = struct
430 let equal_my_int = equal_int
431
432 type nonrec t = t = { a : my_int [@default 2] [@sexp_drop_default.equal] }
433 [@@deriving sexp, sexp_grammar]
434
435 let%test_unit _ = test t_of_sexp sexp_of_t
436 end
437
438 module Compare = struct
439 let compare_my_int = compare_int
440
441 type nonrec t = t = { a : my_int [@default 2] [@sexp_drop_default.compare] }
442 [@@deriving sexp, sexp_grammar]
443
444 let%test_unit _ = test t_of_sexp sexp_of_t
445 end
446
447 module Sexp = struct
448 type nonrec t = t = { a : my_int [@default 2] [@sexp_drop_default.sexp] }
449 [@@deriving sexp, sexp_grammar]
450
451 let%test_unit _ = test t_of_sexp sexp_of_t
452 end
453 end
454
455 module Drop_if = struct
456 type t = { a : int [@default 2] [@sexp_drop_if fun x -> x mod 2 = 0] }
457 [@@deriving sexp, sexp_grammar]
458
459 let%test _ = Sexp.(List [ List [ Atom "a"; Atom "1" ] ]) = sexp_of_t { a = 1 }
460 let%test _ = Sexp.(List []) = sexp_of_t { a = 2 }
461 let%test _ = Sexp.(List [ List [ Atom "a"; Atom "3" ] ]) = sexp_of_t { a = 3 }
462 let%test _ = Sexp.(List []) = sexp_of_t { a = 4 }
463 let%test _ = t_of_sexp Sexp.(List [ List [ Atom "a"; Atom "1" ] ]) = { a = 1 }
464 let%test _ = t_of_sexp Sexp.(List [ List [ Atom "a"; Atom "2" ] ]) = { a = 2 }
465 let%test _ = t_of_sexp Sexp.(List [ List [ Atom "a"; Atom "3" ] ]) = { a = 3 }
466 let%test _ = t_of_sexp Sexp.(List [ List [ Atom "a"; Atom "4" ] ]) = { a = 4 }
467 let%test _ = t_of_sexp Sexp.(List []) = { a = 2 }
468
469 type u =
470 { a : int
471 [@sexp_drop_if
472 fun x ->
473 (* pa_type_conv used to drop parens altogether, causing type errors in the
474 following code *)
475 let pair = x, 2 in
476 match Some pair with
477 | None -> true
478 | Some (x, y) -> x = y]
479 }
480 [@@deriving sexp, sexp_grammar]
481 end
482
483 module Omit_nil = struct
484 type natural_option = int
485
486 let sexp_of_natural_option i = if i >= 0 then sexp_of_int i else sexp_of_unit ()
487
488 let natural_option_of_sexp = function
489 | Sexp.List [] -> -1
490 | sexp -> int_of_sexp sexp
491 ;;
492
493 let natural_option_sexp_grammar : natural_option Sexplib0.Sexp_grammar.t =
494 { untyped = Union [ List Empty; Integer ] }
495 ;;
496
497 let check sexp_of_t t_of_sexp str t =
498 let sexp = Sexplib.Sexp.of_string str in
499 assert (sexp = sexp_of_t t);
500 assert (t_of_sexp sexp = t)
501 ;;
502
503 type t = { a : natural_option [@sexp.omit_nil] } [@@deriving sexp, sexp_grammar]
504
505 let%test_unit _ = check sexp_of_t t_of_sexp "()" { a = -1 }
506 let%test_unit _ = check sexp_of_t t_of_sexp "((a 1))" { a = 1 }
507
508 type t2 = A of { a : int list [@sexp.omit_nil] } [@@deriving sexp, sexp_grammar]
509
510 let%test_unit _ = check sexp_of_t2 t2_of_sexp "(A)" (A { a = [] })
511 let%test_unit _ = check sexp_of_t2 t2_of_sexp "(A (a (1)))" (A { a = [ 1 ] })
512 end
513
514 module No_unused_rec_warning = struct
515 type r = { field : r -> unit } [@@deriving sexp_of]
516 end
517
518 module True_and_false = struct
519 type t =
520 | True
521 | False
522 [@@deriving sexp, sexp_grammar]
523
524 let%test _ = Sexp.to_string (sexp_of_t True) = "True"
525 let%test _ = Sexp.to_string (sexp_of_t False) = "False"
526 let%test _ = True = t_of_sexp (Sexplib.Sexp.of_string "True")
527 let%test _ = False = t_of_sexp (Sexplib.Sexp.of_string "False")
528 let%test _ = True = t_of_sexp (Sexplib.Sexp.of_string "true")
529 let%test _ = False = t_of_sexp (Sexplib.Sexp.of_string "false")
530
531 type u =
532 | True of int
533 | False of int
534 [@@deriving sexp, sexp_grammar]
535
536 let%test _ = Sexp.to_string (sexp_of_u (True 1)) = "(True 1)"
537 let%test _ = Sexp.to_string (sexp_of_u (False 2)) = "(False 2)"
538 let%test _ = True 1 = u_of_sexp (Sexplib.Sexp.of_string "(True 1)")
539 let%test _ = False 2 = u_of_sexp (Sexplib.Sexp.of_string "(False 2)")
540 let%test _ = True 1 = u_of_sexp (Sexplib.Sexp.of_string "(true 1)")
541 let%test _ = False 2 = u_of_sexp (Sexplib.Sexp.of_string "(false 2)")
542
543 exception True [@@deriving sexp]
544
545 let%test _ = "ppx_sexp_test.ml.True_and_false.True" = Sexp.to_string (sexp_of_exn True)
546
547 exception False of int [@@deriving sexp]
548
549 let%test _ =
550 "(ppx_sexp_test.ml.True_and_false.False 1)" = Sexp.to_string (sexp_of_exn (False 1))
551 ;;
552
553 type v =
554 [ `True
555 | `False of int
556 ]
557 [@@deriving sexp, sexp_grammar]
558
559 let%test _ = Sexp.to_string (sexp_of_v `True) = "True"
560 let%test _ = Sexp.to_string (sexp_of_v (`False 2)) = "(False 2)"
561 end
562
563 module Gadt = struct
564 let is_eq sexp str =
565 let sexp2 = Sexplib.Sexp.of_string str in
566 if sexp <> sexp2
567 then (
568 Printf.printf "%S vs %S\n%!" (Sexp.to_string sexp) str;
569 assert false)
570 ;;
571
572 (* plain type without argument *)
573 type 'a s = Packed : 'a s [@@deriving sexp_of]
574
575 let%test_unit _ = is_eq ([%sexp_of: int s] Packed) "Packed"
576
577 (* two kind of existential variables *)
578 type 'a t = Packed : 'a * _ * ('b[@sexp.opaque]) -> 'a t [@warning "-3"]
579 [@@deriving sexp_of]
580
581 let%test_unit _ =
582 is_eq ([%sexp_of: int t] (Packed (2, "asd", 1.))) "(Packed 2 _ <opaque>)"
583 ;;
584
585 (* plain type with argument *)
586 type 'a u = A : 'a -> 'a u [@@deriving sexp_of]
587
588 let%test_unit _ = is_eq ([%sexp_of: int u] (A 2)) "(A 2)"
589
590 (* recursive *)
591 type v = A : v option -> v [@@deriving sexp_of]
592
593 let%test_unit _ = is_eq ([%sexp_of: v] (A (Some (A None)))) "(A((A())))"
594
595 (* implicit existential variable *)
596 type w = A : 'a * int * ('a -> string) -> w [@@deriving sexp_of]
597
598 let%test_unit _ = is_eq ([%sexp_of: w] (A (1., 2, string_of_float))) "(A _ 2 <fun>)"
599
600 (* tricky variable naming *)
601 type 'a x = A : 'a -> 'b x [@@deriving sexp_of]
602
603 let%test_unit _ = is_eq ([%sexp_of: int x] (A 1.)) "(A _)"
604
605 (* interaction with inline record *)
606 type _ x2 = A : { x : 'c } -> 'c x2 [@@deriving sexp_of]
607
608 let%test_unit _ = is_eq ([%sexp_of: int x2] (A { x = 1 })) "(A (x 1))"
609
610 (* unused but colliding variables *)
611 type (_, _) y = A : ('a, 'a) y [@@deriving sexp_of]
612
613 let%test_unit _ = is_eq ([%sexp_of: (int, int) y] A) "A"
614
615 (* making sure we're not reversing parameters *)
616 type (_, _) z = A : ('a * 'b) -> ('a, 'b) z [@@deriving sexp_of]
617
618 let%test_unit _ = is_eq ([%sexp_of: (int, string) z] (A (1, "a"))) "(A (1 a))"
619
620 (* interaction with universal quantifiers *)
621 type _ z2 = A : { x : 'c. 'c option } -> 'c z2 [@@deriving sexp_of]
622
623 let%test_unit _ = is_eq ([%sexp_of: unit z2] (A { x = None })) "(A (x ()))"
624 end
625
626 module Anonymous_variable = struct
627 type _ t = int [@@deriving sexp, sexp_grammar]
628
629 let%test _ = Sexp.to_string ([%sexp_of: _ t] 2) = "2"
630 let%test _ = [%of_sexp: _ t] (Sexplib.Sexp.of_string "2") = 2
631
632 (* making sure we don't generate signatures like (_ -> Sexp.t) -> _ t -> Sexp.t which
633 are too general *)
634 module M : sig
635 type _ t [@@deriving sexp, sexp_grammar]
636 end = struct
637 type 'a t = 'a [@@deriving sexp, sexp_grammar]
638 end
639 end
640
641 module Record_field_disambiguation = struct
642 type a =
643 { fl : float
644 ; b : b
645 }
646
647 and b = { fl : int } [@@deriving sexp, sexp_grammar]
648 end
649
650 module Private = struct
651 type t = private int [@@deriving sexp_of]
652 type ('a, 'b) u = private t [@@deriving sexp_of]
653 type ('a, 'b, 'c) v = private ('a, 'b) u [@@deriving sexp_of]
654 end
655
656 module Nonregular_types = struct
657 type 'a nonregular =
658 | Leaf of 'a
659 | Branch of ('a * 'a) nonregular
660 [@@deriving sexp, sexp_grammar]
661
662 type 'a variant = [ `A of 'a ] [@@deriving sexp, sexp_grammar]
663
664 type ('a, 'b) nonregular_with_variant =
665 | Branch of ([ | 'a list variant ], 'b) nonregular_with_variant
666 [@@deriving sexp, sexp_grammar]
667 end
668
669 module Opaque = struct
670 type t = (int[@sexp.opaque]) list [@@deriving sexp, sexp_grammar]
671
672 let sexp = Sexplib.Sexp.of_string "(<opaque> <opaque>)"
673 let t = [ 1; 2 ]
674 let%test _ = sexp_of_t t = sexp
675
676 let%test _ =
677 match t_of_sexp sexp with
678 | _ -> false
679 | exception _ -> true
680 ;;
681
682 type u = ([ `A of int ][@sexp.opaque]) [@@deriving sexp, sexp_grammar]
683
684 let sexp = Sexplib.Sexp.of_string "<opaque>"
685 let u = `A 1
686 let%test _ = sexp_of_u u = sexp
687
688 let%test _ =
689 match u_of_sexp sexp with
690 | _ -> false
691 | exception _ -> true
692 ;;
693 end
694
695 module Optional = struct
696 type t = { optional : int option [@sexp.option] } [@@deriving sexp, sexp_grammar]
697
698 let sexp = Sexplib.Sexp.of_string "()"
699 let t = { optional = None }
700 let%test _ = t_of_sexp sexp = t
701 let%test _ = sexp_of_t t = sexp
702 let sexp = Sexplib.Sexp.of_string "((optional 5))"
703 let t = { optional = Some 5 }
704 let%test _ = t_of_sexp sexp = t
705 let%test _ = sexp_of_t t = sexp
706 end
707
708 module Nonempty = struct
709 type t =
710 { list : int list [@sexp.list]
711 ; array : int array [@sexp.array]
712 }
713 [@@deriving sexp, sexp_grammar]
714
715 let sexp = Sexplib.Sexp.of_string "()"
716 let t = { list = []; array = [||] }
717 let%test _ = t_of_sexp sexp = t
718 let%test _ = sexp_of_t t = sexp
719 let sexp = Sexplib.Sexp.of_string "((list (1 2 3)) (array (3 2 1)))"
720 let t = { list = [ 1; 2; 3 ]; array = [| 3; 2; 1 |] }
721 let%test _ = t_of_sexp sexp = t
722 let%test _ = sexp_of_t t = sexp
723 end
724
725 module Boolean = struct
726 type t = { no_arg : bool [@sexp.bool] } [@@deriving sexp, sexp_grammar]
727
728 let sexp = Sexplib.Sexp.of_string "()"
729 let t = { no_arg = false }
730 let%test _ = t_of_sexp sexp = t
731 let%test _ = sexp_of_t t = sexp
732 let sexp = Sexplib.Sexp.of_string "((no_arg))"
733 let t = { no_arg = true }
734 let%test _ = t_of_sexp sexp = t
735 let%test _ = sexp_of_t t = sexp
736
737 type t_allow_extra_fields = { no_arg : bool [@sexp.bool] }
738 [@@deriving sexp, sexp_grammar] [@@sexp.allow_extra_fields]
739
740 let%expect_test _ =
741 Expect_test_helpers_core.require_does_raise ~cr:CR_soon [%here] (fun () ->
742 let r = t_allow_extra_fields_of_sexp (Sexplib.Sexp.of_string "((no_arg true))") in
743 print_endline (Bool.to_string r.no_arg));
744 [%expect
745 {|
746 (Of_sexp_error
747 "ppx_sexp_test.ml.Boolean.t_allow_extra_fields_of_sexp: record conversion: a [sexp.bool] field was given a payload."
748 (invalid_sexp ((no_arg true))))
749 |}]
750 ;;
751 end
752
753 module Inline = struct
754 type t = A of int list [@sexp.list] [@@deriving sexp, sexp_grammar]
755
756 let sexp = Sexplib.Sexp.of_string "(A 1 2 3)"
757 let t = A [ 1; 2; 3 ]
758 let%test _ = t_of_sexp sexp = t
759 let%test _ = sexp_of_t t = sexp
760
761 type u = [ `A of int list [@sexp.list] ] [@@deriving sexp, sexp_grammar]
762
763 let sexp = Sexplib.Sexp.of_string "(A 1 2 3)"
764 let u = `A [ 1; 2; 3 ]
765 let%test _ = u_of_sexp sexp = u
766 let%test _ = sexp_of_u u = sexp
767 end
768
769 module Variance = struct
770 type (+'a, -'b, 'c, +_, -_, _) t [@@deriving sexp, sexp_grammar]
771 end
772
773 module Clash = struct
774 (* Same name for type-var and type-name; must be careful when introducing rigid type names. *)
775 type 'hey hey = Hey of 'hey [@@deriving sexp, sexp_grammar]
776 type 'hey rigid_hey = Hey of 'hey [@@deriving sexp, sexp_grammar]
777 type ('foo, 'rigid_foo) foo = Foo of 'foo [@@deriving sexp, sexp_grammar]
778 type 'rigid_bar rigid_rigid_bar = Bar [@@deriving sexp, sexp_grammar]
779 end
780
781 module Applicative_functor_types = struct
782 module Bidirectional_map = struct
783 type ('k1, 'k2) t
784
785 module S (K1 : sig
786 type t
787 end) (K2 : sig
788 type t
789 end) =
790 struct
791 type nonrec t = (K1.t, K2.t) t
792 end
793
794 module type Of_sexpable = sig
795 type t [@@deriving of_sexp, sexp_grammar]
796 end
797
798 let s__t_of_sexp
799 (type k1 k2)
800 (module K1 : Of_sexpable with type t = k1)
801 (module K2 : Of_sexpable with type t = k2)
802 (_ : Sexp.t)
803 : (k1, k2) t
804 =
805 assert false
806 ;;
807
808 (* You would actually have to write this manually for functors. *)
809 let s__t_sexp_grammar
810 (type k1 k2)
811 (module K1 : Of_sexpable with type t = k1)
812 (module K2 : Of_sexpable with type t = k2)
813 =
814 [%sexp_grammar: (K1.t * K2.t) list]
815 ;;
816 end
817
818 module Int = struct
819 type t = int [@@deriving of_sexp, sexp_grammar]
820 end
821
822 module String = struct
823 type t = string [@@deriving of_sexp, sexp_grammar]
824 end
825
826 module M : sig
827 type t = Bidirectional_map.S(String)(Int).t [@@deriving of_sexp, sexp_grammar]
828 end = struct
829 type t = Bidirectional_map.S(String)(Int).t [@@deriving of_sexp, sexp_grammar]
830 end
831 end
832
833 module Type_extensions = struct
834 let _ = ([%sexp_of: int] : [%sexp_of: int])
835 let _ = ([%of_sexp: int] : [%of_sexp: int])
836 end
837
838 module Allow_extra_fields = struct
839 let should_raise f x =
840 try
841 ignore (f x);
842 false
843 with
844 | _ -> true
845 ;;
846
847 module M1 = struct
848 type t1 = { a : int } [@@deriving sexp]
849 type t2 = t1 = { a : int } [@@deriving sexp, sexp_grammar] [@@sexp.allow_extra_fields]
850
851 let sexp = Sexplib.Sexp.of_string "((a 1))"
852 let sexp_extra = Sexplib.Sexp.of_string "((a 1)(b 2))"
853 let%test _ = t2_of_sexp sexp = t2_of_sexp sexp_extra
854 let%test _ = t1_of_sexp sexp = t2_of_sexp sexp
855 let%test _ = should_raise t1_of_sexp sexp_extra
856
857 let%expect_test _ =
858 Expect_test_helpers_core.require_does_raise ~cr:CR_soon [%here] (fun () ->
859 t2_of_sexp (Sexplib.Sexp.of_string "((a 1)(a))"));
860 [%expect
861 {|
862 (Of_sexp_error
863 "ppx_sexp_test.ml.Allow_extra_fields.M1.t2_of_sexp: duplicate fields: a"
864 (invalid_sexp ((a 1) (a))))
865 |}]
866 ;;
867 end
868
869 module M2 = struct
870 type t1 = A of { a : int list } [@@deriving sexp]
871
872 type t2 = t1 = A of { a : int list } [@sexp.allow_extra_fields]
873 [@@deriving sexp, sexp_grammar]
874
875 let sexp = Sexplib.Sexp.of_string "(A (a (0)))"
876 let sexp_extra = Sexplib.Sexp.of_string "(A (a (0))(b 2))"
877 let%test _ = t2_of_sexp sexp = t2_of_sexp sexp_extra
878 let%test _ = t1_of_sexp sexp = t2_of_sexp sexp
879 let%test _ = should_raise t1_of_sexp sexp_extra
880 end
881 end
882
883 module Default_values_and_polymorphism = struct
884 type t =
885 { a : int list [@sexp.list]
886 ; b : 'b. 'b -> int
887 }
888 [@@deriving of_sexp]
889 end