package tezt

  1. Overview
  2. Docs

Assertions.

This module defines predicates that call Test.fail when they do not hold. Using these predicates gives you more consistent error messages.

Comparable Types

type 'a typ

Type descriptions.

A t typ equips a type t with a pretty printer, and either an equality or a comparison function.

All types can be used with eq and neq, but some types will result in an exception when used with lt, le, gt and ge. Such types are said to be non comparable.

val unit : unit typ

The unit type.

Not very useful on its own, but may be useful inside more structured types.

This type is comparable.

val bool : bool typ

The boolean type.

Not very useful on its own since one can write if not b then Test.fail ..., but may be useful inside more structured types.

This type is comparable with true > false.

val char : char typ

The character type.

This type is comparable. Characters are compared by comparing their byte representation.

val int : int typ

The integer type.

This type is comparable.

val int32 : int32 typ

The 32-bit integer type.

This type is comparable.

val int64 : int64 typ

The 64-bit integer type.

This type is comparable.

val float : float typ

The float type.

Note that testing float equality or inequality is often a bad idea. Prefer float_epsilon when it makes sense.

This type is comparable.

val float_epsilon : float -> float typ

The float type with an approximative equality function.

The argument is how much of a difference can two values have and still be considered equal.

This type is comparable.

val string : string typ

The string type.

This type is comparable in lexicographic order, where each character is compared using the same comparison function as char.

val json_u : JSON.u typ

The JSON.u type.

Objects are compared using JSON.equal_u.

val json : JSON.t typ

The JSON.t type.

Objects are compared using JSON.equal.

val option : 'a typ -> 'a option typ

Make an option type.

If the item type is comparable, the result is comparable. None is always lesser than Some _.

val result : 'a typ -> 'e typ -> ('a, 'e) result typ

Make a result type.

This type is comparable if both 'a typ and 'e typ are comparable.

val list : 'a typ -> 'a list typ

Make a list type.

If the item type is comparable, the result is comparable in lexicographic order.

val array : 'a typ -> 'a array typ

Make an array type.

If the item type is comparable, the result is comparable in lexicographic order.

val tuple2 : 'a typ -> 'b typ -> ('a * 'b) typ

Make a 2-tuple type.

If both item types are comparable, the result is comparable in lexicographic order.

val tuple3 : 'a typ -> 'b typ -> 'c typ -> ('a * 'b * 'c) typ

Make a 3-tuple type.

If all item types are comparable, the result is comparable in lexicographic order.

val tuple4 : 'a typ -> 'b typ -> 'c typ -> 'd typ -> ('a * 'b * 'c * 'd) typ

Make a 4-tuple type.

If all item types are comparable, the result is comparable in lexicographic order.

val tuple5 : 'a typ -> 'b typ -> 'c typ -> 'd typ -> 'e typ -> ('a * 'b * 'c * 'd * 'e) typ

Make a 5-tuple type.

If all item types are comparable, the result is comparable in lexicographic order.

val tuple8 : 'a1 typ -> 'a2 typ -> 'a3 typ -> 'a4 typ -> 'a5 typ -> 'a6 typ -> 'a7 typ -> 'a8 typ -> ('a1 * 'a2 * 'a3 * 'a4 * 'a5 * 'a6 * 'a7 * 'a8) typ

Make a 8-tuple type.

If all item types are comparable, the result is comparable in lexicographic order.

val convert : ('a -> 'b) -> 'b typ -> 'a typ

Make a type by encoding to another.

Usage: convert encode typ

Example: convert (fun { x; y } -> x, y) (tuple2 int string)

Values are converted to typ using encode before being pretty-printed or compared. The result type is comparable if typ is comparable.

val get_pp : 'a typ -> Format.formatter -> 'a -> unit

Return the pretty-printer associated to the given type.

val get_equal : 'a typ -> 'a -> 'a -> bool

Return the equality function associated to the given type.

val get_compare : 'a typ -> ('a -> 'a -> int) option

Return the comparison function associated to the given type if it is comparable, else return None.

val equalable : (Format.formatter -> 'a -> unit) -> ('a -> 'a -> bool) -> 'a typ

Make a custom type from a pretty-printer and an equality function.

The result is not comparable.

val comparable : (Format.formatter -> 'a -> unit) -> ('a -> 'a -> int) -> 'a typ

Make a custom type from a pretty-printer and a comparison function.

module type EQUALABLE = sig ... end
val equalable_module : (module EQUALABLE with type t = 'a) -> 'a typ

Same as equalable but takes a module.

Example: equalable_module (module String).

module type COMPARABLE = sig ... end
val comparable_module : (module COMPARABLE with type t = 'a) -> 'a typ

Same as comparable but takes a module.

Example: comparable_module (module String).

Predicates

For all functions of this section, if the test fails, the function calls Test.fail with the error message given in ~error_msg. This ~error_msg may contain placeholders %L and %R where:

  • %L is replaced by the left-hand side value of the operator;
  • %R is replaced by the right-hand side value of the operator.

Here are some examples of ~error_msg for inspiration:

  • "expected filename = %R, got %L"
  • "expected list size >= %R, got %L"
  • "expected f to be monotonous, got f x = %L and f y = %R with x < y"

Comparison Operators

val (=) : 'a -> 'a -> ?__LOC__:string -> 'a typ -> error_msg:string -> unit

Check that a value is equal to another.

Example: Check.((value = expected) int ~error_msg:"expected value = %R, got %L")

val (<>) : 'a -> 'a -> ?__LOC__:string -> 'a typ -> error_msg:string -> unit

Check that a value is not equal to another.

Example: Check.((value <> wrong) int ~error_msg:"expected value <> %R")

val (<) : 'a -> 'a -> ?__LOC__:string -> 'a typ -> error_msg:string -> unit

Check that a value is less than another.

Example: Check.((value < threshold) int ~error_msg:"expected value < %R, got %L")

val (<=) : 'a -> 'a -> ?__LOC__:string -> 'a typ -> error_msg:string -> unit

Check that a value is less than or equal to another.

Example: Check.((value <= threshold) int ~error_msg:"expected value <= %R, got %L")

val (>) : 'a -> 'a -> ?__LOC__:string -> 'a typ -> error_msg:string -> unit

Check that a value is greater than another.

Example: Check.((value > threshold) int ~error_msg:"expected value > %R, got %L")

val (>=) : 'a -> 'a -> ?__LOC__:string -> 'a typ -> error_msg:string -> unit

Check that a value is greater than or equal to another.

Example: Check.((value >= threshold) int ~error_msg:"expected value >= %R, got %L")

val (=~) : string -> Base.rex -> error_msg:string -> unit

Check that a string matches a regular expression.

Example: Check.((value =~ rex) ~error_msg:"expected value =~ %R, got %L")

val (=~!) : string -> Base.rex -> error_msg:string -> unit

Check that a string does not match a regular expression.

Example: Check.((value =~! rex) ~error_msg:"expected value =~! %R, got %L")

Predicates on Lists

val list_mem : 'a typ -> ?__LOC__:string -> 'a -> 'a list -> error_msg:string -> unit

Check that a value belongs to a list.

Example: Check.list_mem int i list int ~error_msg:"expected %L to be in the list") *

val list_not_mem : 'a typ -> ?__LOC__:string -> 'a -> 'a list -> error_msg:string -> unit

Check that a value does not belong to a list.

Example: Check.list_not_mem int i list int ~error_msg:"expected %L to not be in the list") *

Predicates on Exceptions

val raises : ?__LOC__:string -> exn -> (unit -> unit) -> error_msg:string -> unit

Check that evaluating the given function raises the expected exception.

Example: Check.raises f exn ~error_msg:"expected f to raise %L, got %R"

Predicates on files

val file_exists : ?__LOC__:string -> string -> unit

Check that a file with the given name exists.

val file_not_exists : ?__LOC__:string -> string -> unit

Check that a file with the given name does not exist.

val directory_exists : ?__LOC__:string -> string -> unit

Check that a directory with the given name exists.

This directory_exists path succeeds if there is a file at path and it is a directory.

val directory_not_exists : ?__LOC__:string -> string -> unit

Check that a directory with the given name does not exist.

This directory_not_exists path succeeds either if there is a non-directory file at path or if there is no file at path.

Predicates on booleans

val is_true : ?__LOC__:string -> bool -> error_msg:string -> unit

Check that a boolean is true.

Example: Check.is_true cond ~error_msg:"expected condition to be true"

val is_false : ?__LOC__:string -> bool -> error_msg:string -> unit

Check that a boolean is false.

Example: Check.is_false cond ~error_msg:"expected condition to be false"

OCaml

Innovation. Community. Security.