Skip to content

Commit

Permalink
Add tests for cabal file completion
Browse files Browse the repository at this point in the history
  • Loading branch information
VeryMilkyJoe committed May 31, 2023
1 parent 611da62 commit 182189b
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion plugins/hls-cabal-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module Main
( main
) where
Expand All @@ -13,6 +14,7 @@ import qualified Data.ByteString as BS
import Data.Either (isRight)
import qualified Data.Text as Text
import Ide.Plugin.Cabal
import Ide.Plugin.Cabal.Completions
import Ide.Plugin.Cabal.LicenseSuggest (licenseErrorSuggestion)
import qualified Ide.Plugin.Cabal.Parse as Lib
import qualified Language.LSP.Types.Lens as J
Expand All @@ -38,7 +40,8 @@ unitTests :: TestTree
unitTests =
testGroup "Unit Tests"
[ cabalParserUnitTests,
codeActionUnitTests
codeActionUnitTests,
completionUnitTests
]

cabalParserUnitTests :: TestTree
Expand Down Expand Up @@ -66,6 +69,41 @@ codeActionUnitTests = testGroup "Code Action Tests"
@?= [("MiT","MIT"),("MiT","MIT-0")]
]

completionUnitTests :: TestTree
completionUnitTests = testGroup "Context Tests"
[
testCase "Empty File - Start" $ do
-- for a completely empty file, the context needs to
-- be toplevel without a specified keyword
getContext (Position 0 0) [""] @?= Just (TopLevel, None),
testCase "Cabal version keyword - no value" $ do
-- on a file, where the "cabal-version:" keyword is already written
-- the context should still be toplevel but the keyword should be recognized
getContext (Position 0 15) ["cabal-version:"] @?= Just (TopLevel, KeyWord "cabal-version:"),
testCase "Cabal version keyword - cursor in keyword" $ do
-- on a file, where the "cabal-version:" keyword is already written
-- but the cursor is in the middle of the keyword, the keyword context
-- is cabal-version since after the keyword, the value needs to be inserted still
getContext (Position 0 5) ["cabal-version:"] @?= Just (TopLevel, KeyWord "cabal-version:"),
testCase "Cabal version keyword - no value, many spaces" $ do
-- on a file, where the "cabal-version:" keyword is already written
-- the context should still be toplevel but the keyword should be recognized
getContext (Position 0 45) ["cabal-version:" <> Text.replicate 50 " "] @?= Just (TopLevel, KeyWord "cabal-version:"),
testCase "Cabal version keyword - no value, many spaces" $ do
-- in the first line of the file, if the keyword
-- has not been written completely, the keyword context
-- should still be None
getContext (Position 0 5) ["cabal"] @?= Just (TopLevel, None),
testCase "Inside Stanza - no keyword" $ do
-- on a file, where the library stanza has been defined
-- but no keyword is defined afterwards, the stanza context should be recognized
getContext (Position 3 0) libraryStanza @?= Just (Stanza "library", None),
testCase "Inside Stanza - keyword, no value" $ do
-- on a file, where the library stanza and a keyword
-- has been defined, the keyword and stanza should be recognized
getContext (Position 4 21) libraryStanza @?= Just (Stanza "library", KeyWord "build-depends:")
]

-- ------------------------------------------------------------------------
-- Integration Tests
-- ------------------------------------------------------------------------
Expand Down Expand Up @@ -180,3 +218,16 @@ runCabalSession subdir =

testDataDir :: FilePath
testDataDir = "test" </> "testdata"

-- ------------------------------------------------------------------------
-- Test Data
-- ------------------------------------------------------------------------
libraryStanza :: [Text.Text]
libraryStanza =
[
"cabal-version: 3.0",
"name: simple-cabal",
"library ",
"",
" build-depends: "
]

0 comments on commit 182189b

Please sign in to comment.