Code for parsing toplevel expect test files
type chunk
=
{
part : string option; | (** The part the chunk is in, None if it's not in any part. *) |
phrases : Ppx_core.Light.toplevel_phrase list; | |
expectation : Expect_test_matcher.Std.Fmt.t Expect_test_matcher.Std.Cst.t Expect_test_common.Std.Expectation.t; | |
phrases_loc : Ppx_core.Light.Location.t; |
}
val split_chunks : fname:string ‑> Ppx_core.Light.toplevel_phrase list ‑> chunk list * (Ppx_core.Light.toplevel_phrase list * Ppx_core.Light.position * string option) option
Recursively parses toplevel phrases (i.e., contiguous units of code separated by
;;
) into "chunks", one chunk per %%expect
statement.
For example if the mlt contents are:
let x = 1 + 1;;
printf "%d" x + 2;;
[%%expect {|
- : int: 4
|}];;
print_string "f" ^ "o" ^ "o";;
[%%expect {|
- : string: "foo"
|}];;
print_string 3 + 3 + 3;;
then you'd have two chunks, where the first has two phrases ("x = 1 + 1"
and
"printf "%d" x + 2"
) and an expectation.body
of ": int 4"
. The second chunk
would have just the one phrase.
"print_line 3 + 3 + 3"
is not part of a chunk because there is no expectation
following it, so instead it is returned as trailing_code
, which is just a list of
toplevel phrases with some position metadata.
"part" refers to @@@part "foo"
statements, which are arbitrary section breaks. Each
chunk, and the trailing code, belongs to a part (which is just the empty string ""
if none has been specified).
include sig ... end
val mlt_block_of_sexp : Sexplib.Sexp.t ‑> mlt_block
val sexp_of_mlt_block : mlt_block ‑> Sexplib.Sexp.t
val parse : Ppx_core.Light.toplevel_phrase list ‑> contents:string ‑> mlt_block list
Takes a list of toplevel phrases and the raw string they're embedded in and returns a list of labeled blocks, so that for instance the following raw toplevel code:
[%%org {|
Here comes a very /simple/ example.
|}];;
1 + 1;;
[%%expect {|
- : int: 2
|}];;
is parsed into its constituent parts:
[
(Org "Here comes a very /simple/ example.");
(Code "1 + 1");
(Expect "- : int: 2")
]
Note that we only care about these three kinds of element (org blocks, expect blocks, and regular OCaml code blocks); everything else -- including toplevel comments -- is silently discarded.