Skip to content

Commit d067009

Browse files
Refactor getTypeDefinition tests.
1 parent 5fc4966 commit d067009

File tree

1 file changed

+29
-85
lines changed

1 file changed

+29
-85
lines changed

test/functional/TypeDefinition.hs

+29-85
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,55 @@
11
module TypeDefinition (tests) where
22

3+
import Control.Lens ((^.))
34
import Control.Monad.IO.Class
45
import Language.Haskell.LSP.Test
56
import Language.Haskell.LSP.Types
7+
import qualified Language.Haskell.LSP.Types.Lens as L
68
import System.Directory
9+
import System.FilePath ((</>))
710
import Test.Hls.Util
811
import Test.Tasty
912
import Test.Tasty.HUnit
10-
import Test.Tasty.ExpectedFailure (expectFailBecause)
1113

1214
tests :: TestTree
1315
tests = testGroup "type definitions" [
1416
testCase "finds local definition of record variable"
15-
$ runSession hlsCommand fullCaps "test/testdata/gototest"
16-
$ do
17-
doc <- openDoc "src/Lib.hs" "haskell"
18-
defs <- getTypeDefinitions doc (toPos (11, 23))
19-
liftIO $ do
20-
fp <- canonicalizePath "test/testdata/gototest/src/Lib.hs"
21-
defs @?= [ Location (filePathToUri fp)
22-
(Range (toPos (8, 1)) (toPos (8, 29)))
23-
]
24-
17+
$ getTypeDefinitionTest' (11, 23) 8
2518
, testCase "finds local definition of newtype variable"
26-
$ runSession hlsCommand fullCaps "test/testdata/gototest"
27-
$ do
28-
doc <- openDoc "src/Lib.hs" "haskell"
29-
defs <- getTypeDefinitions doc (toPos (16, 21))
30-
liftIO $ do
31-
fp <- canonicalizePath "test/testdata/gototest/src/Lib.hs"
32-
defs @?= [ Location (filePathToUri fp)
33-
(Range (toPos (13, 1)) (toPos (13, 30)))
34-
]
35-
19+
$ getTypeDefinitionTest' (16, 21) 13
3620
, testCase "finds local definition of sum type variable"
37-
$ runSession hlsCommand fullCaps "test/testdata/gototest"
38-
$ do
39-
doc <- openDoc "src/Lib.hs" "haskell"
40-
defs <- getTypeDefinitions doc (toPos (21, 13))
41-
liftIO $ do
42-
fp <- canonicalizePath "test/testdata/gototest/src/Lib.hs"
43-
defs @?= [ Location (filePathToUri fp)
44-
(Range (toPos (18, 1)) (toPos (18, 26)))
45-
]
46-
21+
$ getTypeDefinitionTest' (21, 13) 18
4722
, testCase "finds local definition of sum type constructor"
48-
$ runSession hlsCommand fullCaps "test/testdata/gototest"
49-
$ do
50-
doc <- openDoc "src/Lib.hs" "haskell"
51-
defs <- getTypeDefinitions doc (toPos (24, 7))
52-
liftIO $ do
53-
fp <- canonicalizePath "test/testdata/gototest/src/Lib.hs"
54-
defs
55-
@?= [ Location (filePathToUri fp)
56-
(Range (toPos (18, 1)) (toPos (18, 26)))
57-
]
58-
23+
$ getTypeDefinitionTest' (24, 7) 18
5924
, testCase "finds non-local definition of type def"
60-
$ runSession hlsCommand fullCaps "test/testdata/gototest"
61-
$ do
62-
doc <- openDoc "src/Lib.hs" "haskell"
63-
defs <- getTypeDefinitions doc (toPos (30, 17))
64-
liftIO $ do
65-
fp <- canonicalizePath "test/testdata/gototest/src/Lib.hs"
66-
defs
67-
@?= [ Location (filePathToUri fp)
68-
(Range (toPos (27, 1)) (toPos (27, 17)))
69-
]
70-
25+
$ getTypeDefinitionTest' (30, 17) 27
7126
, testCase "find local definition of type def"
72-
$ runSession hlsCommand fullCaps "test/testdata/gototest"
73-
$ do
74-
doc <- openDoc "src/Lib.hs" "haskell"
75-
defs <- getTypeDefinitions doc (toPos (35, 16))
76-
liftIO $ do
77-
fp <- canonicalizePath "test/testdata/gototest/src/Lib.hs"
78-
defs @?= [ Location (filePathToUri fp)
79-
(Range (toPos (32, 1)) (toPos (32, 18)))
80-
]
81-
27+
$ getTypeDefinitionTest' (35, 16) 32
8228
, expectFailBecause "This test is broken because it needs a proper cradle." $
8329
testCase "find type-definition of type def in component"
84-
$ runSession hlsCommand fullCaps "test/testdata/gototest"
85-
$ do
86-
doc <- openDoc "src/Lib2.hs" "haskell"
87-
otherDoc <- openDoc "src/Lib.hs" "haskell"
88-
closeDoc otherDoc
89-
defs <- getTypeDefinitions doc (toPos (13, 20))
90-
liftIO $ do
91-
fp <- canonicalizePath "test/testdata/gototest/src/Lib.hs"
92-
defs
93-
@?= [ Location (filePathToUri fp)
94-
(Range (toPos (8, 1)) (toPos (8, 29)))
95-
]
96-
30+
$ getTypeDefinitionTest "src/Lib2.hs" (13, 20) "src/Lib.hs" 8
9731
, testCase "find definition of parameterized data type"
98-
$ runSession hlsCommand fullCaps "test/testdata/gototest"
99-
$ do
100-
doc <- openDoc "src/Lib.hs" "haskell"
101-
defs <- getTypeDefinitions doc (toPos (40, 19))
102-
liftIO $ do
103-
fp <- canonicalizePath "test/testdata/gototest/src/Lib.hs"
104-
defs @?= [ Location (filePathToUri fp)
105-
(Range (toPos (37, 1)) (toPos (37, 31)))
106-
]
32+
$ getTypeDefinitionTest' (40, 19) 37
10733
]
10834

35+
getTypeDefinitionTest :: String -> (Int, Int) -> String -> Int -> Assertion
36+
getTypeDefinitionTest symbolFile symbolPosition definitionFile definitionLine =
37+
failIfSessionTimeout . runSession hlsCommand fullCaps "test/testdata/gototest" $ do
38+
doc <- openDoc symbolFile "haskell"
39+
_ <- openDoc definitionFile "haskell"
40+
defs <- getTypeDefinitions doc $ toPos symbolPosition
41+
fp <- liftIO $ canonicalizePath $ "test/testdata/gototest" </> definitionFile
42+
liftIO $ do
43+
length defs == 1 @? "Expecting a list containing one location, but got: " ++ show defs
44+
let [def] = defs
45+
def ^. L.uri @?= filePathToUri fp
46+
def ^. L.range . L.start . L.line @?= definitionLine - 1
47+
def ^. L.range . L.end . L.line @?= definitionLine - 1
48+
49+
getTypeDefinitionTest' :: (Int, Int) -> Int -> Assertion
50+
getTypeDefinitionTest' symbolPosition definitionLine =
51+
getTypeDefinitionTest "src/Lib.hs" symbolPosition "src/Lib.hs" definitionLine
52+
10953
--NOTE: copied from Haskell.Ide.Engine.ArtifactMap
11054
toPos :: (Int,Int) -> Position
11155
toPos (l,c) = Position (l-1) (c-1)

0 commit comments

Comments
 (0)