4
4
{-# LANGUAGE TypeOperators #-}
5
5
{-# LANGUAGE ViewPatterns #-}
6
6
7
- module Main (main ) where
7
+ module Main
8
+ ( main
9
+ ) where
8
10
9
- import qualified Data.ByteString.Lazy as LBS
10
11
import Data.Foldable (find , forM_ )
11
12
import Data.Text (Text )
12
13
import qualified Data.Text as T
13
- import Data.Text.Encoding (encodeUtf8 )
14
14
import qualified Ide.Plugin.RefineImports as RefineImports
15
15
import System.FilePath ((<.>) , (</>) )
16
16
import Test.Hls
@@ -23,24 +23,17 @@ main = defaultTestRunner $
23
23
, codeLensGoldenTest " UsualCase" 1
24
24
]
25
25
26
- plugin :: PluginDescriptor IdeState
27
- plugin = RefineImports. descriptor " refineImports"
26
+ refineImportsPlugin :: PluginDescriptor IdeState
27
+ refineImportsPlugin = RefineImports. descriptor " refineImports"
28
28
29
29
-- code action tests
30
30
31
31
codeActionGoldenTest :: FilePath -> Int -> Int -> TestTree
32
- codeActionGoldenTest fp l c = goldenGitDiff (fp <> " (golden)" ) goldenFilePath $
33
- runSessionWithServer plugin testDataDir $ do
34
- doc <- openDoc hsFilePath " haskell"
35
- actions <- getCodeActions doc (pointRange l c)
36
- case find ((== Just " Refine all imports" ) . caTitle) actions of
37
- Just (InR x) -> do
38
- executeCodeAction x
39
- LBS. fromStrict . encodeUtf8 <$> documentContents doc
40
- _ -> liftIO $ assertFailure " Unable to find CodeAction"
41
- where
42
- hsFilePath = fp <.> " hs"
43
- goldenFilePath = testDataDir </> fp <.> " expected" <.> " hs"
32
+ codeActionGoldenTest fp l c = goldenWithRefineImports fp $ \ doc -> do
33
+ actions <- getCodeActions doc (pointRange l c)
34
+ case find ((== Just " Refine all imports" ) . caTitle) actions of
35
+ Just (InR x) -> executeCodeAction x
36
+ _ -> liftIO $ assertFailure " Unable to find CodeAction"
44
37
45
38
caTitle :: (Command |? CodeAction ) -> Maybe Text
46
39
caTitle (InR CodeAction {_title}) = Just _title
@@ -50,16 +43,10 @@ caTitle _ = Nothing
50
43
-- code lens tests
51
44
52
45
codeLensGoldenTest :: FilePath -> Int -> TestTree
53
- codeLensGoldenTest fp codeLensIdx = goldenGitDiff (fp <> " (golden)" ) goldenFilePath $
54
- runSessionWithServer plugin testDataDir $ do
55
- doc <- openDoc hsFilePath " haskell"
56
- codeLens <- (!! codeLensIdx) <$> getCodeLensesBy isRefineImports doc
57
- mapM_ executeCmd
58
- [c | CodeLens {_command = Just c} <- [codeLens]]
59
- LBS. fromStrict . encodeUtf8 <$> documentContents doc
60
- where
61
- hsFilePath = fp <.> " hs"
62
- goldenFilePath = testDataDir </> fp <.> " expected" <.> " hs"
46
+ codeLensGoldenTest fp codeLensIdx = goldenWithRefineImports fp $ \ doc -> do
47
+ codeLens <- (!! codeLensIdx) <$> getCodeLensesBy isRefineImports doc
48
+ mapM_ executeCmd
49
+ [c | CodeLens {_command = Just c} <- [codeLens]]
63
50
64
51
getCodeLensesBy :: (CodeLens -> Bool ) -> TextDocumentIdentifier -> Session [CodeLens ]
65
52
getCodeLensesBy f doc = filter f <$> getCodeLenses doc
@@ -79,6 +66,9 @@ executeCmd cmd = do
79
66
80
67
-- helpers
81
68
69
+ goldenWithRefineImports :: FilePath -> (TextDocumentIdentifier -> Session () ) -> TestTree
70
+ goldenWithRefineImports fp = goldenWithHaskellDoc refineImportsPlugin (fp <> " (golden)" ) testDataDir fp " expected" " hs"
71
+
82
72
testDataDir :: String
83
73
testDataDir = " test" </> " testdata"
84
74
0 commit comments