Skip to content

Commit 228d669

Browse files
committed
add ghcide InlayHintTests
1 parent 287f7fa commit 228d669

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

ghcide/test/exe/InlayHintTests.hs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
module InlayHintTests (tests) where
2+
3+
import Config (mkIdeTestFs, testWithDummyPlugin,
4+
testWithDummyPluginEmpty)
5+
import Control.Monad (void)
6+
import Control.Monad.IO.Class (MonadIO (liftIO))
7+
import qualified Data.Aeson as A
8+
import Data.Maybe (mapMaybe)
9+
import qualified Data.Text as T
10+
import Language.LSP.Protocol.Types (InlayHint (_textEdits),
11+
Position (Position),
12+
Range (Range, _end, _start),
13+
TextDocumentIdentifier (TextDocumentIdentifier),
14+
VersionedTextDocumentIdentifier (_uri))
15+
import Language.LSP.Test (applyEdit, createDoc,
16+
documentContents, getInlayHints,
17+
openDoc, setConfigSection)
18+
import Test.Hls (Session, expectFail,
19+
waitForTypecheck)
20+
import Test.Hls.FileSystem (copyDir)
21+
import Test.Tasty (TestTree, testGroup)
22+
import Test.Tasty.HUnit ((@?=))
23+
24+
tests :: TestTree
25+
tests = testGroup "inlay hints"
26+
[ whereInlayHintsTests
27+
]
28+
29+
whereInlayHintsTests :: TestTree
30+
whereInlayHintsTests = testGroup "add signature for where clauses"
31+
[ testWithDummyPluginEmpty "No where inlay hints if disabled" $ do
32+
let content = T.unlines
33+
[ "module Sigs where"
34+
, "f :: b"
35+
, "f = undefined"
36+
, " where"
37+
, " g = True"
38+
]
39+
range = Range { _start = Position 4 0
40+
, _end = Position 4 1000
41+
}
42+
doc <- createDoc "Sigs.hs" "haskell" content
43+
setConfigSection "haskell" (createConfig False)
44+
inlayHints <- getInlayHints doc range
45+
liftIO $ length inlayHints @?= 0
46+
, editTest "Simple" "Simple"
47+
, editTest "Tuple" "Tuple"
48+
, editTest "Inline" "Inline"
49+
, editTest "Infix" "Infix"
50+
, editTest "Operator" "Operator"
51+
, expectFail $ editTest "ScopedTypeVariables" "ScopedTypeVariables"
52+
, editTest "Nest" "Nest"
53+
, editTest "No lens" "NoLens"
54+
, expectFail $ editTest "Typeclass" "Typeclass"
55+
, editTest "Quqlified" "Qualified"
56+
]
57+
where
58+
createConfig on =
59+
A.object [ "plugin"
60+
A..= A.object [ "ghcide-type-lenses"
61+
A..= A.object [ "config"
62+
A..= A.object [ "whereInlayHintOn" A..= A.Bool on ]]]]
63+
64+
editTest title file =
65+
testWithDummyPlugin title (mkIdeTestFs [copyDir "local-sig-lens"]) $ do
66+
doc <- openDoc (file ++ ".hs") "haskell"
67+
executeAllHints doc globalRange
68+
real <- documentContents doc
69+
expectedDoc <- openDoc (file ++ ".expected.hs") "haskell"
70+
expected <- documentContents expectedDoc
71+
liftIO $ real @?= expected
72+
73+
executeAllHints :: TextDocumentIdentifier -> Range -> Session ()
74+
executeAllHints doc range = do
75+
void $ waitForTypecheck doc
76+
hints <- getInlayHints doc range
77+
let edits = concat $ mapMaybe _textEdits hints
78+
case edits of
79+
[] -> pure ()
80+
edit : _ -> do
81+
newDoc <- applyEdit doc edit
82+
-- pure ()
83+
executeAllHints (TextDocumentIdentifier $ _uri newDoc) range
84+
85+
globalRange :: Range
86+
globalRange = Range { _start = Position 0 0
87+
, _end = Position 1000 0
88+
}

ghcide/test/exe/Main.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import HaddockTests
5050
import HighlightTests
5151
import IfaceTests
5252
import InitializeResponseTests
53+
import InlayHintTests
5354
import LogType ()
5455
import NonLspCommandLine
5556
import OpenCloseTest
@@ -99,4 +100,5 @@ main = do
99100
, GarbageCollectionTests.tests
100101
, HieDbRetry.tests
101102
, ExceptionTests.tests
103+
, InlayHintTests.tests
102104
]

haskell-language-server.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,6 +2180,7 @@ test-suite ghcide-tests
21802180
THTests
21812181
UnitTests
21822182
WatchedFileTests
2183+
InlayHintTests
21832184

21842185
-- Tests that have been pulled out of the main file
21852186
default-extensions:

0 commit comments

Comments
 (0)