Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(257)

Side by Side Diff: mojo/public/tools/bindings/pylib/mojom_tests/parse/parser_unittest.py

Issue 391003003: Mojo: Mojom: Add AST type for struct fields. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/public/tools/bindings/pylib/mojom/parse/translate.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 }; 144 };
145 145
146 } // module my_module 146 } // module my_module
147 """ 147 """
148 expected = ast.Mojom( 148 expected = ast.Mojom(
149 ast.Module(('IDENTIFIER', 'my_module'), None), 149 ast.Module(('IDENTIFIER', 'my_module'), None),
150 ast.ImportList(), 150 ast.ImportList(),
151 [('STRUCT', 151 [('STRUCT',
152 'MyStruct', 152 'MyStruct',
153 None, 153 None,
154 [('FIELD', 'int32', 'a', None, None), 154 [ast.StructField('a', None, 'int32', None),
155 ('FIELD', 'double', 'b', None, None)])]) 155 ast.StructField('b', None, 'double', None)])])
156 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) 156 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
157 157
158 def testSimpleStructWithoutModule(self): 158 def testSimpleStructWithoutModule(self):
159 """Tests a simple struct without an enclosing module.""" 159 """Tests a simple struct without an enclosing module."""
160 160
161 source = """\ 161 source = """\
162 struct MyStruct { 162 struct MyStruct {
163 int32 a; 163 int32 a;
164 double b; 164 double b;
165 }; 165 };
166 """ 166 """
167 expected = ast.Mojom( 167 expected = ast.Mojom(
168 None, 168 None,
169 ast.ImportList(), 169 ast.ImportList(),
170 [('STRUCT', 170 [('STRUCT',
171 'MyStruct', 171 'MyStruct',
172 None, 172 None,
173 [('FIELD', 'int32', 'a', None, None), 173 [ast.StructField('a', None, 'int32', None),
174 ('FIELD', 'double', 'b', None, None)])]) 174 ast.StructField('b', None, 'double', None)])])
175 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) 175 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
176 176
177 def testMissingModuleName(self): 177 def testMissingModuleName(self):
178 """Tests an (invalid) .mojom with a missing module name.""" 178 """Tests an (invalid) .mojom with a missing module name."""
179 179
180 source1 = """\ 180 source1 = """\
181 // Missing module name. 181 // Missing module name.
182 module { 182 module {
183 struct MyStruct { 183 struct MyStruct {
184 int32 a; 184 int32 a;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 }; 282 };
283 283
284 } // my_module 284 } // my_module
285 """ 285 """
286 expected = ast.Mojom( 286 expected = ast.Mojom(
287 ast.Module(('IDENTIFIER', 'my_module'), None), 287 ast.Module(('IDENTIFIER', 'my_module'), None),
288 ast.ImportList(), 288 ast.ImportList(),
289 [('STRUCT', 289 [('STRUCT',
290 'MyStruct', None, 290 'MyStruct', None,
291 [ast.Const('kNumber', 'int8', '-1'), 291 [ast.Const('kNumber', 'int8', '-1'),
292 ('FIELD', 'int8', 'number', 292 ast.StructField('number', ast.Ordinal(0), 'int8',
293 ast.Ordinal(0), ('IDENTIFIER', 'kNumber'))])]) 293 ('IDENTIFIER', 'kNumber'))])])
294 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) 294 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
295 295
296 def testNoConditionals(self): 296 def testNoConditionals(self):
297 """Tests that ?: is not allowed.""" 297 """Tests that ?: is not allowed."""
298 298
299 source = """\ 299 source = """\
300 module my_module { 300 module my_module {
301 301
302 enum MyEnum { 302 enum MyEnum {
303 MY_ENUM_1 = 1 ? 2 : 3 303 MY_ENUM_1 = 1 ? 2 : 3
(...skipping 26 matching lines...) Expand all
330 }; 330 };
331 331
332 } // module my_module 332 } // module my_module
333 """ 333 """
334 expected = ast.Mojom( 334 expected = ast.Mojom(
335 ast.Module(('IDENTIFIER', 'my_module'), None), 335 ast.Module(('IDENTIFIER', 'my_module'), None),
336 ast.ImportList(), 336 ast.ImportList(),
337 [('STRUCT', 337 [('STRUCT',
338 'MyStruct', 338 'MyStruct',
339 None, 339 None,
340 [('FIELD', 'int32', 'a0', ast.Ordinal(0), None), 340 [ast.StructField('a0', ast.Ordinal(0), 'int32', None),
341 ('FIELD', 'int32', 'a1', ast.Ordinal(1), None), 341 ast.StructField('a1', ast.Ordinal(1), 'int32', None),
342 ('FIELD', 'int32', 'a2', ast.Ordinal(2), None), 342 ast.StructField('a2', ast.Ordinal(2), 'int32', None),
343 ('FIELD', 'int32', 'a9', ast.Ordinal(9), None), 343 ast.StructField('a9', ast.Ordinal(9), 'int32', None),
344 ('FIELD', 'int32', 'a10', ast.Ordinal(10), None), 344 ast.StructField('a10', ast.Ordinal(10), 'int32', None),
345 ('FIELD', 'int32', 'a11', ast.Ordinal(11), None), 345 ast.StructField('a11', ast.Ordinal(11), 'int32', None),
346 ('FIELD', 'int32', 'a29', ast.Ordinal(29), None), 346 ast.StructField('a29', ast.Ordinal(29), 'int32', None),
347 ('FIELD', 'int32', 'a1234567890', ast.Ordinal(1234567890), None)])]) 347 ast.StructField('a1234567890', ast.Ordinal(1234567890), 'int32',
348 None)])])
348 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) 349 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
349 350
350 def testInvalidOrdinals(self): 351 def testInvalidOrdinals(self):
351 """Tests that (lexically) invalid ordinals are correctly detected.""" 352 """Tests that (lexically) invalid ordinals are correctly detected."""
352 353
353 source1 = """\ 354 source1 = """\
354 module my_module { 355 module my_module {
355 356
356 struct MyStruct { 357 struct MyStruct {
357 int32 a_missing@; 358 int32 a_missing@;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 }; 426 };
426 427
427 } // module my.mod 428 } // module my.mod
428 """ 429 """
429 expected = ast.Mojom( 430 expected = ast.Mojom(
430 ast.Module(('IDENTIFIER', 'my.mod'), None), 431 ast.Module(('IDENTIFIER', 'my.mod'), None),
431 ast.ImportList(), 432 ast.ImportList(),
432 [('STRUCT', 433 [('STRUCT',
433 'MyStruct', 434 'MyStruct',
434 None, 435 None,
435 [('FIELD', 'int32', 'a', None, None)])]) 436 [ast.StructField('a', None, 'int32', None)])])
436 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) 437 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
437 438
438 def testValidHandleTypes(self): 439 def testValidHandleTypes(self):
439 """Tests (valid) handle types.""" 440 """Tests (valid) handle types."""
440 441
441 source = """\ 442 source = """\
442 struct MyStruct { 443 struct MyStruct {
443 handle a; 444 handle a;
444 handle<data_pipe_consumer> b; 445 handle<data_pipe_consumer> b;
445 handle <data_pipe_producer> c; 446 handle <data_pipe_producer> c;
446 handle < message_pipe > d; 447 handle < message_pipe > d;
447 handle 448 handle
448 < shared_buffer 449 < shared_buffer
449 > e; 450 > e;
450 }; 451 };
451 """ 452 """
452 expected = ast.Mojom( 453 expected = ast.Mojom(
453 None, 454 None,
454 ast.ImportList(), 455 ast.ImportList(),
455 [('STRUCT', 456 [('STRUCT',
456 'MyStruct', 457 'MyStruct',
457 None, 458 None,
458 [('FIELD', 'handle', 'a', None, None), 459 [ast.StructField('a', None, 'handle', None),
459 ('FIELD', 'handle<data_pipe_consumer>', 'b', None, None), 460 ast.StructField('b', None, 'handle<data_pipe_consumer>', None),
460 ('FIELD', 'handle<data_pipe_producer>', 'c', None, None), 461 ast.StructField('c', None, 'handle<data_pipe_producer>', None),
461 ('FIELD', 'handle<message_pipe>', 'd', None, None), 462 ast.StructField('d', None, 'handle<message_pipe>', None),
462 ('FIELD', 'handle<shared_buffer>', 'e', None, None)])]) 463 ast.StructField('e', None, 'handle<shared_buffer>', None)])])
463 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) 464 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
464 465
465 def testInvalidHandleType(self): 466 def testInvalidHandleType(self):
466 """Tests an invalid (unknown) handle type.""" 467 """Tests an invalid (unknown) handle type."""
467 468
468 source = """\ 469 source = """\
469 struct MyStruct { 470 struct MyStruct {
470 handle<wtf_is_this> foo; 471 handle<wtf_is_this> foo;
471 }; 472 };
472 """ 473 """
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 double a21 = -1.23E10; 507 double a21 = -1.23E10;
507 double a22 = +.123E10; 508 double a22 = +.123E10;
508 }; 509 };
509 """ 510 """
510 expected = ast.Mojom( 511 expected = ast.Mojom(
511 None, 512 None,
512 ast.ImportList(), 513 ast.ImportList(),
513 [('STRUCT', 514 [('STRUCT',
514 'MyStruct', 515 'MyStruct',
515 None, 516 None,
516 [('FIELD', 'int16', 'a0', None, '0'), 517 [ast.StructField('a0', None, 'int16', '0'),
517 ('FIELD', 'uint16', 'a1', None, '0x0'), 518 ast.StructField('a1', None, 'uint16', '0x0'),
518 ('FIELD', 'uint16', 'a2', None, '0x00'), 519 ast.StructField('a2', None, 'uint16', '0x00'),
519 ('FIELD', 'uint16', 'a3', None, '0x01'), 520 ast.StructField('a3', None, 'uint16', '0x01'),
520 ('FIELD', 'uint16', 'a4', None, '0xcd'), 521 ast.StructField('a4', None, 'uint16', '0xcd'),
521 ('FIELD', 'int32', 'a5' , None, '12345'), 522 ast.StructField('a5' , None, 'int32', '12345'),
522 ('FIELD', 'int64', 'a6', None, '-12345'), 523 ast.StructField('a6', None, 'int64', '-12345'),
523 ('FIELD', 'int64', 'a7', None, '+12345'), 524 ast.StructField('a7', None, 'int64', '+12345'),
524 ('FIELD', 'uint32', 'a8', None, '0x12cd3'), 525 ast.StructField('a8', None, 'uint32', '0x12cd3'),
525 ('FIELD', 'uint32', 'a9', None, '-0x12cD3'), 526 ast.StructField('a9', None, 'uint32', '-0x12cD3'),
526 ('FIELD', 'uint32', 'a10', None, '+0x12CD3'), 527 ast.StructField('a10', None, 'uint32', '+0x12CD3'),
527 ('FIELD', 'bool', 'a11', None, 'true'), 528 ast.StructField('a11', None, 'bool', 'true'),
528 ('FIELD', 'bool', 'a12', None, 'false'), 529 ast.StructField('a12', None, 'bool', 'false'),
529 ('FIELD', 'float', 'a13', None, '1.2345'), 530 ast.StructField('a13', None, 'float', '1.2345'),
530 ('FIELD', 'float', 'a14', None, '-1.2345'), 531 ast.StructField('a14', None, 'float', '-1.2345'),
531 ('FIELD', 'float', 'a15', None, '+1.2345'), 532 ast.StructField('a15', None, 'float', '+1.2345'),
532 ('FIELD', 'float', 'a16', None, '123.'), 533 ast.StructField('a16', None, 'float', '123.'),
533 ('FIELD', 'float', 'a17', None, '.123'), 534 ast.StructField('a17', None, 'float', '.123'),
534 ('FIELD', 'double', 'a18', None, '1.23E10'), 535 ast.StructField('a18', None, 'double', '1.23E10'),
535 ('FIELD', 'double', 'a19', None, '1.E-10'), 536 ast.StructField('a19', None, 'double', '1.E-10'),
536 ('FIELD', 'double', 'a20', None, '.5E+10'), 537 ast.StructField('a20', None, 'double', '.5E+10'),
537 ('FIELD', 'double', 'a21', None, '-1.23E10'), 538 ast.StructField('a21', None, 'double', '-1.23E10'),
538 ('FIELD', 'double', 'a22', None, '+.123E10')])]) 539 ast.StructField('a22', None, 'double', '+.123E10')])])
539 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) 540 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
540 541
541 def testValidFixedSizeArray(self): 542 def testValidFixedSizeArray(self):
542 """Tests parsing a fixed size array.""" 543 """Tests parsing a fixed size array."""
543 544
544 source = """\ 545 source = """\
545 struct MyStruct { 546 struct MyStruct {
546 int32[] normal_array; 547 int32[] normal_array;
547 int32[1] fixed_size_array_one_entry; 548 int32[1] fixed_size_array_one_entry;
548 int32[10] fixed_size_array_ten_entries; 549 int32[10] fixed_size_array_ten_entries;
549 }; 550 };
550 """ 551 """
551 expected = ast.Mojom( 552 expected = ast.Mojom(
552 None, 553 None,
553 ast.ImportList(), 554 ast.ImportList(),
554 [('STRUCT', 555 [('STRUCT',
555 'MyStruct', 556 'MyStruct',
556 None, 557 None,
557 [('FIELD', 'int32[]', 'normal_array', None, None), 558 [ast.StructField('normal_array', None, 'int32[]', None),
558 ('FIELD', 'int32[1]', 'fixed_size_array_one_entry', None, None), 559 ast.StructField('fixed_size_array_one_entry', None, 'int32[1]',
559 ('FIELD', 'int32[10]', 'fixed_size_array_ten_entries', None, 560 None),
560 None)])]) 561 ast.StructField('fixed_size_array_ten_entries', None, 'int32[10]',
562 None)])])
561 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) 563 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
562 564
563 def testValidNestedArray(self): 565 def testValidNestedArray(self):
564 """Tests parsing a nested array.""" 566 """Tests parsing a nested array."""
565 567
566 source = "struct MyStruct { int32[][] nested_array; };" 568 source = "struct MyStruct { int32[][] nested_array; };"
567 expected = ast.Mojom( 569 expected = ast.Mojom(
568 None, 570 None,
569 ast.ImportList(), 571 ast.ImportList(),
570 [('STRUCT', 572 [('STRUCT',
571 'MyStruct', 573 'MyStruct',
572 None, 574 None,
573 [('FIELD', 'int32[][]', 'nested_array', None, None)])]) 575 [ast.StructField('nested_array', None, 'int32[][]', None)])])
574 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) 576 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
575 577
576 def testInvalidFixedArraySize(self): 578 def testInvalidFixedArraySize(self):
577 """Tests that invalid fixed array bounds are correctly detected.""" 579 """Tests that invalid fixed array bounds are correctly detected."""
578 580
579 source1 = """\ 581 source1 = """\
580 struct MyStruct { 582 struct MyStruct {
581 int32[0] zero_size_array; 583 int32[0] zero_size_array;
582 }; 584 };
583 """ 585 """
(...skipping 30 matching lines...) Expand all
614 616
615 source1 = "interface MyInterface { MyMethod(int32 a); };" 617 source1 = "interface MyInterface { MyMethod(int32 a); };"
616 expected1 = ast.Mojom( 618 expected1 = ast.Mojom(
617 None, 619 None,
618 ast.ImportList(), 620 ast.ImportList(),
619 [('INTERFACE', 621 [('INTERFACE',
620 'MyInterface', 622 'MyInterface',
621 None, 623 None,
622 [('METHOD', 624 [('METHOD',
623 'MyMethod', 625 'MyMethod',
624 ast.ParameterList(ast.Parameter('int32', 'a', None)), 626 ast.ParameterList(ast.Parameter('a', None, 'int32')),
625 None, 627 None,
626 None)])]) 628 None)])])
627 self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) 629 self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1)
628 630
629 source2 = """\ 631 source2 = """\
630 interface MyInterface { 632 interface MyInterface {
631 MyMethod1@0(int32 a@0, int64 b@1); 633 MyMethod1@0(int32 a@0, int64 b@1);
632 MyMethod2@1() => (); 634 MyMethod2@1() => ();
633 }; 635 };
634 """ 636 """
635 expected2 = ast.Mojom( 637 expected2 = ast.Mojom(
636 None, 638 None,
637 ast.ImportList(), 639 ast.ImportList(),
638 [('INTERFACE', 640 [('INTERFACE',
639 'MyInterface', 641 'MyInterface',
640 None, 642 None,
641 [('METHOD', 643 [('METHOD',
642 'MyMethod1', 644 'MyMethod1',
643 ast.ParameterList([ast.Parameter('int32', 'a', ast.Ordinal(0)), 645 ast.ParameterList([ast.Parameter('a', ast.Ordinal(0), 'int32'),
644 ast.Parameter('int64', 'b', ast.Ordinal(1))]), 646 ast.Parameter('b', ast.Ordinal(1), 'int64')]),
645 ast.Ordinal(0), 647 ast.Ordinal(0),
646 None), 648 None),
647 ('METHOD', 649 ('METHOD',
648 'MyMethod2', 650 'MyMethod2',
649 ast.ParameterList(), 651 ast.ParameterList(),
650 ast.Ordinal(1), 652 ast.Ordinal(1),
651 ast.ParameterList())])]) 653 ast.ParameterList())])])
652 self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) 654 self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2)
653 655
654 source3 = """\ 656 source3 = """\
655 interface MyInterface { 657 interface MyInterface {
656 MyMethod(string a) => (int32 a, bool b); 658 MyMethod(string a) => (int32 a, bool b);
657 }; 659 };
658 """ 660 """
659 expected3 = ast.Mojom( 661 expected3 = ast.Mojom(
660 None, 662 None,
661 ast.ImportList(), 663 ast.ImportList(),
662 [('INTERFACE', 664 [('INTERFACE',
663 'MyInterface', 665 'MyInterface',
664 None, 666 None,
665 [('METHOD', 667 [('METHOD',
666 'MyMethod', 668 'MyMethod',
667 ast.ParameterList(ast.Parameter('string', 'a', None)), 669 ast.ParameterList(ast.Parameter('a', None, 'string')),
668 None, 670 None,
669 ast.ParameterList([ast.Parameter('int32', 'a', None), 671 ast.ParameterList([ast.Parameter('a', None, 'int32'),
670 ast.Parameter('bool', 'b', None)]))])]) 672 ast.Parameter('b', None, 'bool')]))])])
671 self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3) 673 self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3)
672 674
673 def testInvalidMethods(self): 675 def testInvalidMethods(self):
674 """Tests that invalid method declarations are correctly detected.""" 676 """Tests that invalid method declarations are correctly detected."""
675 677
676 # No trailing commas. 678 # No trailing commas.
677 source1 = """\ 679 source1 = """\
678 interface MyInterface { 680 interface MyInterface {
679 MyMethod(string a,); 681 MyMethod(string a,);
680 }; 682 };
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
821 """ 823 """
822 with self.assertRaisesRegexp( 824 with self.assertRaisesRegexp(
823 parser.ParseError, 825 parser.ParseError,
824 r"^my_file\.mojom:2: Error: Unexpected 'module':\n" 826 r"^my_file\.mojom:2: Error: Unexpected 'module':\n"
825 r" *module {}$"): 827 r" *module {}$"):
826 parser.Parse(source2, "my_file.mojom") 828 parser.Parse(source2, "my_file.mojom")
827 829
828 830
829 if __name__ == "__main__": 831 if __name__ == "__main__":
830 unittest.main() 832 unittest.main()
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/pylib/mojom/parse/translate.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698