Module Mavryk_test_helpers.Assert

module Assert contains Alcotest convenience assertions.

Type definitions

type ('ty, 'res) t = ?loc:string -> ?msg:string -> 'ty -> 'ty -> 'res

An assertion that a relation between two elements of 'ty holds true. This assertion returns a value of type 'res when it succeeds, typically unit, as in check2. *

type ('ty, 'res) assertion = ('ty, 'res) t

Alias for t used in EQUALITIES.

type 'ty check2 = ('ty, unit) t

A 'a check2 is an assertion over two elements of type 'a returning unit when it holds.

type 'ty check1 = ?loc:string -> ?msg:string -> 'ty -> unit

A 'a check1 defines the check of a property of a single element of type 'a.

See is_none or is_true for examples.

type ('base_ty, 'container_ty) equality_check = ?eq:('base_ty -> 'base_ty -> bool) -> ?pp:(Stdlib.Format.formatter -> 'base_ty -> unit) -> 'container_ty check2

An equality_check is an assertion that the boolean relation defined by eq holds on two series -- of type 'container_ty -- of elements of 'base_ty,

This typically defines the type of assertions on lists (see equal_list), or arrays, but it is also used to defined new checks based on an equality function.

type ('base_ty, 'container_ty) comparison_check = ?cmp:('base_ty -> 'base_ty -> int) -> ?pp:(Stdlib.Format.formatter -> 'base_ty -> unit) -> 'container_ty check2

A comparison_check is similar to a equality_check except the assertion is based on the optional comparison function cmp.

Base definitions

Failures

val fail : ?loc:string -> ?msg:string -> (Stdlib.Format.formatter -> 'a -> unit) -> 'a -> 'a -> 'b

fail ?loc ?msg pp x y fails after printing message "<loc> <msg>: expected <x> got <y>".

  • when msg is omitted "<msg:> " is not printed
  • when loc is omitted "<loc> " is not printed
val fail_msg : ('a, Stdlib.Format.formatter, unit, 'b) Stdlib.format4 -> 'a

fail_msg fmt fails after pretty-printing fmt

Assertions based on (un)equalities

val equal : ('a, 'a) equality_check

equal ?eq ?pp ?msg ?loc a b checks that eq a b and if not calls fail pp a b msg loc

  • parameter msg

    is not set by default

  • parameter loc

    is not set by default

  • parameter eq

    defaults to Stdlib.( = )

  • parameter pp

    is the pretty-printer for type 'a, defaults to printing nothing

  • raises Failure

    when eq a b is false

val unequal : ('a, 'a) equality_check

unequal ?eq ?pp ?msg ?loc a b checks that eq a b does not hold. See equal for the rest of the documentation.

val equal_list : ('a, 'a list) equality_check

equal_list ?eq ?pp ?msg ?loc l1 l2 is like equal but for polymorphic lists 'a list.

  • parameter msg

    is not set by default

  • parameter loc

    is not set by default

  • parameter eq

    defaults to Stdlib.( = )

  • parameter pp

    is the pretty-printer for type 'a, defaults to printing nothing

  • raises Failure

    when List.equal eq a b is false

val equal_list_list : ('a, 'a list list) equality_check

equal_list_list is like equal but for polymorphic lists of lists 'a list list.

  • parameter msg

    is not set by default

  • parameter loc

    is not set by default

  • parameter eq

    defaults to Stdlib.( = )

  • parameter pp

    is the pretty-printer for type 'a, defaults to printing nothing

  • raises Failure

    when List.equal (List.equal eq) a b is false

Assertions based on comparisons

val leq : ('a, 'a) comparison_check

leq ?cmp ?pp ?loc ?msg a b checks that cmp a b <= 0.

  • parameter msg

    is not set by default

  • parameter loc

    is not set by default

  • parameter cmd

    defaults to Stdlib.compare

  • parameter pp

    is the pretty-printer for type 'a, defaults to printing nothing

  • raises Failure

    when cmp a b > 0

val lt : ('a, 'a) comparison_check

lt ?cmp ?pp ?loc ?msg a b checks that cmp a b < 0.

See leq for details of the parameters.

  • raises Failure

    when cmp a b >= 0

val geq : ('a, 'a) comparison_check

geq ?cmp ?pp ?loc ?msg a b checks that cmp a b >= 0.

See leq for details of the parameters.

  • raises Failure

    when cmp a b < 0

val gt : ('a, 'a) comparison_check

gt ?cmp ?pp ?loc ?msg a b checks that cmp a b > 0.

See leq for details of the parameters.

  • raises Failure

    when cmp a b <= 0

Miscellaneous assertions

val assert_true : string -> bool -> unit

Alcotest check that b is true.

val assert_false : string -> bool -> unit

Alcotest check that b is false.

val impossible : string -> unit

Alcotest version of assert false.

val check_any : ?msg:string -> ('a -> bool) -> 'a list -> unit

Assert that at least one value in l satisfies f.

contains m msg x ls asserts that one testable in ls equals x, and otherwise fails with msg

val contains : 'a Alcotest.testable -> string -> 'a -> 'a list -> unit

Monomorphic equality checks

val is_none : ?loc:string -> ?msg:string -> ?pp:(Stdlib.Format.formatter -> 'a -> unit) -> 'a option -> unit

Check that an option value is None.

  • parameter msg

    is not set by default

  • parameter loc

    is not set by default

  • parameter eq

    defaults to Stdlib.( = )

  • parameter pp

    is the pretty-printer for type 'a, defaults to printing "Some _" as a placeholder for non-None value.

Module type signatures

module type EQUALITIES = sig ... end

Assertions for equality/disequality

module type COMPARISONS = sig ... end

Checks for extra binary relations

Predefined modules for base types

module Bytes : COMPARISONS with type t = bytes
module Bool : COMPARISONS with type t = bool
val is_true : bool check1
val is_false : bool check1
module String : sig ... end
module Int32 : sig ... end
module Int64 : sig ... end
module Int : sig ... end

Functors

module type COMPARABLE = sig ... end
module type EQUALABLE = sig ... end
module Make_equalities (X : EQUALABLE) : EQUALITIES with type t = X.t

Provide equality-based assertions for type X.t

module Make_comparisons (X : COMPARABLE) : COMPARISONS with type t = X.t

Provide all comparisons assertions for type X.t