Skip to content

Commit 996d6a1

Browse files
committed
Add unit tests for code action utilities
1 parent fcd4bff commit 996d6a1

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

plugins/hls-cabal-plugin/hls-cabal-plugin.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ test-suite tests
7070
main-is: Main.hs
7171
build-depends:
7272
, base
73+
, bytestring
7374
, filepath
7475
, ghcide
7576
, hls-cabal-plugin

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{-# LANGUAGE DuplicateRecordFields #-}
2+
{-# LANGUAGE OverloadedStrings #-}
23
{-# LANGUAGE TupleSections #-}
3-
{-# LANGUAGE OverloadedStrings #-}
44
module Ide.Plugin.Cabal.Diagnostics
55
( errorDiagnostic
66
, warningDiagnostic

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ licenseErrorAction
3333
-- ^ Output of 'Ide.Plugin.Cabal.Diag.errorDiagnostic'
3434
-> Maybe CodeAction
3535
licenseErrorAction uri diag =
36-
mkCodeAction <$> licenseErrorSuggestion diag
36+
mkCodeAction <$> licenseErrorSuggestion (_message diag)
3737
where
3838
mkCodeAction (original, suggestion) =
3939
let
@@ -52,17 +52,17 @@ licenseErrorAction uri diag =
5252
edit = WorkspaceEdit (Just $ Map.singleton uri $ List tedit) Nothing Nothing
5353
in CodeAction title (Just CodeActionQuickFix) (Just $ List []) Nothing Nothing (Just edit) Nothing Nothing
5454

55-
-- | Given a diagnostic returned by 'Ide.Plugin.Cabal.Diag.errorDiagnostic',
55+
-- | Given an error message returned by 'Ide.Plugin.Cabal.Diag.errorDiagnostic',
5656
-- if it represents an "Unknown SPDX license identifier"-error along
5757
-- with a suggestion then return the suggestion (after the "Do you mean"-text)
5858
-- along with the incorrect identifier.
5959
licenseErrorSuggestion
60-
:: Diagnostic
60+
:: T.Text
6161
-- ^ Output of 'Ide.Plugin.Cabal.Diag.errorDiagnostic'
6262
-> Maybe (T.Text, T.Text)
6363
-- ^ (Original (incorrect) license identifier, suggested replacement)
64-
licenseErrorSuggestion diag =
65-
mSuggestion (_message diag) >>= \case
64+
licenseErrorSuggestion message =
65+
mSuggestion message >>= \case
6666
[original, suggestion] -> Just (original, suggestion)
6767
_ -> Nothing
6868
where

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

+28-6
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ module Main
55
( main
66
) where
77

8-
import Control.Lens ((^.))
9-
import Data.Either (isRight)
8+
import Control.Lens ((^.))
9+
import qualified Data.ByteString as BS
10+
import Data.Either (isRight)
1011
import Data.Function
11-
import qualified Data.Text as Text
12+
import qualified Data.Text as Text
1213
import Development.IDE.Types.Logger
1314
import Ide.Plugin.Cabal
14-
import qualified Ide.Plugin.Cabal.Parse as Lib
15-
import qualified Language.LSP.Types.Lens as J
15+
import Ide.Plugin.Cabal.LicenseSuggest (licenseErrorSuggestion)
16+
import qualified Ide.Plugin.Cabal.Parse as Lib
17+
import qualified Language.LSP.Types.Lens as J
1618
import System.FilePath
1719
import Test.Hls
18-
import qualified Data.ByteString as BS
20+
1921

2022
cabalPlugin :: Recorder (WithPriority Log) -> PluginDescriptor IdeState
2123
cabalPlugin recorder = descriptor recorder "cabal"
@@ -51,13 +53,33 @@ initialiseRecorder False = do
5153
unitTests :: TestTree
5254
unitTests =
5355
testGroup "Unit Tests"
56+
[ cabalParserUnitTests,
57+
codeActionUnitTests
58+
]
59+
60+
cabalParserUnitTests :: TestTree
61+
cabalParserUnitTests = testGroup "Parsing Cabal"
5462
[ testCase "Simple Parsing works" $ do
5563
(warnings, pm) <- Lib.parseCabalFileContents =<< BS.readFile (testDataDir </> "simple.cabal")
5664
liftIO $ do
5765
null warnings @? "Found unexpected warnings"
5866
isRight pm @? "Failed to parse GenericPackageDescription"
5967
]
6068

69+
codeActionUnitTests :: TestTree
70+
codeActionUnitTests = testGroup "Code Action Tests"
71+
[ testCase "Unknown format" $ do
72+
-- the message has the wrong format
73+
licenseErrorSuggestion "Unknown license identifier: 'BSD3' Do you mean BSD-3-Clause?" @?= Nothing,
74+
75+
testCase "BSD-3-Clause" $ do
76+
licenseErrorSuggestion "Unknown SPDX license identifier: 'BSD3' Do you mean BSD-3-Clause?" @?= Just ("BSD3", "BSD-3-Clause"),
77+
78+
testCase "MIT" $ do
79+
-- contains no suggestion
80+
licenseErrorSuggestion "Unknown SPDX license identifier: 'MIT3'" @?= Nothing
81+
]
82+
6183
-- ------------------------------------------------------------------------
6284
-- Integration Tests
6385
-- ------------------------------------------------------------------------

0 commit comments

Comments
 (0)