Debian Codegraph @

test/test.ml

raw symbols SCIP
1 open Core
2
3 let%test_unit "polymorphic variant, variant, list, literal" =
4 let module M = struct
5 type normal_blop = Blop of int [@@deriving sexp_of]
6
7 type variant_blop =
8 [ `Message of string
9 | `Blop of int
10 ]
11 [@@deriving sexp_of]
12 end
13 in
14 [%test_result: Sexp.t]
15 ~expect:
16 (List
17 [ M.sexp_of_variant_blop (`Message "string")
18 ; M.sexp_of_variant_blop (`Blop 2)
19 ; M.sexp_of_normal_blop (Blop 2)
20 ])
21 [%sexp [ `Message "string"; `Blop 2; Blop 2 ]]
22 ;;
23
24 let%test_unit "record, if" =
25 [%test_result: Sexp.t]
26 ~expect:
27 (List [ List [ Atom "message"; Atom "string" ]; List [ Atom "A.blop"; Atom "1" ] ])
28 [%sexp { message = "string"; A.blop = (if true then 1 else `two) }]
29 ;;
30
31 module A = struct
32 type t = int [@@deriving sexp_of]
33 end
34
35 let a : A.t = 2
36
37 let%test_unit "tuple, explicit types" =
38 [%test_result: Sexp.t]
39 ~expect:(List [ Atom "2"; Atom "1" ])
40 [%sexp (a : A.t), (lazy 1 : int Lazy.t)]
41 ;;
42
43 let%test_unit "constructed list" =
44 let int_list = [ 2; 3 ] in
45 [%test_result: Sexp.t]
46 ~expect:(List [ Atom "one"; Atom "2"; Atom "3" ])
47 [%sexp `one :: (int_list : int list)]
48 ;;
49
50 let%test_unit "strange case doesn't raise an exception" =
51 [%test_result: Sexp.t] ~expect:(List [ Atom "A"; Atom "B" ]) [%sexp `A :: `B]
52 ;;
53
54 let%test_unit "sexp.option everywhere except record fields" =
55 [%test_result: Sexp.t]
56 ~expect:
57 (List
58 [ Atom "A"; List [ Atom "B"; Atom "1" ]; List [ Atom "Some \"D\""; Atom "D" ] ])
59 [%sexp
60 `A
61 , B (Some 1 : (int option[@sexp.option]))
62 , C (None : (int option[@sexp.option]))
63 , ~~(Some "D" : (string option[@sexp.option]))
64 , ~~(None : (string option[@sexp.option]))]
65 ;;
66
67 let%test_module "optional record field via sexp.option" =
68 (module struct
69 let none = None
70 let some x = Some x
71
72 let%test_unit "absent" =
73 [%test_result: Sexp.t]
74 ~expect:(List [ List [ Atom "a"; Atom "1" ]; List [ Atom "c"; Atom "3" ] ])
75 [%sexp { a = 1; b = (none : (int option[@sexp.option])); c = 3 }]
76 ;;
77
78 let%test_unit "present" =
79 [%test_result: Sexp.t]
80 ~expect:
81 (List
82 [ List [ Atom "a"; Atom "1" ]
83 ; List [ Atom "b"; Atom "2" ]
84 ; List [ Atom "c"; Atom "3" ]
85 ])
86 [%sexp { a = 1; b = (some 2 : (int option[@sexp.option])); c = 3 }]
87 ;;
88
89 let%test_unit "all absent" =
90 [%test_result: Sexp.t]
91 ~expect:(List [])
92 [%sexp
93 { a = (none : (int option[@sexp.option]))
94 ; b = (none : (int option[@sexp.option]))
95 }]
96 ;;
97
98 let%test_unit "tail as variable name" =
99 let tail = Some [ "bar"; "bat" ] in
100 [%test_result: Sexp.t]
101 ~expect:
102 (List
103 [ List [ Atom "head"; Atom "foo" ]
104 ; List [ Atom "tail"; List [ Atom "bar"; Atom "bat" ] ]
105 ])
106 [%sexp { head = "foo"; tail : (string list option[@sexp.option]) }]
107 ;;
108 end)
109 ;;
110
111 let%test_unit "omit_nil" =
112 let[@cold] check sexp str =
113 [%test_result: string] (Sexp.to_string_hum sexp) ~expect:str
114 in
115 check [%sexp { a = ([ 1 ] : (int list[@omit_nil])) }] "((a (1)))";
116 check [%sexp { a = ([] : (int list[@omit_nil])) }] "()";
117 check [%sexp A, B ([ 1 ] : (int list[@omit_nil]))] "(A (B (1)))";
118 check [%sexp A, B ([] : (int list[@omit_nil]))] "(A)"
119 ;;
120
121 let%test_module "expressions and their evaluation" =
122 (module struct
123 let%test_unit "at toplevel" =
124 let x = 1 in
125 [%test_result: Sexp.t] ~expect:(List [ Atom "x"; Atom "1" ]) [%sexp ~~(x : int)]
126 ;;
127
128 let%test_unit "anywhere" =
129 let x = 1
130 and y = 2 in
131 [%test_result: Sexp.t]
132 ~expect:
133 (List
134 [ Atom "message"
135 ; List [ Atom "x"; Atom "1" ]
136 ; List [ Atom "x + y"; Atom "3" ]
137 ])
138 [%sexp "message", ~~(x : int), ~~(x + y : int)]
139 ;;
140 end)
141 ;;
142
143 let%test_unit "[%string]" =
144 let b = "b" in
145 [%test_result: Sexp.t] [%sexp [%string "a%{b}c"]] ~expect:[%sexp "abc"]
146 ;;
147
148 let _no_warnings_from_merlin_check_about_overlapping_locations =
149 let module Foo = struct
150 type t = [ `A ]
151
152 let sexp_of_t _ = Sexp.Atom "A"
153 end
154 in
155 let foo = `A in
156 let maybe_foo = Some `A in
157 ( [%sexp { foo : Foo.t }]
158 , [%sexp { foo : [< `A ] }]
159 , [%sexp { foo : [< Foo.t ] }]
160 , [%sexp { maybe_foo : Foo.t option }]
161 , [%sexp { maybe_foo : [< `A ] option }]
162 , [%sexp { maybe_foo : [< Foo.t ] option }] )
163 ;;
164
165 let%test_unit "[%sexp] is not lazy" =
166 let side_effect = ref false in
167 let _ = [%sexp (side_effect := true : unit)] in
168 [%test_result: bool] ~expect:true !side_effect
169 ;;
170
171 let%test_unit "[%lazy_sexp] is lazy" =
172 let side_effect = ref false in
173 let delayed_sexp = [%lazy_sexp (side_effect := true : unit)] in
174 [%test_result: bool] ~expect:false !side_effect;
175 let _ = Lazy.force delayed_sexp in
176 [%test_result: bool] ~expect:true !side_effect
177 ;;
178
179 let%test_module "[%lazy_sexp] output" =
180 (module struct
181 let%test_unit "polymorphic variant, variant, list, literal" =
182 let module M = struct
183 type normal_blop = Blop of int [@@deriving sexp_of]
184
185 type variant_blop =
186 [ `Message of string
187 | `Blop of int
188 ]
189 [@@deriving sexp_of]
190 end
191 in
192 [%test_result: Sexp.t]
193 ~expect:
194 (List
195 [ M.sexp_of_variant_blop (`Message "string")
196 ; M.sexp_of_variant_blop (`Blop 2)
197 ; M.sexp_of_normal_blop (Blop 2)
198 ])
199 [%sexp [ `Message "string"; `Blop 2; Blop 2 ]]
200 ;;
201
202 let%test_unit "record, if" =
203 [%test_result: Sexp.t]
204 ~expect:
205 (List
206 [ List [ Atom "message"; Atom "string" ]; List [ Atom "A.blop"; Atom "1" ] ])
207 [%sexp { message = "string"; A.blop = (if true then 1 else `two) }]
208 ;;
209
210 let%test_unit "tuple, explicit types" =
211 [%test_result: Sexp.t]
212 ~expect:(List [ Atom "2"; Atom "1" ])
213 [%sexp (a : A.t), (lazy 1 : int Lazy.t)]
214 ;;
215
216 let%test_unit "constructed list" =
217 let int_list = [ 2; 3 ] in
218 [%test_result: Sexp.t]
219 ~expect:(List [ Atom "one"; Atom "2"; Atom "3" ])
220 [%sexp `one :: (int_list : int list)]
221 ;;
222
223 let%test_unit "strange case doesn't raise an exception" =
224 [%test_result: Sexp.t] ~expect:(List [ Atom "A"; Atom "B" ]) [%sexp `A :: `B]
225 ;;
226
227 let%test_unit "sexp.option everywhere except record fields" =
228 [%test_result: Sexp.t]
229 ~expect:
230 (List
231 [ Atom "A"
232 ; List [ Atom "B"; Atom "1" ]
233 ; List [ Atom "Some \"D\""; Atom "D" ]
234 ])
235 [%sexp
236 `A
237 , B (Some 1 : (int option[@sexp.option]))
238 , C (None : (int option[@sexp.option]))
239 , ~~(Some "D" : (string option[@sexp.option]))
240 , ~~(None : (string option[@sexp.option]))]
241 ;;
242 end)
243 ;;