package ppx_assert

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

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_assert-v0.10.0.tar.gz
sha256=dba06062d5978bf926f37c93d921fb6aef8345830d4754c998631ed6e8ac2956
md5=3add5a390dc67948ee2057a814f6f7aa

Description

Part of the Jane Street's PPX rewriters collection.

Published: 15 Dec 2017

README

ppx_assert

Assert-like extension nodes that raise useful errors on failure.

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), you can see the values that are not equal, and the assertion is not turned off by -noassert. 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 (12)

  1. ocaml-migrate-parsetree >= "0.4" & < "2.0.0"
  2. jbuilder >= "1.0+beta12"
  3. sexplib >= "v0.10" & < "v0.11"
  4. ppx_type_conv >= "v0.10" & < "v0.11"
  5. ppx_sexp_conv >= "v0.10" & < "v0.11"
  6. ppx_metaquot >= "v0.10" & < "v0.11"
  7. ppx_here >= "v0.10" & < "v0.11"
  8. ppx_driver >= "v0.10" & < "v0.11"
  9. ppx_core >= "v0.10" & < "v0.11"
  10. ppx_compare >= "v0.10" & < "v0.11"
  11. base >= "v0.10" & < "v0.11"
  12. ocaml >= "4.04.1"

Dev Dependencies

None

Used by (11)

  1. core = "v0.10.0"
  2. core_kernel = "v0.10.0"
  3. h1_parser
  4. http
  5. ppx_bap < "v0.14.0"
  6. ppx_expect >= "v0.10.0" & < "v0.11.0"
  7. ppx_jane = "v0.10.0"
  8. ppx_partial
  9. sel
  10. vscoq-language-server
  11. zanuda

Conflicts

None

OCaml

Innovation. Community. Security.