|
1 | 1 | module TypeDefinition (tests) where
|
2 | 2 |
|
| 3 | +import Control.Lens ((^.)) |
3 | 4 | import Control.Monad.IO.Class
|
4 | 5 | import Language.Haskell.LSP.Test
|
5 | 6 | import Language.Haskell.LSP.Types
|
| 7 | +import qualified Language.Haskell.LSP.Types.Lens as L |
6 | 8 | import System.Directory
|
| 9 | +import System.FilePath ((</>)) |
7 | 10 | import Test.Hls.Util
|
8 | 11 | import Test.Tasty
|
9 | 12 | import Test.Tasty.HUnit
|
10 |
| -import Test.Tasty.ExpectedFailure (expectFailBecause) |
11 | 13 |
|
12 | 14 | tests :: TestTree
|
13 | 15 | tests = testGroup "type definitions" [
|
14 | 16 | 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 |
25 | 18 | , 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 |
36 | 20 | , 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 |
47 | 22 | , 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 |
59 | 24 | , 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 |
71 | 26 | , 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 |
82 | 28 | , expectFailBecause "This test is broken because it needs a proper cradle." $
|
83 | 29 | 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 |
97 | 31 | , 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 |
107 | 33 | ]
|
108 | 34 |
|
| 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 | + |
109 | 53 | --NOTE: copied from Haskell.Ide.Engine.ArtifactMap
|
110 | 54 | toPos :: (Int,Int) -> Position
|
111 | 55 | toPos (l,c) = Position (l-1) (c-1)
|
0 commit comments