Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 60 additions & 29 deletions expect-runtime/corrected.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@ 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. *)
| 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]. *)
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
}

Expand Down Expand Up @@ -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 =
Expand All @@ -87,19 +95,34 @@ 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 [{| ... |}]. *)
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
Expand All @@ -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
""
Expand Down Expand Up @@ -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
Expand All @@ -184,36 +206,45 @@ 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 ]
| Reached ->
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
| 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
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
Expand Down
11 changes: 8 additions & 3 deletions expect-runtime/ppx_devtools_expect_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -114,6 +115,7 @@ let register_test
~condition
~expectations
~body_end
~test_start
~last_statement_start
~open_tail
~uncaught_exn
Expand All @@ -129,6 +131,7 @@ let register_test
; condition
; expectations
; body_end
; test_start
; last_statement_start
; open_tail
; uncaught_exn
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down
11 changes: 7 additions & 4 deletions expect-runtime/ppx_devtools_expect_runtime.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -99,10 +100,12 @@ 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;
- [open_tail]: the span of a [match] or [try] the body ends with, which is
parenthesized first.
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], [try] or [function] the body ends
with, which is parenthesized first.

[uncaught_exn] is the exception the test is expected to raise, if any. *)

Expand Down
33 changes: 27 additions & 6 deletions expect-runtime/text.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ let rstrip s =
done;
String.sub s 0 !n

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] ' ' do
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
Expand All @@ -55,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
Expand Down
26 changes: 17 additions & 9 deletions expect/ppx_devtools_expect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -523,6 +530,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]
Expand Down
Loading
Loading