Add test for document change tracking
authorLuke Lau <luke_lau@icloud.com>
Fri, 15 Jun 2018 05:08:49 +0000 (01:08 -0400)
committerLuke Lau <luke_lau@icloud.com>
Sat, 16 Jun 2018 00:27:52 +0000 (20:27 -0400)
haskell-lsp-test.cabal
src/Language/Haskell/LSP/Test.hs
src/Language/Haskell/LSP/Test/Server.hs
test/Test.hs
test/data/refactor/Main.hs [new file with mode: 0644]

index 0c12d5ee7a6efcb272807290b1834306f8922616..9fb63a305ecb7e48d7bfdcf5297998cd045c50ba 100644 (file)
@@ -61,6 +61,8 @@ test-suite tests
                      , haskell-lsp
                      , conduit
                      , conduit-parse
+                     , aeson
+                     , unordered-containers
   other-modules:       ParsingTests
   default-language:    Haskell2010
 
index 4f82498c732ad64263070d2e1d7f7d420dc4caa4..047e35b0ac715d765eeb4f1c02f11a63b015426e 100644 (file)
@@ -157,7 +157,6 @@ processTextChanges (ReqApplyWorkspaceEdit r) = do
               params = DidChangeTextDocumentParams docId (List changeEvents)
           newVFS <- liftIO $ changeVFS oldVFS (fmClientDidChangeTextDocumentNotification params)
           modify (\s -> s { vfs = newVFS })
-          liftIO $ print newVFS
           return params
 
         applyTextEdit uri edits = applyTextDocumentEdit (TextDocumentEdit (VersionedTextDocumentIdentifier uri 0) edits)
index 65011fd3f106b0947243590c8b86af6b014dffd6..8159bef129784d07fcab9cd6e7b842ab4d9d47f5 100644 (file)
@@ -1,4 +1,4 @@
-module Language.Haskell.LSP.Test.Server where
+module Language.Haskell.LSP.Test.Server (withServer) where
 
 import Control.Concurrent
 import Control.Monad
@@ -8,7 +8,8 @@ import System.Process
 
 withServer :: String -> (Handle -> Handle -> Int -> IO a) -> IO a
 withServer serverExe f = do
-  let createProc = (shell serverExe) { std_in = CreatePipe, std_out = CreatePipe, std_err = CreatePipe }
+  let cmd:args = words serverExe
+      createProc = (proc cmd args) { std_in = CreatePipe, std_out = CreatePipe, std_err = CreatePipe }
   (Just serverIn, Just serverOut, Just serverErr, serverProc) <- createProcess createProc
 
   -- Need to continuously consume to stderr else it gets blocked
index ce8a3573c3f92edaf0c5efa4a523be83479625a9..8f715333092c46b503316b77b0163d8596b9fa18 100644 (file)
@@ -1,9 +1,14 @@
 {-# LANGUAGE DuplicateRecordFields #-}
 {-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE DeriveGeneric #-}
+{-# LANGUAGE DeriveAnyClass #-}
 import           Test.Hspec
+import           Data.Aeson
+import qualified Data.HashMap.Strict as HM
 import           Data.Maybe
 import           Control.Monad.IO.Class
 import           Control.Lens hiding (List)
+import           GHC.Generics
 import           Language.Haskell.LSP.Test
 import           Language.Haskell.LSP.Test.Replay
 import           Language.Haskell.LSP.Types
@@ -61,8 +66,36 @@ main = hspec $ do
           fooSymbol ^. name `shouldBe` "foo"
           fooSymbol ^. kind `shouldBe` SkFunction
 
+  describe "text document state" $
+    it "sends back didChange notifications" $
+      runSession "hie --lsp" "test/data/refactor" $ do
+        doc <- openDoc "Main.hs" "haskell"
+
+        let args = toJSON $ AOP (doc ^. uri)
+                                (Position 1 14)
+                                "Redundant bracket"
+            reqParams = ExecuteCommandParams "applyrefact:applyOne" (Just (List [args]))
+        sendRequest WorkspaceExecuteCommand reqParams
+        skipMany anyNotification
+        _ <- response :: Session ExecuteCommandResponse
+
+        editReq <- request :: Session ApplyWorkspaceEditRequest
+        liftIO $ do
+          let (Just cs) = editReq ^. params . edit . changes
+              [(u, List es)] = HM.toList cs
+          u `shouldBe` doc ^. uri
+          es `shouldBe` [TextEdit (Range (Position 1 0) (Position 1 18)) "main = return 42"]
+
+        checkNoDiagnostics
+
   parsingSpec
 
+data ApplyOneParams = AOP
+  { file      :: Uri
+  , start_pos :: Position
+  , hintTitle :: String
+  } deriving (Generic, ToJSON)
+
 checkNoDiagnostics :: Session ()
 checkNoDiagnostics = do
   diagsNot <- notification :: Session PublishDiagnosticsNotification
diff --git a/test/data/refactor/Main.hs b/test/data/refactor/Main.hs
new file mode 100644 (file)
index 0000000..625e3b8
--- /dev/null
@@ -0,0 +1,2 @@
+main :: IO Int
+main = return (42)
\ No newline at end of file