Commits

Yit Phang Khoo committed ebddbc2

Add assert_raises to MyOUnit.

Comments (0)

Files changed (1)

Test/TestUtil/MyOUnit.ml

 let assert_string msg =
     if not (msg = "") then assert_failure "@[%s@]@." msg
 
+let assert_raises ?msg exn fn =
+    try
+        fn ();
+        let exn = Printexc.to_string exn in
+        begin match msg with
+            | Some m -> assert_failure "@[%s@]@\n @[<2>expected exception not raised:@ %s@]" m exn
+            | None -> assert_failure "@[<2>expected exception not raised:@ %s@]" exn
+        end
+    with exn' ->
+        if exn' <> exn then
+            let exn = Printexc.to_string exn in
+            let exn' = Printexc.to_string exn' in
+            match msg with
+                | Some m -> assert_failure "@[%s@]@\n @[@[<2>expected exception:@ %s@]@ @[<2>but got:@ %s@]@]" m exn exn'
+                | None -> assert_failure "@[@[<2>expected exception:@ %s@]@ @[<2>but got:@ %s@]@]" exn exn'
+
 let assert_equal ?(eq=Pervasives.(=)) ?printer ?msg expected actual =
     if not (eq expected actual) then begin match printer, msg with
         | Some p, Some m -> assert_failure "@[%s@]@\n  @[@[<2>expected:@ %a@]@ @[<2>but got:@ %a@]@]" m p expected p actual