Skip to content

Commit 5f84e27

Browse files
committed
Tests for both resolve and non resolve variants
1 parent bcfb899 commit 5f84e27

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

plugins/hls-overloaded-record-dot-plugin/hls-overloaded-record-dot-plugin.cabal

+3
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ test-suite tests
5959
build-depends:
6060
, base
6161
, filepath
62+
, ghcide
6263
, text
6364
, hls-overloaded-record-dot-plugin
65+
, lens
6466
, lsp-test
67+
, lsp-types
6568
, hls-test-utils
6669

plugins/hls-overloaded-record-dot-plugin/src/Ide/Plugin/OverloadedRecordDot.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import qualified Data.Map as Map
2727
import Data.Maybe (fromJust, mapMaybe,
2828
maybeToList)
2929
import Data.Text (Text)
30-
import Data.Unique
30+
import Data.Unique (hashUnique, newUnique)
3131
import Development.IDE (IdeState,
3232
NormalizedFilePath,
3333
NormalizedUri,

plugins/hls-overloaded-record-dot-plugin/test/Main.hs

+69-7
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,64 @@
55

66
module Main ( main ) where
77

8+
import Control.Lens (_Just, set, (^.))
89
import Data.Either (rights)
10+
import Data.Functor (void)
911
import qualified Data.Text as T
12+
import qualified Data.Text.Lazy as TL
13+
import qualified Data.Text.Lazy.Encoding as TL
14+
import Development.IDE.Types.Logger (Doc, Logger (Logger),
15+
Pretty (pretty),
16+
Priority (Debug),
17+
Recorder (Recorder, logger_),
18+
WithPriority (WithPriority, priority),
19+
cfilter, cmapWithPrio,
20+
makeDefaultStderrRecorder)
1021
import qualified Ide.Plugin.OverloadedRecordDot as OverloadedRecordDot
11-
import System.FilePath ((</>))
22+
import Language.LSP.Protocol.Lens as L
23+
import System.FilePath ((<.>), (</>))
1224
import Test.Hls
1325

14-
1526
main :: IO ()
16-
main = defaultTestRunner test
27+
main =
28+
defaultTestRunner test
1729

1830
plugin :: PluginTestDescriptor OverloadedRecordDot.Log
1931
plugin = mkPluginTestDescriptor OverloadedRecordDot.descriptor "overloaded-record-dot"
2032

2133
test :: TestTree
22-
test = testGroup "overloaded-record-dot"
23-
[ mkTest "Simple" "Simple" "name" 10 7 10 15,
34+
test = testGroup "overloaded-record-dot" $
35+
[testGroup "without resolve" [ mkTest "Simple" "Simple" "name" 10 7 10 15,
2436
mkTest "NoPragmaNeeded" "NoPragmaNeeded" "name" 11 7 11 15,
2537
mkTest "NestedParens" "NestedParens" "name" 15 7 15 24,
2638
mkTest "NestedDot" "NestedDot" "name" 17 7 17 22,
2739
mkTest "NestedDollar" "NestedDollar" "name" 15 7 15 24,
2840
mkTest "MultilineCase" "MultilineCase" "name" 10 7 12 15,
2941
mkTest "Multiline" "Multiline" "name" 10 7 11 15,
3042
mkTest "MultilineExpanded" "MultilineExpanded" "owner" 28 8 28 19
31-
]
43+
],
44+
testGroup "with Resolve" [ mkResolveTest "Simple" "Simple" "name" 10 7 10 15,
45+
mkResolveTest "NoPragmaNeeded" "NoPragmaNeeded" "name" 11 7 11 15,
46+
mkResolveTest "NestedParens" "NestedParens" "name" 15 7 15 24,
47+
mkResolveTest "NestedDot" "NestedDot" "name" 17 7 17 22,
48+
mkResolveTest "NestedDollar" "NestedDollar" "name" 15 7 15 24,
49+
mkResolveTest "MultilineCase" "MultilineCase" "name" 10 7 12 15,
50+
mkResolveTest "Multiline" "Multiline" "name" 10 7 11 15,
51+
mkResolveTest "MultilineExpanded" "MultilineExpanded" "owner" 28 8 28 19
52+
]]
3253

3354
mkTest :: TestName -> FilePath -> T.Text -> UInt -> UInt -> UInt -> UInt -> TestTree
3455
mkTest title fp selectorName x1 y1 x2 y2 =
35-
goldenWithHaskellDoc plugin title testDataDir fp "expected" "hs" $ \doc -> do
56+
goldenWithHaskellAndCaps noResolveCaps plugin title testDataDir fp "expected" "hs" $ \doc -> do
3657
(act:_) <- getExplicitFieldsActions doc selectorName x1 y1 x2 y2
3758
executeCodeAction act
59+
where noResolveCaps = set (L.textDocument . _Just . L.codeAction . _Just . L.dataSupport . _Just) False fullCaps
60+
61+
mkResolveTest :: TestName -> FilePath -> T.Text -> UInt -> UInt -> UInt -> UInt -> TestTree
62+
mkResolveTest title fp selectorName x1 y1 x2 y2 =
63+
goldenWithHaskellDoc plugin title testDataDir fp "expected" "hs" $ \doc -> do
64+
((Right act):_) <- getAndResolveExplicitFieldsActions doc selectorName x1 y1 x2 y2
65+
executeCodeAction act
3866

3967
getExplicitFieldsActions
4068
:: TextDocumentIdentifier
@@ -46,6 +74,19 @@ getExplicitFieldsActions doc selectorName x1 y1 x2 y2 =
4674
where
4775
range = Range (Position x1 y1) (Position x2 y2)
4876

77+
getAndResolveExplicitFieldsActions
78+
:: TextDocumentIdentifier
79+
-> T.Text
80+
-> UInt -> UInt -> UInt -> UInt
81+
-> Session [Either ResponseError CodeAction]
82+
getAndResolveExplicitFieldsActions doc selectorName x1 y1 x2 y2 = do
83+
actions <- findExplicitFieldsAction selectorName <$> getCodeActions doc range
84+
rsp <- mapM (request SMethod_CodeActionResolve) actions
85+
pure $ (^. L.result) <$> rsp
86+
87+
where
88+
range = Range (Position x1 y1) (Position x2 y2)
89+
4990
findExplicitFieldsAction :: T.Text -> [a |? CodeAction] -> [CodeAction]
5091
findExplicitFieldsAction selectorName = filter (isExplicitFieldsCodeAction selectorName) . rights . map toEither
5192

@@ -55,3 +96,24 @@ isExplicitFieldsCodeAction selectorName CodeAction {_title} =
5596

5697
testDataDir :: FilePath
5798
testDataDir = "test" </> "testdata"
99+
100+
goldenWithHaskellAndCaps
101+
:: Pretty b
102+
=> ClientCapabilities
103+
-> PluginTestDescriptor b
104+
-> TestName
105+
-> FilePath
106+
-> FilePath
107+
-> FilePath
108+
-> FilePath
109+
-> (TextDocumentIdentifier -> Session ())
110+
-> TestTree
111+
goldenWithHaskellAndCaps clientCaps plugin title testDataDir path desc ext act =
112+
goldenGitDiff title (testDataDir </> path <.> desc <.> ext)
113+
$ runSessionWithServerAndCaps plugin clientCaps testDataDir
114+
$ TL.encodeUtf8 . TL.fromStrict
115+
<$> do
116+
doc <- openDoc (path <.> ext) "haskell"
117+
void waitForBuildQueue
118+
act doc
119+
documentContents doc

0 commit comments

Comments
 (0)