-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathParserSpec.hs
268 lines (261 loc) · 8.89 KB
/
ParserSpec.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
{-# LANGUAGE OverloadedStrings #-}
module Ormolu.Fixity.ParserSpec (spec) where
import Data.List.NonEmpty (NonEmpty (..))
import Data.Map.Strict qualified as Map
import Data.Text (Text)
import Data.Text qualified as T
import Ormolu.Fixity
import Ormolu.Fixity.Parser
import Test.Hspec
import Test.Hspec.Megaparsec
import Text.Megaparsec.Error (ErrorFancy (..))
spec :: Spec
spec = do
describe "parseDotOrmolu" $ do
it "parses the empty input without choking" $
parseDotOrmolu "" ""
`shouldParse` (FixityOverrides Map.empty, ModuleReexports Map.empty)
it "parses a collection of fixity declarations" $
-- The example is taken from base.
parseDotOrmolu
""
( T.unlines
[ "infixr 9 .",
"infixr 5 ++",
"infixl 4 <$",
"infixl 1 >>, >>=",
"infixr 1 =<<",
"infixr 0 $, $!",
"infixl 4 <*>, <*, *>, <**>"
]
)
`shouldParse` ( exampleFixityOverrides,
ModuleReexports Map.empty
)
it "accepts fractional operator precedences" $
parseDotOrmolu
""
( T.unlines
[ "infixr 3 >~<",
"infixr 3.3 |~|",
"infixr 3.7 <~>"
]
)
`shouldParse` ( fractionalFixityOverrides,
ModuleReexports Map.empty
)
it "combines conflicting fixity declarations correctly" $
parseDotOrmolu
""
( T.unlines
[ "infixr 9 ., ^",
"infixr 7 ., $",
"infixr 9 ^ ",
"infixl 7 $"
]
)
`shouldParse` ( FixityOverrides
( Map.fromList
[ ("$", FixityInfo InfixL 7),
(".", FixityInfo InfixR 7),
("^", FixityInfo InfixR 9)
]
),
ModuleReexports Map.empty
)
it "handles CRLF line endings correctly" $
parseDotOrmolu ""
`shouldSucceedOn` unlinesCrlf
[ "infixr 9 .",
"infixr 5 ++"
]
it "parses inputs without a trailing newline" $
parseDotOrmolu ""
`shouldSucceedOn` T.intercalate
"\n"
[ "infixr 9 .",
"infixr 5 ++"
]
it "fails with correct parse error (keyword wrong second line)" $
parseDotOrmolu "" "infixr 5 .\nfoobar 5 $"
`shouldFailWith` err
11
( mconcat
[ utok 'f',
etoks "infix",
etoks "infixl",
etoks "infixr",
etoks "module",
eeof
]
)
it "parses module re-exports and combines them correctly" $
parseDotOrmolu
""
( T.unlines
[ "module Control.Lens exports Control.Lens.Lens",
"module Control.Lens exports \"lens\" Control.Lens.At",
"module Text.Megaparsec exports Control.Monad.Combinators"
]
)
`shouldParse` (FixityOverrides Map.empty, exampleModuleReexports)
it "parses fixity declarations + module re-export declarations with blanks" $
parseDotOrmolu
""
( T.unlines
[ "module Control.Lens exports Control.Lens.Lens",
"",
"infixr 5 ++",
"infixl 4 <$",
"",
"",
"module Control.Lens exports \"lens\" Control.Lens.At",
"infixr 9 .",
"module Text.Megaparsec exports Control.Monad.Combinators",
"infixl 1 >>, >>=",
"infixr 1 =<<",
"",
"infixr 0 $, $!",
"infixl 4 <*>, <*, *>, <**>"
]
)
`shouldParse` (exampleFixityOverrides, exampleModuleReexports)
describe "parseFixtiyDeclaration" $ do
it "parses a simple infixr declaration" $
parseFixityDeclaration "infixr 5 $"
`shouldParse` [("$", FixityInfo InfixR 5)]
it "parses a simple infixl declaration" $
parseFixityDeclaration "infixl 5 $"
`shouldParse` [("$", FixityInfo InfixL 5)]
it "parses a simple infix declaration" $
parseFixityDeclaration "infix 5 $"
`shouldParse` [("$", FixityInfo InfixN 5)]
it "parses a declaration for a ticked identifier" $
parseFixityDeclaration "infixl 5 `foo`"
`shouldParse` [("foo", FixityInfo InfixL 5)]
it "parses a declaration for a ticked identifier (constructor case)" $
parseFixityDeclaration "infixl 5 `Foo`"
`shouldParse` [("Foo", FixityInfo InfixL 5)]
it "parses a multi-operator declaration" $
parseFixityDeclaration "infixl 5 $, ., `Foo`, `bar`"
`shouldParse` [ ("$", FixityInfo InfixL 5),
(".", FixityInfo InfixL 5),
("Foo", FixityInfo InfixL 5),
("bar", FixityInfo InfixL 5)
]
it "parses a declaration with a unicode operator" $
parseFixityDeclaration "infixr 5 ×"
`shouldParse` [("×", FixityInfo InfixR 5)]
it "fails with correct parse error (keyword wrong)" $
parseFixityDeclaration "foobar 5 $"
`shouldFailWith` err
0
( mconcat
[ utoks "foobar",
etoks "infix",
etoks "infixl",
etoks "infixr"
]
)
it "fails with correct parse error (missing operator)" $
parseFixityDeclaration "infixr 5 "
`shouldFailWith` err
9
( mconcat
[ ueof,
etok '`',
elabel "operator character"
]
)
it "fails with correct parse error (trailing comma)" $
parseFixityDeclaration "infixr 5 ., "
`shouldFailWith` err
12
( mconcat
[ ueof,
etok '`',
elabel "operator character"
]
)
it "fails with correct parse error (precedence greater than 9)" $
parseFixityDeclaration "infixl 10 $"
`shouldFailWith` errFancy
7
(fancy (ErrorFail "precedence should not be greater than 9"))
describe "parseModuleReexportDeclaration" $ do
it "parses a re-export declaration" $
parseModuleReexportDeclaration "module Control.Lens exports Control.Lens.Lens"
`shouldParse` ( "Control.Lens",
(Nothing, "Control.Lens.Lens") :| []
)
it "parses a re-export declaration (explicit package)" $
parseModuleReexportDeclaration "module Control.Lens exports \"lens\" Control.Lens.Lens"
`shouldParse` ( "Control.Lens",
(Just "lens", "Control.Lens.Lens") :| []
)
it "fails with correct parse error (keyword wrong)" $
parseModuleReexportDeclaration "foo Control.Lens exports Control.Lens.Lens"
`shouldFailWith` err
0
( mconcat
[ utoks "foo Co",
etoks "module"
]
)
it "fails with correct parse error (module syntax)" $
parseModuleReexportDeclaration "module control.Lens exports Control.Lens.Lens"
`shouldFailWith` err
7
( mconcat
[ utok 'c',
elabel "module name"
]
)
it "fails with correct parse error (typo: export intead exports)" $
parseModuleReexportDeclaration "module Control.Lens export Control.Lens.Lens"
`shouldFailWith` err
20
( mconcat
[ utoks "export ",
etoks "exports"
]
)
exampleFixityOverrides :: FixityOverrides
exampleFixityOverrides =
FixityOverrides
( Map.fromList
[ ("$", FixityInfo InfixR 0),
("$!", FixityInfo InfixR 0),
("*>", FixityInfo InfixL 4),
("++", FixityInfo InfixR 5),
(".", FixityInfo InfixR 9),
("<$", FixityInfo InfixL 4),
("<*", FixityInfo InfixL 4),
("<**>", FixityInfo InfixL 4),
("<*>", FixityInfo InfixL 4),
("=<<", FixityInfo InfixR 1),
(">>", FixityInfo InfixL 1),
(">>=", FixityInfo InfixL 1)
]
)
fractionalFixityOverrides :: FixityOverrides
fractionalFixityOverrides =
FixityOverrides
( Map.fromList
[ (">~<", FixityInfo InfixR 3),
("|~|", FixityInfo InfixR 3.3),
("<~>", FixityInfo InfixR 3.7)
]
)
exampleModuleReexports :: ModuleReexports
exampleModuleReexports =
ModuleReexports . Map.fromList $
[ ( "Control.Lens",
(Nothing, "Control.Lens.Lens") :| [(Just "lens", "Control.Lens.At")]
),
( "Text.Megaparsec",
(Nothing, "Control.Monad.Combinators") :| []
)
]
unlinesCrlf :: [Text] -> Text
unlinesCrlf = T.concat . fmap (<> "\r\n")