-
Notifications
You must be signed in to change notification settings - Fork 34
/
TypecheckerTestLenient.hs
283 lines (248 loc) · 8.01 KB
/
TypecheckerTestLenient.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
{-
Copyright (c) Meta Platforms, Inc. and affiliates.
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
-}
{-# LANGUAGE NamedFieldPuns #-}
module TypecheckerTestLenient where
import Data.Text ( Text )
import qualified Data.Text as Text
import Test.HUnit
import TestRunner
import qualified Data.Map.Strict as Map
import Thrift.Compiler.Options
import Thrift.Compiler.Parser
import Thrift.Compiler.Plugin
import Thrift.Compiler.Plugins.Linter
import Thrift.Compiler.Pretty ( renderTypeError )
import Thrift.Compiler.Types as T
import Thrift.Compiler.Typechecker
import Thrift.Compiler.Typechecker.Monad
mkModuleMap :: Text -> ([Parsed Header], [Parsed Decl]) -> ModuleMap
mkModuleMap name (headers, decls) =
let path = Text.unpack name ++ ".thrift"
in Map.singleton path ThriftFile
{ thriftName = name
, thriftPath = path
, thriftHeaders = headers
, thriftDecls = decls
, thriftSplice = Nothing
, thriftComments = []
}
lenientOptions :: LangOpts l -> Bool -> Options l
lenientOptions opts optsLenient = (defaultOptions opts){ optsLenient }
typecheckIncludes
:: Typecheckable l
=> LangOpts l
-> Bool
-> [(Text, String)]
-> Either String (Either [TypeError l] (Program l Loc, [Program l Loc]))
typecheckIncludes opts lenient files =
case parseIncludes files of
Left err -> Left ("parse error: " ++ err)
Right moduleMap -> Right $
typecheck (lenientOptions opts lenient) moduleMap
where
parseInclude :: (Text, String) -> Either String ModuleMap
parseInclude (name, input) =
let path = Text.unpack name ++ ".thrift"
in case runParser parseThrift path input of
Left err -> Left ("parse error: " ++ path ++ " : " ++ err)
Right hsds -> Right (mkModuleMap name hsds)
parseIncludes :: [(Text, String)] -> Either String ModuleMap
parseIncludes = fmap mconcat . traverse parseInclude
typecheckIncludesTest :: String -> [(Text, String)] -> Test
typecheckIncludesTest label files =
TestLabel (label ++ " should pass") $ TestCase $
case typecheckIncludes NoOpts True files of
Right (Right _) -> return ()
Right (Left errors) -> assertFailure $
"should have typechecked: " <> show (map renderTypeError errors)
Left err -> assertFailure err
typecheckIncludesErrorTest :: String -> [(Text, String)] -> Test
typecheckIncludesErrorTest label files =
TestLabel (label ++ " should fail") $ TestCase $
case typecheckIncludes NoOpts False files of
Right (Left errors)
| any isTypeError errors -> return ()
| otherwise -> assertFailure $
"wrong errors: " <> show (map renderTypeError errors)
Right (Right _) -> assertFailure "should be a type error but typechecked"
Left err -> assertFailure err
parseAndTypecheck
:: Typecheckable l
=> LangOpts l
-> Bool
-> String
-> Either String (Either [TypeError l] (Program l Loc, [Program l Loc]))
parseAndTypecheck opts lenient input =
case runParser parseThrift "ttl.thrift" input of
Left err -> Left ("parse error: " ++ err)
Right hsds -> Right $
typecheck (lenientOptions opts lenient) (mkModuleMap "ttl" hsds)
typecheckTest :: String -> String -> Test
typecheckTest label input = TestLabel (label ++ " should pass") $ TestCase $
case parseAndTypecheck NoOpts True input of
Right (Right _) -> return ()
Right (Left errors) -> assertFailure $
"should have typechecked: " <> show (map renderTypeError errors)
Left err -> assertFailure err
typeErrorTest :: String -> String -> Test
typeErrorTest label input = TestLabel (label ++ " should fail") $ TestCase $
case parseAndTypecheck NoOpts False input of
Right (Left errors)
| any isTypeError errors -> return ()
| otherwise -> assertFailure $
"wrong errors: " <> show (map renderTypeError errors)
Right (Right _) -> assertFailure "should be a type error but typechecked"
Left err -> assertFailure err
isTypeError :: TypeError l -> Bool
isTypeError TypeError{} = True
isTypeError EmptyInput = False
isTypeError CyclicModules{} = False
--------------------------------------------------------------------------------
emptySet1 :: String
emptySet1 = unlines
[ "struct Foo {"
, " 2: set<i32> emptySet = {},"
, "}"
]
emptySet2 :: String
emptySet2 = unlines [ "const set<i32> EmptySet = {}" ]
enumToIntDefaults :: String
enumToIntDefaults = unlines
[ "enum Bar {"
, " A = 1,"
, " B = 2,"
, " C = 3,"
, "}"
, ""
, "struct Foo {"
, " 1: i32 enumToInt = A,"
, " 2: list<i16> enumToIntList = [A, B, C],"
, " 3: set<byte> enumToIntSet = [A, B, C],"
, " 4: map<i64, bool> enumToIntMapKey = { A : true, B : false, C : true },"
, " 5: map<string, i16> enumToMapValue = "
, " { \"7\" : A, \"8\" : B, \"9\": C },"
, "}"
]
enumToIntConst :: String
enumToIntConst = unlines
[ "enum Bar {"
, " A = 1,"
, " B = 2,"
, " C = 3,"
, "}"
, ""
, "const i32 enumToInt = A"
, "const list<i32> qualifiedEnumToIntList = [Bar.A, Bar.B, Bar.C]"
, "const list<i16> enumToIntList = [A, B, C]"
, "const set<byte> enumToIntSet = [A, B, C]"
, "const map<i64, bool> enumToIntMapKey = { A : true, B : false, C : true }"
, "const map<string, i16> enumToMapValue = "
, " { \"7\" : A, \"8\" : B, \"9\": C }"
]
enumConstToIntConst :: String
enumConstToIntConst = unlines
[ "enum Bar {"
, " A = 1,"
, " B = 2,"
, " C = 3,"
, "}"
, ""
, "const Bar iAmB = B"
, "typedef i16 Foo"
, "const Foo fooIs2 = iAmB"
]
selfQualification :: String
selfQualification = unlines
[ "enum Bar {"
, " A = 1,"
, " B = 2,"
, " C = 3,"
, "}"
, ""
, "const ttl.Bar iAmB = ttl.Bar.B"
]
ignoreWrongEnumKeys :: String
ignoreWrongEnumKeys = unlines
[ "enum Bar {"
, " A = 1,"
, " B = 2,"
, " C = 3,"
, "}"
, "enum Wrong {"
, " WrongA = 1"
, " WrongB = 10"
, " WrongC = 100"
, "}"
, "const map<Bar, i16> barMap = {"
, " A : 1,"
, " B : 2,"
, " C : 3,"
, " WrongA : 11,"
, " WrongB : 12,"
, " WrongC : 13,"
, "}"
]
noAltUnion :: String
noAltUnion = unlines
[ "union Zero {"
, "}"
]
emptyListMap :: String
emptyListMap = unlines
[ "const list<i32> EmptyList = {}"
, "const map<i32,i32> EmptyMap = []"
]
selfQualType :: String
selfQualType = unlines
[ "struct Foo {"
, " 1: string name,"
, "}"
, "service S {"
, " ttl.Foo fun("
, " 1: ttl.Foo param1,"
, " )"
, "}"
]
lenientInputs :: [(String, String)]
lenientInputs =
[ ( "T43181705 empty set1", emptySet1 )
, ( "T43181705 empty set2", emptySet2 )
, ( "T43181363 enum to Int Defaults", enumToIntDefaults )
, ( "T43181363 enum to Int Constants", enumToIntConst )
, ( "T43181363 enum Const to Int Constant", enumConstToIntConst )
, ( "T43181635 self qualification", selfQualification )
, ( "T45688659 ignore wrong enum keys", ignoreWrongEnumKeys )
, ( "T46325195 allow unions without alts", noAltUnion )
, ( "T43181705 empty list/map", emptyListMap )
, ( "T43181635 self qualified type", selfQualType )
]
lenientTests :: Test
lenientTests = TestList $
map (uncurry typecheckTest) lenientInputs
++
map (uncurry typeErrorTest) lenientInputs
--------------------------------------------------------------------------------
includeA :: (Text, String)
includeA = ("file_a", "typedef i64 A")
includeB :: (Text, String)
includeB = ("file_b", "include \"file_a.thrift\"")
transitiveC :: (Text, String)
transitiveC = (,) "file_c" $ unlines
[ "include \"file_b.thrift\""
, "const file_a.A deep = 17"
]
transitiveInputs :: (String, [(Text, String)])
transitiveInputs =
("T43181464 transitive import", [ transitiveC, includeB, includeA ])
lenientTransitiveInclude :: Test
lenientTransitiveInclude = TestList
[ uncurry typecheckIncludesTest transitiveInputs
, uncurry typecheckIncludesErrorTest transitiveInputs
]
--------------------------------------------------------------------------------
main :: IO ()
main = testRunner $ TestList [ lenientTests, lenientTransitiveInclude ]