Add TryCatchOp support for runEvalIO

This commit is contained in:
2024-10-06 15:09:14 +02:00
parent 94ba5579c8
commit 95ad5d0b02
2 changed files with 22 additions and 1 deletions

View File

@ -94,3 +94,8 @@ runEvalIO evalm = do
runEvalIO' r db m
Left e -> pure $ Left e
runEvalIO' _ _ (Free (ErrorOp e)) = pure $ Left e
runEvalIO' r db (Free (TryCatchOp m1 m2)) = do
result <- runEvalIO' r db m1
case result of
Right x -> pure $ Right x
Left _ -> runEvalIO' r db m2

View File

@ -142,7 +142,23 @@ ioTests =
getState
r @?= Right [(ValInt 0, ValInt 5)],
--
---
testCase "TryCatch try1" $ do
r <- evalIO' (TryCatch (CstInt 1) (CstInt 2))
r @?= Right (ValInt 1),
---
testCase "TryCatch try2" $ do
r <- evalIO' (TryCatch (CstInt 1) (Div (CstInt 1) (CstInt 0)))
r @?= Right (ValInt 1),
---
testCase "TryCatch catch1" $ do
r <- evalIO' (TryCatch (Div (CstInt 1) (CstInt 0)) (CstInt 1))
r @?= Right (ValInt 1),
---
testCase "TryCatch catch2" $ do
r <- evalIO' (TryCatch (Div (CstInt 1) (CstInt 0)) (Div (CstInt 1) (CstInt 0)))
r @?= Left "Division by zero",
---
testCase "KvPutOp" $ do
r <- evalIO' (KvPut (CstInt 1) (CstInt 2))
r @?= Right (ValInt 2),