|
| 1 | +module Spec.Tests.Go.Completion (tests) where |
| 2 | + |
| 3 | +import Control.Lens |
| 4 | +import Control.Monad.IO.Unlift |
| 5 | +import qualified Data.List as L |
| 6 | +import Data.String.Interpolate |
| 7 | +import Data.Text (Text) |
| 8 | +import Language.LSP.Protocol.Lens |
| 9 | +import Language.LSP.Protocol.Types |
| 10 | +import Language.LSP.Test |
| 11 | +import qualified Language.LSP.Test.Helpers as Helpers |
| 12 | +import Spec.Tests.Go.Common |
| 13 | +import Test.Sandwich as Sandwich |
| 14 | +import Test.Sandwich.Waits (waitUntil) |
| 15 | +import TestLib.LSP |
| 16 | +import TestLib.Types |
| 17 | + |
| 18 | + |
| 19 | +tests :: (LspContext context m, HasNixEnvironment context) => SpecFree context m () |
| 20 | +tests = describe "Completions" $ do |
| 21 | + describe "main.ipynb" $ do |
| 22 | + it "provides fmt. completions" $ doSession' "main.ipynb" lsName fmtCompletionCodeNotebook $ \(Helpers.LspSessionInfo {..}) -> do |
| 23 | + ident <- openDoc lspSessionInfoFileName LanguageKind_Go |
| 24 | + |
| 25 | + waitUntil 60 $ do |
| 26 | + completions <- getCompletions ident (Position 1 4) |
| 27 | + info [i|Got completions: #{completions}|] |
| 28 | + let insertTexts = fmap (^. label) completions |
| 29 | + insertTexts `listShouldContain` "Println" |
| 30 | + insertTexts `listShouldContain` "Printf" |
| 31 | + |
| 32 | + it "provides local variable completions" $ doSession' "main.ipynb" lsName localVarCodeNotebook $ \(Helpers.LspSessionInfo {..}) -> do |
| 33 | + ident <- openDoc lspSessionInfoFileName LanguageKind_Go |
| 34 | + |
| 35 | + waitUntil 60 $ do |
| 36 | + completions <- getCompletions ident (Position 2 2) |
| 37 | + info [i|Got completions: #{completions}|] |
| 38 | + let insertTexts = fmap (^. label) completions |
| 39 | + insertTexts `listShouldContain` "myVariable" |
| 40 | + insertTexts `listShouldContain` "myFloat" |
| 41 | + |
| 42 | + describe "test.go" $ do |
| 43 | + it "provides fmt. completions" $ doSession' "test.go" lsName fmtCompletionCodeStandalone $ \(Helpers.LspSessionInfo {..}) -> do |
| 44 | + ident <- openDoc lspSessionInfoFileName LanguageKind_Go |
| 45 | + |
| 46 | + waitUntil 60 $ do |
| 47 | + completions <- getCompletions ident (Position 3 8) |
| 48 | + info [i|Got completions: #{completions}|] |
| 49 | + let insertTexts = fmap (^. label) completions |
| 50 | + insertTexts `listShouldContain` "Println" |
| 51 | + insertTexts `listShouldContain` "Printf" |
| 52 | + |
| 53 | + it "provides local variable completions" $ doSession' "test.go" lsName localVarCodeStandalone $ \(Helpers.LspSessionInfo {..}) -> do |
| 54 | + ident <- openDoc lspSessionInfoFileName LanguageKind_Go |
| 55 | + |
| 56 | + waitUntil 60 $ do |
| 57 | + completions <- getCompletions ident (Position 4 6) |
| 58 | + info [i|Got completions: #{completions}|] |
| 59 | + let insertTexts = fmap (^. label) completions |
| 60 | + insertTexts `listShouldContain` "myVariable" |
| 61 | + insertTexts `listShouldContain` "myFloat" |
| 62 | + |
| 63 | +fmtCompletionCodeNotebook :: Text |
| 64 | +fmtCompletionCodeNotebook = [__i|import "fmt" |
| 65 | + fmt.|] |
| 66 | + |
| 67 | +localVarCodeNotebook :: Text |
| 68 | +localVarCodeNotebook = [__i|myVariable := 42 |
| 69 | + myFloat := 3.14 |
| 70 | + my|] |
| 71 | + |
| 72 | +fmtCompletionCodeStandalone :: Text |
| 73 | +fmtCompletionCodeStandalone = [__i|package main |
| 74 | + import "fmt" |
| 75 | + func main() { |
| 76 | + fmt. |
| 77 | + }|] |
| 78 | + |
| 79 | +localVarCodeStandalone :: Text |
| 80 | +localVarCodeStandalone = [__i|package main |
| 81 | + func main() { |
| 82 | + myVariable := 42 |
| 83 | + myFloat := 3.14 |
| 84 | + my |
| 85 | + }|] |
| 86 | + |
| 87 | +listShouldContain :: (MonadIO m, Eq a, Show a) => [a] -> a -> m () |
| 88 | +listShouldContain haystack needle = case L.elem needle haystack of |
| 89 | + True -> return () |
| 90 | + False -> expectationFailure [i|Expected list to contain #{show needle}, but had: #{show haystack}|] |
0 commit comments