@@ -36,25 +36,31 @@ let formatter
3636 } :
3737 (output , behavior ) t )
3838 =
39+ let count_leading_spaces line =
40+ line |> String. to_list |> List. take_while ~f: (Char. ( = ) ' ' ) |> List. length
41+ in
3942 Output.Formatter. create
4043 @@ fun str ->
4144 let lines =
42- (* Whitespace splitting/stripping specifically targets ['\n'] and [' '] so that
43- unusual characters like ['\r'] and ['\t'] get displayed explicitly in
44- expectations . *)
45+ (* In pretty payloads, we normalize all newlines to ['\n']. [[%expect_exact ""]]
46+ can be used in cases where a user wants to inspect the whitespace produced by
47+ their output more closely . *)
4548 let stripped =
4649 str
47- |> String. split ~on: '\n'
48- |> List. map ~f: (String. rstrip ~drop: ( Char. equal ' ' ) )
50+ |> String. split_lines
51+ |> List. map ~f: (String. rstrip ~drop: Char. is_whitespace )
4952 |> List. drop_while ~f: String. is_empty
5053 |> List. rev
5154 |> List. drop_while ~f: String. is_empty
5255 |> List. rev
5356 in
5457 let indent_and_contents =
5558 List. map stripped ~f: (fun line ->
56- let unindented = String. lstrip ~drop: (Char. equal ' ' ) line in
57- String. length line - String. length unindented, unindented)
59+ (* The legacy behavior is to only count the longest prefix of actual spaces
60+ ([' ']) for indentation, but to strip all whitespace (including, e.g., ['\t']).
61+ Note that this means [" \t contents"] is counted as having contents
62+ ["contents"] and indentation [1]. *)
63+ count_leading_spaces line, String. strip line)
5864 in
5965 match
6066 indent_and_contents
@@ -216,23 +222,23 @@ let expect_no_uncaught_exn virtual_loc =
216222module For_apply_style = struct
217223 let format_payload mk_node =
218224 Staged. stage
219- @@ fun ~expect_node_formatting ~loc tag contents ->
225+ @@ fun ~expect_node_formatting ~payload_loc ~loc tag contents ->
226+ let node =
227+ mk_node ~payload_loc: (Some payload_loc) ({ tag; contents } : _ Payload.t ) loc
228+ in
220229 let formatted_contents =
221- Output.Formatter. apply
222- (formatter
223- ~expect_node_formatting
224- (mk_node ~payload_loc: None ({ tag; contents } : _ Payload.t ) loc))
225- contents
230+ Output.Formatter. apply (formatter ~expect_node_formatting node) contents
231+ in
232+ let node_shape =
233+ match node.on_incorrect_output with
234+ | T { hand = Longhand ; name = _ ; kind = _ } -> None
235+ | node_shape -> Some node_shape
226236 in
227237 match Output. reconcile ~expected_output: contents ~test_output: formatted_contents with
228238 | Pass -> None
229239 | Fail contents ->
230240 Some
231- (Output. to_source_code_string
232- ~expect_node_formatting
233- ~node_shape: None
234- ~tag
235- contents)
241+ (Output. to_source_code_string ~expect_node_formatting ~node_shape ~tag contents)
236242 ;;
237243
238244 let format_expect_payload = format_payload expect |> Staged. unstage
0 commit comments