Skip to content

Commit 6daf799

Browse files
committed
Add goto dependency definition test
1 parent c532025 commit 6daf799

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed

ghcide/ghcide.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ test-suite ghcide-tests
418418
ReferenceTests
419419
GarbageCollectionTests
420420
OpenCloseTest
421+
Dependency
421422
default-extensions:
422423
BangPatterns
423424
DeriveFunctor
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Dependency where
2+
3+
import Control.Concurrent.Async (AsyncCancelled (..))
4+
5+
asyncCancelled :: AsyncCancelled
6+
asyncCancelled = AsyncCancelled
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: dependency
2+
version: 0.1.0.0
3+
cabal-version: 2.0
4+
build-type: Simple
5+
6+
library
7+
exposed-modules: Dependency
8+
default-language: Haskell2010
9+
build-depends: base
10+
, async == 2.2.4
11+
ghc-options: -fwrite-ide-info

ghcide/test/data/dependency/hie.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cradle:
2+
cabal:

ghcide/test/exe/Dependency.hs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE ExplicitNamespaces #-}
3+
{-# LANGUAGE GADTs #-}
4+
module Dependency where
5+
6+
import qualified Control.Applicative as Applicative
7+
import Control.Applicative.Combinators (skipManyTill)
8+
import Control.Monad.IO.Class (liftIO)
9+
import qualified Data.Aeson as A
10+
import Data.Bool (bool)
11+
import Data.List (isSuffixOf)
12+
import Data.Proxy (Proxy (..))
13+
import Language.LSP.Protocol.Message (TCustomMessage (NotMess),
14+
TNotificationMessage (..))
15+
import Language.LSP.Protocol.Types (Definition (..),
16+
Location (..), Position (..),
17+
Range (..),
18+
type (|?) (InL, InR),
19+
uriToFilePath)
20+
import Language.LSP.Test (Session, anyMessage,
21+
customNotification,
22+
getDefinitions, openDoc)
23+
import System.FilePath (splitDirectories, (<.>),
24+
(</>))
25+
import Test.Tasty (TestTree, testGroup)
26+
import Test.Tasty.HUnit (assertBool, assertFailure,
27+
(@?=))
28+
import TestUtils (testSessionWithExtraFiles)
29+
30+
tests :: TestTree
31+
tests =
32+
testGroup "gotoDefinition for dependencies"
33+
[ dependencyTest
34+
]
35+
where
36+
dependencyTest :: TestTree
37+
dependencyTest = testSessionWithExtraFiles "dependency" "gotoDefinition in async" $
38+
\dir -> do
39+
doc <- openDoc (dir </> "Dependency" <.> "hs") "haskell"
40+
_hieFile <- fileDoneIndexing ["Control", "Concurrent", "Async.hie"]
41+
defs <- getDefinitions doc (Position 5 20)
42+
let expRange = Range (Position 430 22) (Position 430 36)
43+
case defs of
44+
InL (Definition (InR [Location fp actualRange])) ->
45+
liftIO $ do
46+
let locationDirectories :: [String]
47+
locationDirectories =
48+
maybe [] splitDirectories $
49+
uriToFilePath fp
50+
assertBool "AsyncCancelled found in a module that is not Control.Concurrent Async"
51+
$ ["Control", "Concurrent", "Async.hs"]
52+
`isSuffixOf` locationDirectories
53+
actualRange @?= expRange
54+
wrongLocation ->
55+
liftIO $
56+
assertFailure $ "Wrong location for AsyncCancelled: "
57+
++ show wrongLocation
58+
fileDoneIndexing :: [String] -> Session FilePath
59+
fileDoneIndexing fpSuffix =
60+
skipManyTill anyMessage indexedFile
61+
where
62+
indexedFile :: Session FilePath
63+
indexedFile = do
64+
NotMess TNotificationMessage{_params} <-
65+
customNotification (Proxy @"ghcide/reference/ready")
66+
case A.fromJSON _params of
67+
A.Success fp -> do
68+
let fpDirs :: [String]
69+
fpDirs = splitDirectories fp
70+
bool Applicative.empty (pure fp) $
71+
fpSuffix `isSuffixOf` fpDirs
72+
other -> error $ "Failed to parse ghcide/reference/ready file: " <> show other

ghcide/test/exe/Main.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import ClientSettingsTests
7676
import ReferenceTests
7777
import GarbageCollectionTests
7878
import ExceptionTests
79+
import Dependency
7980

8081
main :: IO ()
8182
main = do
@@ -124,4 +125,5 @@ main = do
124125
, GarbageCollectionTests.tests
125126
, HieDbRetry.tests
126127
, ExceptionTests.tests recorder logger
128+
, Dependency.tests
127129
]

0 commit comments

Comments
 (0)