【发布时间】:2011-05-13 18:12:10
【问题描述】:
在没有实际克隆 JUnit 或其他东西的情况下,我将一些实用函数放在一起来帮助测试一些 SML 代码。我确实知道 QCheck,但它也不能做这件事,也不是我通常想要的。 (但如果您知道另一个 SML 自动化测试框架,请说出来。)
我希望能够断言某些函数会抛出异常,例如,给定一个函数
fun broken x = raise Fail
我希望能够写出类似的东西
throws ("ERROR: function is not broken enough!", fn () => broken 1, Fail)
如果给定函数没有引发预期的异常,则让它抛出错误。
我尝试编写一个throws 类型为(string * exn * (unit -> unit)) -> unit 的函数,如下所示:
fun throws (msg, e, func) = func ()
handle e' => if e = e'
then ()
else raise ERROR (SOME msg)
但这会产生一堆编译时错误,显然是因为 ML 没有定义例外的平等:
sexp-tests.sml:54.31-57.49 Error: types of rules don't agree [equality type required]
earlier rule(s): ''Z -> unit
this rule: exn -> 'Y
in rule:
exn => raise exn
sexp-tests.sml:54.31-57.49 Error: handler domain is not exn [equality type required]
handler domain: ''Z
in expression:
func ()
handle
e' => if e = e' then () else raise (ERROR <exp>)
| exn => raise exn
作为一种解决方法,我怀疑我可以重用现有的 assert 函数:
assert ((broken 1; false) handle Fail => true | _ => false)
但它需要更多的思考和打字。
那么,有没有办法在 SML 中编写 throws 函数?
【问题讨论】:
标签: exception testing equality sml