This commit is contained in:
2024-10-06 15:30:00 +02:00
parent 95ad5d0b02
commit 067f70622f
2 changed files with 35 additions and 15 deletions

View File

@ -23,14 +23,13 @@ runEval = runEval' envEmpty stateInitial
Nothing -> ([], Left ("Cannot find key: "++(show key))) Nothing -> ([], Left ("Cannot find key: "++(show key)))
runEval' r s (Free (KvPutOp key val m)) = runEval' r s (Free (KvPutOp key val m)) =
runEval' r ((key,val):s) m runEval' r ((key,val):s) m
runEval' r s (Free (TransactionOp l m)) = do runEval' r s (Free (TransactionOp l m)) =
(p, resl) <- (runEval' r s l) let (p, resl) = runEval (do l >> getState)
case (runEval' r s l) of in case resl of
Right _ -> do Right s' ->
s' <- getState let (ps, resm) = runEval' r s' m
(ps, resm) <- (runEval' r s' m) in (p ++ ps, resm)
return (p ++ ps, resm) Left _ ->
Left _ -> do let (ps, resm) = runEval' r s m
(ps, resm) <- runEval' r s m in (p ++ ps, resm)
return (p ++ ps, resm)
runEval' _ _ (Free (ErrorOp e)) = ([], Left e) runEval' _ _ (Free (ErrorOp e)) = ([], Left e)

View File

@ -79,7 +79,7 @@ pureTests =
eval' (TryCatch (Div (CstInt 1) (CstInt 0)) (CstInt 1)) eval' (TryCatch (Div (CstInt 1) (CstInt 0)) (CstInt 1))
@?= ([], Right (ValInt 1)), @?= ([], Right (ValInt 1)),
-- --
testCase "TryCatch catch1" $ testCase "TryCatch catch2" $
eval' (TryCatch (Div (CstInt 1) (CstInt 0)) (Div (CstInt 1) (CstInt 0))) eval' (TryCatch (Div (CstInt 1) (CstInt 0)) (Div (CstInt 1) (CstInt 0)))
@?= ([], Left "Division by zero"), @?= ([], Left "Division by zero"),
-- --
@ -100,16 +100,37 @@ pureTests =
@?= ([], Left "Cannot find key: ValInt 1"), @?= ([], Left "Cannot find key: ValInt 1"),
-- --
testCase "TransactionOp 1" $ testCase "TransactionOp 1" $
runEval (transaction (evalKvPut (ValInt 0) (ValInt 1))) let goodPut = (evalKvPut (ValInt 0) (ValInt 1)) in
runEval (transaction goodPut)
@?= ([], Right ()), @?= ([], Right ()),
-- --
testCase "TransactionOp 2" $ testCase "TransactionOp 2" $
runEval ((transaction (evalKvPut (ValInt 0) (ValInt 1))) >> (eval (KvGet (CstInt 0)))) let goodPut = evalKvPut (ValInt 0) (ValInt 1) in
let get0 = eval (KvGet (CstInt 0)) in
runEval (transaction goodPut >> get0)
@?= ([], Right (ValInt 1)), @?= ([], Right (ValInt 1)),
-- --
testCase "TransactionOp Fail" $ testCase "TransactionOp Fail" $
runEval ((transaction ((evalKvPut (ValInt 0) (ValInt 1)) >> (failure "die"))) >> (eval (KvGet (CstInt 0)))) let badPut = (evalKvPut (ValInt 0) (ValInt 1)) >> (failure "die") in
@?= ([], Left "Cannot find key: ValInt 0") let get0 = eval (KvGet (CstInt 0)) in
runEval (transaction badPut >> get0)
@?= ([], Left "Cannot find key: ValInt 0"),
--
testCase "TransactionOp Propagation" $
let badPut = (evalKvPut (ValInt 0) (ValInt 1)) >> (failure "die") in
runEval (transaction badPut)
@?= ([], Right ()),
--
testCase "TransactionOp Printing" $
runEval (transaction (evalPrint "weee" >> failure "oh shit"))
@?= (["weee"], Right ()),
--
testCase "TransactionOp Nested" $
let badPut = (evalKvPut (ValInt 0) (ValInt 1)) >> (failure "die") in
let goodPut = evalKvPut (ValInt 0) (ValInt 1) in
let get0 = eval (KvGet (CstInt 0)) in
runEval (transaction (goodPut >> transaction badPut) >> get0)
@?= ([], Right (ValInt 1))
] ]
ioTests :: TestTree ioTests :: TestTree