Skip to content

Commit

Permalink
Add test for source quotation when the driver reports an exception
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Rebours <[email protected]>
  • Loading branch information
NathanReb committed Feb 28, 2024
1 parent 18c14b6 commit 14932c3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/driver/source-quotation-in-errors/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(executable
(name raising_driver)
(libraries ppxlib))

(cram
(deps raising_driver.exe))
14 changes: 14 additions & 0 deletions test/driver/source-quotation-in-errors/raising_driver.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
open Ppxlib

let rules =
[
Extension.V3.declare "raise" Extension.Context.expression
Ast_pattern.(pstr nil)
(fun ~ctxt ->
let loc = Expansion_context.Extension.extension_point_loc ctxt in
Location.raise_errorf ~loc "An exception, raise be!")
|> Context_free.Rule.extension;
]

let () = Driver.V2.register_transformation ~rules "raise"
let () = Driver.standalone ()
26 changes: 26 additions & 0 deletions test/driver/source-quotation-in-errors/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
When the ppxlib driver reports an error by itself, source quotation should work
properly.

We start off by explicitly setting the error reporting style to contextual to
ensure source quotation is enabled:

$ export OCAML_ERROR_STYLE=contextual

Here we have a driver compiled with a single rule that will raise a located
exception for every "[%raise]" extension point.

We need an input file:

$ cat > file.ml << EOF
> let x = [%raise]
> EOF

When running the driver on this file, it should report the error and show
the relevant quoted source:

$ ./raising_driver.exe -impl file.ml
File "file.ml", line 1, characters 8-16:
1 | let x = [%raise]
^^^^^^^^
Error: An exception, raise be!
[1]

0 comments on commit 14932c3

Please sign in to comment.