@@ -4,6 +4,7 @@ module Completion(tests) where
4
4
5
5
import Control.Lens hiding ((.=) )
6
6
import Data.Aeson (object , (.=) )
7
+ import Data.Foldable (find )
7
8
import qualified Data.Text as T
8
9
import Ide.Plugin.Config (maxCompletions )
9
10
import Language.LSP.Types.Lens hiding (applyEdit )
@@ -19,7 +20,7 @@ tests = testGroup "completions" [
19
20
_ <- applyEdit doc te
20
21
21
22
compls <- getCompletions doc (Position 5 9 )
22
- let item = head $ filter (( == " putStrLn" ) . ( ^. label)) compls
23
+ item <- getCopletionByLabel " putStrLn" compls
23
24
liftIO $ do
24
25
item ^. label @?= " putStrLn"
25
26
item ^. kind @?= Just CiFunction
@@ -35,7 +36,7 @@ tests = testGroup "completions" [
35
36
_ <- applyEdit doc te
36
37
37
38
compls <- getCompletions doc (Position 5 9 )
38
- let item = head $ filter (( == " putStrLn" ) . ( ^. label)) compls
39
+ item <- getCopletionByLabel " putStrLn" compls
39
40
resolvedRes <- request SCompletionItemResolve item
40
41
let eResolved = resolvedRes ^. result
41
42
case eResolved of
@@ -56,7 +57,7 @@ tests = testGroup "completions" [
56
57
_ <- applyEdit doc te
57
58
58
59
compls <- getCompletions doc (Position 1 23 )
59
- let item = head $ filter (( == " Maybe" ) . ( ^. label)) compls
60
+ item <- getCopletionByLabel " Maybe" compls
60
61
liftIO $ do
61
62
item ^. label @?= " Maybe"
62
63
item ^. detail @?= Just " Data.Maybe"
@@ -71,7 +72,7 @@ tests = testGroup "completions" [
71
72
_ <- applyEdit doc te
72
73
73
74
compls <- getCompletions doc (Position 2 24 )
74
- let item = head $ filter (( == " List" ) . ( ^. label)) compls
75
+ item <- getCopletionByLabel " List" compls
75
76
liftIO $ do
76
77
item ^. label @?= " List"
77
78
item ^. detail @?= Just " Data.List"
@@ -91,7 +92,7 @@ tests = testGroup "completions" [
91
92
_ <- applyEdit doc te
92
93
93
94
compls <- getCompletions doc (Position 5 4 )
94
- let item = head $ filter ( \ c -> c ^. label == " accessor" ) compls
95
+ item <- getCopletionByLabel " accessor" compls
95
96
liftIO $ do
96
97
item ^. label @?= " accessor"
97
98
item ^. kind @?= Just CiFunction
@@ -101,7 +102,7 @@ tests = testGroup "completions" [
101
102
let te = TextEdit (Range (Position 5 7 ) (Position 5 9 )) " id"
102
103
_ <- applyEdit doc te
103
104
compls <- getCompletions doc (Position 5 9 )
104
- let item = head $ filter (( == " id" ) . ( ^. label)) compls
105
+ item <- getCopletionByLabel " id" compls
105
106
liftIO $ do
106
107
item ^. detail @?= Just " :: a -> a"
107
108
@@ -111,7 +112,7 @@ tests = testGroup "completions" [
111
112
let te = TextEdit (Range (Position 5 7 ) (Position 5 24 )) " flip"
112
113
_ <- applyEdit doc te
113
114
compls <- getCompletions doc (Position 5 11 )
114
- let item = head $ filter (( == " flip" ) . ( ^. label)) compls
115
+ item <- getCopletionByLabel " flip" compls
115
116
liftIO $
116
117
item ^. detail @?= Just " :: (a -> b -> c) -> b -> a -> c"
117
118
@@ -128,7 +129,7 @@ tests = testGroup "completions" [
128
129
_ <- applyEdit doc te
129
130
130
131
compls <- getCompletions doc (Position 0 31 )
131
- let item = head $ filter (( == " Alternative" ) . ( ^. label)) compls
132
+ item <- getCopletionByLabel " Alternative" compls
132
133
liftIO $ do
133
134
item ^. label @?= " Alternative"
134
135
item ^. kind @?= Just CiFunction
@@ -141,7 +142,7 @@ tests = testGroup "completions" [
141
142
_ <- applyEdit doc te
142
143
143
144
compls <- getCompletions doc (Position 0 41 )
144
- let item = head $ filter (( == " liftA" ) . ( ^. label)) compls
145
+ item <- getCopletionByLabel " liftA" compls
145
146
liftIO $ do
146
147
item ^. label @?= " liftA"
147
148
item ^. kind @?= Just CiFunction
@@ -159,7 +160,7 @@ snippetTests = testGroup "snippets" [
159
160
_ <- applyEdit doc te
160
161
161
162
compls <- getCompletions doc (Position 5 14 )
162
- let item = head $ filter (( == " Nothing" ) . ( ^. label)) compls
163
+ item <- getCopletionByLabel " Nothing" compls
163
164
liftIO $ do
164
165
item ^. insertTextFormat @?= Just Snippet
165
166
item ^. insertText @?= Just " Nothing "
@@ -171,7 +172,7 @@ snippetTests = testGroup "snippets" [
171
172
_ <- applyEdit doc te
172
173
173
174
compls <- getCompletions doc (Position 5 11 )
174
- let item = head $ filter (( == " foldl" ) . ( ^. label)) compls
175
+ item <- getCopletionByLabel " foldl" compls
175
176
liftIO $ do
176
177
item ^. label @?= " foldl"
177
178
item ^. kind @?= Just CiFunction
@@ -185,7 +186,7 @@ snippetTests = testGroup "snippets" [
185
186
_ <- applyEdit doc te
186
187
187
188
compls <- getCompletions doc (Position 5 11 )
188
- let item = head $ filter (( == " mapM" ) . ( ^. label)) compls
189
+ item <- getCopletionByLabel " mapM" compls
189
190
liftIO $ do
190
191
item ^. label @?= " mapM"
191
192
item ^. kind @?= Just CiFunction
@@ -199,7 +200,7 @@ snippetTests = testGroup "snippets" [
199
200
_ <- applyEdit doc te
200
201
201
202
compls <- getCompletions doc (Position 5 18 )
202
- let item = head $ filter (( == " filter" ) . ( ^. label)) compls
203
+ item <- getCopletionByLabel " filter" compls
203
204
liftIO $ do
204
205
item ^. label @?= " filter"
205
206
item ^. kind @?= Just CiFunction
@@ -213,7 +214,7 @@ snippetTests = testGroup "snippets" [
213
214
_ <- applyEdit doc te
214
215
215
216
compls <- getCompletions doc (Position 5 18 )
216
- let item = head $ filter (( == " filter" ) . ( ^. label)) compls
217
+ item <- getCopletionByLabel " filter" compls
217
218
liftIO $ do
218
219
item ^. label @?= " filter"
219
220
item ^. kind @?= Just CiFunction
@@ -227,7 +228,7 @@ snippetTests = testGroup "snippets" [
227
228
_ <- applyEdit doc te
228
229
229
230
compls <- getCompletions doc (Position 5 29 )
230
- let item = head $ filter (( == " intersperse" ) . ( ^. label)) compls
231
+ item <- getCopletionByLabel " intersperse" compls
231
232
liftIO $ do
232
233
item ^. label @?= " intersperse"
233
234
item ^. kind @?= Just CiFunction
@@ -241,7 +242,7 @@ snippetTests = testGroup "snippets" [
241
242
_ <- applyEdit doc te
242
243
243
244
compls <- getCompletions doc (Position 5 29 )
244
- let item = head $ filter (( == " intersperse" ) . ( ^. label)) compls
245
+ item <- getCopletionByLabel " intersperse" compls
245
246
liftIO $ do
246
247
item ^. label @?= " intersperse"
247
248
item ^. kind @?= Just CiFunction
@@ -268,7 +269,9 @@ snippetTests = testGroup "snippets" [
268
269
_ <- applyEdit doc te
269
270
270
271
compls <- getCompletions doc (Position 1 6 )
271
- let item = head $ filter (\ c -> (c ^. label == " MkFoo" ) && maybe False (" MkFoo {" `T.isPrefixOf` ) (c ^. insertText)) compls
272
+ item <- case find (\ c -> (c ^. label == " MkFoo" ) && maybe False (" MkFoo {" `T.isPrefixOf` ) (c ^. insertText)) compls of
273
+ Just c -> pure c
274
+ Nothing -> liftIO . assertFailure $ " Completion with label 'MkFoo' and insertText starting with 'MkFoo {' not found among " <> show compls
272
275
liftIO $ do
273
276
item ^. insertTextFormat @?= Just Snippet
274
277
item ^. insertText @?= Just " MkFoo {arg1=${1:_arg1}, arg2=${2:_arg2}, arg3=${3:_arg3}, arg4=${4:_arg4}, arg5=${5:_arg5}}"
@@ -279,7 +282,7 @@ snippetTests = testGroup "snippets" [
279
282
_ <- applyEdit doc te
280
283
281
284
compls <- getCompletions doc (Position 5 11 )
282
- let item = head $ filter (( == " foldl" ) . ( ^. label)) compls
285
+ item <- getCopletionByLabel " foldl" compls
283
286
liftIO $ do
284
287
item ^. label @?= " foldl"
285
288
item ^. kind @?= Just CiFunction
@@ -327,11 +330,21 @@ contextTests = testGroup "contexts" [
327
330
]
328
331
329
332
shouldContainCompl :: [CompletionItem ] -> T. Text -> Assertion
330
- compls `shouldContainCompl` x =
331
- any ((== x ) . (^. label)) compls
332
- @? " Should contain completion: " ++ show x
333
+ compls `shouldContainCompl` lbl =
334
+ any ((== lbl ) . (^. label)) compls
335
+ @? " Should contain completion: " ++ show lbl
333
336
334
337
shouldNotContainCompl :: [CompletionItem ] -> T. Text -> Assertion
335
- compls `shouldNotContainCompl` x =
336
- all ((/= x) . (^. label)) compls
337
- @? " Should not contain completion: " ++ show x
338
+ compls `shouldNotContainCompl` lbl =
339
+ all ((/= lbl) . (^. label)) compls
340
+ @? " Should not contain completion: " ++ show lbl
341
+
342
+ getCopletionByLabel :: T. Text -> [CompletionItem ] -> Session CompletionItem
343
+ getCopletionByLabel lbl compls =
344
+ case find (\ c -> c ^. label == lbl) compls of
345
+ Just c -> pure c
346
+ Nothing ->
347
+ let knownLabels = compls ^.. traversed . label
348
+ in liftIO . assertFailure $
349
+ " Completion with label " <> show lbl
350
+ <> " not found in " <> show knownLabels
0 commit comments