Skip to content

Commit

Permalink
Add seq testable
Browse files Browse the repository at this point in the history
  • Loading branch information
xvw committed Sep 5, 2024
1 parent 99ebda9 commit 7c5b1b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/alcotest-engine/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ let testable (type a) (pp : a Fmt.t) (equal : a -> a -> bool) : a testable =
(module M)

let contramap f t =
let pp ppf = (Fmt.using f (pp t)) ppf
and equal a b = (equal t (f a) (f b)) in
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 ( = )
Expand Down Expand Up @@ -83,6 +82,15 @@ let list e =
in
testable (Fmt.Dump.list (pp e)) eq

let seq e =
let rec eq s1 s2 =
match (Seq.uncons s1, Seq.uncons s2) with
| Some (x, xs), Some (y, ys) -> equal e x y && eq xs ys
| None, None -> true
| _ -> false
in
testable (Fmt.Dump.seq (pp e)) eq

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
Expand Down
3 changes: 3 additions & 0 deletions src/alcotest-engine/test.mli
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ val unit : unit testable
val list : 'a testable -> 'a list testable
(** [list t] tests lists of [t]s. *)

val seq : 'a testable -> 'a Seq.t testable
(** [seq t] tests sequence of [t]s. *)

val slist : 'a testable -> ('a -> 'a -> int) -> 'a list testable
(** [slist t comp] tests sorted lists of [t]s. The list are sorted using [comp]. *)

Expand Down

0 comments on commit 7c5b1b7

Please sign in to comment.