This repository was archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathFormatSpec.hs
206 lines (180 loc) · 7.53 KB
/
FormatSpec.hs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
{-# LANGUAGE OverloadedStrings #-}
module FormatSpec where
import Control.Monad.IO.Class
import Data.Aeson
import qualified Data.Text as T
import Language.Haskell.LSP.Test
import Language.Haskell.LSP.Types
import Test.Hspec
import TestUtils
spec :: Spec
spec = do
describe "format document" $ do
it "works" $ runSession hieCommand fullCaps "test/testdata" $ do
doc <- openDoc "Format.hs" "haskell"
formatDoc doc (FormattingOptions 2 True)
documentContents doc >>= liftIO . (`shouldBe` formattedDocTabSize2)
it "works with custom tab size" $ runSession hieCommand fullCaps "test/testdata" $ do
doc <- openDoc "Format.hs" "haskell"
formatDoc doc (FormattingOptions 5 True)
documentContents doc >>= liftIO . (`shouldBe` formattedDocTabSize5)
describe "format range" $ do
it "works" $ runSession hieCommand fullCaps "test/testdata" $ do
doc <- openDoc "Format.hs" "haskell"
formatRange doc (FormattingOptions 2 True) (Range (Position 1 0) (Position 3 10))
documentContents doc >>= liftIO . (`shouldBe` formattedRangeTabSize2)
it "works with custom tab size" $ runSession hieCommand fullCaps "test/testdata" $ do
doc <- openDoc "Format.hs" "haskell"
formatRange doc (FormattingOptions 5 True) (Range (Position 4 0) (Position 7 19))
documentContents doc >>= liftIO . (`shouldBe` formattedRangeTabSize5)
describe "formatting provider" $ do
let formatConfig provider = defaultConfig { lspConfig = Just (formatLspConfig provider) }
it "respects none" $ runSessionWithConfig (formatConfig "none") hieCommand fullCaps "test/testdata" $ do
doc <- openDoc "Format.hs" "haskell"
orig <- documentContents doc
formatDoc doc (FormattingOptions 2 True)
documentContents doc >>= liftIO . (`shouldBe` orig)
formatRange doc (FormattingOptions 2 True) (Range (Position 1 0) (Position 3 10))
documentContents doc >>= liftIO . (`shouldBe` orig)
it "can change on the fly" $ runSession hieCommand fullCaps "test/testdata" $ do
doc <- openDoc "Format.hs" "haskell"
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "brittany"))
formatDoc doc (FormattingOptions 2 True)
documentContents doc >>= liftIO . (`shouldBe` formattedDocTabSize2)
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "floskell"))
formatDoc doc (FormattingOptions 2 True)
documentContents doc >>= liftIO . (`shouldBe` formattedFloskell)
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "brittany"))
formatDoc doc (FormattingOptions 2 True)
documentContents doc >>= liftIO . (`shouldBe` formattedBrittanyPostFloskell)
describe "brittany" $ do
it "formats a document with LF endings" $ runSession hieCommand fullCaps "test/testdata" $ do
doc <- openDoc "BrittanyLF.hs" "haskell"
let opts = DocumentFormattingParams doc (FormattingOptions 4 True) Nothing
ResponseMessage _ _ (Just edits) _ <- request TextDocumentFormatting opts
liftIO $ edits `shouldBe` [TextEdit (Range (Position 0 0) (Position 3 0))
"foo :: Int -> String -> IO ()\nfoo x y = do\n print x\n return 42\n"]
it "formats a document with CRLF endings" $ runSession hieCommand fullCaps "test/testdata" $ do
doc <- openDoc "BrittanyCRLF.hs" "haskell"
let opts = DocumentFormattingParams doc (FormattingOptions 4 True) Nothing
ResponseMessage _ _ (Just edits) _ <- request TextDocumentFormatting opts
liftIO $ edits `shouldBe` [TextEdit (Range (Position 0 0) (Position 3 0))
"foo :: Int -> String -> IO ()\nfoo x y = do\n print x\n return 42\n"]
it "formats a range with LF endings" $ runSession hieCommand fullCaps "test/testdata" $ do
doc <- openDoc "BrittanyLF.hs" "haskell"
let range = Range (Position 1 0) (Position 2 22)
opts = DocumentRangeFormattingParams doc range (FormattingOptions 4 True) Nothing
ResponseMessage _ _ (Just edits) _ <- request TextDocumentRangeFormatting opts
liftIO $ edits `shouldBe` [TextEdit (Range (Position 1 0) (Position 3 0))
"foo x y = do\n print x\n return 42\n"]
it "formats a range with CRLF endings" $ runSession hieCommand fullCaps "test/testdata" $ do
doc <- openDoc "BrittanyCRLF.hs" "haskell"
let range = Range (Position 1 0) (Position 2 22)
opts = DocumentRangeFormattingParams doc range (FormattingOptions 4 True) Nothing
ResponseMessage _ _ (Just edits) _ <- request TextDocumentRangeFormatting opts
liftIO $ edits `shouldBe` [TextEdit (Range (Position 1 0) (Position 3 0))
"foo x y = do\n print x\n return 42\n"]
describe "ormolu" $ do
it "formats correctly" $ runSession hieCommand fullCaps "test/testdata" $ do
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "ormolu"))
doc <- openDoc "Format.hs" "haskell"
formatDoc doc (FormattingOptions 2 True)
docContent <- documentContents doc
let formatted = liftIO $ docContent `shouldBe` formattedOrmolu
case ghcVersion of
GHC88 -> formatted
GHC86 -> formatted
_ -> liftIO $ docContent `shouldBe` unchangedOrmolu
formatLspConfig :: Value -> Value
formatLspConfig provider =
object [ "languageServerHaskell" .= object ["formattingProvider" .= provider] ]
formattedDocTabSize2 :: T.Text
formattedDocTabSize2 =
"module Format where\n\
\foo :: Int -> Int\n\
\foo 3 = 2\n\
\foo x = x\n\
\bar :: String -> IO String\n\
\bar s = do\n\
\ x <- return \"hello\"\n\
\ return \"asdf\"\n\n"
formattedDocTabSize5 :: T.Text
formattedDocTabSize5 =
"module Format where\n\
\foo :: Int -> Int\n\
\foo 3 = 2\n\
\foo x = x\n\
\bar :: String -> IO String\n\
\bar s = do\n\
\ x <- return \"hello\"\n\
\ return \"asdf\"\n\n"
formattedRangeTabSize2 :: T.Text
formattedRangeTabSize2 =
"module Format where\n\
\foo :: Int -> Int\n\
\foo 3 = 2\n\
\foo x = x\n\
\bar :: String -> IO String\n\
\bar s = do\n\
\ x <- return \"hello\"\n\
\ return \"asdf\"\n\
\ \n"
formattedRangeTabSize5 :: T.Text
formattedRangeTabSize5 =
"module Format where\n\
\foo :: Int -> Int\n\
\foo 3 = 2\n\
\foo x = x\n\
\bar :: String -> IO String\n\
\bar s = do\n\
\ x <- return \"hello\"\n\
\ return \"asdf\"\n\
\ \n"
formattedFloskell :: T.Text
formattedFloskell =
"module Format where\n\
\\n\
\foo :: Int -> Int\n\
\foo 3 = 2\n\
\foo x = x\n\
\\n\
\bar :: String -> IO String\n\
\bar s = do\n\
\ x <- return \"hello\"\n\
\ return \"asdf\"\n\n\
\"
formattedBrittanyPostFloskell :: T.Text
formattedBrittanyPostFloskell =
"module Format where\n\
\\n\
\foo :: Int -> Int\n\
\foo 3 = 2\n\
\foo x = x\n\
\\n\
\bar :: String -> IO String\n\
\bar s = do\n\
\ x <- return \"hello\"\n\
\ return \"asdf\"\n\n"
formattedOrmolu :: T.Text
formattedOrmolu =
"module Format where\n\
\\n\
\foo :: Int -> Int\n\
\foo 3 = 2\n\
\foo x = x\n\
\\n\
\bar :: String -> IO String\n\
\bar s = do\n\
\ x <- return \"hello\"\n\
\ return \"asdf\"\n"
unchangedOrmolu :: T.Text
unchangedOrmolu =
"module Format where\n\
\foo :: Int -> Int\n\
\foo 3 = 2\n\
\foo x = x\n\
\bar :: String -> IO String\n\
\bar s = do\n\
\ x <- return \"hello\"\n\
\ return \"asdf\"\n\
\ \n"