Skip to content

Commit

Permalink
Escape block-end sequence if contained in the error-block
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Oct 23, 2023
1 parent c35dddb commit 48c582a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ let pp_error ?syntax ?delim ppf outputs =
Fmt.pf ppf "]%a[\n{err@mdx-error[\n%a\n]err}"
Fmt.(option string)
delim
Fmt.(list ~sep:(any "\n") Output.pp)
Fmt.(list ~sep:(any "\n") Output.(pp ~escape_strings:[ "]err}" ]))
outputs
| _ -> ()

Expand Down
6 changes: 4 additions & 2 deletions lib/output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ let dump ppf = function
| `Output s -> Fmt.pf ppf "`Output %S" s
| `Ellipsis -> Fmt.pf ppf "`Ellipsis"

let pp ?(pad = 0) ppf = function
| `Output s -> Fmt.pf ppf "%a%s" Pp.pp_pad pad s
let pp ?(pad = 0) ?(escape_strings = []) ppf = function
| `Output s ->
let s = Util.String.escape ~escape_strings s in
Fmt.pf ppf "%a%s" Pp.pp_pad pad s
| `Ellipsis -> Fmt.pf ppf "%a..." Pp.pp_pad pad

let equals_sub l r start length =
Expand Down
2 changes: 1 addition & 1 deletion lib/output.mli
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ val merge : [ `Output of string ] list -> t list -> t list
(** [merge output test] merges any [`Ellipsis] items from [test] into
[output]. *)

val pp : ?pad:int -> t Fmt.t
val pp : ?pad:int -> ?escape_strings:string list -> t Fmt.t
(** [pp] is the pretty-printer for test outputs. [pad] is the size of
the optional whitespace left-padding (by default it is 0). *)

Expand Down
8 changes: 8 additions & 0 deletions lib/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ module String = struct

let english_conjonction words = english_concat ~last_sep:"and" words
let all_blank = Astring.String.for_all Astring.Char.Ascii.is_white

let replace ~before ~after s =
s |> Astring.String.cuts ~sep:before |> Astring.String.concat ~sep:after

let escape ~escape_strings s =
List.fold_left
(fun acc seq -> replace ~before:seq ~after:("\\" ^ seq) acc)
s escape_strings
end

module List = struct
Expand Down
3 changes: 3 additions & 0 deletions lib/util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ module String : sig

val all_blank : string -> bool
(** [all_blank s] is true if every character of s is a whitespace *)

val escape : escape_strings:string list -> string -> string
(** [escape ~escape_strings s] escape all occurrences in [s] of each string listed in [escape_strings]. *)
end

module Sexp : sig
Expand Down

0 comments on commit 48c582a

Please sign in to comment.