forked from facebookincubator/hsthrift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstParserTest.hs
170 lines (160 loc) · 5.36 KB
/
ConstParserTest.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
{-
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.
-}
module ConstParserTest where
import Test.HUnit
import TestRunner
import Thrift.Compiler.Parser
import Thrift.Compiler.Types
baseTypesTest :: Test
baseTypesTest = TestLabel "base types test" $ TestCase $ do
let input = unlines
[ "struct Foo {"
, " 1: byte foo1 = 1"
, " 2: i16 foo2 = 2"
, " 3: i32 foo3 = 3"
, " 4: i64 foo4 = 4"
, " 5: float foo3 = 3.14159"
, " 6: double foo4 = 3.141592654"
, " 7: string foo5 = 'hello world'"
, " 8: bool foo6 = false"
, "}"
]
let decls = snd <$> runParser parseThrift "" input
case decls of
Right
[ D_Struct Struct
{ structName = "Foo"
, structMembers =
[ Field {fieldId=1,fieldVal=Just (UntypedConst _ (IntConst 1 _))}
, Field {fieldId=2,fieldVal=Just (UntypedConst _ (IntConst 2 _))}
, Field {fieldId=3,fieldVal=Just (UntypedConst _ (IntConst 3 _))}
, Field {fieldId=4,fieldVal=Just (UntypedConst _ (IntConst 4 _))}
, Field {fieldId=5,fieldVal=Just (UntypedConst _ (DoubleConst 3.14159 _))}
, Field {fieldId=6,fieldVal=Just (UntypedConst _ (DoubleConst 3.141592654 _))}
, Field {fieldId=7,fieldVal=Just (UntypedConst _ (StringConst "hello world" _))}
, Field {fieldId=8,fieldVal=Just (UntypedConst _ (BoolConst False))}
]
}
] -> return ()
_ -> assertFailure "not equal"
listTest :: Test
listTest = TestLabel "simple list test" $ TestCase $ do
let input = unlines
[ "struct Foo {"
, " 1: list<i32> foo1 = [1,2,3]"
, "}"
]
let decls = snd <$> runParser parseThrift "" input
case decls of
Right
[ D_Struct Struct
{ structName = "Foo"
, structMembers =
[ Field
{ fieldId = 1
, fieldVal = Just
(UntypedConst _
(ListConst
{ lvElems =
[ ListElem { leElem=UntypedConst _ (IntConst 1 _) }
, ListElem { leElem=UntypedConst _ (IntConst 2 _) }
, ListElem { leElem=UntypedConst _ (IntConst 3 _) }
]
}))
}
]
}
] -> return ()
_ -> assertFailure "not equal"
nestedListTest :: Test
nestedListTest = TestLabel "nested list test" $ TestCase $ do
let input = unlines
[ "struct Foo {"
, " 1: list<list<i32>> foo1 = [[1,2], [3,4]]"
, "}"
]
let decls = snd <$> runParser parseThrift "" input
case decls of
Right
[ D_Struct Struct
{ structName = "Foo"
, structMembers =
[ Field
{ fieldId = 1
, fieldVal = Just
(UntypedConst _
(ListConst
{ lvElems =
[ ListElem
{ leElem = UntypedConst _ ListConst
{ lvElems =
[ ListElem { leElem = UntypedConst _ (IntConst 1 _) }
, ListElem { leElem = UntypedConst _ (IntConst 2 _) }
]
}
}
, ListElem
{ leElem = UntypedConst _ ListConst
{ lvElems =
[ ListElem { leElem = UntypedConst _ (IntConst 3 _) }
, ListElem { leElem = UntypedConst _ (IntConst 4 _) }
]
}
}
]
}))
}
]
}
] -> return ()
_ -> assertFailure "not equal"
mapTest :: Test
mapTest = TestLabel "map test" $ TestCase $ do
let input = unlines
[ "struct Foo {"
, " 1: map<string, list<bool>> foo1 = { 'x' : [], 'y' : [1,0] }"
, "}"
]
let decls = snd <$> runParser parseThrift "" input
case decls of
Right
[ D_Struct Struct
{ structName = "Foo"
, structMembers =
[ Field
{ fieldId = 1
, fieldVal =
(Just
(UntypedConst _ MapConst
{ mvElems =
[ ListElem
{ leElem = MapPair
{ mpKey = UntypedConst _ (StringConst "x" _)
, mpVal = UntypedConst _ (ListConst [] _)
}
}
, ListElem
{ leElem = MapPair
{ mpKey = UntypedConst _ (StringConst "y" _)
, mpVal = UntypedConst _ ListConst
{ lvElems =
[ ListElem{leElem=UntypedConst _ (IntConst 1 _)}
, ListElem{leElem=UntypedConst _ (IntConst 0 _)}
]
}
}
}
]
}))
}
]
}
] -> return ()
_ -> assertFailure "not equal"
main :: IO ()
main = testRunner $ TestList
[ baseTypesTest, listTest, nestedListTest, mapTest ]