package ppx_assert

All checked builds pass
  1. Overview
  2. Docs
Assert-like extension nodes that raise useful errors on failure

Install

dune-project
 Dependency

Authors

Maintainers

Sources

v0.17.0.tar.gz
sha256=94c47289a6393642b1cca7d2cdb8decdbf387c3cee4faf50d9b00efc871cce8b

Description

Part of the Jane Street's PPX rewriters collection.

Added to opam-repository:

README

ppx_assert

Extension nodes to compare value and raise useful errors if they differ.

This ppx rewriter defines 3 extension nodes.

[%test_eq: typ] in expressions

[%test_eq: typ] in expressions expands to a function of type:

?equal:(type -> type -> bool) -> ?here:Lexing.position list -> type -> type -> unit

i.e., it asserts the equality of its two anonymous arguments, using the provided equality or [%compare: typ] (sadly the type need to be comparable even if you provide an equality), and if they are not equal, an exception containing the value displayed by using [%sexp_of: typ] is thrown.

The exception also contains the source code position of the extension node and the additional positions from the here parameter. The ?here parameter is meant for cases where you want additional locations, usually because the backtrace is lacking information.

[%test_result: typ] in expressions

[%test_result: typ] is very similar to [%test_eq:typ]. It has a slightly improved error message for the common case where rather than comparing two arbitrary values, you have one expected value, and one computed value.

[%test_result: typ] expands to a function of type:

?here:Lexing.position list -> ?message:string -> ?equal:(type -> type -> bool) -> expect:typ -> typ -> unit

[%test_pred: typ] in expressions

This one is the least useful. [%test_pred: typ] expands to a function of type:

?here:Lexing.position list -> ?message:string -> (type -> bool) -> type -> unit

It simply applies the given predicate to the given value, and if the predicate returns false, then an exception containing the value shown using [%sexp_of: typ] is thrown.

Intended usage

These assertions are very useful when testing. Compared to using assert (x = y), errors display the values that are not equal. Also, there is no mechanism to remove these tests in production builds like -noassert does for assert. Compared to using the various assert_bool or assert_string functions you can find in various unit testing libraries, it works with any sexpable and comparable type for zero effort. For instance, tests commonly look like this:

let%test_unit "List.length" =
  [%test_result: int] (List.length [1; 2]) ~expect:2
let%test_unit "List.tail" =
  [%test_result: int list] (List.tail [1; 2]) ~expect:[2]

However convenient these extensions are for testing, it is also possible to use these extensions even outside of test, in production code, for instance in a function that checks invariants, or when checking some form of precondition.

Dependencies (8)

  1. ppxlib >= "0.28.0"
  2. dune >= "3.11.0"
  3. ppx_sexp_conv >= "v0.17" & < "v0.18~"
  4. ppx_here >= "v0.17" & < "v0.18~"
  5. ppx_compare >= "v0.17" & < "v0.18~"
  6. ppx_cold >= "v0.17" & < "v0.18~"
  7. base >= "v0.17" & < "v0.18~"
  8. ocaml >= "5.1.0"

Dev Dependencies

None

Optional Dependencies

None

Conflicts

None

Build status

Results of building ppx_assert with each compiler on check.ci.ocaml.org, run of against opam-repository 8a77e58. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
v0.17.0 (this version) n/a n/a good good good
v0.16.0 n/a good good n/a n/a
v0.15.0 good good good n/a n/a
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure