Source file ppx.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
open Migrate_parsetree
open Ast_405
open Ppxx.Compilerlib
open Parsetree
open Asttypes
open Ast_mapper
open Longident
open Ppxx.Helper
open Ppxx.Utils
module Ppx = Ppxx.Ppx
let ($.) l s = Ldot(l,s)
let exp_of_position p =
let open Lexing in
let mklid s = at (Lident "Lexing" $. s)
in
let open Exp in
record [
mklid "pos_fname", string p.pos_fname;
mklid "pos_lnum", int p.pos_lnum;
mklid "pos_bol", int p.pos_bol;
mklid "pos_cnum", int p.pos_cnum
] None
let exp_of_location l =
let open Location in
let mklid s = at ~loc:(ghost l) (Lident "Ppx_test" $. "Location" $. s) in
let open Exp in
record ~loc:l [
mklid "loc_start", exp_of_position l.loc_start;
mklid "loc_end", exp_of_position l.loc_end;
mklid "loc_ghost", bool l.loc_ghost
] None
let rec exp_of_longident =
let open Exp in
let mklid s = at (Lident "Ppx_test" $. "Longident" $. s) in
function
| Lident s ->
construct (mklid "Lident") (Some (string s))
| Ldot (t, s) ->
construct (mklid "Ldot") (Some (tuple [exp_of_longident t; string s]))
| Lapply (t1, t2) ->
construct (mklid "Lapply") (Some (tuple [exp_of_longident t1; exp_of_longident t2]))
let extend_package super =
let expr self e = match e with
| { pexp_desc = Pexp_ident { txt= Lident "__FOR_PACKAGE__"; loc } } ->
begin match !Clflags.for_package with
| None ->
let open Exp in
constraint_ ~loc
(construct ~loc { txt = Lident "None"; loc } None)
Typ.(constr { txt = Lident "option"; loc } [ constr { txt = Lident "string"; loc } [] ])
| Some s ->
Exp.construct ~loc {txt = Lident "Some"; loc } & Some (Exp.string s)
end
| _ -> super.expr self e
in
{ super with expr }
let with_ref ref update f =
let back = !ref in
update ();
let res = f () in
ref := back;
res
let current_structure_or_signature : [ `Sig of signature | `Str of structure ] option ref = ref None
let _with_current_structure_or_signature x =
with_ref
current_structure_or_signature
(fun () -> current_structure_or_signature := Some x)
module Current_module_path = struct
let x = ref None
let top_module () =
match !Location.input_name with
| "//toplevel//" as s -> Lident s
| fname ->
let base = Filename.basename fname in
let chopped = try Filename.chop_extension base with _ -> base in
let mn = String.capitalize_ascii chopped in
match !Clflags.for_package with
| None -> Lident mn
| Some p -> Ldot (Lident p, mn)
let set y = x := Some y
let get () = match !x with
| None ->
let y = top_module () in
x := Some y;
y
| Some y -> y
let with_ x f =
let back = get () in
set x;
let res = f () in
set back;
res
end
let attr_module_path mp =
(at "ppx_test_module_path",
PStr [ Str.mk (Pstr_eval (Exp.ident (at mp), [])) ])
let get_module_path_from_attr = function
| ({ txt="ppx_test_module_path" },
PStr [ {pstr_desc= Pstr_eval ({pexp_desc= Pexp_ident {txt}}, [])}]) -> Some txt
| _ -> None
let get_module_path_from_attrs ats =
match
List.partition_map (fun a ->
match get_module_path_from_attr a with
| Some txt -> `Right txt
| None -> `Left a) ats
with
| (rest, [x]) -> Some x, rest
| (rest, [] ) -> None, rest
| _ -> assert false
let rec annotate_module_expr mp mexp =
let add mexp =
with_loc mexp.pmod_loc & fun () ->
{ mexp with pmod_attributes = attr_module_path mp :: mexp.pmod_attributes } in
match mexp.pmod_desc with
| Pmod_structure _ -> add mexp
| Pmod_constraint (mexp, mty) ->
{ mexp with pmod_desc = Pmod_constraint (annotate_module_expr mp mexp,
annotate_module_type mp mty) }
| Pmod_functor (({txt} as n), mtyo, mexp') ->
let mp = Lapply(mp, Lident txt) in
{ mexp with pmod_desc = Pmod_functor (n, mtyo, annotate_module_expr mp mexp') }
| _ -> mexp
and annotate_module_type mp mty =
let add mty = { mty with pmty_attributes = attr_module_path mp :: mty.pmty_attributes } in
match mty.pmty_desc with
| Pmty_signature _ -> add mty
| _ -> mty
let extend_module_path_tracking super =
let module_binding self mb =
let mp = Ldot (Current_module_path.get (), mb.pmb_name.txt) in
let mb = { mb with pmb_expr = annotate_module_expr mp mb.pmb_expr } in
Current_module_path.with_ mp & fun () ->
super.module_binding self mb
in
let module_declaration self md =
let mp = Ldot (Current_module_path.get (), md.pmd_name.txt) in
let md = { md with pmd_type = annotate_module_type mp md.pmd_type } in
Current_module_path.with_ mp & fun () ->
super.module_declaration self md
in
let module_expr self mexp =
match get_module_path_from_attrs mexp.pmod_attributes with
| Some mp, rest ->
Current_module_path.with_ mp & fun () ->
super.module_expr self {mexp with pmod_attributes = rest}
| None, rest ->
let mp = Ldot (Current_module_path.get (), "_") in
Current_module_path.with_ mp & fun () ->
super.module_expr self {mexp with pmod_attributes = rest}
in
let module_type self mty =
match get_module_path_from_attrs mty.pmty_attributes with
| Some mp, rest ->
Current_module_path.with_ mp & fun () ->
super.module_type self { mty with pmty_attributes = rest }
| None, rest ->
let mp = Ldot (Current_module_path.get (), "_") in
Current_module_path.with_ mp & fun () ->
super.module_type self { mty with pmty_attributes = rest }
in
{ super with module_binding; module_declaration; module_expr; module_type }
type test_type = Unit | Bool | Fail
let drop_tests = ref false
let warn_dupes = ref false
let top_name = ref None
let rec lident_concat l1 = function
| Lident s -> Ldot (l1, s)
| Ldot (t, s) -> Ldot (lident_concat l1 t, s)
| Lapply (t1, t2) -> Lapply (lident_concat l1 t1, t2)
let return_type lid =
match lid with
| Lident s | Ldot (_, s) ->
if s.[String.length s - 1] = '_' then Unit
else if String.(length s > 5 && sub s (length s - 5) 5 = "_fail") then Fail
else Bool
| _ -> Bool
let add_top_name lid =
match !top_name with
| None -> lid
| Some top_lid -> lident_concat top_lid lid
module Tests = Set.Make(struct type t = Longident.t let compare = compare end)
let tests = ref Tests.empty
let add_test loc lid =
let anon = function
| Lident "_" -> true
| Lident _ -> false
| Ldot(_, "_") -> true
| Ldot(_, _) -> false
| Lapply _ -> assert false
in
if anon lid then ()
else
if Tests.mem lid !tests then begin
if !warn_dupes then
Ppxx.Utils.warnf "%a: ppx_test: Test name %s is already defined"
Location.format loc
(Format.sprintf "%a" Longident.format lid)
end else tests := Tests.add lid !tests
let test_item self test = function
| Pstr_eval (e, _attr) ->
let name =
let lid = Current_module_path.get () in
let mpath = Ldot (lid, "_") in
add_top_name & mpath
in
let test = "test" in
let open Exp in
Str.value Nonrecursive [
Vb.mk (Pat.unit ())
& open_ (lid "PTest.TestTool")
& apply
(ident & at & Lident "PTest" $. test)
[ Nolabel, exp_of_location e.pexp_loc;
Nolabel, exp_of_longident name;
Nolabel, fun_ Nolabel None (Pat.unit ()) (self.expr self e) ]
]
| Pstr_value (Nonrecursive, vbs) ->
let f vb =
let loc = vb.pvb_loc in
let name = match vb.pvb_pat with
| { ppat_desc = Ppat_any } ->
let lid = Current_module_path.get () in
let mpath = Ldot (lid, "_") in
add_top_name & mpath
| { ppat_desc = Ppat_constant (Pconst_string (name, _)) } ->
let mpath = Current_module_path.get () in
add_top_name & lident_concat mpath (Lident name)
| { ppat_desc = Ppat_var {txt=name} } ->
let lid = Current_module_path.get () in
let mpath = Ldot (lid, name) in
add_top_name & mpath
| { ppat_desc = Ppat_construct ({ txt= lid }, None) } ->
let mpath = Current_module_path.get () in
add_top_name & lident_concat mpath lid
| _ -> assert false
in
add_test loc name;
let return_type = match test with
| "TEST_UNIT" -> Unit
| "TEST" -> return_type name
| "TEST_FAIL" -> Fail
| _ -> assert false
in
let test = match return_type with
| Unit -> "test_unit"
| Bool -> "test"
| Fail -> "test_fail"
in
let open Exp in
Vb.mk (Pat.unit ())
(apply
(ident & at & Lident "PTest" $. test)
[ Nolabel, exp_of_location loc;
Nolabel, exp_of_longident name;
Nolabel, fun_ Nolabel None (Pat.unit ()) (self.expr self vb.pvb_expr) ])
in
Str.value Nonrecursive (List.map f vbs)
| _ -> assert false
let extend_let_test super =
let structure_item self = function
| { pstr_desc= Pstr_extension ( ( { txt = ("TEST" | "TEST_UNIT" | "TEST_FAIL") }, _ ), _) } when !drop_tests -> []
| { pstr_desc=
Pstr_extension (
( { txt = ("TEST" | "TEST_UNIT" | "TEST_FAIL") }, _ ),
_) } as sitem when !drop_tests ->
[ { sitem with pstr_desc= Pstr_eval (Exp.unit (), []) } ]
| { pstr_desc=
Pstr_extension (
( { txt = ("TEST" | "TEST_UNIT" | "TEST_FAIL" as test) },
PStr [ { pstr_desc= (Pstr_value (Nonrecursive, _vbs) as desc)} ]),
_) } ->
[ test_item self test desc ]
| { pstr_desc=
Pstr_extension (
( { txt = ("TEST" as test) },
PStr sitems),
_) } ->
List.map (fun sitem -> test_item self test sitem.pstr_desc) sitems
| x -> [ super.structure_item self x ]
in
let structure self sitems = List.concat & List.map (structure_item self) sitems
in
{ super with structure; }
let make_mapper () =
current_structure_or_signature := None;
Current_module_path.x := None;
tests := Tests.empty;
extend_let_test
& extend_module_path_tracking
& extend_package default_mapper
let parse_as_lident s = match Longident.parse s with
| Lident "" -> None
| l -> Some l
let opts =
[ "-drop-tests", Arg.Set drop_tests, "Drop tests"
; "-warn-dupes", Arg.Set warn_dupes, "Warn tests with the same names"
; "-top-name",
Arg.String (fun s ->
match parse_as_lident s with
| None -> raise_errorf "-top-name must take a valid module path"
| Some l -> top_name := Some l),
"Set the top module name"
]
include Ppxx.Ppx.Make(struct
let name = "ppx_test"
let options = opts
let make_mapper _config _cookies = make_mapper ()
end)
let () = register ()