-
-
Notifications
You must be signed in to change notification settings - Fork 430
Expand file tree
/
Copy pathMain.hs
More file actions
68 lines (62 loc) · 3.24 KB
/
Main.hs
File metadata and controls
68 lines (62 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{-# LANGUAGE OverloadedStrings #-}
module Main
( main
) where
import Control.Lens ((^.))
import Data.Aeson
import qualified Data.Aeson.KeyMap as KM
import Data.Functor
import qualified Data.Map as M
import qualified Data.Text as T
import Ide.Plugin.Config
import qualified Ide.Plugin.Fourmolu as Fourmolu
import qualified Language.LSP.Protocol.Lens as L
import Language.LSP.Protocol.Types
import Language.LSP.Test
import System.FilePath
import Test.Hls
main :: IO ()
main = defaultTestRunner tests
fourmoluPlugin :: PluginTestDescriptor Fourmolu.LogEvent
fourmoluPlugin = mkPluginTestDescriptor Fourmolu.descriptor "fourmolu"
tests :: TestTree
tests =
testGroup "fourmolu" $
[False, True] <&> \cli ->
testGroup
(if cli then "cli" else "lib")
[ goldenWithFourmolu cli "formats correctly" "Fourmolu" "formatted" $ \doc -> do
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
, goldenWithFourmolu cli "formats imports correctly" "Fourmolu2" "formatted" $ \doc -> do
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
, goldenWithFourmolu cli "uses correct operator fixities" "Fourmolu3" "formatted" $ \doc -> do
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
, testCase "error message contains stderr output" $ do
let cliConfig = def {
formattingProvider = "fourmolu",
plugins = M.fromList [("fourmolu", def { plcConfig = KM.fromList ["external" .= True] })]
}
runSessionWithServer cliConfig fourmoluPlugin testDataDir $ do
doc <- openDoc "FormatError.hs" "haskell"
void waitForBuildQueue
resp <- request SMethod_TextDocumentFormatting $
DocumentFormattingParams Nothing doc (FormattingOptions 4 True Nothing Nothing Nothing)
liftIO $ case resp ^. L.result of
Left err -> do
let msg = err ^. L.message
-- Verify the error message structure:
-- 1. Contains the exit code prefix (base message intact)
assertBool ("Expected exit code prefix, got: " <> T.unpack msg)
("failed with exit code" `T.isInfixOf` msg)
-- 2. Contains a stable parse-error phrase from formatter stderr
assertBool ("Expected parse error details from stderr, got: " <> T.unpack msg)
("parse error on input" `T.isInfixOf` msg)
Right _ ->
assertFailure "Expected formatting to fail on unparsable file"
]
goldenWithFourmolu :: Bool -> TestName -> FilePath -> FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree
goldenWithFourmolu cli title path desc = goldenWithHaskellDocFormatter def fourmoluPlugin "fourmolu" conf title testDataDir path desc "hs"
where
conf = def{plcConfig = KM.fromList ["external" .= cli]}
testDataDir :: FilePath
testDataDir = "plugins" </> "hls-fourmolu-plugin" </> "test" </> "testdata"