Skip to content

Commit 2038fdb

Browse files
committed
Add tests for cabal file completion
1 parent 611da62 commit 2038fdb

File tree

1 file changed

+51
-1
lines changed
  • plugins/hls-cabal-plugin/test

1 file changed

+51
-1
lines changed

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE DisambiguateRecordFields #-}
44
{-# LANGUAGE NamedFieldPuns #-}
55
{-# LANGUAGE TypeOperators #-}
6+
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
67
module Main
78
( main
89
) where
@@ -13,6 +14,7 @@ import qualified Data.ByteString as BS
1314
import Data.Either (isRight)
1415
import qualified Data.Text as Text
1516
import Ide.Plugin.Cabal
17+
import Ide.Plugin.Cabal.Completions
1618
import Ide.Plugin.Cabal.LicenseSuggest (licenseErrorSuggestion)
1719
import qualified Ide.Plugin.Cabal.Parse as Lib
1820
import qualified Language.LSP.Types.Lens as J
@@ -38,7 +40,8 @@ unitTests :: TestTree
3840
unitTests =
3941
testGroup "Unit Tests"
4042
[ cabalParserUnitTests,
41-
codeActionUnitTests
43+
codeActionUnitTests,
44+
completionUnitTests
4245
]
4346

4447
cabalParserUnitTests :: TestTree
@@ -66,6 +69,41 @@ codeActionUnitTests = testGroup "Code Action Tests"
6669
@?= [("MiT","MIT"),("MiT","MIT-0")]
6770
]
6871

72+
completionUnitTests :: TestTree
73+
completionUnitTests = testGroup "Context Tests"
74+
[
75+
testCase "Empty File - Start" $ do
76+
-- for a completely empty file, the context needs to
77+
-- be toplevel without a specified keyword
78+
getContext (Position 0 0) [""] @?= Just (TopLevel, None),
79+
testCase "Cabal version keyword - no value" $ do
80+
-- on a file, where the "cabal-version:" keyword is already written
81+
-- the context should still be toplevel but the keyword should be recognized
82+
getContext (Position 0 15) ["cabal-version:"] @?= Just (TopLevel, KeyWord "cabal-version:"),
83+
testCase "Cabal version keyword - cursor in keyword" $ do
84+
-- on a file, where the "cabal-version:" keyword is already written
85+
-- but the cursor is in the middle of the keyword, the keyword context
86+
-- is cabal-version since after the keyword, the value needs to be inserted still
87+
getContext (Position 0 5) ["cabal-version:"] @?= Just (TopLevel, KeyWord "cabal-version:"),
88+
testCase "Cabal version keyword - no value, many spaces" $ do
89+
-- on a file, where the "cabal-version:" keyword is already written
90+
-- the context should still be toplevel but the keyword should be recognized
91+
getContext (Position 0 45) ["cabal-version:" <> Text.replicate 50 " "] @?= Just (TopLevel, KeyWord "cabal-version:"),
92+
testCase "Cabal version keyword - no value, many spaces" $ do
93+
-- in the first line of the file, if the keyword
94+
-- has not been written completely, the keyword context
95+
-- should still be None
96+
getContext (Position 0 5) ["cabal"] @?= Just (TopLevel, None),
97+
testCase "Inside Stanza - no keyword" $ do
98+
-- on a file, where the library stanza has been defined
99+
-- but no keyword is defined afterwards, the stanza context should be recognized
100+
getContext (Position 3 0) libraryStanza @?= Just (Stanza "library", None),
101+
testCase "Inside Stanza - keyword, no value" $ do
102+
-- on a file, where the library stanza and a keyword
103+
-- has been defined, the keyword and stanza should be recognized
104+
getContext (Position 4 21) libraryStanza @?= Just (Stanza "library", KeyWord "build-depends:")
105+
]
106+
69107
-- ------------------------------------------------------------------------
70108
-- Integration Tests
71109
-- ------------------------------------------------------------------------
@@ -180,3 +218,15 @@ runCabalSession subdir =
180218

181219
testDataDir :: FilePath
182220
testDataDir = "test" </> "testdata"
221+
222+
-- ------------------------------------------------------------------------
223+
-- Test Data
224+
-- ------------------------------------------------------------------------
225+
libraryStanza =
226+
[
227+
"cabal-version: 3.0",
228+
"name: simple-cabal",
229+
"library ",
230+
"",
231+
" build-depends: "
232+
]

0 commit comments

Comments
 (0)