我正在经历射线追踪挑战,我试图断言当将一个点添加到另一个点时会引发异常(点数为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 ;
}tuple_test.ml
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 -use-ocamlfind -tag debug tuple_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.我是个新手,有人能告诉我我错过了什么吗?
发布于 2020-06-21 20:29:57
表达式assert_raises (AddingPoints "Cannot add points") p2没有什么问题,更多的是关于它周围的代码。您正在使用(>:::),并可能尝试创建一个带有标签的测试。在文档中,您可以看到val (>:::) : string -> test list -> test,这意味着您不应该将突出显示的表达式为unit传递给它,而应该传递给test。
您可以使用OUnit2.test_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_list感兴趣,它似乎是一个编写测试的函数。
有相当不错的文档可用。
https://stackoverflow.com/questions/62491142
复制相似问题