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
open! Base
open Types
module Correction = struct
type t =
| New_payload :
[< Expectation.Behavior_type.t ] Expectation.t * Output.Reconciled.t
-> t
| Unreachable : [ `Expect ] Expectation.t -> t
(** [Some (loc, patch)] if [correction] warrants inserting [patch] into the rewritten
file at [loc], [None] if no change is needed from [correction]. *)
let to_patch_opt ~(expect_node_formatting : Expect_node_formatting.t) correction =
match correction with
| New_payload
( { position
; behavior
; on_incorrect_output = T on_incorrect_output
; inconsistent_outputs_message = _
; payload_type = _
}
, test_output ) ->
let whitespace =
match position with
| Insert { body_loc = { start_pos; start_bol; _ }; _ } ->
let let_offset = start_pos - start_bol in
let indent =
let_offset
+
match on_incorrect_output.kind with
| Extension -> expect_node_formatting.indent
| Attribute -> 0
in
let whitespace = "\n" ^ String.make indent ' ' in
whitespace
| Overwrite _ -> ""
in
let tag =
match behavior with
| Expect { payload = { tag; _ }; on_unreachable = _; reachability = _ } -> tag
| Unreachable _ -> String_node_format.Delimiter.default
in
let loc, correction =
match position, on_incorrect_output with
| ( Overwrite { payload = Some payload_loc; whole_node = _ }
, { kind = Extension; hand = Longhand; name = _ } ) ->
let correction =
Output.to_formatted_payload ~tag test_output
|> Output.Payload.to_source_code_string
in
payload_loc, correction
| (Overwrite { payload = _; whole_node = loc } | Insert { loc; body_loc = _ }), _
->
( loc
, Output.to_source_code_string
~expect_node_formatting
~node_shape:(T on_incorrect_output)
~tag
test_output )
in
Some (loc, whitespace ^ correction)
| Unreachable
{ behavior = Expect { on_unreachable; payload = _; reachability = _ }
; on_incorrect_output = T on_incorrect_output
; position
; inconsistent_outputs_message = _
; payload_type = _
} ->
let loc = Expectation.Insert_loc.loc position in
(match on_unreachable with
| Silent -> None
| Delete -> Some (loc, "")
| Replace_with_unreachable ->
let prefix =
match on_incorrect_output.kind with
| Extension -> expect_node_formatting.extension_sigil
| Attribute -> expect_node_formatting.attribute_sigil
in
Some (loc, Printf.sprintf "[%sexpect.unreachable]" prefix))
;;
let to_diffs ~expect_node_formatting ~original_file_contents correction =
let safe_byte_get string i =
if i >= 0 && i < String.length string then Some (String.get string i) else None
in
match to_patch_opt ~expect_node_formatting correction with
| None -> []
| Some (loc, diff) ->
let ({ start_bol; start_pos; end_pos } : Compact_loc.t) = loc in
let main_correction = [ loc, diff ] in
let additional_corrections =
let remove_empty_line_from_deleted_uncaught_exn =
match correction with
| Unreachable { on_incorrect_output = T { kind = Attribute; _ }; _ } ->
(match
( safe_byte_get original_file_contents (start_pos - 1)
, safe_byte_get original_file_contents end_pos )
with
| Some '\n', (None | Some '\n') ->
Some
( { Compact_loc.start_pos = start_pos - 1
; end_pos = start_pos
; start_bol
}
, "" )
| _ -> None)
| _ -> None
in
let add_semicolon_before_trailing_expect =
match correction with
| New_payload
( { on_incorrect_output = T { kind = Extension; _ }
; position = Insert { body_loc; _ }
; _
}
, _ ) -> Some ({ body_loc with start_pos = body_loc.end_pos }, ";")
| _ -> None
in
List.concat_map
~f:Option.to_list
[ remove_empty_line_from_deleted_uncaught_exn
; add_semicolon_before_trailing_expect
]
in
additional_corrections @ main_correction
;;
end
type one_output =
{ result : Output.Test_result.t
; raw : string
}
type one_run =
| Reached_with_output of one_output
| Did_not_reach
type 'behavior inner =
| Test :
{ expectation : ([< Expectation.Behavior_type.t ] as 'behavior) Expectation.t
; results : one_run Queue.t
; mutable reached_this_run : bool
}
-> 'behavior inner
type t = T : 'behavior inner -> t
let to_correction
~expect_node_formatting
~cr_for_multiple_outputs
(T (Test { expectation; results; reached_this_run = _ }))
: Correction.t option
=
let results_list = Queue.to_list results in
let unreached_list, outputs_list =
List.partition_map results_list ~f:(function
| Did_not_reach -> First ()
| Reached_with_output output -> Second output)
in
let distinct_outputs =
List.dedup_and_sort
~compare:
(Comparable.lift ~f:(fun { result; _ } -> result) Output.Test_result.compare)
outputs_list
in
let was_reached = List.is_empty unreached_list in
let reachability_behavior =
match expectation.behavior with
| Expect { reachability; payload = _; on_unreachable = _ } -> reachability
| Unreachable { reachability_of_corrected } -> reachability_of_corrected
in
let correction_for_single_result : Output.Test_result.t -> Correction.t option
= function
| Pass -> None
| Fail received -> Some (New_payload (expectation, received))
in
match distinct_outputs, (was_reached, reachability_behavior) with
| [], (_, _) ->
(match expectation.behavior with
| Unreachable _ -> None
| Expect _ as behavior ->
Some (Unreachable (Expectation.with_behavior expectation behavior)))
| [ { result; _ } ], (true, _ | _, Can_reach) ->
correction_for_single_result result
| _ :: _ :: _, _ | _, (false, Must_reach) ->
let outputs =
results_list
|> List.map ~f:(function
| Reached_with_output { raw; _ } -> raw
| Did_not_reach ->
Printf.sprintf
"<expect test ran without %s>"
expectation.inconsistent_outputs_message)
in
cr_for_multiple_outputs ~output_name:expectation.inconsistent_outputs_message ~outputs
|> Output.Formatter.apply (Expectation.formatter ~expect_node_formatting expectation)
|> Output.fail
|> correction_for_single_result
;;
let record_and_return_result
(type behavior)
~expect_node_formatting
~failure_ref
~test_output_raw
(Test ({ expectation; results; reached_this_run = _ } as t) : behavior inner)
=
let test_output =
Output.Formatter.apply
(Expectation.formatter ~expect_node_formatting expectation)
test_output_raw
in
let (result : Output.Test_result.t), (tag : String_node_format.Delimiter.t) =
match expectation.behavior with
| Unreachable _ -> Output.fail test_output, T (Tag "")
| Expect { payload = { contents; tag }; on_unreachable = _; reachability = _ } ->
Output.reconcile ~expected_output:contents ~test_output, tag
in
(match result with
| Fail _ -> failure_ref := true
| Pass -> ());
Queue.enqueue results (Reached_with_output { result; raw = test_output_raw });
t.reached_this_run <- true;
result, tag
;;
let of_expectation expectation =
T (Test { expectation; results = Queue.create (); reached_this_run = false })
;;
let record_end_of_run t =
let (T (Test { expectation = _; results; reached_this_run })) = t in
if not reached_this_run then Queue.enqueue results Did_not_reach
;;
let record_result ~expect_node_formatting ~failure_ref ~test_output_raw (T inner) =
ignore
(record_and_return_result ~expect_node_formatting ~failure_ref ~test_output_raw inner
: Output.Test_result.t * String_node_format.Delimiter.t)
;;
module Global_results_table = struct
type node = t
type postprocess = node list Write_corrected_file.Patch_with_file_contents.t
type file =
{ expectations : node Hashtbl.M(Expectation_id).t
; postprocess : postprocess
}
let global_results_table : file Hashtbl.M(String).t = Hashtbl.create (module String)
let find_test ~absolute_filename ~(test_id : Expectation_id.t) =
Hashtbl.find global_results_table absolute_filename
|> Option.bind ~f:(fun { expectations; _ } -> Hashtbl.find expectations test_id)
|> Option.value_exn
~error:
(Error.of_string
(Printf.sprintf
"Internal expect test bug: could not find test\nFile: %s\nID: %d"
absolute_filename
(Expectation_id.to_int_exn test_id)))
;;
let initialize_and_register_tests ~absolute_filename tests postprocess =
let tests_as_in_table = Queue.create () in
Hashtbl.update global_results_table absolute_filename ~f:(fun file ->
let file =
Option.value
file
~default:{ expectations = Hashtbl.create (module Expectation_id); postprocess }
in
let tests = Hashtbl.of_alist_exn (module Expectation_id) tests in
Hashtbl.merge_into
~src:tests
~dst:file.expectations
~f:(fun ~key:test_id new_test existing_test ->
let (T (Test t) as test) = Option.value existing_test ~default:new_test in
t.reached_this_run <- false;
Queue.enqueue tests_as_in_table (test_id, test);
Set_to test);
file);
Queue.to_list tests_as_in_table
;;
let process_each_file ~f =
global_results_table
|> Hashtbl.to_alist
|> List.sort ~compare:(Comparable.lift ~f:fst String.compare)
|> List.map ~f:(fun (filename, { expectations; postprocess }) ->
let test_nodes = Hashtbl.data expectations in
f ~filename ~test_nodes ~postprocess)
;;
end
module Create = struct
let expect ~formatting_flexibility ~node_loc ~located_payload =
of_expectation (Expectation.expect ~formatting_flexibility ~node_loc ~located_payload)
;;
let expect_exact ~formatting_flexibility ~node_loc ~located_payload =
of_expectation
(Expectation.expect_exact ~formatting_flexibility ~node_loc ~located_payload)
;;
let expect_unreachable ~node_loc =
of_expectation (Expectation.expect_unreachable ~node_loc)
;;
end
module For_mlt = struct
let loc (T (Test { expectation = { position; _ }; results = _; reached_this_run = _ })) =
Expectation.Insert_loc.loc position
;;
let expectation_of_t (T (Test { expectation; results = _; reached_this_run = _ })) =
match expectation.behavior with
| Expect { payload = { contents; tag = _ }; on_unreachable = _; reachability = _ } ->
Some contents
| Unreachable _ -> None
;;
let record_and_return_number_of_lines_in_correction
~expect_node_formatting
~failure_ref
~test_output_raw
(T (Test inner))
=
match
record_and_return_result
~expect_node_formatting
~failure_ref
~test_output_raw
(Test inner)
with
| Fail contents, tag ->
let correction =
Output.to_formatted_payload ~tag contents |> Output.Payload.to_source_code_string
in
Some (String.count ~f:(Char.equal '\n') correction + 1)
| Pass, _ -> None
;;
let to_diffs ~cr_for_multiple_outputs ~expect_node_formatting ~original_file_contents t =
match to_correction ~expect_node_formatting ~cr_for_multiple_outputs t with
| None -> []
| Some correction ->
Correction.to_diffs correction ~expect_node_formatting ~original_file_contents
;;
end