Skip to content

Commit c9626da

Browse files
committed
Add tests for top-level context snippets
1 parent afcd5c7 commit c9626da

1 file changed

Lines changed: 40 additions & 17 deletions

File tree

ghcide-test/exe/CompletionTests.hs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ testSessionEmpty name = testWithDummyPlugin name (mkIdeTestFs [FS.directCradle [
5353
testSessionEmptyWithCradle :: TestName -> T.Text -> Session () -> TestTree
5454
testSessionEmptyWithCradle name cradle = testWithDummyPlugin name (mkIdeTestFs [file "hie.yaml" (text cradle)])
5555

56-
testSessionSingleFile :: TestName -> FilePath -> T.Text -> Session () -> TestTree
57-
testSessionSingleFile testName fp txt session =
58-
testWithDummyPlugin testName (mkIdeTestFs [FS.directCradle [T.pack fp] , file fp (text txt)]) session
56+
testSessionSingleFile :: TestName -> FilePath -> [T.Text] -> Session () -> TestTree
57+
testSessionSingleFile testName fp txts session =
58+
testWithDummyPlugin testName (mkIdeTestFs [FS.directCradle [T.pack fp] , file fp (text (T.unlines txts))]) session
5959

6060
completionTest :: HasCallStack => String -> [T.Text] -> Position -> [(T.Text, CompletionItemKind, T.Text, Bool, Bool, Maybe [TextEdit])] -> TestTree
61-
completionTest name src pos expected = testSessionSingleFile name "A.hs" (T.unlines src) $ do
61+
completionTest name src pos expected = testSessionSingleFile name "A.hs" src $ do
6262
docId <- openDoc "A.hs" "haskell"
6363
_ <- waitForDiagnostics
6464

@@ -518,15 +518,25 @@ projectCompletionTests =
518518
contextCompletionTests :: [TestTree]
519519
contextCompletionTests =
520520
[ testSessionSingleFile "import snippets at top level" "A.hs"
521-
(T.unlines ["module A where", "imp"]) $ do
521+
[ "module A where"
522+
, "imp"
523+
] $ do
522524
doc <- openDoc "A.hs" "haskell"
523525
_ <- waitForDiagnostics
524526
compls <- getCompletions doc (Position 1 3)
525527
let importSnippets = [ c | c@CompletionItem{..} <- compls
526-
, _kind == Just CompletionItemKind_Snippet
527-
, _label == "import" ]
528+
, _kind == Just CompletionItemKind_Snippet
529+
, _label == "import" ]
528530
liftIO $ length importSnippets @?= 4
529531

532+
, completionTest "no import snippet past a declaration"
533+
[ "module A where"
534+
, "foo = ()"
535+
, "imp"
536+
]
537+
(Position 2 3)
538+
[]
539+
530540
, completionTest
531541
"function snippet at top level"
532542
[ "module A where"
@@ -538,6 +548,14 @@ contextCompletionTests =
538548
"${1:identifier} :: ${2:type}\n${1:identifier} = ${3:body}",
539549
False, False, Nothing)]
540550

551+
, completionTest "no function snippet past an import"
552+
[ "module A where"
553+
, "fun"
554+
, "import Control.Monad hiding (join)"
555+
]
556+
(Position 2 3)
557+
[]
558+
541559
, completionTest
542560
"class snippet at top level"
543561
[ "module A where"
@@ -556,7 +574,9 @@ contextCompletionTests =
556574
False, False, Nothing)]
557575

558576
, testSessionSingleFile "no snippets in value binding" "A.hs"
559-
(T.unlines ["module A where", "foo = imp"]) $ do
577+
[ "module A where"
578+
, "foo = imp"
579+
] $ do
560580
doc <- openDoc "A.hs" "haskell"
561581
_ <- waitForDiagnostics
562582
compls <- getCompletions doc (Position 1 9)
@@ -566,13 +586,12 @@ contextCompletionTests =
566586
liftIO $ snippets @?= []
567587

568588
, testSessionSingleFile "no snippets in instance body" "A.hs"
569-
(T.unlines
570-
[ "module A where"
571-
, "class Foo a where"
572-
, " bar :: a -> ()"
573-
, "instance Foo Int where"
574-
, " bar _ = imp"
575-
]) $ do
589+
[ "module A where"
590+
, "class Foo a where"
591+
, " bar :: a -> ()"
592+
, "instance Foo Int where"
593+
, " bar _ = imp"
594+
] $ do
576595
doc <- openDoc "A.hs" "haskell"
577596
_ <- waitForDiagnostics
578597
compls <- getCompletions doc (Position 4 13)
@@ -582,15 +601,19 @@ contextCompletionTests =
582601
liftIO $ snippets @?= []
583602

584603
, testSessionSingleFile "top level excludes regular completions" "A.hs"
585-
(T.unlines ["module A where", "hea"]) $ do
604+
[ "module A where"
605+
, "hea"
606+
] $ do
586607
doc <- openDoc "A.hs" "haskell"
587608
_ <- waitForDiagnostics
588609
compls <- getCompletions doc (Position 1 3)
589610
let headCompls = [ c | c@CompletionItem{..} <- compls, _label == "head" ]
590611
liftIO $ headCompls @?= []
591612

592613
, testSessionSingleFile "unmatched prefix at top level returns empty" "A.hs"
593-
(T.unlines ["module A where", "xyz"]) $ do
614+
[ "module A where"
615+
, "xyz"
616+
] $ do
594617
doc <- openDoc "A.hs" "haskell"
595618
_ <- waitForDiagnostics
596619
compls <- getCompletions doc (Position 1 3)

0 commit comments

Comments
 (0)