2016-10-12 17:00:19 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2017-11-14 03:36:14 +04:00
|
|
|
(* Copyright (c) 2014 - 2017. *)
|
2016-10-12 17:00:19 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
2016-09-30 13:43:50 +04:00
|
|
|
|
|
|
|
include Kaputt.Assertion
|
|
|
|
|
2017-04-05 20:24:26 +04:00
|
|
|
module Assert = Kaputt.Abbreviations.Assert
|
|
|
|
|
2016-09-30 13:43:50 +04:00
|
|
|
let format_msg = function None -> None | Some msg -> Some (msg ^ "\n")
|
|
|
|
|
2017-04-05 20:24:26 +04:00
|
|
|
let is_error ?(msg="") = function
|
2017-01-23 14:09:45 +04:00
|
|
|
| Error _ -> ()
|
|
|
|
| Ok _ -> fail "Error _" "Ok _" msg
|
|
|
|
|
2017-04-05 20:24:26 +04:00
|
|
|
let is_ok ?(msg="") = function
|
2017-01-23 14:09:45 +04:00
|
|
|
| Ok _ -> ()
|
|
|
|
| Error _ -> fail "Ok _" "Error _" msg
|
|
|
|
|
2017-04-05 20:24:26 +04:00
|
|
|
let equal_string_list_list ?msg l1 l2 =
|
2016-09-30 13:43:50 +04:00
|
|
|
let msg = format_msg msg in
|
|
|
|
let pr_persist l =
|
|
|
|
let res =
|
|
|
|
String.concat ";" (List.map (fun s -> Printf.sprintf "%S" s) l) in
|
|
|
|
Printf.sprintf "[%s]" res in
|
|
|
|
Assert.make_equal_list ?msg (=) pr_persist l1 l2
|
|
|
|
|
2016-11-14 19:26:34 +04:00
|
|
|
let equal_string_list ?msg l1 l2 =
|
2016-10-06 20:30:04 +04:00
|
|
|
let msg = format_msg msg in
|
2016-11-14 19:26:34 +04:00
|
|
|
Assert.make_equal_list ?msg (=) (fun x -> x) l1 l2
|
2016-10-06 20:30:04 +04:00
|
|
|
|
2016-09-30 13:43:50 +04:00
|
|
|
let equal_string_option ?msg o1 o2 =
|
|
|
|
let msg = format_msg msg in
|
|
|
|
let prn = function
|
|
|
|
| None -> "None"
|
|
|
|
| Some s -> s in
|
|
|
|
Assert.equal ?msg ~prn o1 o2
|
|
|
|
|
|
|
|
let equal_result ?msg r1 r2 ~equal_ok ~equal_err =
|
|
|
|
let msg = format_msg msg in
|
|
|
|
match r1, r2 with
|
|
|
|
| Ok r1, Ok r2 -> equal_ok ?msg r1 r2
|
|
|
|
| Error e1, Error e2 -> equal_err ?msg e1 e2
|
2017-04-05 20:24:26 +04:00
|
|
|
| Ok _, Error _ | Error _, Ok _ ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "Results are not the same"
|
2016-10-20 18:15:31 +04:00
|
|
|
|
|
|
|
let equal_exn ?msg exn1 exn2 =
|
|
|
|
let msg = format_msg msg in
|
|
|
|
let prn = Printexc.to_string in
|
|
|
|
Assert.equal ?msg ~prn exn1 exn2
|
|
|
|
|
|
|
|
let test_fail ?(msg = "") ?(prn = Assert.default_printer) f may_fail =
|
|
|
|
try
|
|
|
|
let value = f () in
|
|
|
|
fail "any exception" ("no exception: " ^ prn value) msg
|
|
|
|
with exn ->
|
|
|
|
if not (may_fail exn) then
|
|
|
|
fail "exception"
|
|
|
|
(Printf.sprintf "unexpectec exception: %s" (Printexc.to_string exn))
|
|
|
|
msg
|
|
|
|
|
|
|
|
let fail_msg fmt =
|
|
|
|
Format.kasprintf Assert.fail_msg fmt
|
|
|
|
|
|
|
|
let fail expected given fmt =
|
|
|
|
Format.kasprintf (Assert.fail expected given) fmt
|
|
|
|
|
2017-03-30 13:26:11 +04:00
|
|
|
let equal_float ?eq ?prn ?msg f1 f2 =
|
|
|
|
match classify_float f1, classify_float f2 with
|
|
|
|
| FP_nan, FP_nan -> ()
|
|
|
|
| _ -> equal ?eq ?prn ?msg f1 f2
|