--- todo: Maybe make a reader monad and a fail function for it?
-listenServer
- :: [B.ByteString]
- -> Handle
- -> (MVar LSP.LspIdRsp, MVar LSP.LspId)
- -> MVar Bool
- -> IO ()
-listenServer [] _ _ passVar = putMVar passVar True
-listenServer expectedMsgs h semas@(reqSema, rspSema) passVar = do
- msg <- getNextMessage h
- putStrLn $ "Remaining messages " ++ show (length expectedMsgs)
+-- | The internal monad for tests that can fail or pass,
+-- ending execution early.
+type Session = ReaderT (MVar Bool) IO
+
+failSession :: String -> Session ()
+failSession reason = do
+ lift $ putStrLn reason
+ passVar <- ask
+ lift $ putMVar passVar False
+
+passSession :: Session ()
+passSession = do
+ passVar <- ask
+ lift $ putMVar passVar True
+
+-- | Listens to the server output, makes sure it matches the record and
+-- signals any semaphores
+listenServer :: [B.ByteString] -> Handle -> (MVar LSP.LspIdRsp, MVar LSP.LspId) -> Session ()
+listenServer [] _ _ = passSession
+listenServer expectedMsgs h semas@(reqSema, rspSema) = do
+ msg <- lift $ getNextMessage h
+ lift $ putStrLn $ "Remaining messages " ++ show (length expectedMsgs)