Skip to content

Commit b3a4a62

Browse files
authored
Merge pull request #419 from psafont/exnpect
[Breaking] Change match_raises to be more intuitive
2 parents f49a310 + 3d6c1ca commit b3a4a62

File tree

7 files changed

+73
-2
lines changed

7 files changed

+73
-2
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
### unreleased
22

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

58
### 1.8.0 (2024-07-25)
69

src/alcotest-engine/test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ let match_raises ?here ?pos msg exnp f =
263263
Fmt.pf ppf "%t%a %s: got nothing." (pp_location ?here ?pos) Pp.tag
264264
`Fail msg)
265265
| Some e ->
266-
if exnp e then
266+
if not (exnp e) then
267267
check_err (fun ppf () ->
268268
Fmt.pf ppf "%t%a %s: got %a." (pp_location ?here ?pos) Pp.tag `Fail
269269
msg Fmt.exn e)

src/alcotest-engine/test.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ val check_raises : (string -> exn -> (unit -> unit) -> unit) extra_info
158158

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

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

test/e2e/alcotest/passing/dune.inc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
isatty
1616
json_output
1717
list_tests
18+
match_raises
1819
only_monadic_effects
1920
quick_only
2021
quick_only_regex
@@ -41,6 +42,7 @@
4142
isatty
4243
json_output
4344
list_tests
45+
match_raises
4446
only_monadic_effects
4547
quick_only
4648
quick_only_regex
@@ -621,6 +623,44 @@
621623
(action
622624
(diff list_tests-js.expected list_tests-js.processed)))
623625

626+
(rule
627+
(target match_raises.actual)
628+
(action
629+
(with-outputs-to %{target}
630+
(with-accepted-exit-codes (or 0 124 125)
631+
(run %{dep:match_raises.exe})))))
632+
633+
(rule
634+
(target match_raises.processed)
635+
(action
636+
(with-outputs-to %{target}
637+
(run ../../strip_randomness.exe %{dep:match_raises.actual}))))
638+
639+
(rule
640+
(alias runtest)
641+
(package alcotest)
642+
(action
643+
(diff match_raises.expected match_raises.processed)))
644+
645+
(rule
646+
(target match_raises-js.actual)
647+
(action
648+
(with-outputs-to %{target}
649+
(with-accepted-exit-codes (or 0 124 125)
650+
(run node %{dep:match_raises.bc.js})))))
651+
652+
(rule
653+
(target match_raises-js.processed)
654+
(action
655+
(with-outputs-to %{target}
656+
(run ../../strip_randomness.exe %{dep:match_raises-js.actual}))))
657+
658+
(rule
659+
(alias runtest-js)
660+
(package alcotest)
661+
(action
662+
(diff match_raises-js.expected match_raises-js.processed)))
663+
624664
(rule
625665
(target only_monadic_effects.actual)
626666
(action
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Testing `Exceptions'.
2+
This run has ID `<uuid>'.
3+
4+
[OK] matches_raises 0 True means the exception is expec...
5+
6+
Full test results in `<build-context>/_build/_tests/<test-dir>'.
7+
Test Successful in <test-duration>s. 1 test run.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Testing `Exceptions'.
2+
This run has ID `<uuid>'.
3+
4+
[OK] matches_raises 0 True means the exception is expec...
5+
6+
Full test results in `<build-context>/_build/_tests/<test-dir>'.
7+
Test Successful in <test-duration>s. 1 test run.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let to_test () = raise (Failure "")
2+
let expect_failure = function Failure _ -> true | _ -> false
3+
let test () = Alcotest.match_raises "Generates Failure" expect_failure to_test
4+
5+
let () =
6+
Alcotest.run "Exceptions"
7+
[
8+
( "matches_raises",
9+
[
10+
Alcotest.test_case "True means the exception is expected" `Quick test;
11+
] );
12+
]

0 commit comments

Comments
 (0)