Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testable for seq and contramap (for testable) #412

Merged
merged 5 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### unreleased

- Add `seq`, a testable for `Seq.t` and `contramap` (#412 @xvw)

### 1.8.0 (2024-07-25)

- Add `match_raises`, a generalized version of `check_raises`
Expand Down
13 changes: 13 additions & 0 deletions src/alcotest-engine/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ let testable (type a) (pp : a Fmt.t) (equal : a -> a -> bool) : a testable =
end in
(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
testable pp equal

let int32 = testable Fmt.int32 ( = )
let int64 = testable Fmt.int64 ( = )
let int = testable Fmt.int ( = )
Expand Down Expand Up @@ -78,6 +82,15 @@ let list e =
in
testable (Fmt.Dump.list (pp e)) eq

MisterDA marked this conversation as resolved.
Show resolved Hide resolved
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
7 changes: 7 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 Expand Up @@ -107,6 +110,10 @@ val neg : 'a testable -> 'a testable
(** [neg t] is [t]'s negation: it is [true] when [t] is [false] and it is
[false] when [t] is [true]. *)

val contramap : ('b -> 'a) -> 'a testable -> 'b testable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind naming this map -- even if it doesn't follow the theory, that's what more users would expect :p

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about keeping the Fmt convention by calling it using? (Because I find that calling it map makes reading its signature ... complicated).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, btw you are right, map works well, fixed on 415abb4

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using could work too - no strong opinion between map and using

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I go for map :)

(** [contramap f t] lift a ['a testable] to a ['b testable],
converting ['b] to ['a]. *)

(** {1 Assertion functions}

Functions for asserting various properties within unit-tests. A failing
Expand Down