on fbc5b30 help sign in
effectful/tests/ErrorTests.hs
36 lines 1.1 kB history raw
1module ErrorTests (errorTests) where
3import Control.Monad.IO.Class
4import Test.Tasty
5import Test.Tasty.HUnit
6import GHC.Stack (getCallStack)
8import Effectful.Error
9import Effectful.Interpreter
10import Effectful.Monad
12errorTests :: TestTree
13errorTests = testGroup "Error"
14 [ testCase "error from interpret" test_errorFromInterpret
15 ]
17test_errorFromInterpret :: Assertion
18test_errorFromInterpret = runIOE $ do
19 result <- runError @String . runNestedErr $ do
20 runError @String nestedErr
21 liftIO $ case result of
22 Left (cs, _) -> assertBool "stack trace points to the correct action" $
23 "nestedErr" == fst (last $ getCallStack cs)
24 Right _ -> assertFailure "error caught by the wrong (inner) handler"
26----------------------------------------
27-- Helpers
29data NestedErr :: Effect where
30 NestedErr :: NestedErr m ()
32nestedErr :: (HasCallStack, NestedErr :> es) => Eff es ()
33nestedErr = send NestedErr
35runNestedErr :: Error String :> es => Eff (NestedErr : es) a -> Eff es a
36runNestedErr = interpret $ \NestedErr -> throwError "nested error"