@@ -3,12 +3,15 @@ module Format (tests) where
33
44import Control.Monad.IO.Class
55import Data.Aeson
6+ import qualified Data.ByteString.Lazy as BS
67import qualified Data.Text as T
8+ import qualified Data.Text.Encoding as T
79import Language.Haskell.LSP.Test
810import Language.Haskell.LSP.Types
911import Test.Hls.Util
1012import Test.Tasty
1113import Test.Tasty.ExpectedFailure (ignoreTestBecause )
14+ import Test.Tasty.Golden
1215import Test.Tasty.HUnit
1316import Test.Hspec.Expectations
1417
@@ -71,34 +74,18 @@ providerTests = testGroup "formatting provider" [
7174
7275stylishHaskellTests :: TestTree
7376stylishHaskellTests = testGroup " stylish-haskell" [
74- testCase " formats a file " $ runSession hieCommand fullCaps " test/testdata" $ do
77+ goldenVsStringDiff " formats a document " goldenGitDiff " test/testdata/StylishHaksell.format_document.hs " $ runSession hieCommand fullCaps " test/testdata" $ do
7578 sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig " stylish-haskell" ))
7679 doc <- openDoc " StylishHaskell.hs" " haskell"
7780 formatDoc doc (FormattingOptions 2 True )
7881 contents <- documentContents doc
79- liftIO $ contents `shouldBe`
80- " import Data.Char\n \
81- \import qualified Data.List\n \
82- \import Data.String\n \
83- \\n \
84- \bar :: Maybe (Either String Integer) -> Integer\n \
85- \bar Nothing = 0\n \
86- \bar (Just (Left _)) = 0\n \
87- \bar (Just (Right x)) = x\n "
88- , testCase " formats a range" $ runSession hieCommand fullCaps " test/testdata" $ do
82+ return $ BS. fromStrict $ T. encodeUtf8 contents
83+ , goldenVsStringDiff " formats a range" goldenGitDiff " test/testdata/StylishHaksell.format_range.hs" $ runSession hieCommand fullCaps " test/testdata" $ do
8984 sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig " stylish-haskell" ))
9085 doc <- openDoc " StylishHaskell.hs" " haskell"
9186 formatRange doc (FormattingOptions 2 True ) (Range (Position 0 0 ) (Position 2 21 ))
9287 contents <- documentContents doc
93- liftIO $ contents `shouldBe`
94- " import Data.Char\n \
95- \import qualified Data.List\n \
96- \import Data.String\n \
97- \\n \
98- \bar :: Maybe (Either String Integer) -> Integer\n \
99- \bar Nothing = 0\n \
100- \bar (Just (Left _)) = 0\n \
101- \bar (Just (Right x)) = x\n "
88+ return $ BS. fromStrict $ T. encodeUtf8 contents
10289 ]
10390
10491brittanyTests :: TestTree
@@ -157,6 +144,9 @@ formatLspConfig provider = object [ "languageServerHaskell" .= object ["formatti
157144formatConfig :: Value -> SessionConfig
158145formatConfig provider = defaultConfig { lspConfig = Just (formatLspConfig provider) }
159146
147+ goldenGitDiff :: FilePath -> FilePath -> [String ]
148+ goldenGitDiff fRef fNew = [" git" , " diff" , " --no-index" , " --text" , " --exit-code" , fRef, fNew]
149+
160150
161151formattedDocTabSize2 :: T. Text
162152formattedDocTabSize2 =
0 commit comments