Skip to content

Commit e4f8df8

Browse files
committed
Introduce golden testing for stylish-haskell formatter
1 parent 84b0073 commit e4f8df8

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

haskell-language-server.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ test-suite func-test
246246
, ghcide:ghcide-test-preprocessor
247247
build-depends: base >=4.7 && <5
248248
, aeson
249+
, bytestring
249250
, data-default
250251
, directory
251252
, filepath
@@ -258,6 +259,7 @@ test-suite func-test
258259
, tasty
259260
, tasty-ant-xml >= 1.1.6
260261
, tasty-expected-failure
262+
, tasty-golden
261263
, tasty-hunit
262264
, tasty-rerun
263265
, text

test/functional/Format.hs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ module Format (tests) where
33

44
import Control.Monad.IO.Class
55
import Data.Aeson
6+
import qualified Data.ByteString.Lazy as BS
67
import qualified Data.Text as T
8+
import qualified Data.Text.Encoding as T
79
import Language.Haskell.LSP.Test
810
import Language.Haskell.LSP.Types
911
import Test.Hls.Util
1012
import Test.Tasty
1113
import Test.Tasty.ExpectedFailure (ignoreTestBecause)
14+
import Test.Tasty.Golden
1215
import Test.Tasty.HUnit
1316
import Test.Hspec.Expectations
1417

@@ -71,34 +74,18 @@ providerTests = testGroup "formatting provider" [
7174

7275
stylishHaskellTests :: TestTree
7376
stylishHaskellTests = 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

10491
brittanyTests :: TestTree
@@ -157,6 +144,9 @@ formatLspConfig provider = object [ "languageServerHaskell" .= object ["formatti
157144
formatConfig :: Value -> SessionConfig
158145
formatConfig 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

161151
formattedDocTabSize2 :: T.Text
162152
formattedDocTabSize2 =
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Data.Char
2+
import qualified Data.List
3+
import Data.String
4+
5+
bar :: Maybe (Either String Integer) -> Integer
6+
bar Nothing = 0
7+
bar (Just (Left _)) = 0
8+
bar (Just (Right x)) = x
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Data.Char
2+
import qualified Data.List
3+
import Data.String
4+
5+
bar :: Maybe (Either String Integer) -> Integer
6+
bar Nothing = 0
7+
bar (Just (Left _)) = 0
8+
bar (Just (Right x)) = x

0 commit comments

Comments
 (0)