Library
Module
Module type
Parameter
Class
Class type
Assertions.
This module defines predicates that call Test.fail
when they do not hold. Using these predicates gives you more consistent error messages.
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
.
Make an option type.
If the item type is comparable, the result is comparable. None
is always lesser than Some _
.
Make a result type.
This type is comparable if both 'a typ
and 'e typ
are comparable.
Make a list type.
If the item type is comparable, the result is comparable in lexicographic order.
Make an array type.
If the item type is comparable, the result is comparable in lexicographic order.
Make a 2-tuple type.
If both item types are comparable, the result is comparable in lexicographic order.
Make a 3-tuple type.
If all item types are comparable, the result is comparable in lexicographic order.
Make a 4-tuple type.
If all item types are comparable, the result is comparable in lexicographic order.
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.
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
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)
.
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"
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")
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")
*
Check that evaluating the given function raises the expected exception.
Example: Check.raises f exn ~error_msg:"expected f to raise %L, got %R"
Check that a file with the given name does not exist.
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.
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
.
Check that a boolean is true.
Example: Check.is_true cond ~error_msg:"expected condition to be true"