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

added match_raises a generalized version of check_raises #386

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
### unreleased

- Add `match_raises`, a generalized version of `check_raises`
(#88, #386, @JoanThibault)

### 1.7.0 (2023-02-24)

- Allow skipping a test case from inside the test case (#368, @apeschar)
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 @@ -239,5 +239,18 @@ let check_raises ?here ?pos msg exn f =
Fmt.pf ppf "%t%a %s: expecting %s, got %s." (pp_location ?here ?pos)
Pp.tag `Fail msg (Printexc.to_string exn) (Printexc.to_string e))

let match_raises ?here ?pos msg exnp f =
show_assert msg;
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 exnp e then
check_err (fun ppf () ->
Fmt.pf ppf "%t%a %s: got %s." (pp_location ?here ?pos) Pp.tag `Fail
msg (Printexc.to_string e))

let skip () = raise Core.Skip
let () = at_exit (Format.pp_print_flush Format.err_formatter)
4 changes: 4 additions & 0 deletions src/alcotest-engine/test.mli
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ val failf : (('a, Format.formatter, unit, 'b) format4 -> 'a) extra_info
val check_raises : (string -> exn -> (unit -> unit) -> unit) extra_info
(** Check that an exception is raised. *)

val match_raises :
(string -> (exn -> bool) -> (unit -> unit) -> unit) extra_info
(** Check that an exception is raised. *)

val skip : unit -> 'a
(** Skip the current test case.

Expand Down