Module Core.Only_in_testSource
This module can be used to safely expose functions and values in signatures that should only be used in unit tests.
Under the hood, 'a t = 'a Lazy.t and the only thing that ever forces them is the force function below which should only be called in unit tests.
For example, suppose in some module, type t is actually an int. You want to keep the type definition opaque, but use the underlying representation in unit tests. You could write in the ml:
let test_to_int t = Only_in_test.return t
let test_of_int n = Only_in_test.return nYou would then expose in the mli:
type t
val test_to_int : t -> int Only_in_test.t
val test_of_int : int -> t Only_in_test.tFinally, if you have specific values that you might want to use in unit tests, but that have top-level side-effects or take too long to compute, you can delay the side-effects or computation until the unit tests are run by writing, e.g.:
let (test_special_value : t Only_in_test.t) = Only_in_test.of_thunk (fun () -> factorial 100)
instead of
let (test_special_value : t Only_in_test.t) = Only_in_test.return (factorial 100)
include Base.Monad.S with type 'a t := 'a Core.Only_in_test.t
val return : 'a 'i 'p 'q. 'a -> 'a Core.Only_in_test.tConvert a value to a t.
val map :
'a 'b 'i 'j 'p 'q. 'a Core.Only_in_test.t ->
f:('a -> 'b) ->
'b Core.Only_in_test.tTransforms the contents of a t.
val bind :
'a 'b 'i 'j 'k 'p 'q. 'a Core.Only_in_test.t ->
f:('a -> 'b Core.Only_in_test.t) ->
'b Core.Only_in_test.tSequences computations. bind t ~f computes f v for value(s) v in t. Well-behaved monads satisfy these "laws" (where ( >>= ) is the infix bind operator):
map t ~fis equivalent tobind t ~f:(fun x -> return (f x))return x >>= fis equivalent tof xt >>= returnis equivalent tot(t >>= f) >>= gis equivalent tot >>= fun x -> f x >>= g
val join :
'a 'i 'j 'k 'p 'q. 'a Core.Only_in_test.t Core.Only_in_test.t ->
'a Core.Only_in_test.tCombines nested t into just one layer. Equivalent to bind t ~f:Fn.id.
val ignore_m :
'a 'i 'j 'p 'q. 'a Core.Only_in_test.t ->
unit Core.Only_in_test.tIgnores contained values of t. Equivalent to map t ~f:ignore.
val all :
'a 'i 'p 'q. 'a Core.Only_in_test.t list ->
'a list Core.Only_in_test.tCombines a list of t.
val all_unit :
'i 'p 'q. unit Core.Only_in_test.t list ->
unit Core.Only_in_test.tCombines a list of t whose contents are unimportant.
val (>>=) :
'a 'b 'i 'j 'k 'p 'q. 'a Core.Only_in_test.t ->
('a -> 'b Core.Only_in_test.t) ->
'b Core.Only_in_test.tInfix bind.
val (>>|) :
'a 'b 'i 'j 'p 'q. 'a Core.Only_in_test.t ->
('a -> 'b) ->
'b Core.Only_in_test.tInfix map.
val force : 'a Core.Only_in_test.t -> 'a @@ portable