module ErrorTest where import ErrorPath import Test.HUnit hiding (Path) import SimpleExpr main :: IO () main = runTestTT tests >> return () tests :: Test tests = test [ assertOkay 6 $ calc (2 + 4) , assertFailWith [0] $ calc (1 + 4) , assertFailWith [0] $ calc (5 + 5) , assertFailWith [1] $ calc (2 + 5) , assertFailWith [1] $ calc ((2 + 2) + 3) , assertFailWith [1,1] $ calc (2 + (2 + 3)) , assertFailWith [0,1] $ calc ((2 + 3) + 3) ] calc :: Expr -> ErrorPath String Int calc = foldExprE evalAlg assertFailWith :: Path -> ErrorPath e a -> Assertion assertFailWith p ep = case runErrorPath ep of Left (_, p') -> assertEqual "" p p' _ -> assertFailure "expected failure but found success" assertOkay :: (Eq a, Show a) => a -> ErrorPath e a -> Assertion assertOkay v ep = case runErrorPath ep of Right v' -> assertEqual "" v v' _ -> assertFailure "expected success but found failure"