From aea95228bd59aef0c93ff3d221bc5d155f878fdf Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Thu, 29 Oct 2026 03:17:47 +0100 Subject: [PATCH 1/2] ppx_devtools.expect: indent promoted code like the code around it - Indentation is copied from the source, tabs included, instead of being counted in spaces. Literals indented with tabs now compare equal to the output. - A new [%expect] after a body written on the [let%expect_test] line is indented one step past the test instead of starting at column 0. - An inserted [@@expect.uncaught_exn] is indented like its test, e.g. inside a module. Also use our own wording for the header of an [%expect] that saw different outputs: "CR ppx_devtools-expect: this [%expect] saw different outputs". Co-Authored-By: Claude Opus 5.5 --- expect-runtime/corrected.ml | 74 ++++++++++++------- expect-runtime/ppx_devtools_expect_runtime.ml | 11 ++- .../ppx_devtools_expect_runtime.mli | 5 +- expect-runtime/text.ml | 4 +- expect/ppx_devtools_expect.ml | 1 + test/promote/different_outputs.ml | 12 +-- test/promote/promote_me.expected | 60 +++++++++++---- test/promote/promote_me.ml | 21 ++++++ 8 files changed, 135 insertions(+), 53 deletions(-) diff --git a/expect-runtime/corrected.ml b/expect-runtime/corrected.ml index beba414..9b8aded 100644 --- a/expect-runtime/corrected.ml +++ b/expect-runtime/corrected.ml @@ -23,8 +23,8 @@ type kind = | Literal (* Replace the literal of an [[@@expect.uncaught_exn]] attribute in [payload_start, payload_end), or insert a new attribute there when the - span is empty. *) - | Uncaught_exn + span is empty, indented like the test starting at [test_start]. *) + | Uncaught_exn of { test_start : int } (* Replace [payload_start, payload_end), the name of a reached [%expect.unreachable], with that of an [%expect] holding [actual]. What surrounds the name, such as a [@when], is kept. *) @@ -34,9 +34,11 @@ type kind = (* Delete [payload_start, payload_end). *) | Delete (* Insert a new [%expect] at [payload_start] (= [payload_end]), indented - like the line holding [indent_pos]. *) + like the line holding [indent_pos]. When that is the first line of the + test starting at [test_start], it is indented one step further. *) | New_expect of { indent_pos : int + ; test_start : int ; parens : parens } @@ -73,11 +75,17 @@ let contains s sub = in loop 0 -let column_of source pos = - let rec line_start i = - if i <= 0 || Char.equal source.[i - 1] '\n' then i else line_start (i - 1) - in - pos - line_start pos +let line_start source pos = + let rec loop i = if i <= 0 || Char.equal source.[i - 1] '\n' then i else loop (i - 1) in + loop pos + +(* Whitespace lining up with [pos]: what precedes it on its line, with every + character but tabs turned into a space. *) +let padding_to source pos = + let start = line_start source pos in + String.map + (fun c -> if Char.equal c '\t' then c else ' ') + (String.sub source start (pos - start)) (* [source] has an already parenthesized expression at [pos]. *) let parenthesized source pos = @@ -87,11 +95,25 @@ let parenthesized source pos = in has "(" || has "begin " || has "begin\n" +(* Leading whitespace of the line holding [pos]. *) let indentation_of source pos = - let start = pos - column_of source pos in + let start = line_start source pos in let len = String.length source in - let rec loop i = if i < len && Char.equal source.[i] ' ' then loop (i + 1) else i in - loop start - start + let rec loop i = + if i < len && (Char.equal source.[i] ' ' || Char.equal source.[i] '\t') + then loop (i + 1) + else i + in + String.sub source start (loop start - start) + +(* Indentation of a statement following the one at [pos], in the test + starting at [test_start]. *) +let statement_indentation source ~test_start pos = + if line_start source pos = line_start source test_start + then indentation_of source test_start ^ " " + else indentation_of source pos + +let spaces s = String.make (String.length s) ' ' (* Pick the shortest [{tag| ... |tag}] delimiter that does not clash with the body. [tag = ""] gives the usual [{| ... |}]. *) @@ -99,7 +121,8 @@ let rec choose_tag body n = let tag = String.make n '_' in if contains body ("|" ^ tag ^ "}") then choose_tag body (n + 1) else tag -let render_literal ~column ~actual = +(* [indent] is the whitespace in front of the lines of a multi-line literal. *) +let render_literal ~indent ~actual = let lines = Text.canonical_lines actual in let body = String.concat "\n" lines in let tag = choose_tag body 0 in @@ -108,7 +131,6 @@ let render_literal ~column ~actual = | [] -> opening ^ " " ^ closing | [ single ] -> Printf.sprintf "%s %s %s" opening single closing | _ -> - let indent = String.make column ' ' in let body = String.concat "" @@ -173,7 +195,7 @@ let write ~root corrections = in let literal = render_literal - ~column:(column_of source c.payload_start + String.length opening) + ~indent:(padding_to source c.payload_start ^ spaces opening) ~actual:c.actual in (* A bare [%expect] has no literal to replace (the span is @@ -184,12 +206,14 @@ let write ~root corrections = if c.payload_start = c.payload_end then " " ^ literal else literal in [ c.payload_start, c.payload_end, opening ^ literal ^ closing ] - | Uncaught_exn -> - (* Laid out like ppx_expect does. *) - let literal = render_literal ~column:2 ~actual:c.actual in + | Uncaught_exn { test_start } -> + let indent = indentation_of source test_start in + let literal = + render_literal ~indent:(indent ^ " ") ~actual:c.actual + in let literal = if c.payload_start = c.payload_end - then "\n[@@expect.uncaught_exn " ^ literal ^ "]" + then "\n" ^ indent ^ "[@@expect.uncaught_exn " ^ literal ^ "]" else literal in [ c.payload_start, c.payload_end, literal ] @@ -197,23 +221,19 @@ let write ~root corrections = let opening = "expect " in let literal = render_literal - ~column:(column_of source c.payload_start + String.length opening) + ~indent:(padding_to source c.payload_start ^ spaces opening) ~actual:c.actual in [ c.payload_start, c.payload_end, opening ^ literal ] | Replace s -> [ c.payload_start, c.payload_end, s ] | Delete -> [ c.payload_start, c.payload_end, "" ] - | New_expect { indent_pos; parens } -> ( - let indent = indentation_of source indent_pos in + | New_expect { indent_pos; test_start; parens } -> ( + let indent = statement_indentation source ~test_start indent_pos in let opening = "[%expect " in let literal = - render_literal - ~column:(indent + String.length opening) - ~actual:c.actual - in - let expect = - Printf.sprintf ";\n%s%s%s]" (String.make indent ' ') opening literal + render_literal ~indent:(indent ^ spaces opening) ~actual:c.actual in + let expect = Printf.sprintf ";\n%s%s%s]" indent opening literal in let insert pos s = pos, pos, s in (* Insertions at the same offset stay in list order. *) match parens with diff --git a/expect-runtime/ppx_devtools_expect_runtime.ml b/expect-runtime/ppx_devtools_expect_runtime.ml index 2ee7fba..9d7f27d 100644 --- a/expect-runtime/ppx_devtools_expect_runtime.ml +++ b/expect-runtime/ppx_devtools_expect_runtime.ml @@ -95,6 +95,7 @@ type test = ; condition : unit -> bool (* reified [@when] on the test; gates running *) ; expectations : variant array array ; body_end : int + ; test_start : int ; last_statement_start : int ; open_tail : (int * int) option ; uncaught_exn : uncaught_exn option @@ -114,6 +115,7 @@ let register_test ~condition ~expectations ~body_end + ~test_start ~last_statement_start ~open_tail ~uncaught_exn @@ -129,6 +131,7 @@ let register_test ; condition ; expectations ; body_end + ; test_start ; last_statement_start ; open_tail ; uncaught_exn @@ -143,6 +146,7 @@ type run = ; expectations : variant array array ; filename : string ; description : string + ; test_start : int ; outputs : string list array (* per group, the distinct normalized outputs *) ; mutable corrections : Corrected.correction list ; mutable reports : string list @@ -195,13 +199,13 @@ let check_unexpected r ~pos ~indent_pos ~parens actual = ; payload_start = pos ; payload_end = pos ; actual - ; kind = New_expect { indent_pos; parens } + ; kind = New_expect { indent_pos; test_start = r.test_start; parens } } (* An assertion point that ran several times with different outputs gets each distinct one under a CR (ppx_expect lists every run, repeats included). *) let cr_for_multiple_outputs outputs = - let cr = "(* CR expect_test: Test ran multiple times with different test outputs *)" in + let cr = "(* CR ppx_devtools-expect: this [%expect] saw different outputs *)" in let n = List.length outputs in let header i = let header = Printf.sprintf "=== Output %d / %d ===" (i + 1) n in @@ -345,7 +349,7 @@ let check_uncaught_exn r ~body_end ~expected exn trailing = ; payload_start ; payload_end ; actual - ; kind = Uncaught_exn + ; kind = Uncaught_exn { test_start = r.test_start } }; if not (String.equal backtrace "") then r.reports <- (" backtrace:\n" ^ backtrace) :: r.reports @@ -669,6 +673,7 @@ let exit () = ; expectations = test.expectations ; filename = test.filename ; description = descr + ; test_start = test.test_start ; outputs = Array.make (Array.length test.expectations) [] ; corrections = [] ; reports = [] diff --git a/expect-runtime/ppx_devtools_expect_runtime.mli b/expect-runtime/ppx_devtools_expect_runtime.mli index 013c7a4..f82435b 100644 --- a/expect-runtime/ppx_devtools_expect_runtime.mli +++ b/expect-runtime/ppx_devtools_expect_runtime.mli @@ -86,6 +86,7 @@ val register_test : -> condition:(unit -> bool) -> expectations:variant array array -> body_end:int + -> test_start:int -> last_statement_start:int -> open_tail:(int * int) option -> uncaught_exn:uncaught_exn option @@ -99,8 +100,10 @@ val register_test : Output printed after the last assertion point gets a new [%expect] at the end of the body, which is described by byte offsets in the source: - [body_end]: the end of the test body, where the [%expect] is inserted; + - [test_start]: the start of the [let%expect_test]; - [last_statement_start]: the start of its last statement, whose - indentation the [%expect] gets; + indentation the [%expect] gets (one step past the test's when it is on + the line of the [let%expect_test]); - [open_tail]: the span of a [match] or [try] the body ends with, which is parenthesized first. diff --git a/expect-runtime/text.ml b/expect-runtime/text.ml index 17bd2e3..f2b37ca 100644 --- a/expect-runtime/text.ml +++ b/expect-runtime/text.ml @@ -31,10 +31,12 @@ let rstrip s = done; String.sub s 0 !n +(* Tabs count as one column, like spaces: the lines of a literal indented with + tabs share the same prefix. *) let leading_spaces s = let n = String.length s in let i = ref 0 in - while !i < n && Char.equal s.[!i] ' ' do + while !i < n && (Char.equal s.[!i] ' ' || Char.equal s.[!i] '\t') do incr i done; !i diff --git a/expect/ppx_devtools_expect.ml b/expect/ppx_devtools_expect.ml index ee54ef2..59052df 100644 --- a/expect/ppx_devtools_expect.ml +++ b/expect/ppx_devtools_expect.ml @@ -523,6 +523,7 @@ let expand_test ~loc ~item_attributes (vb : value_binding) : structure_item = ~condition:[%e condition] ~expectations:[%e expectations] ~body_end:[%e Ast_builder.Default.eint ~loc body_end] + ~test_start:[%e Ast_builder.Default.eint ~loc loc.loc_start.pos_cnum] ~last_statement_start:[%e Ast_builder.Default.eint ~loc last_statement_start] ~open_tail:[%e open_tail] ~uncaught_exn:[%e uncaught_exn] diff --git a/test/promote/different_outputs.ml b/test/promote/different_outputs.ml index b94a9fb..421bbae 100644 --- a/test/promote/different_outputs.ml +++ b/test/promote/different_outputs.ml @@ -7,10 +7,10 @@ let%expect_test "different outputs, promoted" = (fun s -> print_string s; [%expect {| - (* CR expect_test: Test ran multiple times with different test outputs *) - ============================= Output 1 / 2 ============================== + (* CR ppx_devtools-expect: this [%expect] saw different outputs *) + ========================== Output 1 / 2 ========================== a - ============================= Output 2 / 2 ============================== + ========================== Output 2 / 2 ========================== b |}]) [ "b"; "a"; "b" ] @@ -22,10 +22,10 @@ let%expect_test "different outputs, promoted, in a group" = print_string s; [%expect ({| other |} [@when ocaml_version < (4, 0, 0)])]; [%expect {| - (* CR expect_test: Test ran multiple times with different test outputs *) - ============================= Output 1 / 2 ============================== + (* CR ppx_devtools-expect: this [%expect] saw different outputs *) + ========================== Output 1 / 2 ========================== a - ============================= Output 2 / 2 ============================== + ========================== Output 2 / 2 ========================== b |}]) [ "b"; "a"; "b" ] diff --git a/test/promote/promote_me.expected b/test/promote/promote_me.expected index 9cb6dc6..4dc33e8 100644 --- a/test/promote/promote_me.expected +++ b/test/promote/promote_me.expected @@ -108,10 +108,10 @@ let%expect_test "different outputs" = (fun s -> print_string s; [%expect {| - (* CR expect_test: Test ran multiple times with different test outputs *) - ============================= Output 1 / 2 ============================== + (* CR ppx_devtools-expect: this [%expect] saw different outputs *) + ========================== Output 1 / 2 ========================== a - ============================= Output 2 / 2 ============================== + ========================== Output 2 / 2 ========================== b |}]) [ "b"; "a"; "b" ] @@ -228,18 +228,18 @@ let%expect_test "different outputs, twice" = (fun s -> print_string s; [%expect {| - (* CR expect_test: Test ran multiple times with different test outputs *) - ============================= Output 1 / 2 ============================== + (* CR ppx_devtools-expect: this [%expect] saw different outputs *) + ========================== Output 1 / 2 ========================== a - ============================= Output 2 / 2 ============================== + ========================== Output 2 / 2 ========================== b |}]; print_string s; [%expect {| - (* CR expect_test: Test ran multiple times with different test outputs *) - ============================= Output 1 / 2 ============================== + (* CR ppx_devtools-expect: this [%expect] saw different outputs *) + ========================== Output 1 / 2 ========================== a - ============================= Output 2 / 2 ============================== + ========================== Output 2 / 2 ========================== b |}]) [ "a"; "b" ] @@ -256,10 +256,10 @@ let%expect_test "different outputs, [%expect.unreachable]" = (fun s -> print_string s; [%expect {| - (* CR expect_test: Test ran multiple times with different test outputs *) - ============================= Output 1 / 2 ============================== + (* CR ppx_devtools-expect: this [%expect] saw different outputs *) + ========================== Output 1 / 2 ========================== a - ============================= Output 2 / 2 ============================== + ========================== Output 2 / 2 ========================== b |}]) [ "a"; "b" ] @@ -270,16 +270,46 @@ let%expect_test "different outputs, no variant applies" = print_string s; [%expect ({| other |} [@when ocaml_version < (4, 0, 0)])]; [%expect {| - (* CR expect_test: Test ran multiple times with different test outputs *) - ============================= Output 1 / 2 ============================== + (* CR ppx_devtools-expect: this [%expect] saw different outputs *) + ========================== Output 1 / 2 ========================== a - ============================= Output 2 / 2 ============================== + ========================== Output 2 / 2 ========================== b |}]) [ "a"; "b" ] +let%expect_test "trailing output, one-line body" = print_string "trailing"; + [%expect {| trailing |}] + let%expect_test "trailing output, after an attribute" = print_string "trailing" [@ocaml.warning "-26"]; [%expect {| trailing |}] +module Nested = struct + let%expect_test "uncaught exception, nested" = + print_string "trailing"; + raise Exit + [@@expect.uncaught_exn {| + Stdlib.Exit + Trailing output + --------------- + trailing + |}] +end + +[@@@ocamlformat "disable"] + +(* Indented with tabs. *) +module Tabs = struct + let%expect_test "trailing output, tabs" = + print_string "checked"; + [%expect {| checked |}]; + print_string "line 1\nline 2"; + [%expect {| + line 1 + line 2 + |}] +end +[@@@ocamlformat "enable"] + let () = Ppx_devtools_expect_runtime.exit () diff --git a/test/promote/promote_me.ml b/test/promote/promote_me.ml index 3fc63c4..d9adbd5 100644 --- a/test/promote/promote_me.ml +++ b/test/promote/promote_me.ml @@ -229,7 +229,28 @@ let%expect_test "different outputs, no variant applies" = [%expect ({| other |} [@when ocaml_version < (4, 0, 0)])]) [ "a"; "b" ] +let%expect_test "trailing output, one-line body" = print_string "trailing" + let%expect_test "trailing output, after an attribute" = print_string "trailing" [@ocaml.warning "-26"] +module Nested = struct + let%expect_test "uncaught exception, nested" = + print_string "trailing"; + raise Exit +end + +[@@@ocamlformat "disable"] + +(* Indented with tabs. *) +module Tabs = struct + let%expect_test "trailing output, tabs" = + print_string "checked"; + [%expect {| + stale 1 + stale 2 |}]; + print_string "line 1\nline 2" +end +[@@@ocamlformat "enable"] + let () = Ppx_devtools_expect_runtime.exit () From c89dd6f5d00f43f7e73bf193d92847ae3710f79e Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Thu, 29 Oct 2026 03:25:42 +0100 Subject: [PATCH 2/2] ppx_devtools.expect: fix three promotion bugs found in review - Deleting a [@@expect.uncaught_exn] attribute deleted everything from the end of the test body, including other attributes such as [@@if] and comments. Only the attribute and the whitespace in front of it are deleted now. - A [match], [try] or [function] ending the body as the last argument of an application (e.g. [print_string @@ match ...]) got the appended [%expect] in its last case. The search for such an expression now goes through applications, constructors and field assignments. - Counting a tab as a column of common indentation made outputs that differ in tabs and spaces compare equal. The common indentation is now a common whitespace prefix. Co-Authored-By: Claude Opus 5.5 --- expect-runtime/corrected.ml | 15 +++++++-- .../ppx_devtools_expect_runtime.mli | 6 ++-- expect-runtime/text.ml | 33 +++++++++++++++---- expect/ppx_devtools_expect.ml | 25 +++++++++----- test/promote/promote_me.expected | 20 +++++++++++ test/promote/promote_me.ml | 18 ++++++++++ 6 files changed, 96 insertions(+), 21 deletions(-) diff --git a/expect-runtime/corrected.ml b/expect-runtime/corrected.ml index 9b8aded..1bc55aa 100644 --- a/expect-runtime/corrected.ml +++ b/expect-runtime/corrected.ml @@ -31,7 +31,7 @@ type kind = | Reached (* Replace [payload_start, payload_end) with the given text. *) | Replace of string - (* Delete [payload_start, payload_end). *) + (* Delete [payload_start, payload_end), and the whitespace in front of it. *) | Delete (* Insert a new [%expect] at [payload_start] (= [payload_end]), indented like the line holding [indent_pos]. When that is the first line of the @@ -226,7 +226,18 @@ let write ~root corrections = in [ c.payload_start, c.payload_end, opening ^ literal ] | Replace s -> [ c.payload_start, c.payload_end, s ] - | Delete -> [ c.payload_start, c.payload_end, "" ] + | Delete -> + let rec start i = + if + i > 0 + && + match source.[i - 1] with + | ' ' | '\t' | '\n' | '\r' -> true + | _ -> false + then start (i - 1) + else i + in + [ start c.payload_start, c.payload_end, "" ] | New_expect { indent_pos; test_start; parens } -> ( let indent = statement_indentation source ~test_start indent_pos in let opening = "[%expect " in diff --git a/expect-runtime/ppx_devtools_expect_runtime.mli b/expect-runtime/ppx_devtools_expect_runtime.mli index f82435b..25b9f16 100644 --- a/expect-runtime/ppx_devtools_expect_runtime.mli +++ b/expect-runtime/ppx_devtools_expect_runtime.mli @@ -69,7 +69,7 @@ type uncaught_exn = { expected_exn : string (* contents of the literal *) ; exn_payload_start : int (* byte offset of the literal token in the source *) ; exn_payload_end : int (* byte offset just past the literal token *) - ; attribute_start : int (* end of the test body, before the attribute *) + ; attribute_start : int (* byte offset of the attribute *) ; attribute_end : int (* byte offset just past the attribute *) } (** The [[@@expect.uncaught_exn {| ... |}]] attribute of a test: the exception @@ -104,8 +104,8 @@ val register_test : - [last_statement_start]: the start of its last statement, whose indentation the [%expect] gets (one step past the test's when it is on the line of the [let%expect_test]); - - [open_tail]: the span of a [match] or [try] the body ends with, which is - parenthesized first. + - [open_tail]: the span of a [match], [try] or [function] the body ends + with, which is parenthesized first. [uncaught_exn] is the exception the test is expected to raise, if any. *) diff --git a/expect-runtime/text.ml b/expect-runtime/text.ml index f2b37ca..35b5ec8 100644 --- a/expect-runtime/text.ml +++ b/expect-runtime/text.ml @@ -31,15 +31,21 @@ let rstrip s = done; String.sub s 0 !n -(* Tabs count as one column, like spaces: the lines of a literal indented with - tabs share the same prefix. *) -let leading_spaces s = +let leading_whitespace s = let n = String.length s in let i = ref 0 in while !i < n && (Char.equal s.[!i] ' ' || Char.equal s.[!i] '\t') do incr i done; - !i + String.sub s 0 !i + +let common_prefix a b = + let n = min (String.length a) (String.length b) in + let i = ref 0 in + while !i < n && Char.equal a.[!i] b.[!i] do + incr i + done; + String.sub a 0 !i let drop_leading_blanks lines = let rec loop = function @@ -57,13 +63,26 @@ let canonical_lines s = match lines with | [] -> [] | _ -> + (* The common indentation is a common prefix of whitespace, so lines + indented with tabs lose them, but a tab never matches a space. *) let indent = List.fold_left - (fun acc l -> if String.length l = 0 then acc else min acc (leading_spaces l)) - max_int + (fun acc l -> + if String.length l = 0 + then acc + else + let w = leading_whitespace l in + match acc with + | None -> Some w + | Some acc -> Some (common_prefix acc w)) + None lines in - let indent = if indent = max_int then 0 else indent in + let indent = + match indent with + | None -> 0 + | Some w -> String.length w + in List.map (fun l -> if String.length l <= indent diff --git a/expect/ppx_devtools_expect.ml b/expect/ppx_devtools_expect.ml index 59052df..54e4a1f 100644 --- a/expect/ppx_devtools_expect.ml +++ b/expect/ppx_devtools_expect.ml @@ -402,13 +402,21 @@ let () = | Some name -> library_name := name | None -> ()) -(* The [match] or [try] that [e] ends with, if any. It would capture a - statement appended after [e] into its last case: promotion parenthesizes it - first. *) +(* The [match], [try] or [function] that [e] ends with, if any, possibly as + the last argument of an application (e.g. [print_string @@ match ...]). It + would capture a statement appended after [e] into its last case: promotion + parenthesizes it first. *) let rec open_tail (e : expression) : location option = match e.pexp_desc with - | Pexp_match _ | Pexp_try _ -> Some e.pexp_loc + | Pexp_match _ | Pexp_try _ | Pexp_function _ -> Some e.pexp_loc + | Pexp_apply (_, args) -> ( + match List.rev args with + | (_, e) :: _ -> open_tail e + | [] -> None) | Pexp_sequence (_, e) + | Pexp_construct (_, Some e) + | Pexp_variant (_, Some e) + | Pexp_setfield (_, _, e) | Pexp_letmodule (_, _, e) | Pexp_letexception (_, e) | Pexp_open (_, e) @@ -430,9 +438,8 @@ let rec open_tail (e : expression) : location option = | Error _ -> None) (* [@@expect.uncaught_exn {| ... |}] on the test: the exception it is expected - to raise, as in ppx_expect. Its span starts at [body_end] so that deleting - it also deletes the whitespace in front of it. *) -let uncaught_exn_of_attributes ~loc ~body_end (attrs : attribute list) : expression = + to raise. *) +let uncaught_exn_of_attributes ~loc (attrs : attribute list) : expression = match List.find_opt attrs ~f:(fun (a : attribute) -> String.equal a.attr_name.txt "expect.uncaught_exn") @@ -459,7 +466,7 @@ let uncaught_exn_of_attributes ~loc ~body_end (attrs : attribute list) : express { Ppx_devtools_expect_runtime.expected_exn = [%e estring ~loc s] ; exn_payload_start = [%e eint (str_loc.loc_start.pos_cnum - delim_len)] ; exn_payload_end = [%e eint (str_loc.loc_end.pos_cnum + delim_len)] - ; attribute_start = [%e eint body_end] + ; attribute_start = [%e eint attr.attr_loc.loc_start.pos_cnum] ; attribute_end = [%e eint attr.attr_loc.loc_end.pos_cnum] }] | _ -> invalid ~loc:attr.attr_loc "[@@expect.uncaught_exn] expects a string literal" @@ -500,7 +507,7 @@ let expand_test ~loc ~item_attributes (vb : value_binding) : structure_item = , [%e Ast_builder.Default.eint ~loc l.loc_end.pos_cnum] )] in let uncaught_exn = - uncaught_exn_of_attributes ~loc ~body_end (vb.pvb_attributes @ item_attributes) + uncaught_exn_of_attributes ~loc (vb.pvb_attributes @ item_attributes) in let body, groups = transform_body vb.pvb_expr in let expectations = eexpectations ~loc groups in diff --git a/test/promote/promote_me.expected b/test/promote/promote_me.expected index 4dc33e8..c8b7534 100644 --- a/test/promote/promote_me.expected +++ b/test/promote/promote_me.expected @@ -278,6 +278,14 @@ let%expect_test "different outputs, no variant applies" = |}]) [ "a"; "b" ] +let%expect_test "trailing output, after a match in an application" = + print_string + @@ + (match Some 1 with + | Some _ -> "some" + | None -> "none"); + [%expect {| some |}] + let%expect_test "trailing output, one-line body" = print_string "trailing"; [%expect {| trailing |}] @@ -309,7 +317,19 @@ module Tabs = struct line 1 line 2 |}] + + let%expect_test "tabs and spaces in the output" = + print_string "\ta\n b"; + [%expect {| + a + b + |}] end + +let%expect_test "uncaught exception, no longer raised, after an attribute" = + print_string "trailing"; + [%expect {| trailing |}] +[@@ocaml.warning "-26"] (* kept *) [@@@ocamlformat "enable"] let () = Ppx_devtools_expect_runtime.exit () diff --git a/test/promote/promote_me.ml b/test/promote/promote_me.ml index d9adbd5..ce8fe8d 100644 --- a/test/promote/promote_me.ml +++ b/test/promote/promote_me.ml @@ -229,6 +229,13 @@ let%expect_test "different outputs, no variant applies" = [%expect ({| other |} [@when ocaml_version < (4, 0, 0)])]) [ "a"; "b" ] +let%expect_test "trailing output, after a match in an application" = + print_string + @@ + match Some 1 with + | Some _ -> "some" + | None -> "none" + let%expect_test "trailing output, one-line body" = print_string "trailing" let%expect_test "trailing output, after an attribute" = @@ -250,7 +257,18 @@ module Tabs = struct stale 1 stale 2 |}]; print_string "line 1\nline 2" + + let%expect_test "tabs and spaces in the output" = + print_string "\ta\n b"; + [%expect {| + a + b |}] end + +let%expect_test "uncaught exception, no longer raised, after an attribute" = + print_string "trailing" +[@@ocaml.warning "-26"] (* kept *) +[@@expect.uncaught_exn {| Stdlib.Exit |}] [@@@ocamlformat "enable"] let () = Ppx_devtools_expect_runtime.exit ()