Skip to content

Commit 3c4d9d8

Browse files
committed
Add golden test for fixing field typos
1 parent 388e036 commit 3c4d9d8

File tree

4 files changed

+105
-27
lines changed

4 files changed

+105
-27
lines changed

plugins/hls-cabal-plugin/test/Completer.hs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import Ide.Plugin.Cabal.Completion.Types (CabalPrefixInfo
2828
import qualified Language.LSP.Protocol.Lens as L
2929
import System.FilePath
3030
import Test.Hls
31+
import qualified Text.Fuzzy.Parallel as Fuzzy
3132
import Utils
3233

3334
completerTests :: TestTree
@@ -270,7 +271,7 @@ filePathExposedModulesTests =
270271
callFilePathsForExposedModules :: [FilePath] -> IO [T.Text]
271272
callFilePathsForExposedModules srcDirs = do
272273
let prefInfo = simpleCabalPrefixInfoFromFp "" exposedTestDir
273-
filePathsForExposedModules mempty srcDirs prefInfo
274+
filePathsForExposedModules mempty srcDirs prefInfo $ Fuzzy.Matcher $ Fuzzy.simpleFilter Fuzzy.defChunkSize Fuzzy.defMaxResults
274275

275276
exposedModuleCompleterTests :: TestTree
276277
exposedModuleCompleterTests =
@@ -366,11 +367,19 @@ simpleCompleterData sName dir pref = do
366367
cabalContents <- ByteString.readFile $ testDataDir </> "exposed.cabal"
367368
pure $ parseGenericPackageDescriptionMaybe cabalContents,
368369
getCabalCommonSections = undefined,
369-
stanzaName = sName
370+
stanzaName = sName,
371+
matcher = Fuzzy.Matcher $ Fuzzy.simpleFilter Fuzzy.defChunkSize Fuzzy.defMaxResults
370372
}
371373

372374
mkCompleterData :: CabalPrefixInfo -> CompleterData
373-
mkCompleterData prefInfo = CompleterData {getLatestGPD = undefined, getCabalCommonSections = undefined, cabalPrefixInfo = prefInfo, stanzaName = Nothing}
375+
mkCompleterData prefInfo =
376+
CompleterData
377+
{ getLatestGPD = undefined,
378+
getCabalCommonSections = undefined,
379+
cabalPrefixInfo = prefInfo,
380+
stanzaName = Nothing,
381+
matcher = Fuzzy.Matcher $ Fuzzy.simpleFilter Fuzzy.defChunkSize Fuzzy.defMaxResults
382+
}
374383

375384
exposedTestDir :: FilePath
376385
exposedTestDir = addTrailingPathSeparator $ testDataDir </> "src-modules"

plugins/hls-cabal-plugin/test/Main.hs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import Completer (completerTests)
1414
import Context (contextTests)
1515
import Control.Lens ((^.))
1616
import Control.Lens.Fold ((^?))
17-
import Control.Monad (guard)
17+
import Control.Monad (forM_, guard)
1818
import qualified Data.ByteString as BS
1919
import Data.Either (isRight)
20-
import Data.List.Extra (nubOrdOn)
2120
import qualified Data.Maybe as Maybe
2221
import Data.Text (Text)
2322
import qualified Data.Text as T
@@ -26,6 +25,7 @@ import Definition (gotoDefinitionTests)
2625
import Development.IDE.Test
2726
import Ide.Plugin.Cabal.LicenseSuggest (licenseErrorSuggestion)
2827
import qualified Ide.Plugin.Cabal.Parse as Lib
28+
import Language.LSP.Protocol.Lens (HasRange (..))
2929
import qualified Language.LSP.Protocol.Lens as L
3030
import qualified Language.LSP.Protocol.Message as L
3131
import Outline (outlineTests)
@@ -191,32 +191,29 @@ codeActionTests = testGroup "Code Actions"
191191
, " build-depends: base"
192192
, " default-language: Haskell2010"
193193
]
194-
, runCabalGoldenSession "Code Actions - Can fix field names" "code-actions" "FieldSuggestions" $ \doc -> do
195-
_ <- waitForDiagnosticsFrom doc
196-
cas <- Maybe.mapMaybe (^? _R) <$> getAllCodeActions doc
197-
-- Filter out the code actions we want to invoke.
198-
-- We only want to invoke Code Actions with certain titles, and
199-
-- we want to invoke them only once, not once for each cursor request.
200-
-- 'getAllCodeActions' iterates over each cursor position and requests code actions.
201-
let selectedCas = nubOrdOn (^. L.title) $ filter
202-
(\ca -> (ca ^. L.title) `elem`
203-
[ "Replace with license"
204-
, "Replace with build-type"
205-
, "Replace with extra-doc-files"
206-
, "Replace with ghc-options"
207-
, "Replace with location"
208-
, "Replace with default-language"
209-
, "Replace with import"
210-
, "Replace with build-depends"
211-
, "Replace with main-is"
212-
, "Replace with hs-source-dirs"
213-
]) cas
214-
mapM_ executeCodeAction selectedCas
215-
pure ()
194+
, runCabalGoldenSession
195+
"Code Actions - Can complete field names"
196+
"code-actions"
197+
"FieldSuggestions"
198+
executeFirstActionPerDiagnostic
199+
, runCabalGoldenSession
200+
"Code Actions - Can fix field typos"
201+
"code-actions"
202+
"FieldSuggestionsTypos"
203+
executeFirstActionPerDiagnostic
216204
, cabalAddDependencyTests
217205
, cabalAddModuleTests
218206
]
219207
where
208+
executeFirstActionPerDiagnostic doc = do
209+
_ <- waitForDiagnosticsFrom doc
210+
diagnotics <- getCurrentDiagnostics doc
211+
-- Execute the first code action at each diagnostic point
212+
forM_ diagnotics $ \diagnostic -> do
213+
codeActions <- getCodeActions doc (diagnostic ^. range)
214+
case codeActions of
215+
[] -> pure ()
216+
ca : _ -> mapM_ executeCodeAction (ca ^? _R)
220217
getLicenseAction :: T.Text -> [Command |? CodeAction] -> [CodeAction]
221218
getLicenseAction license codeActions = do
222219
InR action@CodeAction{_title} <- codeActions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
cabal-version: 3.0
2+
name: FieldSuggestions
3+
version: 0.1.0
4+
liqns: BSD-3-Clause
5+
6+
quil-type: Simple
7+
8+
qqxtra-doc-fils:
9+
ChangeLog
10+
11+
-- Default warnings in HLS
12+
common warnings
13+
ghq-option: -Wall
14+
-Wredundant-constraints
15+
-Wunused-packages
16+
-Wno-name-shadowing
17+
-Wno-unticked-promoted-constructors
18+
19+
source-repository head
20+
type: git
21+
locqt: fake
22+
23+
library
24+
qqjfault-lang: Haskell2010
25+
-- Import isn't supported right now.
26+
iqqor: warnings
27+
bqqld-dep: base
28+
29+
executable my-exe
30+
mbn-is: Main.hs
31+
32+
test-suite Test
33+
type: exitcode-stdio-1.0
34+
main-is: Test.hs
35+
hqqqsource-drs:
36+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
cabal-version: 3.0
2+
name: FieldSuggestions
3+
version: 0.1.0
4+
license: BSD-3-Clause
5+
6+
build-type: Simple
7+
8+
extra-doc-files:
9+
ChangeLog
10+
11+
-- Default warnings in HLS
12+
common warnings
13+
ghc-options: -Wall
14+
-Wredundant-constraints
15+
-Wunused-packages
16+
-Wno-name-shadowing
17+
-Wno-unticked-promoted-constructors
18+
19+
source-repository head
20+
type: git
21+
location: fake
22+
23+
library
24+
default-language: Haskell2010
25+
-- Import isn't supported right now.
26+
import: warnings
27+
build-depends: base
28+
29+
executable my-exe
30+
main-is: Main.hs
31+
32+
test-suite Test
33+
type: exitcode-stdio-1.0
34+
main-is: Test.hs
35+
hs-source-dirs:
36+

0 commit comments

Comments
 (0)