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

[Breaking] Change match_raises to be more intuitive #419

Merged
merged 2 commits into from
Mar 13, 2025
Merged
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,6 +1,9 @@
### unreleased

- Add `seq`, a testable for `Seq.t` and `contramap` (#412 @xvw)
- BREAKING: `match_raises` now expects the user-defined function to return
true for expected exceptions. Previously false was interpreted as an
expected exception. (#418, #419, @psafont)

### 1.8.0 (2024-07-25)

2 changes: 1 addition & 1 deletion src/alcotest-engine/test.ml
Original file line number Diff line number Diff line change
@@ -263,7 +263,7 @@ let match_raises ?here ?pos msg exnp f =
Fmt.pf ppf "%t%a %s: got nothing." (pp_location ?here ?pos) Pp.tag
`Fail msg)
| Some e ->
if exnp e then
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)
4 changes: 3 additions & 1 deletion src/alcotest-engine/test.mli
Original file line number Diff line number Diff line change
@@ -158,7 +158,9 @@ val check_raises : (string -> exn -> (unit -> unit) -> unit) extra_info

val match_raises :
(string -> (exn -> bool) -> (unit -> unit) -> unit) extra_info
(** Check that an exception is raised. *)
(** [match_raises msg exception_is_expected f] Runs [f ()], and passes the
raised exception to [exception_is_expected]. The check fails when no
exception is raised, or [exception_is_expected] returns false. *)

val skip : unit -> 'a
(** Skip the current test case.
40 changes: 40 additions & 0 deletions test/e2e/alcotest/passing/dune.inc
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
isatty
json_output
list_tests
match_raises
only_monadic_effects
quick_only
quick_only_regex
@@ -41,6 +42,7 @@
isatty
json_output
list_tests
match_raises
only_monadic_effects
quick_only
quick_only_regex
@@ -621,6 +623,44 @@
(action
(diff list_tests-js.expected list_tests-js.processed)))

(rule
(target match_raises.actual)
(action
(with-outputs-to %{target}
(with-accepted-exit-codes (or 0 124 125)
(run %{dep:match_raises.exe})))))

(rule
(target match_raises.processed)
(action
(with-outputs-to %{target}
(run ../../strip_randomness.exe %{dep:match_raises.actual}))))

(rule
(alias runtest)
(package alcotest)
(action
(diff match_raises.expected match_raises.processed)))

(rule
(target match_raises-js.actual)
(action
(with-outputs-to %{target}
(with-accepted-exit-codes (or 0 124 125)
(run node %{dep:match_raises.bc.js})))))

(rule
(target match_raises-js.processed)
(action
(with-outputs-to %{target}
(run ../../strip_randomness.exe %{dep:match_raises-js.actual}))))

(rule
(alias runtest-js)
(package alcotest)
(action
(diff match_raises-js.expected match_raises-js.processed)))

(rule
(target only_monadic_effects.actual)
(action
7 changes: 7 additions & 0 deletions test/e2e/alcotest/passing/match_raises-js.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Testing `Exceptions'.
This run has ID `<uuid>'.

[OK] matches_raises 0 True means the exception is expec...

Full test results in `<build-context>/_build/_tests/<test-dir>'.
Test Successful in <test-duration>s. 1 test run.
7 changes: 7 additions & 0 deletions test/e2e/alcotest/passing/match_raises.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Testing `Exceptions'.
This run has ID `<uuid>'.

[OK] matches_raises 0 True means the exception is expec...

Full test results in `<build-context>/_build/_tests/<test-dir>'.
Test Successful in <test-duration>s. 1 test run.
12 changes: 12 additions & 0 deletions test/e2e/alcotest/passing/match_raises.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let to_test () = raise (Failure "")
let expect_failure = function Failure _ -> true | _ -> false
let test () = Alcotest.match_raises "Generates Failure" expect_failure to_test

let () =
Alcotest.run "Exceptions"
[
( "matches_raises",
[
Alcotest.test_case "True means the exception is expected" `Quick test;
] );
]