@@ -3,25 +3,28 @@ module Format (tests) where
3
3
4
4
import Control.Monad.IO.Class
5
5
import Data.Aeson
6
+ import qualified Data.ByteString.Lazy as BS
6
7
import qualified Data.Text as T
8
+ import qualified Data.Text.Encoding as T
7
9
import Language.Haskell.LSP.Test
8
10
import Language.Haskell.LSP.Types
9
11
import Test.Hls.Util
10
12
import Test.Tasty
11
13
import Test.Tasty.ExpectedFailure (ignoreTestBecause )
14
+ import Test.Tasty.Golden
12
15
import Test.Tasty.HUnit
13
16
import Test.Hspec.Expectations
14
17
15
18
tests :: TestTree
16
19
tests = testGroup " format document" [
17
- ignoreTestBecause " Broken " $ testCase " works " $ runSession hieCommand fullCaps " test/testdata" $ do
20
+ goldenVsStringDiff " works " goldenGitDiff " test/testdata/Format.formatted_document.hs " $ runSession hieCommand fullCaps " test/testdata" $ do
18
21
doc <- openDoc " Format.hs" " haskell"
19
22
formatDoc doc (FormattingOptions 2 True )
20
- documentContents doc >>= liftIO . ( `shouldBe` formattedDocTabSize2)
21
- , ignoreTestBecause " Broken " $ testCase " works with custom tab size" $ runSession hieCommand fullCaps " test/testdata" $ do
23
+ BS. fromStrict . T. encodeUtf8 <$> documentContents doc
24
+ , goldenVsStringDiff " works with custom tab size" goldenGitDiff " test/testdata/Format.formatted_document_with_tabsize.hs " $ runSession hieCommand fullCaps " test/testdata" $ do
22
25
doc <- openDoc " Format.hs" " haskell"
23
26
formatDoc doc (FormattingOptions 5 True )
24
- documentContents doc >>= liftIO . ( `shouldBe` formattedDocTabSize5)
27
+ BS. fromStrict . T. encodeUtf8 <$> documentContents doc
25
28
, rangeTests
26
29
, providerTests
27
30
, stylishHaskellTests
@@ -31,14 +34,14 @@ tests = testGroup "format document" [
31
34
32
35
rangeTests :: TestTree
33
36
rangeTests = testGroup " format range" [
34
- ignoreTestBecause " Broken " $ testCase " works " $ runSession hieCommand fullCaps " test/testdata" $ do
37
+ goldenVsStringDiff " works " goldenGitDiff " test/testdata/Format.formatted_range.hs " $ runSession hieCommand fullCaps " test/testdata" $ do
35
38
doc <- openDoc " Format.hs" " haskell"
36
39
formatRange doc (FormattingOptions 2 True ) (Range (Position 1 0 ) (Position 3 10 ))
37
- documentContents doc >>= liftIO . ( `shouldBe` formattedRangeTabSize2)
38
- , ignoreTestBecause " Broken " $ testCase " works with custom tab size" $ runSession hieCommand fullCaps " test/testdata" $ do
40
+ BS. fromStrict . T. encodeUtf8 <$> documentContents doc
41
+ , goldenVsStringDiff " works with custom tab size" goldenGitDiff " test/testdata/Format.formatted_range_with_tabsize.hs " $ runSession hieCommand fullCaps " test/testdata" $ do
39
42
doc <- openDoc " Format.hs" " haskell"
40
43
formatRange doc (FormattingOptions 5 True ) (Range (Position 4 0 ) (Position 7 19 ))
41
- documentContents doc >>= liftIO . ( `shouldBe` formattedRangeTabSize5)
44
+ BS. fromStrict . T. encodeUtf8 <$> documentContents doc
42
45
]
43
46
44
47
providerTests :: TestTree
@@ -58,7 +61,7 @@ providerTests = testGroup "formatting provider" [
58
61
59
62
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig " brittany" ))
60
63
formatDoc doc (FormattingOptions 2 True )
61
- documentContents doc >>= liftIO . (`shouldBe` formattedDocTabSize2 )
64
+ documentContents doc >>= liftIO . (`shouldBe` formattedBrittany )
62
65
63
66
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig " floskell" ))
64
67
formatDoc doc (FormattingOptions 2 True )
@@ -71,84 +74,58 @@ providerTests = testGroup "formatting provider" [
71
74
72
75
stylishHaskellTests :: TestTree
73
76
stylishHaskellTests = testGroup " stylish-haskell" [
74
- testCase " formats a file " $ runSession hieCommand fullCaps " test/testdata" $ do
77
+ goldenVsStringDiff " formats a document " goldenGitDiff " test/testdata/StylishHaksell.formatted_document.hs " $ runSession hieCommand fullCaps " test/testdata" $ do
75
78
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig " stylish-haskell" ))
76
79
doc <- openDoc " StylishHaskell.hs" " haskell"
77
80
formatDoc doc (FormattingOptions 2 True )
78
- 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
81
+ BS. fromStrict . T. encodeUtf8 <$> documentContents doc
82
+ , goldenVsStringDiff " formats a range" goldenGitDiff " test/testdata/StylishHaksell.formatted_range.hs" $ runSession hieCommand fullCaps " test/testdata" $ do
89
83
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig " stylish-haskell" ))
90
84
doc <- openDoc " StylishHaskell.hs" " haskell"
91
85
formatRange doc (FormattingOptions 2 True ) (Range (Position 0 0 ) (Position 2 21 ))
92
- 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 "
86
+ BS. fromStrict . T. encodeUtf8 <$> documentContents doc
102
87
]
103
88
104
89
brittanyTests :: TestTree
105
90
brittanyTests = testGroup " brittany" [
106
- ignoreTestBecause " Broken " $ testCase " formats a document with LF endings" $ runSession hieCommand fullCaps " test/testdata" $ do
91
+ goldenVsStringDiff " formats a document with LF endings" goldenGitDiff " test/testdata/BrittanyLF.formatted_document.hs " $ runSession hieCommand fullCaps " test/testdata" $ do
107
92
doc <- openDoc " BrittanyLF.hs" " haskell"
108
- let opts = DocumentFormattingParams doc (FormattingOptions 4 True ) Nothing
109
- ResponseMessage _ _ (Right edits) <- request TextDocumentFormatting opts
110
- liftIO $ edits `shouldBe` [TextEdit (Range (Position 0 0 ) (Position 3 0 ))
111
- " foo :: Int -> String -> IO ()\n foo x y = do\n print x\n return 42\n " ]
93
+ formatDoc doc (FormattingOptions 4 True )
94
+ BS. fromStrict . T. encodeUtf8 <$> documentContents doc
112
95
113
- , ignoreTestBecause " Broken " $ testCase " formats a document with CRLF endings" $ runSession hieCommand fullCaps " test/testdata" $ do
96
+ , goldenVsStringDiff " formats a document with CRLF endings" goldenGitDiff " test/testdata/BrittanyCRLF.formatted_document.hs " $ runSession hieCommand fullCaps " test/testdata" $ do
114
97
doc <- openDoc " BrittanyCRLF.hs" " haskell"
115
- let opts = DocumentFormattingParams doc (FormattingOptions 4 True ) Nothing
116
- ResponseMessage _ _ (Right edits) <- request TextDocumentFormatting opts
117
- liftIO $ edits `shouldBe` [TextEdit (Range (Position 0 0 ) (Position 3 0 ))
118
- " foo :: Int -> String -> IO ()\n foo x y = do\n print x\n return 42\n " ]
98
+ formatDoc doc (FormattingOptions 4 True )
99
+ BS. fromStrict . T. encodeUtf8 <$> documentContents doc
119
100
120
- , ignoreTestBecause " Broken " $ testCase " formats a range with LF endings" $ runSession hieCommand fullCaps " test/testdata" $ do
101
+ , goldenVsStringDiff " formats a range with LF endings" goldenGitDiff " test/testdata/BrittanyLF.formatted_range.hs " $ runSession hieCommand fullCaps " test/testdata" $ do
121
102
doc <- openDoc " BrittanyLF.hs" " haskell"
122
103
let range = Range (Position 1 0 ) (Position 2 22 )
123
- opts = DocumentRangeFormattingParams doc range (FormattingOptions 4 True ) Nothing
124
- ResponseMessage _ _ (Right edits) <- request TextDocumentRangeFormatting opts
125
- liftIO $ edits `shouldBe` [TextEdit (Range (Position 1 0 ) (Position 3 0 ))
126
- " foo x y = do\n print x\n return 42\n " ]
104
+ formatRange doc (FormattingOptions 4 True ) range
105
+ BS. fromStrict . T. encodeUtf8 <$> documentContents doc
127
106
128
- , ignoreTestBecause " Broken " $ testCase " formats a range with CRLF endings" $ runSession hieCommand fullCaps " test/testdata" $ do
107
+ , goldenVsStringDiff " formats a range with CRLF endings" goldenGitDiff " test/testdata/BrittanyCRLF.formatted_range.hs " $ runSession hieCommand fullCaps " test/testdata" $ do
129
108
doc <- openDoc " BrittanyCRLF.hs" " haskell"
130
109
let range = Range (Position 1 0 ) (Position 2 22 )
131
- opts = DocumentRangeFormattingParams doc range (FormattingOptions 4 True ) Nothing
132
- ResponseMessage _ _ (Right edits) <- request TextDocumentRangeFormatting opts
133
- liftIO $ edits `shouldBe` [TextEdit (Range (Position 1 0 ) (Position 3 0 ))
134
- " foo x y = do\n print x\n return 42\n " ]
110
+ formatRange doc (FormattingOptions 4 True ) range
111
+ BS. fromStrict . T. encodeUtf8 <$> documentContents doc
135
112
]
136
113
137
114
ormoluTests :: TestTree
138
115
ormoluTests = testGroup " ormolu" [
139
- ignoreTestBecause " Broken " $ testCase " formats correctly " $ runSession hieCommand fullCaps " test/testdata" $ do
116
+ goldenVsStringDiff " formats correctly " goldenGitDiff ( " test/testdata/Format.ormolu. " ++ ormoluGoldenSuffix ++ " .hs " ) $ runSession hieCommand fullCaps " test/testdata" $ do
140
117
let formatLspConfig provider =
141
118
object [ " languageServerHaskell" .= object [" formattingProvider" .= (provider :: Value )] ]
142
119
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig " ormolu" ))
143
120
doc <- openDoc " Format.hs" " haskell"
144
121
formatDoc doc (FormattingOptions 2 True )
145
- docContent <- documentContents doc
146
- let formatted = liftIO $ docContent `shouldBe` formattedOrmolu
147
- case ghcVersion of
148
- GHC88 -> formatted
149
- GHC86 -> formatted
150
- _ -> liftIO $ docContent `shouldBe` unchangedOrmolu
122
+ BS. fromStrict . T. encodeUtf8 <$> documentContents doc
151
123
]
124
+ where
125
+ ormoluGoldenSuffix = case ghcVersion of
126
+ GHC88 -> " formatted"
127
+ GHC86 -> " formatted"
128
+ _ -> " unchanged"
152
129
153
130
154
131
formatLspConfig :: Value -> Value
@@ -157,9 +134,12 @@ formatLspConfig provider = object [ "languageServerHaskell" .= object ["formatti
157
134
formatConfig :: Value -> SessionConfig
158
135
formatConfig provider = defaultConfig { lspConfig = Just (formatLspConfig provider) }
159
136
137
+ goldenGitDiff :: FilePath -> FilePath -> [String ]
138
+ goldenGitDiff fRef fNew = [" git" , " diff" , " --no-index" , " --text" , " --exit-code" , fRef, fNew]
160
139
161
- formattedDocTabSize2 :: T. Text
162
- formattedDocTabSize2 =
140
+
141
+ formattedBrittany :: T. Text
142
+ formattedBrittany =
163
143
" module Format where\n \
164
144
\foo :: Int -> Int\n \
165
145
\foo 3 = 2\n \
@@ -170,44 +150,6 @@ formattedDocTabSize2 =
170
150
\ return \" asdf\"\n\n \
171
151
\data Baz = Baz { a :: Int, b :: String }\n\n "
172
152
173
- formattedDocTabSize5 :: T. Text
174
- formattedDocTabSize5 =
175
- " module Format where\n \
176
- \foo :: Int -> Int\n \
177
- \foo 3 = 2\n \
178
- \foo x = x\n \
179
- \bar :: String -> IO String\n \
180
- \bar s = do\n \
181
- \ x <- return \" hello\"\n \
182
- \ return \" asdf\"\n\n \
183
- \data Baz = Baz { a :: Int, b :: String }\n\n "
184
-
185
- formattedRangeTabSize2 :: T. Text
186
- formattedRangeTabSize2 =
187
- " module Format where\n \
188
- \foo :: Int -> Int\n \
189
- \foo 3 = 2\n \
190
- \foo x = x\n \
191
- \bar :: String -> IO String\n \
192
- \bar s = do\n \
193
- \ x <- return \" hello\"\n \
194
- \ return \" asdf\"\n \
195
- \\n \
196
- \data Baz = Baz { a :: Int, b :: String }\n\n "
197
-
198
- formattedRangeTabSize5 :: T. Text
199
- formattedRangeTabSize5 =
200
- " module Format where\n \
201
- \foo :: Int -> Int\n \
202
- \foo 3 = 2\n \
203
- \foo x = x\n \
204
- \bar :: String -> IO String\n \
205
- \bar s = do\n \
206
- \ x <- return \" hello\"\n \
207
- \ return \" asdf\"\n \
208
- \\n \
209
- \data Baz = Baz { a :: Int, b :: String }\n\n "
210
-
211
153
formattedFloskell :: T. Text
212
154
formattedFloskell =
213
155
" module Format where\n \
@@ -235,30 +177,3 @@ formattedBrittanyPostFloskell =
235
177
\ x <- return \" hello\"\n \
236
178
\ return \" asdf\"\n\n \
237
179
\data Baz = Baz { a :: Int, b :: String }\n\n "
238
-
239
- formattedOrmolu :: T. Text
240
- formattedOrmolu =
241
- " module Format where\n \
242
- \\n \
243
- \foo :: Int -> Int\n \
244
- \foo 3 = 2\n \
245
- \foo x = x\n \
246
- \\n \
247
- \bar :: String -> IO String\n \
248
- \bar s = do\n \
249
- \ x <- return \" hello\"\n \
250
- \ return \" asdf\"\n\n \
251
- \data Baz = Baz {a :: Int, b :: String}\n "
252
-
253
- unchangedOrmolu :: T. Text
254
- unchangedOrmolu =
255
- " module Format where\n \
256
- \foo :: Int -> Int\n \
257
- \foo 3 = 2\n \
258
- \foo x = x\n \
259
- \bar :: String -> IO String\n \
260
- \bar s = do\n \
261
- \ x <- return \" hello\"\n \
262
- \ return \" asdf\"\n \
263
- \\n \
264
- \data Baz = Baz { a :: Int, b :: String }\n\n "
0 commit comments