1module Main (main) where
3import Control.Concurrent.Async.Lifted
4import Control.Monad.IO.Class
7import qualified Control.Monad.Catch as E
8import qualified Control.Exception.Lifted as LE
9import qualified UnliftIO.Exception as UE
15main = defaultMain $ testGroup "effectful"
17 [ testCase "runState & execState" test_runState
18 , testCase "evalState" test_evalState
19 , testCase "stateM" test_stateM
20 , testCase "deep stack" test_deepStack
21 , testCase "exceptions" test_exceptions
22 , testCase "concurrency" test_concurrentState
26test_runState :: Assertion
27test_runState = runEffIO $ do
28 (end, len) <- runState (0::Int) . execState collatzStart $ collatz
29 assertEqual_ "correct end" 1 end
30 assertEqual_ "correct len" collatzLength len
32test_evalState :: Assertion
33test_evalState = runEffIO $ do
34 len <- evalState (0::Int) . evalState collatzStart $ collatz *> get @Int
35 assertEqual_ "correct len" collatzLength len
37test_stateM :: Assertion
38test_stateM = runEffIO $ do
39 (a, b) <- runState "hi" . stateM $ \s -> pure (s, s ++ "!!!")
40 assertEqual_ "correct a" "hi" a
41 assertEqual_ "correct b" "hi!!!" b
43test_deepStack :: Assertion
44test_deepStack = runEffIO $ do
45 n <- evalState () . execState (0::Int) $ do
46 evalState () . evalState () $ do
48 evalState () . evalState () . evalState () $ do
55test_exceptions :: Assertion
56test_exceptions = runEffIO $ do
57 testTry "exceptions" E.try
58 testCatch "exceptions" E.catch
59 testTry "lifted-base" LE.try
60 testCatch "lifted-base" LE.catch
61 testTry "unliftio" UE.try
62 testCatch "unliftio" UE.catch
66 -> (forall a es. IOE :> es => Eff es a -> Eff es (Either Ex a))
68 testTry lib tryImpl = do
69 e <- tryImpl $ runState (0::Int) action
70 assertEqual_ (lib ++ " - exception caught") e (Left Ex)
71 s <- execState (0::Int) $ tryImpl action
72 assertEqual_ (lib ++ " - state partially updated") s 1
76 -> (forall a es. IOE :> es => Eff es a -> (Ex -> Eff es a) -> Eff es a)
78 testCatch lib catchImpl = do
79 s <- execState (0::Int) $ do
80 _ <- (evalState () action) `catchImpl` \Ex -> modify @Int (+4)
82 assertEqual_ (lib ++ " - state correctly updated") s 13
84 action :: State Int :> es => Eff es ()
90test_concurrentState :: Assertion
91test_concurrentState = runEffIO . evalState x $ do
92 replicateConcurrently_ 2 $ do
94 assertEqual_ "x = n" x r
99 goDownward :: State Int :> es => Int -> Eff es Int
100 goDownward acc = get @Int >>= \case
106----------------------------------------
109data Ex = Ex deriving (Eq, Show)
110instance E.Exception Ex
112runEffIO :: Eff '[IOE] a -> IO a
113runEffIO = runEff . runIOE
115assertEqual_ :: (Eq a, Show a, IOE :> es) => String -> a -> a -> Eff es ()
116assertEqual_ msg expected given = liftIO $ assertEqual msg expected given
118----------------------------------------
120collatzStart :: Integer
121collatzStart = 9780657630
126-- | Tests multiple 'State'S, 'put', 'get' and 'modify'.
127collatz :: (State Integer :> es, State Int :> es) => Eff es ()
128collatz = get @Integer >>= \case
131 then do put $ n `div` 2
134 else do put $ 3*n + 1
137{-# NOINLINE collatz #-}
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list