OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import imp | 5 import imp |
6 import os.path | 6 import os.path |
7 import sys | 7 import sys |
8 import unittest | 8 import unittest |
9 | 9 |
10 # Disable lint check for finding modules: | 10 # Disable lint check for finding modules: |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 // This line intentionally left unblank. | 193 // This line intentionally left unblank. |
194 | 194 |
195 { | 195 { |
196 } | 196 } |
197 """ | 197 """ |
198 with self.assertRaisesRegexp( | 198 with self.assertRaisesRegexp( |
199 parser.ParseError, | 199 parser.ParseError, |
200 r"^my_file\.mojom:4: Error: Unexpected '{':\n *{$"): | 200 r"^my_file\.mojom:4: Error: Unexpected '{':\n *{$"): |
201 parser.Parse(source2, "my_file.mojom") | 201 parser.Parse(source2, "my_file.mojom") |
202 | 202 |
203 def testEnumInitializers(self): | 203 def testEnums(self): |
204 """Tests an enum with simple initialized values.""" | 204 """Tests that enum statements are correctly parsed.""" |
205 | 205 |
206 source = """\ | 206 source = """\ |
207 module my_module { | 207 module my_module { |
208 | 208 enum MyEnum1 { VALUE1, VALUE2 }; // No trailing comma. |
209 enum MyEnum { | 209 enum MyEnum2 { |
210 MY_ENUM_NEG1 = -1, | 210 VALUE1 = -1, |
211 MY_ENUM_ZERO = 0, | 211 VALUE2 = 0, |
212 MY_ENUM_1 = +1, | 212 VALUE3 = + 987, // Check that space is allowed. |
213 MY_ENUM_2, | 213 VALUE4 = 0xAF12, |
| 214 VALUE5 = -0x09bcd, |
| 215 VALUE6 = VALUE5, |
| 216 VALUE7, // Leave trailing comma. |
214 }; | 217 }; |
215 | |
216 } // my_module | 218 } // my_module |
217 """ | 219 """ |
218 expected = \ | 220 expected = \ |
219 [('MODULE', | 221 [('MODULE', |
220 'my_module', | 222 'my_module', |
221 None, | 223 None, |
222 [('ENUM', | 224 [('ENUM', |
223 'MyEnum', | 225 'MyEnum1', |
224 [('ENUM_FIELD', 'MY_ENUM_NEG1', '-1'), | 226 [('ENUM_VALUE', 'VALUE1', None), |
225 ('ENUM_FIELD', 'MY_ENUM_ZERO', '0'), | 227 ('ENUM_VALUE', 'VALUE2', None)]), |
226 ('ENUM_FIELD', 'MY_ENUM_1', '+1'), | 228 ('ENUM', |
227 ('ENUM_FIELD', 'MY_ENUM_2', None)])])] | 229 'MyEnum2', |
| 230 [('ENUM_VALUE', 'VALUE1', '-1'), |
| 231 ('ENUM_VALUE', 'VALUE2', '0'), |
| 232 ('ENUM_VALUE', 'VALUE3', '+987'), |
| 233 ('ENUM_VALUE', 'VALUE4', '0xAF12'), |
| 234 ('ENUM_VALUE', 'VALUE5', '-0x09bcd'), |
| 235 ('ENUM_VALUE', 'VALUE6', ('IDENTIFIER', 'VALUE5')), |
| 236 ('ENUM_VALUE', 'VALUE7', None)])])] |
228 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) | 237 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) |
229 | 238 |
230 def testConst(self): | 239 def testInvalidEnumInitializers(self): |
231 """Tests some constants and struct memebers initialized with them.""" | 240 """Tests that invalid enum initializers are correctly detected.""" |
| 241 |
| 242 # No values. |
| 243 source1 = """\ |
| 244 enum MyEnum { |
| 245 }; |
| 246 """ |
| 247 with self.assertRaisesRegexp( |
| 248 parser.ParseError, |
| 249 r"^my_file\.mojom:2: Error: Unexpected '}':\n" |
| 250 r" *};$"): |
| 251 parser.Parse(source1, "my_file.mojom") |
| 252 |
| 253 # Floating point value. |
| 254 source2 = "enum MyEnum { VALUE = 0.123 };" |
| 255 with self.assertRaisesRegexp( |
| 256 parser.ParseError, |
| 257 r"^my_file\.mojom:1: Error: Unexpected '0\.123':\n" |
| 258 r"enum MyEnum { VALUE = 0\.123 };$"): |
| 259 parser.Parse(source2, "my_file.mojom") |
| 260 |
| 261 # Boolean value. |
| 262 source2 = "enum MyEnum { VALUE = true };" |
| 263 with self.assertRaisesRegexp( |
| 264 parser.ParseError, |
| 265 r"^my_file\.mojom:1: Error: Unexpected 'true':\n" |
| 266 r"enum MyEnum { VALUE = true };$"): |
| 267 parser.Parse(source2, "my_file.mojom") |
| 268 |
| 269 def testConsts(self): |
| 270 """Tests some constants and struct members initialized with them.""" |
232 | 271 |
233 source = """\ | 272 source = """\ |
234 module my_module { | 273 module my_module { |
235 | 274 |
236 struct MyStruct { | 275 struct MyStruct { |
237 const int8 kNumber = -1; | 276 const int8 kNumber = -1; |
238 int8 number@0 = kNumber; | 277 int8 number@0 = kNumber; |
239 }; | 278 }; |
240 | 279 |
241 } // my_module | 280 } // my_module |
242 """ | 281 """ |
243 expected = \ | 282 expected = \ |
244 [('MODULE', | 283 [('MODULE', |
245 'my_module', | 284 'my_module', |
246 None, | 285 None, |
247 [('STRUCT', | 286 [('STRUCT', |
248 'MyStruct', None, | 287 'MyStruct', None, |
249 [('CONST', 'int8', 'kNumber', '-1'), | 288 [('CONST', 'int8', 'kNumber', '-1'), |
250 ('FIELD', 'int8', 'number', | 289 ('FIELD', 'int8', 'number', |
251 ast.Ordinal(0), ('IDENTIFIER', 'kNumber'))])])] | 290 ast.Ordinal(0), ('IDENTIFIER', 'kNumber'))])])] |
252 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) | 291 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) |
253 | 292 |
254 def testNoConditionals(self): | 293 def testNoConditionals(self): |
255 """Tests that ?: is not allowed.""" | 294 """Tests that ?: is not allowed.""" |
256 | 295 |
257 source = """\ | 296 source = """\ |
258 module my_module { | 297 module my_module { |
259 | 298 |
260 enum MyEnum { | 299 enum MyEnum { |
261 MY_ENUM_1 = 1 ? 2 : 3 | 300 MY_ENUM_1 = 1 ? 2 : 3 |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 # Missing key. | 779 # Missing key. |
741 source3 = "[=MyName] struct MyStruct {};" | 780 source3 = "[=MyName] struct MyStruct {};" |
742 with self.assertRaisesRegexp( | 781 with self.assertRaisesRegexp( |
743 parser.ParseError, | 782 parser.ParseError, |
744 r"^my_file\.mojom:1: Error: Unexpected '=':\n" | 783 r"^my_file\.mojom:1: Error: Unexpected '=':\n" |
745 r"\[=MyName\] struct MyStruct {};$"): | 784 r"\[=MyName\] struct MyStruct {};$"): |
746 parser.Parse(source3, "my_file.mojom") | 785 parser.Parse(source3, "my_file.mojom") |
747 | 786 |
748 if __name__ == "__main__": | 787 if __name__ == "__main__": |
749 unittest.main() | 788 unittest.main() |
OLD | NEW |