Skip to content

Commit

Permalink
Add unit tests for code action utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor committed Nov 19, 2022
1 parent fcd4bff commit 996d6a1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
1 change: 1 addition & 0 deletions plugins/hls-cabal-plugin/hls-cabal-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ test-suite tests
main-is: Main.hs
build-depends:
, base
, bytestring
, filepath
, ghcide
, hls-cabal-plugin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE OverloadedStrings #-}
module Ide.Plugin.Cabal.Diagnostics
( errorDiagnostic
, warningDiagnostic
Expand Down
10 changes: 5 additions & 5 deletions plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/LicenseSuggest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ licenseErrorAction
-- ^ Output of 'Ide.Plugin.Cabal.Diag.errorDiagnostic'
-> Maybe CodeAction
licenseErrorAction uri diag =
mkCodeAction <$> licenseErrorSuggestion diag
mkCodeAction <$> licenseErrorSuggestion (_message diag)
where
mkCodeAction (original, suggestion) =
let
Expand All @@ -52,17 +52,17 @@ licenseErrorAction uri diag =
edit = WorkspaceEdit (Just $ Map.singleton uri $ List tedit) Nothing Nothing
in CodeAction title (Just CodeActionQuickFix) (Just $ List []) Nothing Nothing (Just edit) Nothing Nothing

-- | Given a diagnostic returned by 'Ide.Plugin.Cabal.Diag.errorDiagnostic',
-- | Given an error message returned by 'Ide.Plugin.Cabal.Diag.errorDiagnostic',
-- if it represents an "Unknown SPDX license identifier"-error along
-- with a suggestion then return the suggestion (after the "Do you mean"-text)
-- along with the incorrect identifier.
licenseErrorSuggestion
:: Diagnostic
:: T.Text
-- ^ Output of 'Ide.Plugin.Cabal.Diag.errorDiagnostic'
-> Maybe (T.Text, T.Text)
-- ^ (Original (incorrect) license identifier, suggested replacement)
licenseErrorSuggestion diag =
mSuggestion (_message diag) >>= \case
licenseErrorSuggestion message =
mSuggestion message >>= \case
[original, suggestion] -> Just (original, suggestion)
_ -> Nothing
where
Expand Down
34 changes: 28 additions & 6 deletions plugins/hls-cabal-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ module Main
( main
) where

import Control.Lens ((^.))
import Data.Either (isRight)
import Control.Lens ((^.))
import qualified Data.ByteString as BS
import Data.Either (isRight)
import Data.Function
import qualified Data.Text as Text
import qualified Data.Text as Text
import Development.IDE.Types.Logger
import Ide.Plugin.Cabal
import qualified Ide.Plugin.Cabal.Parse as Lib
import qualified Language.LSP.Types.Lens as J
import Ide.Plugin.Cabal.LicenseSuggest (licenseErrorSuggestion)
import qualified Ide.Plugin.Cabal.Parse as Lib
import qualified Language.LSP.Types.Lens as J
import System.FilePath
import Test.Hls
import qualified Data.ByteString as BS


cabalPlugin :: Recorder (WithPriority Log) -> PluginDescriptor IdeState
cabalPlugin recorder = descriptor recorder "cabal"
Expand Down Expand Up @@ -51,13 +53,33 @@ initialiseRecorder False = do
unitTests :: TestTree
unitTests =
testGroup "Unit Tests"
[ cabalParserUnitTests,
codeActionUnitTests
]

cabalParserUnitTests :: TestTree
cabalParserUnitTests = testGroup "Parsing Cabal"
[ testCase "Simple Parsing works" $ do
(warnings, pm) <- Lib.parseCabalFileContents =<< BS.readFile (testDataDir </> "simple.cabal")
liftIO $ do
null warnings @? "Found unexpected warnings"
isRight pm @? "Failed to parse GenericPackageDescription"
]

codeActionUnitTests :: TestTree
codeActionUnitTests = testGroup "Code Action Tests"
[ testCase "Unknown format" $ do
-- the message has the wrong format
licenseErrorSuggestion "Unknown license identifier: 'BSD3' Do you mean BSD-3-Clause?" @?= Nothing,

testCase "BSD-3-Clause" $ do
licenseErrorSuggestion "Unknown SPDX license identifier: 'BSD3' Do you mean BSD-3-Clause?" @?= Just ("BSD3", "BSD-3-Clause"),

testCase "MIT" $ do
-- contains no suggestion
licenseErrorSuggestion "Unknown SPDX license identifier: 'MIT3'" @?= Nothing
]

-- ------------------------------------------------------------------------
-- Integration Tests
-- ------------------------------------------------------------------------
Expand Down

0 comments on commit 996d6a1

Please sign in to comment.