@@ -20,10 +20,8 @@ import Control.Concurrent
2020import qualified Data.HashMap.Strict as H
2121import Data.Aeson.Encode.Pretty
2222import Data.Aeson.Types
23- import Data.Aeson
2423import Data.Aeson.KeyMap
2524import Control.Concurrent.Async
26- import Control.Concurrent
2725import Control.Exception
2826import qualified Data.ByteString.Lazy.Char8 as BL8 ( hPutStrLn )
2927import Network.Simple.TCP hiding (send )
@@ -32,8 +30,7 @@ import System.IO
3230import Data.String.Conversions
3331import Test.Hspec
3432----------------------------------------------------------------------------
35- import DAP.Utils hiding (send )
36- import DAP.Internal
33+ import DAP.Utils
3734import DAP.Types
3835import DAP.Event
3936import DAP.Server
@@ -45,62 +42,62 @@ main = withServer $
4542 describe " Should connect to the mock DAP server from a client" $ do
4643
4744 it " Should increment sequence number properly" $ do
48- withNewClient $ \ handle -> do
49- send handle
45+ withNewClient $ \ h -> do
46+ send h
5047 [ " command" .= (" initialize" :: String )
5148 , " seq" .= (1 :: Int )
5249 , " type" .= (" request" :: String )
5350 ]
54- shouldReceive handle
51+ shouldReceive h
5552 [ " seq" .= (2 :: Int )
5653 , " request_seq" .= (1 :: Int )
5754 ]
5855
5956 it " Should connect / disconnect 100 clients" $ do
6057 replicateM_ 100 $
61- withNewClient $ \ handle -> do
62- send handle
58+ withNewClient $ \ h -> do
59+ send h
6360 [ " command" .= (" initialize" :: String )
6461 , " seq" .= (1 :: Int )
6562 , " type" .= (" request" :: String )
6663 ]
67- shouldReceive handle
64+ shouldReceive h
6865 [ " seq" .= (2 :: Int )
6966 , " request_seq" .= (1 :: Int )
7067 ]
7168
7269 it " Should perform req/resp. for initialize and receive initialized event" $ do
73- withNewClient $ \ handle -> do
74- send handle
70+ withNewClient $ \ h -> do
71+ send h
7572 [ " command" .= (" initialize" :: String )
7673 , " seq" .= (1 :: Int )
7774 , " type" .= (" request" :: String )
7875 ]
79- shouldReceive handle
76+ shouldReceive h
8077 [ " seq" .= (2 :: Int )
8178 , " request_seq" .= (1 :: Int )
8279 , " command" .= (" initialize" :: String )
8380 , " type" .= (" response" :: String )
8481 ]
85- shouldReceive handle
82+ shouldReceive h
8683 [ " type" .= (" event" :: String )
8784 , " event" .= (" initialized" :: String )
8885 ]
8986
9087 it " Should receive configuration done and stop event" $ do
91- withNewClient $ \ handle -> do
92- send handle
88+ withNewClient $ \ h -> do
89+ send h
9390 [ " command" .= (" configurationDone" :: String )
9491 , " seq" .= (100 :: Int )
9592 , " type" .= (" request" :: String )
9693 ]
97- shouldReceive handle
94+ shouldReceive h
9895 [ " seq" .= (101 :: Int )
9996 , " request_seq" .= (100 :: Int )
10097 , " command" .= (" configurationDone" :: String )
10198 , " type" .= (" response" :: String )
10299 ]
103- shouldReceive handle
100+ shouldReceive h
104101 [ " type" .= (" event" :: String )
105102 , " event" .= (" stopped" :: String )
106103 ]
@@ -116,6 +113,7 @@ mockServerTalk CommandInitialize = do
116113mockServerTalk CommandConfigurationDone = do
117114 sendConfigurationDoneResponse
118115 sendStoppedEvent defaultStoppedEvent
116+ mockServerTalk _ = pure ()
119117
120118-- | Sample port shared amongst client and server
121119--
@@ -133,7 +131,7 @@ withServer :: IO () -> IO ()
133131withServer test = withAsync server (const test)
134132 where
135133 server = runDAPServer config mockServerTalk
136- capabilities = defaultCapabilities
134+ sc = defaultCapabilities
137135 { supportsConfigurationDoneRequest = True
138136 , supportsHitConditionalBreakpoints = True
139137 , supportsModulesRequest = True
@@ -149,7 +147,7 @@ withServer test = withAsync server (const test)
149147 config = ServerConfig
150148 { host = testHost
151149 , port = testPort
152- , serverCapabilities = capabilities
150+ , serverCapabilities = sc
153151 , debugLogging = False
154152 }
155153
@@ -158,9 +156,9 @@ withServer test = withAsync server (const test)
158156withNewClient :: (Handle -> IO () ) -> IO ()
159157withNewClient continue = flip catch exceptionHandler $
160158 connect testHost (show testPort) $ \ (socket, _) -> do
161- handle <- socketToHandle socket ReadWriteMode
162- hSetNewlineMode handle NewlineMode { inputNL = CRLF , outputNL = CRLF }
163- continue handle `finally` hClose handle
159+ h <- socketToHandle socket ReadWriteMode
160+ hSetNewlineMode h NewlineMode { inputNL = CRLF , outputNL = CRLF }
161+ continue h `finally` hClose h
164162 where
165163 exceptionHandler :: SomeException -> IO ()
166164 exceptionHandler _ = do
@@ -171,8 +169,8 @@ withNewClient continue = flip catch exceptionHandler $
171169-- | Helper to send JSON payloads to the server
172170--
173171send :: Handle -> [Pair ] -> IO ()
174- send handle message
175- = BL8. hPutStrLn handle
172+ send h message
173+ = BL8. hPutStrLn h
176174 $ cs (encodeBaseProtocolMessage (object message))
177175
178176-- | Helper to receive JSON payloads to the client
@@ -185,9 +183,11 @@ shouldReceive
185183 -- ^ Subset of JSON values that should be present in the payload
186184 -> IO ()
187185shouldReceive h expected = do
188- let Object ex = object expected
189- readPayload h >>= \ case
190- Left e -> fail e
191- Right actual
192- | toHashMapText ex `H.isSubmapOf` toHashMapText actual -> pure ()
193- | otherwise -> encodePretty actual `shouldBe` encodePretty ex
186+ case object expected of
187+ Object ex ->
188+ readPayload h >>= \ case
189+ Left e -> fail e
190+ Right actual
191+ | toHashMapText ex `H.isSubmapOf` toHashMapText actual -> pure ()
192+ | otherwise -> encodePretty actual `shouldBe` encodePretty ex
193+ _ -> fail " Invalid JSON"
0 commit comments