Skip to content

Commit d0d0f94

Browse files
add tests for cabal goto-definition
1 parent 26dcc40 commit d0d0f94

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

Diff for: plugins/hls-cabal-plugin/test/Main.hs

+55
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import qualified Data.Text as Text
2020
import Ide.Plugin.Cabal.LicenseSuggest (licenseErrorSuggestion)
2121
import qualified Ide.Plugin.Cabal.Parse as Lib
2222
import qualified Language.LSP.Protocol.Lens as L
23+
import qualified Language.LSP.Protocol.Types as LSP
2324
import Outline (outlineTests)
2425
import System.FilePath
2526
import Test.Hls
@@ -36,6 +37,7 @@ main = do
3637
, contextTests
3738
, outlineTests
3839
, codeActionTests
40+
, gotoDefinitionTests
3941
]
4042

4143
-- ------------------------------------------------------------------------
@@ -227,3 +229,56 @@ codeActionTests = testGroup "Code Actions"
227229
InR action@CodeAction{_title} <- codeActions
228230
guard (_title == "Replace with " <> license)
229231
pure action
232+
233+
-- ----------------------------------------------------------------------------
234+
-- Goto Definition Tests
235+
-- ----------------------------------------------------------------------------
236+
237+
gotoDefinitionTests :: TestTree
238+
gotoDefinitionTests = testGroup "Goto Definition"
239+
[ positiveTest "middle of identifier" (mkP 27 16) (mkR 6 0 7 22)
240+
, positiveTest "left of identifier" (mkP 30 12) (mkR 10 0 17 40)
241+
, positiveTest "right of identifier" (mkP 33 22) (mkR 20 0 23 34)
242+
, positiveTest "left of '-' in identifier" (mkP 36 20) (mkR 6 0 7 22)
243+
, positiveTest "right of '-' in identifier" (mkP 39 19) (mkR 10 0 17 40)
244+
, positiveTest "identifier in identifier list" (mkP 42 16) (mkR 20 0 23 34)
245+
, positiveTest "left of ',' right of identifier" (mkP 45 33) (mkR 10 0 17 40)
246+
, positiveTest "right of ',' left of identifier" (mkP 48 34) (mkR 6 0 7 22)
247+
248+
, negativeTest "right of ',' left of space" (mkP 51 23)
249+
, negativeTest "right of ':' left of space" (mkP 54 11)
250+
, negativeTest "not a definition" (mkP 57 8)
251+
, negativeTest "empty space" (mkP 59 7)
252+
]
253+
where
254+
mkP :: UInt -> UInt -> Position
255+
mkP x1 y1 = Position x1 y1
256+
257+
mkR :: UInt -> UInt -> UInt -> UInt -> Range
258+
mkR x1 y1 x2 y2 = Range (mkP x1 y1) (mkP x2 y2)
259+
260+
getDefinition :: Show b => (Definition |? b) -> Range
261+
getDefinition (InL (Definition (InL loc))) = loc^.L.range
262+
getDefinition unk = error $ "Unexpected pattern '" ++ show unk ++ "' , expected '(InL (Definition (InL loc))'"
263+
264+
-- A positive test checks if the provided range is equal
265+
-- to the expected range from the definition in the test file.
266+
-- The test emulates a goto-definition request of an actual definition.
267+
positiveTest :: TestName -> Position -> Range -> TestTree
268+
positiveTest testName cursorPos expectedRange =
269+
runCabalTestCaseSession testName "goto-definition" $ do
270+
doc <- openDoc "simple-with-common.cabal" "cabal"
271+
definitions <- getDefinitions doc cursorPos
272+
let locationRange = getDefinition definitions
273+
liftIO $ locationRange @?= expectedRange
274+
275+
-- A negative test checks if the request failed and
276+
-- the provided result is empty, i.e. `InR $ InR Null`.
277+
-- The test emulates a goto-definition request of anything but an
278+
-- actual definition.
279+
negativeTest :: TestName -> Position -> TestTree
280+
negativeTest testName cursorPos =
281+
runCabalTestCaseSession testName "goto-definition" $ do
282+
doc <- openDoc "simple-with-common.cabal" "cabal"
283+
empty <- getDefinitions doc cursorPos
284+
liftIO $ empty @?= (InR $ InR LSP.Null)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
cabal-version: 3.0
2+
name: simple-cabal
3+
version: 0.1.0.0
4+
license: MIT
5+
6+
-- Range : (6, 0) - (7, 22)
7+
common warnings-0
8+
ghc-options: -Wall
9+
10+
-- Range : (10, 0) - (17, 40)
11+
common warnings-1
12+
ghc-options: -Wall
13+
-Wredundant-constraints
14+
-Wunused-packages
15+
16+
-Wno-name-shadowing
17+
18+
-Wno-unticked-promoted-constructors
19+
20+
-- Range : (20, 0) - (23, 34)
21+
common warnings-2
22+
ghc-options: -Wall
23+
-Wredundant-constraints
24+
-Wunused-packages
25+
26+
library
27+
28+
import: warnings-0
29+
-- ^ Position: (27, 16), middle of identifier
30+
31+
import: warnings-1
32+
-- ^ Position: (30, 12), left of identifier
33+
34+
import: warnings-2
35+
-- ^ Position: (33, 22), right of identifier
36+
37+
import: warnings-0
38+
-- ^ Position: (36, 20), left of '-' in identifier
39+
40+
import: warnings-1
41+
-- ^ Position: (39, 19), right of "-" in identifier
42+
43+
import: warnings-2,warnings-1,warnings-0
44+
-- ^ Position: (42, 16), identifier in identifier list
45+
46+
import: warnings-2,warnings-1,warnings-0
47+
-- ^ Position: (45, 33), left of ',' right of identifier
48+
49+
import: warnings-2,warnings-1,warnings-0
50+
-- ^ Position: (48, 34), right of ',' left of identifier
51+
52+
import: warnings-2, warnings-1,warnings-0
53+
-- ^ Position: (51, 37), right of ',' left of space
54+
55+
import: warnings-0
56+
-- ^ Position: (54, 11), right of ':' left of space
57+
58+
import: warnings-0
59+
-- ^ Position: (57, 8), not a definition
60+
61+
-- EOL
62+
-- ^ Position: (59, 7), empty space

0 commit comments

Comments
 (0)