Sure, here’s an example: https://github.com/camlp5/pa_ppx_regexp/blob/master/README.asciidoc
Look at the bottom of the file.
This is both the README and a unit-test for pa_ppx_regexp
, which provides compact Perl-like regexp syntax for OCaml. So, uh … it might be pretty cryptic for non-Perl users. I’ve used mdx
to write other tests, but (read below) not any “failing to compile” tests, until now. [again, read on for why; I was actually surprised I hadn’t done so, b/c I certainly wrote code to support it.]
Some comments and caveats:
-
This is written using and for Camlp5-based PPX infrastructure. Since mdx
supported this sort of thing before, I’m sure it’s possible to make it work with the standard PPX infrastructure. Indeed, I had to submit a (tiny) patch to MDX to make it work with Camlp5.
-
I use Makefiles, so Dune users might have trouble figuring out how to duplicate; I can’t help there, b/c I don’t use dune at all. At toplevel, I run make test
which in turn runs make README.asciidoc.TEST
. That does the following:
$ make README.asciidoc.TEST
env TOP=. ocamlfind camlp5-buildscripts/LAUNCH -- ocaml-mdx test -o README.asciidoc.corrected
README.asciidoc
perl -p -i -e 's,.*: added to search path,: added to search path,' README.asciidoc.corrected
perl -p -i -e 's,.*: loaded,: loaded,' README.asciidoc.corrected
diff -Bwiu README.asciidoc.corrected README.asciidoc
What’s going on here?
- First, run
mdx
on README.asciidoc
- If you look at the file, you’ll see that I load packages (typically the case, since all PPX rewriters are in packages), and those packages are installed at directories that differ from installation to installation; so we need to erase the pathnames that are printed-out by the loading process
- then diff the output file (
.corrected
) against the original: if they’re identical, then the test passes
-
Funny thing, I didn’t have any examples of compiler errors in my MDX-based tests. I could have sworn I did, b/c I certainly added the support for them. So hey presto, I added a couple of tests at the end of the above-cited file.
-
Because Camlp5 produces errors a little differently than the OCaml parser, I had to insert a tiny bit of error-conversion code – you can see it here: https://github.com/camlp5/pa_ppx_regexp/blob/479fbaa67ba69850a8c95c12d062e4a71f72d9ee/README.asciidoc?plain=1#L91
Again, this should not be necessary with the standard PPX infrastructure.
OK, with those caveats, here are the two tests I just added. I’m quoting the tests from README.asciidoc
, but even still, it’s going to get displayed wrong b/c both .asciidoc and Markdown use “three-backticks” for preformatted text, AND mdx
uses it with either ocaml
or sh
after the three backticks, to select how to process the block (you can read more about that in the mdx
documentation – it’s very straightforward).
I REPEAT: the code below is NOT an example to use: you need to go to the bottom of README.asciidoc
to see the actual code, b/c the rendering process in this forum software is mangling the code.
== Examples of Errors
=== A match regexp with an unrecognized modifier
# [%match {|(a)?|} / foo];;
File "_none_", line 1, characters 19-22:
Failure: extract_options: malformed option: <<(MLast.ExLid (<loc>,
(Pp_MLast.Ploc.VaVal "foo")))>>
Line 1, characters 0-0:
Error: Stdlib.Exit
=== A match regexp specifying more than one regexp syntax
# [%match {|(a)?|} / re_perl pcre];;
File "_none_", line 1, characters 0-32:
Failure: match extension: can specify at most one of <<re>>, <<pcre>>: re_perl pcre
Line 1, characters 0-0:
Error: Stdlib.Exit