简体   繁体   中英

How to assert that a function throws an exception in OCaml?

in OCaml I can catch exceptions:

try some_fn () with | e -> print_endline "exception caught!"

but what about the other way around, detecting that a function does not throw an exception (when it should be)?

I managed to write some very hacky way to achieve that: I create a wrapper around the function I want to test, which throws some specific exception at the end. I catch anything but that exception.

exception Finished

let check (fn : unit -> unit) =
  let wrapper _ = (
    fn ();
    raise Finished)
  in
  try wrapper () with e when e <> Finished -> ()

in utop this works:

utop # check (fun _ -> ());;
Exception: Finished.
utop # check (fun _ -> failwith "hey");;
- : unit = ()

EDIT: my coworker suggested:

let should_fail fn =
  if Result.try_with fn |> Result.is_ok then failwith "this test should fail"

Exceptions can be handled within pattern matching cases, allowing very clean expression for this check:

match some_fn () with 
| exception _ -> print_endline "exception caught!"
| _           -> print_endline "error: no exception raised!"

We can abstract this check into a function:

let throws_exception f =
  match f () with 
  | exception _ -> true
  | _           -> false

Which we can use in an assertion like so:

assert (throws_exception (fun () -> raise (Failure "msg")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM