Skip to content

Commit 46d60bb

Browse files
peterwicksstringfieldwz1000
authored andcommitted
Update testcase.
Getting a type definition can produce more than one result. E.g. the type of the symbol "pid" in: 1: data Parameter a = Parameter a 2: f :: forall a. Parameter a -> Parameter a 3: f pid = pid is (Parameter a), and the definition of this type is two part: the definition of Parameter on line 1, and the definition of a on line 2.
1 parent f6443b9 commit 46d60bb

File tree

3 files changed

+46
-57
lines changed

3 files changed

+46
-57
lines changed

test/functional/Reference.hs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
module Reference (tests) where
22

3-
import Control.Lens
43
import Control.Monad.IO.Class
5-
import Control.Monad (forM)
6-
import qualified Data.Set as Set
4+
import Data.Tuple.Extra (first3)
75
import Language.Haskell.LSP.Test
86
import Language.Haskell.LSP.Types
9-
import Language.Haskell.LSP.Types.Lens
10-
import System.Directory (canonicalizePath)
117
import System.FilePath ((</>))
128
import System.Time.Extra (sleep)
139
import Test.Hls.Util
@@ -136,10 +132,6 @@ tests = testGroup "references"
136132
]
137133
]
138134

139-
-- | To locate a symbol, we provide a path to the file from the HLS root
140-
-- directory, the line number, and the column number. (0 indexed.)
141-
type SymbolLocation = (FilePath, Int, Int)
142-
143135
-- | When we ask for all references to symbol "foo", should the declaration "foo
144136
-- = 2" be among the references returned?
145137
data IncludeDeclaration =
@@ -153,19 +145,6 @@ getReferences' (file, l, c) includeDeclaration = do
153145
where toBool YesIncludeDeclaration = True
154146
toBool NoExcludeDeclaration = False
155147

156-
expectSameLocations :: [Location] -> [SymbolLocation] -> Assertion
157-
actual `expectSameLocations` expected = do
158-
let actual' =
159-
Set.map (\location -> (location ^. uri
160-
, location ^. range . start . line
161-
, location ^. range . start . character))
162-
$ Set.fromList actual
163-
expected' <- Set.fromList <$>
164-
(forM expected $ \(file, l, c) -> do
165-
fp <- canonicalizePath $ referencesPath </> file
166-
return (filePathToUri fp, l, c))
167-
actual' @?= expected'
168-
169148
referencesPath :: FilePath
170149
referencesPath = "test/testdata/references"
171150

@@ -189,4 +168,4 @@ referenceTest :: SymbolLocation -> IncludeDeclaration -> [SymbolLocation] -> Ass
189168
referenceTest loc includeDeclaration expected =
190169
referenceTestSession $ do
191170
actual <- getReferences' loc includeDeclaration
192-
liftIO $ actual `expectSameLocations` expected
171+
liftIO $ actual `expectSameLocations` map (first3 (referencesPath </>)) expected

test/functional/TypeDefinition.hs

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,47 @@
11
module TypeDefinition (tests) where
22

3-
import Control.Lens ((^.))
43
import Control.Monad.IO.Class
4+
import Data.Tuple.Extra (first3)
55
import Language.Haskell.LSP.Test
66
import Language.Haskell.LSP.Types
7-
import qualified Language.Haskell.LSP.Types.Lens as L
8-
import System.Directory
97
import System.FilePath ((</>))
108
import Test.Hls.Util
119
import Test.Tasty
12-
import Test.Tasty.ExpectedFailure (expectFailBecause)
1310
import Test.Tasty.HUnit
1411

1512
tests :: TestTree
1613
tests = testGroup "type definitions" [
1714
testCase "finds local definition of record variable"
18-
$ getTypeDefinitionTest' (11, 23) 8
15+
$ getTypeDefinitionTest' 10 23 7 0
1916
, testCase "finds local definition of newtype variable"
20-
$ getTypeDefinitionTest' (16, 21) 13
17+
$ getTypeDefinitionTest' 15 21 12 0
2118
, testCase "finds local definition of sum type variable"
22-
$ getTypeDefinitionTest' (21, 13) 18
19+
$ getTypeDefinitionTest' 20 13 17 0
2320
, knownBrokenForGhcVersions [GHC88] "Definition of sum type not found from data constructor in GHC 8.8.x" $
2421
testCase "finds local definition of sum type constructor"
25-
$ getTypeDefinitionTest' (24, 7) 18
22+
$ getTypeDefinitionTest' 23 7 17 0
2623
, testCase "finds non-local definition of type def"
27-
$ getTypeDefinitionTest' (30, 17) 27
24+
$ getTypeDefinitionTest' 29 17 26 0
2825
, testCase "find local definition of type def"
29-
$ getTypeDefinitionTest' (35, 16) 32
26+
$ getTypeDefinitionTest' 34 16 31 0
3027
, testCase "find type-definition of type def in component"
31-
$ getTypeDefinitionTest "src/Lib2.hs" (13, 20) "src/Lib.hs" 8
32-
, expectFailBecause "Why is this failing?" $
33-
testCase "find definition of parameterized data type"
34-
$ getTypeDefinitionTest' (40, 19) 37
28+
$ getTypeDefinitionTest ("src/Lib2.hs", 12, 20) [("src/Lib.hs", 7, 0)]
29+
, testCase "find definition of parameterized data type"
30+
$ getTypeDefinitionTest ("src/Lib.hs", 39, 19) [ ("src/Lib.hs", 36, 0)
31+
, ("src/Lib.hs", 38, 0)]
3532
]
3633

37-
getTypeDefinitionTest :: String -> (Int, Int) -> String -> Int -> Assertion
38-
getTypeDefinitionTest symbolFile symbolPosition definitionFile definitionLine =
39-
failIfSessionTimeout . runSession hlsCommand fullCaps "test/testdata/gototest" $ do
40-
doc <- openDoc symbolFile "haskell"
41-
_ <- openDoc definitionFile "haskell"
42-
defs <- getTypeDefinitions doc $ toPos symbolPosition
43-
fp <- liftIO $ canonicalizePath $ "test/testdata/gototest" </> definitionFile
44-
liftIO $ do
45-
length defs == 1 @? "Expecting a list containing one location, but got: " ++ show defs
46-
let [def] = defs
47-
def ^. L.uri @?= filePathToUri fp
48-
def ^. L.range . L.start . L.line @?= definitionLine - 1
49-
def ^. L.range . L.end . L.line @?= definitionLine - 1
34+
definitionsPath :: FilePath
35+
definitionsPath = "test/testdata/gototest"
5036

51-
getTypeDefinitionTest' :: (Int, Int) -> Int -> Assertion
52-
getTypeDefinitionTest' symbolPosition definitionLine =
53-
getTypeDefinitionTest "src/Lib.hs" symbolPosition "src/Lib.hs" definitionLine
37+
getTypeDefinitionTest :: SymbolLocation -> [SymbolLocation] -> Assertion
38+
getTypeDefinitionTest (symbolFile, symbolLine, symbolCol) definitionLocations =
39+
failIfSessionTimeout . runSession hlsCommand fullCaps definitionsPath $ do
40+
doc <- openDoc symbolFile "haskell"
41+
defs <- getTypeDefinitions doc $ Position symbolLine symbolCol
42+
liftIO $ defs `expectSameLocations` map (first3 (definitionsPath </>)) definitionLocations
5443

55-
--NOTE: copied from Haskell.Ide.Engine.ArtifactMap
56-
toPos :: (Int,Int) -> Position
57-
toPos (l,c) = Position (l-1) (c-1)
44+
getTypeDefinitionTest' :: Int -> Int -> Int -> Int -> Assertion
45+
getTypeDefinitionTest' symbolLine symbolCol definitionLine definitionCol =
46+
getTypeDefinitionTest ("src/Lib.hs", symbolLine, symbolCol)
47+
[("src/Lib.hs", definitionLine, definitionCol)]

test/utils/Test/Hls/Util.hs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Test.Hls.Util
66
, expectCodeAction
77
, expectDiagnostic
88
, expectNoMoreDiagnostics
9+
, expectSameLocations
910
, failIfSessionTimeout
1011
, flushStackEnvironment
1112
, fromAction
@@ -22,6 +23,7 @@ module Test.Hls.Util
2223
, knownBrokenForGhcVersions
2324
, logFilePath
2425
, setupBuildToolFiles
26+
, SymbolLocation
2527
, waitForDiagnosticsFrom
2628
, waitForDiagnosticsFromSource
2729
, waitForDiagnosticsFromSourceWithTimeout
@@ -38,6 +40,7 @@ import Data.Default
3840
import Data.List (intercalate)
3941
import Data.List.Extra (find)
4042
import Data.Maybe
43+
import qualified Data.Set as Set
4144
import qualified Data.Text as T
4245
import Language.Haskell.LSP.Core
4346
import Language.Haskell.LSP.Messages (FromServerMessage(NotLogMessage))
@@ -55,7 +58,7 @@ import Test.Hspec.Runner
5558
import Test.Hspec.Core.Formatters hiding (Seconds)
5659
import Test.Tasty (TestTree)
5760
import Test.Tasty.ExpectedFailure (ignoreTestBecause, expectFailBecause)
58-
import Test.Tasty.HUnit (assertFailure)
61+
import Test.Tasty.HUnit (Assertion, assertFailure, (@?=))
5962
import Text.Blaze.Renderer.String (renderMarkup)
6063
import Text.Blaze.Internal hiding (null)
6164

@@ -397,3 +400,20 @@ failIfSessionTimeout action = action `catch` errorHandler
397400
where errorHandler :: Test.SessionException -> IO a
398401
errorHandler e@(Test.Timeout _) = assertFailure $ show e
399402
errorHandler e = throwIO e
403+
404+
-- | To locate a symbol, we provide a path to the file from the HLS root
405+
-- directory, the line number, and the column number. (0 indexed.)
406+
type SymbolLocation = (FilePath, Int, Int)
407+
408+
expectSameLocations :: [Location] -> [SymbolLocation] -> Assertion
409+
actual `expectSameLocations` expected = do
410+
let actual' =
411+
Set.map (\location -> (location ^. L.uri
412+
, location ^. L.range . L.start . L.line
413+
, location ^. L.range . L.start . L.character))
414+
$ Set.fromList actual
415+
expected' <- Set.fromList <$>
416+
(forM expected $ \(file, l, c) -> do
417+
fp <- canonicalizePath file
418+
return (filePathToUri fp, l, c))
419+
actual' @?= expected'

0 commit comments

Comments
 (0)