Skip to content

Commit f731d92

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 7fa3ff6 commit f731d92

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

ghcide/test/exe/Main.hs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,9 @@ extendImportTests = testGroup "extend import actions"
12351235
])
12361236
]
12371237
where
1238-
template setUpModules moduleUnderTest range expectedActions expectedContentB = do
1238+
codeActionTitle CodeAction{_title=x} = x
1239+
1240+
template setUpModules moduleUnderTest range expectedTitles expectedContentB = do
12391241
sendNotification WorkspaceDidChangeConfiguration
12401242
(DidChangeConfigurationParams $ toJSON
12411243
def{checkProject = overrideCheckProject})
@@ -1245,14 +1247,23 @@ extendImportTests = testGroup "extend import actions"
12451247
docB <- createDoc (fst moduleUnderTest) "haskell" (snd moduleUnderTest)
12461248
_ <- waitForDiagnostics
12471249
void (skipManyTill anyMessage message :: Session WorkDoneProgressEndNotification)
1248-
codeActions <- filter (\(CACodeAction CodeAction{_title=x}) -> T.isPrefixOf "Add" x)
1249-
<$> getCodeActions docB range
1250-
let expectedTitles = (\(CACodeAction CodeAction{_title=x}) ->x) <$> codeActions
1251-
liftIO $ expectedActions @=? expectedTitles
1252-
1253-
-- Get the first action and execute the first action
1254-
let CACodeAction action : _
1255-
= sortOn (\(CACodeAction CodeAction{_title=x}) -> x) codeActions
1250+
actionsOrCommands <- getCodeActions docB range
1251+
let codeActions =
1252+
filter
1253+
(T.isPrefixOf "Add" . codeActionTitle)
1254+
[ca | CACodeAction ca <- actionsOrCommands]
1255+
actualTitles = codeActionTitle <$> codeActions
1256+
-- Note that we are not testing the order of the actions, as the
1257+
-- order of the expected actions indicates which one we'll execute
1258+
-- in this test, i.e., the first one.
1259+
liftIO $ sort expectedTitles @=? sort actualTitles
1260+
1261+
-- Execute the action with the same title as the first expected one.
1262+
-- Since we tested that both lists have the same elements (possibly
1263+
-- in a different order), this search cannot fail.
1264+
let firstTitle:_ = expectedTitles
1265+
action = fromJust $
1266+
find ((firstTitle ==) . codeActionTitle) codeActions
12561267
executeCodeAction action
12571268
contentAfterAction <- documentContents docB
12581269
liftIO $ expectedContentB @=? contentAfterAction

0 commit comments

Comments
 (0)