简体   繁体   中英

Propagating errors to cause HUnit tests to fail

I am writing an HUnit test for a function eval :: Wff -> Assignment -> Maybe Bool . Wff is a custom data type which is an abstract parse tree for a simplified subset of boolean expressions:

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 :

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 . 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?

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]
          {- ... -}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM