[英]Propagating errors to cause HUnit tests to fail
I am writing an HUnit test for a function eval :: Wff -> Assignment -> Maybe Bool
. 我正在为功能eval :: Wff -> Assignment -> Maybe Bool
编写HUnit测试。 Wff
is a custom data type which is an abstract parse tree for a simplified subset of boolean expressions: Wff
是一种自定义数据类型,它是布尔表达式的简化子集的抽象分析树:
data Wff = Var Name
| Not Wff
| Or Wff Wff
deriving (Eq)
and Assignment
is a type alias for an associative list which gives a boolean value for each variable in a Wff
: 而Assignment
是关联列表的类型别名,该列表为Wff
每个变量提供布尔值:
type Assignment = [(Name, Bool)]
My current test looks like this: 我当前的测试如下:
testEval :: Test
testEval = "Test eval"
~: TestList $ zipWith (\e (Just a) -> e ~=? a) expected (eval wff <$> assignments)
where expected = [True, False]
assignments = [[('p', True)], [('p', False)]]
Right wff = parse wffStr
wffStr = "p"
Both of the tests constructed pass. 两项测试均通过。 However, the test is not very robust. 但是,该测试不是很可靠。 If I modify it with a more complicated value for wffStr
but make a typo, the pattern Right wff
will fail because parse
will return a Left String
instead of a Rigth Wff
. 如果我使用wffStr
的更复杂的值对其进行wffStr
但输入错误,则模式Right wff
将失败,因为parse
将返回Left String
而不是Rigth Wff
。 This causes the test run to abort and I'd rather get a failure for the few bad tests and the actual results for the rest. 这会导致测试运行中止,而我宁愿因为一些不好的测试而失败,而其余的则是实际结果。 How can I modify my current structure so that the error propagates to cause the test to fail instead of aborting all together? 如何修改当前结构,使错误传播导致测试失败,而不是一起放弃?
I don't know anything about HUnit, but: can you just tell it what to do when the parse fails? 我对HUnit一无所知,但是:您能告诉它解析失败时该怎么做吗?
testEval = case parse wffStr of
Left _ -> {- use HUnit's functions to make a failing test case -}
Right wff -> "Test eval" ~: TestList $ {- ... -}
where expected = [True, False]
{- ... -}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.