Skip to content

Commit 25cf2bb

Browse files
committed
Make it possible to choose the code action in extendImportTests
Let the order of the expected code actions dictate which one to execute, i.e., the first one. This means we no longer test the *order* of the suggested code actions. Through this simple change, we can now test the execution of a code action that doesn't come first in the list of suggested code actions.
1 parent 1a34357 commit 25cf2bb

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

ghcide/test/exe/Main.hs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,19 +1231,29 @@ extendImportTests = testGroup "extend import actions"
12311231
])
12321232
]
12331233
where
1234-
template setUpModules moduleUnderTest range expectedActions expectedContentB = do
1234+
codeActionTitle CodeAction{_title=x} = x
1235+
1236+
template setUpModules moduleUnderTest range expectedTitles expectedContentB = do
12351237
mapM_ (\x -> createDoc (fst x) "haskell" (snd x)) setUpModules
12361238
docB <- createDoc (fst moduleUnderTest) "haskell" (snd moduleUnderTest)
12371239
_ <- waitForDiagnostics
12381240
void (skipManyTill anyMessage message :: Session WorkDoneProgressEndNotification)
1239-
codeActions <- filter (\(CACodeAction CodeAction{_title=x}) -> T.isPrefixOf "Add" x)
1240-
<$> getCodeActions docB range
1241-
let expectedTitles = (\(CACodeAction CodeAction{_title=x}) ->x) <$> codeActions
1242-
liftIO $ expectedActions @=? expectedTitles
1243-
1244-
-- Get the first action and execute the first action
1245-
let CACodeAction action : _
1246-
= sortOn (\(CACodeAction CodeAction{_title=x}) -> x) codeActions
1241+
actionsOrCommands <- getCodeActions docB range
1242+
let codeActions =
1243+
filter
1244+
(T.isPrefixOf "Add" . codeActionTitle)
1245+
[ca | CACodeAction ca <- actionsOrCommands]
1246+
actualTitles = codeActionTitle <$> codeActions
1247+
-- Note that we are not testing the order of the actions, as the order of
1248+
-- the expected actions indicates which one we'll execute in this test,
1249+
-- i.e., the first one.
1250+
liftIO $ sort expectedTitles @=? sort actualTitles
1251+
1252+
-- Execute the action with the same title as the first expected one. Since
1253+
-- we tested that both lists have the same elements (possibly in a
1254+
-- different order), this search cannot fail.
1255+
let action = fromJust $
1256+
find ((head expectedTitles ==) . codeActionTitle) codeActions
12471257
executeCodeAction action
12481258
contentAfterAction <- documentContents docB
12491259
liftIO $ expectedContentB @=? contentAfterAction

0 commit comments

Comments
 (0)