package alcotest

Build not yet checked
  1. Overview
  2. Docs
Alcotest is a lightweight and colourful test framework

Install

dune-project
 Dependency

Authors

Maintainers

Sources

alcotest-1.10.1.tbz
sha256=62c095deda5cdd66c30684f7b1f25b5775d1d3d3ded927c7b893b4a6834ddb47
sha512=6f779fa96faf96b5b462ddc428aeaaa73a6be96771dbbad6a05b7bb00dae246648891c211e26174729c1f40128410ad96f2512101ae8b7d61f968ee61534b3d0

doc/src/alcotest.engine/test.ml.html

Source file test.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
(*
 * Copyright (c) 2013-2016 Thomas Gazagnaire <thomas@gazagnaire.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

module type TESTABLE = sig
  type t

  val pp : t Fmt.t
  val equal : t -> t -> bool
end

type 'a testable = (module TESTABLE with type t = 'a)

let pp (type a) (t : a testable) =
  let (module T) = t in
  T.pp

let equal (type a) (t : a testable) =
  let (module T) = t in
  T.equal

let testable (type a) (pp : a Fmt.t) (equal : a -> a -> bool) : a testable =
  let module M = struct
    type t = a

    let pp = pp
    let equal = equal
  end in
  (module M)

let map f t =
  let pp ppf = (Fmt.using f (pp t)) ppf and equal a b = equal t (f a) (f b) in
  testable pp equal

let int32 = testable Fmt.int32 ( = )
let int64 = testable Fmt.int64 ( = )
let int = testable Fmt.int ( = )

let float eps =
  let same x y =
    (Float.is_nan x && Float.is_nan y)
    (* compare infinities *)
    || x = y
    || abs_float (x -. y) <= eps
  in
  testable Fmt.float same

let char =
  let pp_char ppf x = Fmt.pf ppf "%C" x in
  testable pp_char ( = )

let uchar =
  let pp_uchar ppf x = Fmt.Dump.uchar ppf x in
  testable pp_uchar ( = )

(* Escapes like [String.escaped], but keeps the bytes of non-ASCII characters, as
   the OCaml toplevel does since ocaml/ocaml#1231. *)
let escaped_ascii s =
  let buf = Buffer.create (String.length s) in
  String.iter
    (fun c ->
      if Char.code c < 0x80 then
        Buffer.add_string buf (String.escaped (String.make 1 c))
      else Buffer.add_char buf c)
    s;
  Buffer.contents buf

let string =
  let pp_string ppf x =
    if Fmt.utf_8 ppf && String.is_valid_utf_8 x then
      Fmt.pf ppf "\"%s\"" (escaped_ascii x)
    else Fmt.pf ppf "%S" x
  in
  testable pp_string ( = )

let bytes =
  testable (fun fmt bytes -> Fmt.fmt "%S" fmt (Bytes.to_string bytes)) ( = )

let bool = testable Fmt.bool ( = )
let unit = testable (Fmt.any "()") ( = )
let list e = testable (Fmt.Dump.list (pp e)) (List.equal (equal e))
let seq e = testable (Fmt.Dump.seq (pp e)) (Seq.equal (equal e))

let slist (type a) (a : a testable) compare =
  let l = list a in
  let eq l1 l2 = equal l (List.sort compare l1) (List.sort compare l2) in
  testable (pp l) eq

let array e =
  let eq a1 a2 =
    Array.length a1 = Array.length a2 && Array.for_all2 (equal e) a1 a2
  in
  testable (Fmt.Dump.array (pp e)) eq

let pair a b =
  let eq (a1, b1) (a2, b2) = equal a a1 a2 && equal b b1 b2 in
  testable (Fmt.Dump.pair (pp a) (pp b)) eq

let triple a b c =
  let eq (a1, b1, c1) (a2, b2, c2) =
    equal a a1 a2 && equal b b1 b2 && equal c c1 c2
  in
  let pp =
    Fmt.(
      parens
        (using (fun (x, _, _) -> x) (box (pp a))
        ++ comma
        ++ using (fun (_, x, _) -> x) (box (pp b))
        ++ comma
        ++ using (fun (_, _, x) -> x) (box (pp c))))
  in
  testable pp eq

let option e = testable (Fmt.Dump.option (pp e)) (Option.equal (equal e))

let result a e =
  testable
    (Fmt.Dump.result ~ok:(pp a) ~error:(pp e))
    (Result.equal ~ok:(equal a) ~error:(equal e))

let of_pp pp = testable pp ( = )

let pass (type a) =
  let module M = struct
    type t = a

    let pp fmt _ = Fmt.string fmt "Alcotest.pass"
    let equal _ _ = true
  end in
  (module M : TESTABLE with type t = M.t)

let reject (type a) =
  let module M = struct
    type t = a

    let pp fmt _ = Fmt.string fmt "Alcotest.reject"
    let equal _ _ = false
  end in
  (module M : TESTABLE with type t = M.t)

let show_assert = function
  | "" -> ()
  | msg ->
      Fmt.(flush (Formatters.get_stdout () :> Format.formatter))
        () (* Flush any test stdout preceding the assert *);
      Format.fprintf
        (Formatters.get_stderr () :> Format.formatter)
        "%a %s\n%!" Pp.tag `Assert msg

let check_err fmt = raise (Core.Check_error fmt)

module Source_code_position = struct
  type here = Lexing.position
  type pos = string * int * int * int
end

type 'a extra_info =
  ?here:Source_code_position.here -> ?pos:Source_code_position.pos -> 'a

let pp_location ?here ?pos ppf =
  match (here, pos) with
  | Some (here : Source_code_position.here), _ ->
      Pp.location ppf
        (here.pos_fname, here.pos_lnum, here.pos_cnum - here.pos_bol)
  | _, Some (fname, lnum, cnum, _) -> Pp.location ppf (fname, lnum, cnum)
  | None, None -> ()

let check (type a) ?here ?pos (t : a testable) msg (expected : a) (actual : a) =
  if equal t expected actual then show_assert msg
  else
    let open Fmt in
    let s = const string in
    let pp_error =
      match msg with
      | "" -> nop
      | _ -> const Pp.tag `Fail ++ s (" " ^ msg) ++ cut
    and pp_expected ppf () =
      Fmt.pf ppf "   Expected: `%a'" (styled `Green (pp t)) expected;
      Format.pp_print_if_newline ppf ();
      Fmt.cut ppf ();
      ()
    and pp_actual ppf () =
      Fmt.pf ppf "   Received: `%a'" (styled `Red (pp t)) actual
    in
    raise
      (Core.Check_error
         Fmt.(
           vbox
             ((fun ppf () -> pp_location ?here ?pos ppf)
             ++ pp_error
             ++ cut
             ++ pp_expected
             ++ cut
             ++ pp_actual)
           ++ cut))

let check' ?here ?pos t ~msg ~expected ~actual =
  check ?here ?pos t msg expected actual

let fail ?here ?pos msg =
  check_err (fun ppf () ->
      Fmt.pf ppf "%t%a %s" (pp_location ?here ?pos) Pp.tag `Fail msg)

let failf ?here ?pos fmt = Fmt.kstr (fun msg -> fail ?here ?pos msg) fmt
let neg t = testable (pp t) (fun x y -> not (equal t x y))

let collect_exception f =
  try
    f ();
    None
  with e -> Some e

let check_raises ?here ?pos msg exn f =
  match collect_exception f with
  | None ->
      check_err (fun ppf () ->
          Fmt.pf ppf "%t%a %s: expecting %a, got nothing."
            (pp_location ?here ?pos) Pp.tag `Fail msg Fmt.exn exn)
  | Some e ->
      if e <> exn then
        check_err (fun ppf () ->
            Fmt.pf ppf "%t%a %s: expecting %a, got %a." (pp_location ?here ?pos)
              Pp.tag `Fail msg Fmt.exn exn Fmt.exn e)
      else show_assert msg

let match_raises ?here ?pos msg exnp f =
  match collect_exception f with
  | None ->
      check_err (fun ppf () ->
          Fmt.pf ppf "%t%a %s: got nothing." (pp_location ?here ?pos) Pp.tag
            `Fail msg)
  | Some e ->
      if not (exnp e) then
        check_err (fun ppf () ->
            Fmt.pf ppf "%t%a %s: got %a." (pp_location ?here ?pos) Pp.tag `Fail
              msg Fmt.exn e)
      else show_assert msg

let skip () = raise Core.V1.Skip

include Test_iarray.Make (struct
  type 'a t = 'a testable

  let map = map
  let array = array
end)