22{-# LANGUAGE OverloadedLabels #-}
33{-# LANGUAGE OverloadedLists #-}
44{-# LANGUAGE OverloadedStrings #-}
5- {-# OPTIONS_GHC -Wall #-}
6- {-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
75
86module Main
97 ( main
@@ -13,10 +11,10 @@ import Control.Exception (catch)
1311import Control.Lens (Prism' , prism' , view , (^.) ,
1412 (^..) , (^?) )
1513import Control.Monad (void )
14+ import Data.Foldable (find )
1615import Data.Maybe
1716import Data.Row ((.==) )
1817import qualified Data.Text as T
19- import Development.IDE.Core.Compile (sourceTypecheck )
2018import qualified Ide.Plugin.Class as Class
2119import qualified Language.LSP.Protocol.Lens as L
2220import Language.LSP.Protocol.Message
@@ -47,35 +45,35 @@ codeActionTests = testGroup
4745 , " Add placeholders for all missing methods"
4846 , " Add placeholders for all missing methods with signature(s)"
4947 ]
50- , goldenWithClass " Creates a placeholder for '=='" " T1" " eq" $ \ (eqAction : _) -> do
51- executeCodeAction eqAction
52- , goldenWithClass " Creates a placeholder for '/='" " T1" " ne" $ \ (_ : _ : neAction : _) -> do
53- executeCodeAction neAction
54- , goldenWithClass " Creates a placeholder for both '==' and '/='" " T1" " all" $ \ (_ : _ : _ : _ : allMethodsAction : _) -> do
55- executeCodeAction allMethodsAction
56- , goldenWithClass " Creates a placeholder for 'fmap'" " T2" " fmap" $ \ (_ : _ : _ : _ : _ : _ : fmapAction : _) -> do
57- executeCodeAction fmapAction
58- , goldenWithClass " Creates a placeholder for multiple methods 1" " T3" " 1" $ \ (mmAction : _) -> do
59- executeCodeAction mmAction
60- , goldenWithClass " Creates a placeholder for multiple methods 2" " T3" " 2" $ \ (_ : _ : mmAction : _) -> do
61- executeCodeAction mmAction
62- , goldenWithClass " Creates a placeholder for a method starting with '_'" " T4" " " $ \ (_fAction : _) -> do
63- executeCodeAction _fAction
64- , goldenWithClass " Creates a placeholder for '==' with extra lines" " T5" " " $ \ (eqAction : _) -> do
65- executeCodeAction eqAction
66- , goldenWithClass " Creates a placeholder for only the unimplemented methods of multiple methods" " T6" " 1" $ \ (gAction : _) -> do
67- executeCodeAction gAction
68- , goldenWithClass " Creates a placeholder for other two methods" " T6" " 2" $ \ (_ : _ : ghAction : _) -> do
69- executeCodeAction ghAction
48+ , goldenWithClass " Creates a placeholder for '=='" " T1" " eq" $
49+ getActionByTitle " Add placeholders for '==' "
50+ , goldenWithClass " Creates a placeholder for '/='" " T1" " ne" $
51+ getActionByTitle " Add placeholders for '/=' "
52+ , goldenWithClass " Creates a placeholder for both '==' and '/='" " T1" " all" $
53+ getActionByTitle " Add placeholders for all missing methods "
54+ , goldenWithClass " Creates a placeholder for 'fmap'" " T2" " fmap" $
55+ getActionByTitle " Add placeholders for 'fmap' "
56+ , goldenWithClass " Creates a placeholder for multiple methods 1" " T3" " 1" $
57+ getActionByTitle " Add placeholders for 'f','g' "
58+ , goldenWithClass " Creates a placeholder for multiple methods 2" " T3" " 2" $
59+ getActionByTitle " Add placeholders for 'g','h' "
60+ , goldenWithClass " Creates a placeholder for a method starting with '_'" " T4" " " $
61+ getActionByTitle " Add placeholders for '_f' "
62+ , goldenWithClass " Creates a placeholder for '==' with extra lines" " T5" " " $
63+ getActionByTitle " Add placeholders for '==' "
64+ , goldenWithClass " Creates a placeholder for only the unimplemented methods of multiple methods" " T6" " 1" $
65+ getActionByTitle " Add placeholders for 'g' "
66+ , goldenWithClass " Creates a placeholder for other two methods" " T6" " 2" $
67+ getActionByTitle " Add placeholders for 'g','h' "
7068 , onlyRunForGhcVersions [GHC92 , GHC94 ] " Only ghc-9.2+ enabled GHC2021 implicitly" $
71- goldenWithClass " Don't insert pragma with GHC2021" " InsertWithGHC2021Enabled" " " $ \ (_ : eqWithSig : _) -> do
72- executeCodeAction eqWithSig
73- , goldenWithClass " Insert pragma if not exist" " InsertWithoutPragma" " " $ \ (_ : eqWithSig : _) -> do
74- executeCodeAction eqWithSig
75- , goldenWithClass " Don't insert pragma if exist" " InsertWithPragma" " " $ \ (_ : eqWithSig : _) -> do
76- executeCodeAction eqWithSig
77- , goldenWithClass " Only insert pragma once" " InsertPragmaOnce" " " $ \ (_ : multi : _) -> do
78- executeCodeAction multi
69+ goldenWithClass " Don't insert pragma with GHC2021" " InsertWithGHC2021Enabled" " " $
70+ getActionByTitle " Add placeholders for '==' with signature(s) "
71+ , goldenWithClass " Insert pragma if not exist" " InsertWithoutPragma" " " $
72+ getActionByTitle " Add placeholders for '==' with signature(s) "
73+ , goldenWithClass " Don't insert pragma if exist" " InsertWithPragma" " " $
74+ getActionByTitle " Add placeholders for '==' with signature(s) "
75+ , goldenWithClass " Only insert pragma once" " InsertPragmaOnce" " " $
76+ getActionByTitle " Add placeholders for 'pure','<*>' with signature(s) "
7977 , expectCodeActionsAvailable " No code action available when minimal requirements meet" " MinimalDefinitionMeet" []
8078 , expectCodeActionsAvailable " Add placeholders for all missing methods is unavailable when all methods are required" " AllMethodsRequired"
8179 [ " Add placeholders for 'f','g'"
@@ -162,14 +160,20 @@ goldenCodeLens title path idx =
162160 executeCommand $ fromJust $ (lens !! idx) ^. L. command
163161 void $ skipManyTill anyMessage (message SMethod_WorkspaceApplyEdit )
164162
165- goldenWithClass :: TestName -> FilePath -> FilePath -> ([CodeAction ] -> Session () ) -> TestTree
166- goldenWithClass title path desc act =
163+ goldenWithClass :: TestName -> FilePath -> FilePath -> ([CodeAction ] -> Session CodeAction ) -> TestTree
164+ goldenWithClass title path desc findAction =
167165 goldenWithHaskellDoc def classPlugin title testDataDir path (desc <.> " expected" ) " hs" $ \ doc -> do
168166 _ <- waitForDiagnosticsFrom doc
169167 actions <- concatMap (^.. _CACodeAction) <$> getAllCodeActions doc
170- act actions
168+ action <- findAction actions
169+ executeCodeAction action
171170 void $ skipManyTill anyMessage (getDocumentEdit doc)
172171
172+ getActionByTitle :: T. Text -> [CodeAction ] -> Session CodeAction
173+ getActionByTitle title actions = case find (\ a -> a ^. L. title == title) actions of
174+ Just a -> pure a
175+ Nothing -> liftIO $ assertFailure $ " Action " <> show title <> " not found in " <> show [a ^. L. title | a <- actions]
176+
173177expectCodeActionsAvailable :: TestName -> FilePath -> [T. Text ] -> TestTree
174178expectCodeActionsAvailable title path actionTitles =
175179 testCase title $ do
0 commit comments