使用ocaml OUnit2断言

使用ocaml OUnit2断言,ocaml,tdd,Ocaml,Tdd,我正在经历光线跟踪器挑战,我试图断言在将一个点添加到另一个点(点的w=1)时会引发异常 基本上这就是我得到的: tuple.ml type tuple = {x: float; y: float; z: float; w: float} let point a b c = {x = a; y = b; z = c; w = 1.0} exception AddingPoints of string let (+..) a b = if ((a.w =. 1.0 &&

我正在经历光线跟踪器挑战,我试图断言在将一个点添加到另一个点(点的w=1)时会引发异常

基本上这就是我得到的:

tuple.ml

type tuple = {x: float; y: float; z: float; w: float}

let point a b c =
  {x = a; y = b; z = c; w = 1.0}

exception AddingPoints of string

let (+..) a b =
  if ((a.w =. 1.0 && b.w =. 1.0) = false) 
  then raise (AddingPoints "Cannot add points")
  else 
    { 
      x = a.x +. b.x ;
      y = a.y +. b.y ;
      z = a.z +. b.z ;
      w = a.w +. b.w ;
    }
open OUnit2

let p = point 4.3 (-4.2) 3.1

let test_add_point_point_should_fail = "testing adding point to point, it should fail" >::: [
  let p2 = fun () -> p +.. p in
  assert_raises (AddingPoints "Cannot add points") p2;
]
tuple_test.ml

type tuple = {x: float; y: float; z: float; w: float}

let point a b c =
  {x = a; y = b; z = c; w = 1.0}

exception AddingPoints of string

let (+..) a b =
  if ((a.w =. 1.0 && b.w =. 1.0) = false) 
  then raise (AddingPoints "Cannot add points")
  else 
    { 
      x = a.x +. b.x ;
      y = a.y +. b.y ;
      z = a.z +. b.z ;
      w = a.w +. b.w ;
    }
open OUnit2

let p = point 4.3 (-4.2) 3.1

let test_add_point_point_should_fail = "testing adding point to point, it should fail" >::: [
  let p2 = fun () -> p +.. p in
  assert_raises (AddingPoints "Cannot add points") p2;
]
运行后:
ocamlbuild-使用ocamlfind-标记调试元组\u test.byte

我得到:

File "tuple_test.ml", line 41, characters 2-53:
41 |   assert_raises (AddingPoints "Cannot add points") p2;
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This expression has type unit but an expression was expected of type
         OUnit2.test = OUnitTest.test

Command exited with code 2.

我是ocaml的新手,有人能告诉我我缺少什么吗?

expression
assert\u raises(AddingPoints“Cannot add points”)p2没有错,它更多的是关于它周围的代码。您正在使用
(>:)
,并且可能正在尝试创建带有标签的测试。在文档中,您可以看到
val(>:):string->test list->test
,这意味着您不应该向其传递突出显示的表达式为的
单元,而应该传递
测试

您可以使用
OUnit2.test\u case
创建测试,并使用
(>:)
为其创建标签:

open OUnit2

let tst = OUnit2.test_case (fun _test_ctx -> assert_raises (Failure "hd") (fun () -> List.hd []));;

let tst_with_label = "extracting head of an empty list throws an exception" >: tst
您还可以对
OUnit2.test\u list
感兴趣,它似乎是一个编写测试的函数

那里有很多很好的医生