Skip to content

Commit c6aad68

Browse files
committed
regex tests
1 parent 3ff4ede commit c6aad68

File tree

5 files changed

+86
-26
lines changed

5 files changed

+86
-26
lines changed

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/CabalAdd.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
module Ide.Plugin.Cabal.CabalAdd
1111
( findResponsibleCabalFile
1212
, hiddenPackageAction
13+
, hiddenPackageSuggestion
1314
, cabalAddCommand
1415
, command
1516
, Log
@@ -143,15 +144,14 @@ hiddenPackageSuggestion :: Int -> T.Text -> [(T.Text, T.Text)]
143144
hiddenPackageSuggestion maxCompletions msg = take maxCompletions $ getMatch (msg =~ regex)
144145
where
145146
regex :: T.Text -- TODO: Support multiple packages suggestion
146-
regex = "It is a member of the hidden package [\8216']([a-z-]+)(-([0-9\\.]*))?[\8217']"
147+
regex = "It is a member of the hidden package [\8216']([a-zA-Z0-9-]*[a-zA-Z0-9])(-([0-9\\.]*))?[\8217']"
148+
-- Have to do this matching because `Regex.TDFA` doesn't(?) support
149+
-- not-capturing groups like (?:message)
147150
getMatch :: (T.Text, T.Text, T.Text, [T.Text]) -> [(T.Text, T.Text)]
148151
getMatch (_, _, _, []) = []
149-
getMatch (_, _, _, [dependency]) = [(dependency, T.empty)]
150-
getMatch (_, _, _, [dependency, dashedVersion]) = [(dependency, T.empty)] -- failed to get version
151-
getMatch (_, _, _, [dependency, dashedVersion, cleanVersion]) = [(dependency, cleanVersion)]
152+
getMatch (_, _, _, [dependency, _, cleanVersion]) = [(dependency, cleanVersion)]
152153
getMatch (_, _, _, _) = error "Impossible pattern matching case"
153154

154-
155155
cabalAddCommand :: IsString p => p
156156
cabalAddCommand = "cabalAdd"
157157

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

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import Outline (outlineTests)
2525
import System.FilePath
2626
import Test.Hls
2727
import Utils
28+
import Ide.Plugin.Cabal.CabalAdd (hiddenPackageSuggestion)
29+
import Distribution.Utils.Generic (safeHead)
2830

2931
main :: IO ()
3032
main = do
@@ -223,15 +225,84 @@ codeActionTests = testGroup "Code Actions"
223225
pure ()
224226
, runHaskellTestCaseSession "Code Actions - Can add hidden package" ("cabal-add-testdata" </> "cabal-add-exe")
225227
(generateHiddenPackageTestSession "cabal-add-exe.cabal" ("src" </> "Main.hs") "split" [253])
226-
, runHaskellTestCaseSession "Code Actions - Can add dashed hidden package" ("cabal-add-testdata" </> "cabal-add-dashed")
227-
(generateHiddenPackageTestSession "cabal-add-dashed.cabal" ("src" </> "Main.hs") "ghc-boot" [260])
228228
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a library" ("cabal-add-testdata" </> "cabal-add-lib")
229229
(generateHiddenPackageTestSession "cabal-add-lib.cabal" ("src" </> "MyLib.hs") "split" [348])
230230
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a test" ("cabal-add-testdata" </> "cabal-add-tests")
231231
(generateHiddenPackageTestSession "cabal-add-tests.cabal" ("test" </> "Main.hs") "split" [478])
232232
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a benchmark" ("cabal-add-testdata" </> "cabal-add-bench")
233233
(generateHiddenPackageTestSession "cabal-add-bench.cabal" ("bench" </> "Main.hs") "split" [403])
234-
234+
, testHiddenPackageSuggestions "Check CabalAdd's parser, no version"
235+
[ "It is a member of the hidden package 'base'"
236+
, "It is a member of the hidden package 'Blammo-wai'"
237+
, "It is a member of the hidden package 'BlastHTTP'"
238+
, "It is a member of the hidden package 'CC-delcont-ref-tf'"
239+
, "It is a member of the hidden package '3d-graphics-examples'"
240+
, "It is a member of the hidden package 'AAI'"
241+
, "It is a member of the hidden package 'AWin32Console'"
242+
]
243+
[ ("base", T.empty)
244+
, ("Blammo-wai", T.empty)
245+
, ("BlastHTTP", T.empty)
246+
, ("CC-delcont-ref-tf", T.empty)
247+
, ("3d-graphics-examples", T.empty)
248+
, ("AAI", T.empty)
249+
, ("AWin32Console", T.empty)
250+
]
251+
, testHiddenPackageSuggestions "Check CabalAdd's parser, with version"
252+
[ "It is a member of the hidden package 'base-0.1.0.0'"
253+
, "It is a member of the hidden package 'Blammo-wai-0.11.0'"
254+
, "It is a member of the hidden package 'BlastHTTP-2.6.4.3'"
255+
, "It is a member of the hidden package 'CC-delcont-ref-tf-0.0.0.2'"
256+
, "It is a member of the hidden package '3d-graphics-examples-1.1.6'"
257+
, "It is a member of the hidden package 'AAI-0.1'"
258+
, "It is a member of the hidden package 'AWin32Console-1.19.1'"
259+
]
260+
[ ("base","0.1.0.0")
261+
, ("Blammo-wai", "0.11.0")
262+
, ("BlastHTTP", "2.6.4.3")
263+
, ("CC-delcont-ref-tf", "0.0.0.2")
264+
, ("3d-graphics-examples", "1.1.6")
265+
, ("AAI", "0.1")
266+
, ("AWin32Console", "1.19.1")
267+
]
268+
, testHiddenPackageSuggestions "Check CabalAdd's parser, no version, unicode comma"
269+
[ "It is a member of the hidden package \8216base\8217"
270+
, "It is a member of the hidden package \8216Blammo-wai\8217"
271+
, "It is a member of the hidden package \8216BlastHTTP\8217"
272+
, "It is a member of the hidden package \8216CC-delcont-ref-tf\8217"
273+
, "It is a member of the hidden package \8216AAI\8217"
274+
, "It is a member of the hidden package \8216AWin32Console\8217"
275+
]
276+
[ ("base", T.empty)
277+
, ("Blammo-wai", T.empty)
278+
, ("BlastHTTP", T.empty)
279+
, ("CC-delcont-ref-tf", T.empty)
280+
, ("AAI", T.empty)
281+
, ("AWin32Console", T.empty)
282+
]
283+
, testHiddenPackageSuggestions "Check CabalAdd's parser, with version, unicode comma"
284+
[ "It is a member of the hidden package \8216base-0.1.0.0\8217"
285+
, "It is a member of the hidden package \8216Blammo-wai-0.11.0\8217"
286+
, "It is a member of the hidden package \8216BlastHTTP-2.6.4.3\8217"
287+
, "It is a member of the hidden package \8216CC-delcont-ref-tf-0.0.0.2\8217"
288+
, "It is a member of the hidden package \8216AAI-0.1\8217"
289+
, "It is a member of the hidden package \8216AWin32Console-1.19.1\8217"
290+
]
291+
[ ("base","0.1.0.0")
292+
, ("Blammo-wai", "0.11.0")
293+
, ("BlastHTTP", "2.6.4.3")
294+
, ("CC-delcont-ref-tf", "0.0.0.2")
295+
, ("AAI", "0.1")
296+
, ("AWin32Console", "1.19.1")
297+
]
298+
, expectFailBecause "TODO fix regex for these cases" $
299+
testHiddenPackageSuggestions "Check CabalAdd's parser, with version, unicode comma"
300+
[ "It is a member of the hidden package \82163d-graphics-examples\8217"
301+
, "It is a member of the hidden package \82163d-graphics-examples-1.1.6\8217"
302+
]
303+
[ ("3d-graphics-examples", T.empty)
304+
, ("3d-graphics-examples", "1.1.6")
305+
]
235306
]
236307
where
237308
getLicenseAction :: T.Text -> [Command |? CodeAction] -> [CodeAction]
@@ -250,3 +321,10 @@ codeActionTests = testGroup "Code Actions"
250321
_ <- skipManyTill anyMessage $ getDocumentEdit cabDoc -- Wait for the changes in cabal file
251322
contents <- documentContents cabDoc
252323
liftIO $ assertEqual (T.unpack dependency <> " isn't found in the cabal file") indicesRes (Text.indices dependency contents)
324+
testHiddenPackageSuggestions :: String -> [T.Text] -> [(T.Text, T.Text)] -> TestTree
325+
testHiddenPackageSuggestions testTitle messages suggestions =
326+
let suggestions' = map (safeHead . hiddenPackageSuggestion 1) messages
327+
assertions = zipWith (@?=) suggestions' (map Just suggestions)
328+
testNames = map (\(f, s) -> "Check if " ++ T.unpack f ++ "-" ++ T.unpack s ++ " was parsed correctly") suggestions
329+
test = testGroup testTitle $ zipWith testCase testNames assertions
330+
in test

plugins/hls-cabal-plugin/test/testdata/cabal-add-testdata/cabal-add-dashed/cabal-add-dashed.cabal

Lines changed: 0 additions & 12 deletions
This file was deleted.

plugins/hls-cabal-plugin/test/testdata/cabal-add-testdata/cabal-add-dashed/src/Main.hs

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
packages: cabal-add-exe
2-
cabal-add-dashed
32
cabal-add-lib
43
cabal-add-tests
54
cabal-add-bench

0 commit comments

Comments
 (0)