Source file expectation.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
open! Base
open Types
include Expectation_intf.Definitions
module Insert_loc = struct
include Insert_loc
let loc = function
| Overwrite { whole_node; payload = _ } -> whole_node
| Insert { loc; body_loc = _ } -> loc
;;
end
let with_behavior
{ position
; behavior = _
; payload_type
; on_incorrect_output
; inconsistent_outputs_message
}
behavior
=
{ position; behavior; payload_type; on_incorrect_output; inconsistent_outputs_message }
;;
let formatter
(type behavior)
~(expect_node_formatting : Expect_node_formatting.t)
({ position
; behavior
; payload_type
; on_incorrect_output = T on_incorrect_output
; inconsistent_outputs_message = _
} :
behavior t)
=
let count_leading_spaces line =
line |> String.to_list |> List.take_while ~f:(Char.( = ) ' ') |> List.length
in
Output.Formatter.create
@@
match payload_type with
| Exact -> Fn.id
| Pretty ->
fun str ->
let lines =
let stripped =
str
|> String.split_lines
|> List.map ~f:(String.rstrip ~drop:Char.is_whitespace)
|> List.drop_while ~f:String.is_empty
|> List.rev
|> List.drop_while ~f:String.is_empty
|> List.rev
in
let indent_and_contents =
List.map stripped ~f:(fun line ->
count_leading_spaces line, String.strip line)
in
match
indent_and_contents
|> List.filter_map ~f:(function
| _indent, "" -> None
| indent, _ -> Some indent)
|> List.min_elt ~compare:Int.compare
with
| None -> []
| Some min_indent ->
List.map indent_and_contents ~f:(fun (indent, line) ->
Int.max 0 (indent - min_indent), line)
in
let (T tag) =
match (behavior : _ Behavior.t) with
| Expect { payload = { tag; contents = _ }; _ } -> tag
| Unreachable _ -> String_node_format.Delimiter.default
in
(match lines, expect_node_formatting.always_on_own_line with
| [], _ ->
(match tag with
| Tag _ -> " "
| Quote -> "")
| [ (_indent, line) ], false ->
(match tag with
| Tag _ -> String.concat [ " "; line; " " ]
| Quote -> line)
| lines, _ ->
let location_indent =
expect_node_formatting.indent
+
match position with
| Overwrite { whole_node = { start_bol; start_pos; end_pos = _ }; payload = _ }
->
start_pos - start_bol
| Insert { body_loc = { start_bol; start_pos; end_pos = _ }; loc = _ } ->
start_pos
- start_bol
+
(match on_incorrect_output.kind with
| Extension -> expect_node_formatting.indent
| Attribute -> 0)
in
let spaces n = String.make n ' ' in
let first_line, indentation, last_line =
match tag with
| Quote ->
" ", 1, " "
| Tag _ -> "", location_indent, spaces location_indent
in
let lines =
List.map lines ~f:(function
| _indent, "" -> ""
| line_indent, line -> spaces (indentation + line_indent) ^ line)
in
[ [ first_line ]; lines; [ last_line ] ]
|> List.concat
|> String.concat ~sep:"\n")
;;
let extension_syntax extension_name ~payload_loc ~node_loc =
let contains (outer : Compact_loc.t) ~(inner : Compact_loc.t) =
outer.start_pos <= inner.start_pos && outer.end_pos >= inner.end_pos
in
match payload_loc with
| Some payload_loc when contains payload_loc ~inner:node_loc ->
(T { name = extension_name; kind = Extension; hand = Shorthand }
: String_node_format.Shape.t)
| _ -> T { name = extension_name; kind = Extension; hand = Longhand }
;;
let possibly_relax_strictness
~(formatting_flexibility : Expect_node_formatting.Flexibility.t)
(t : [ `Expect ] t)
=
match formatting_flexibility with
| Exactly_formatted -> t
| Flexible_modulo expect_node_formatting ->
let fmt = formatter ~expect_node_formatting t in
let (Expect { payload = { contents; tag }; on_unreachable; reachability }) =
t.behavior
in
(match
Output.reconcile
~expected_output:contents
~test_output:(Output.Formatter.apply fmt contents)
with
| Pass -> t
| Fail contents ->
let payload = Output.to_formatted_payload ~tag contents in
with_behavior t (Expect { payload; on_unreachable; reachability }))
;;
let expected_string_and_payload_loc = function
| Some (a, b) -> a, Some b
| None -> Output.Payload.default "", None
;;
let expect ~formatting_flexibility ~node_loc ~located_payload =
let payload, payload_loc = expected_string_and_payload_loc located_payload in
{ position = Overwrite { whole_node = node_loc; payload = payload_loc }
; behavior =
Expect
{ payload; on_unreachable = Replace_with_unreachable; reachability = Can_reach }
; payload_type = Pretty
; on_incorrect_output = extension_syntax "expect" ~payload_loc ~node_loc
; inconsistent_outputs_message = "test output"
}
|> possibly_relax_strictness ~formatting_flexibility
;;
let expect_exact ~formatting_flexibility ~node_loc ~located_payload =
let payload, payload_loc = expected_string_and_payload_loc located_payload in
{ position = Overwrite { whole_node = node_loc; payload = payload_loc }
; behavior =
Expect
{ payload; on_unreachable = Replace_with_unreachable; reachability = Can_reach }
; payload_type = Exact
; on_incorrect_output = extension_syntax "expect_exact" ~payload_loc ~node_loc
; inconsistent_outputs_message = "test output"
}
|> possibly_relax_strictness ~formatting_flexibility
;;
let expect_unreachable ~node_loc =
{ position = Overwrite { whole_node = node_loc; payload = None }
; behavior = Unreachable { reachability_of_corrected = Can_reach }
; payload_type = Pretty
; on_incorrect_output = T { name = "expect"; kind = Extension; hand = Longhand }
; inconsistent_outputs_message = "test output"
}
;;
let expect_uncaught_exn ~formatting_flexibility ~node_loc ~located_payload =
let payload, payload_loc = expected_string_and_payload_loc located_payload in
{ position = Overwrite { whole_node = node_loc; payload = payload_loc }
; behavior = Expect { payload; on_unreachable = Delete; reachability = Must_reach }
; payload_type = Pretty
; on_incorrect_output =
T { name = "expect.uncaught_exn"; kind = Attribute; hand = Longhand }
; inconsistent_outputs_message = "uncaught exception"
}
|> possibly_relax_strictness ~formatting_flexibility
;;
let expect_trailing ~insert_loc =
{ position = Insert insert_loc
; behavior =
Expect
{ payload = Output.Payload.default " "
; on_unreachable = Silent
; reachability = Can_reach
}
; payload_type = Pretty
; on_incorrect_output = T { name = "expect"; kind = Extension; hand = Longhand }
; inconsistent_outputs_message = "trailing output"
}
;;
let expect_no_uncaught_exn ~insert_loc =
{ position = Insert insert_loc
; behavior = Unreachable { reachability_of_corrected = Must_reach }
; payload_type = Pretty
; on_incorrect_output =
T { name = "expect.uncaught_exn"; kind = Attribute; hand = Longhand }
; inconsistent_outputs_message = "uncaught exception"
}
;;
module For_apply_style = struct
let format_payload mk_node =
Staged.stage
@@ fun ~expect_node_formatting ~payload_loc ~node_loc tag contents ->
let node =
mk_node
~formatting_flexibility:(Exactly_formatted : Expect_node_formatting.Flexibility.t)
~node_loc
~located_payload:(Some (({ tag; contents } : Output.Payload.t), payload_loc))
in
let formatted_contents =
Output.Formatter.apply (formatter ~expect_node_formatting node) contents
in
match Output.reconcile ~expected_output:contents ~test_output:formatted_contents with
| Pass -> None
| Fail contents ->
let source_code_string =
match node.on_incorrect_output with
| T { hand = Longhand; _ } ->
Output.to_formatted_payload ~tag contents
|> Output.Payload.to_source_code_string
| T { hand = Shorthand; _ } as node_shape ->
Output.to_source_code_string ~expect_node_formatting ~node_shape ~tag contents
in
Some source_code_string
;;
let format_expect_payload = format_payload expect |> Staged.unstage
let format_uncaught_exn_payload = format_payload expect_uncaught_exn |> Staged.unstage
end