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 654 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 interface MyInterface { | 665 interface MyInterface { |
666 MyMethod(, string a); | 666 MyMethod(, string a); |
667 }; | 667 }; |
668 """ | 668 """ |
669 with self.assertRaisesRegexp( | 669 with self.assertRaisesRegexp( |
670 parser.ParseError, | 670 parser.ParseError, |
671 r"^my_file\.mojom:2: Error: Unexpected ',':\n" | 671 r"^my_file\.mojom:2: Error: Unexpected ',':\n" |
672 r" *MyMethod\(, string a\);$"): | 672 r" *MyMethod\(, string a\);$"): |
673 parser.Parse(source2, "my_file.mojom") | 673 parser.Parse(source2, "my_file.mojom") |
674 | 674 |
| 675 def testValidAttributes(self): |
| 676 """Tests parsing attributes (and attribute lists).""" |
| 677 |
| 678 # Note: We use structs because they have (optional) attribute lists. |
| 679 |
| 680 # Empty attribute list. |
| 681 source1 = "[] struct MyStruct {};" |
| 682 expected1 = \ |
| 683 [('MODULE', |
| 684 '', |
| 685 None, |
| 686 [('STRUCT', |
| 687 'MyStruct', |
| 688 ast.AttributeList(), |
| 689 None)])] |
| 690 self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) |
| 691 |
| 692 # One-element attribute list, with name value. |
| 693 source2 = "[MyAttribute=MyName] struct MyStruct {};" |
| 694 expected2 = \ |
| 695 [('MODULE', |
| 696 '', |
| 697 None, |
| 698 [('STRUCT', |
| 699 'MyStruct', |
| 700 ast.AttributeList(ast.Attribute("MyAttribute", "MyName")), |
| 701 None)])] |
| 702 self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) |
| 703 |
| 704 # Two-element attribute list, with one string value and one integer value. |
| 705 source3 = "[MyAttribute1 = \"hello\", MyAttribute2 = 5] struct MyStruct {};" |
| 706 expected3 = \ |
| 707 [('MODULE', |
| 708 '', |
| 709 None, |
| 710 [('STRUCT', |
| 711 'MyStruct', |
| 712 ast.AttributeList([ast.Attribute("MyAttribute1", "hello"), |
| 713 ast.Attribute("MyAttribute2", 5)]), |
| 714 None)])] |
| 715 self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3) |
| 716 |
| 717 # TODO(vtl): Boolean attributes don't work yet. (In fact, we just |eval()| |
| 718 # literal (non-name) values, which is extremely dubious.) |
| 719 |
| 720 def testInvalidAttributes(self): |
| 721 """Tests that invalid attributes and attribute lists are correctly |
| 722 detected.""" |
| 723 |
| 724 # Trailing commas not allowed. |
| 725 source1 = "[MyAttribute=MyName,] struct MyStruct {};" |
| 726 with self.assertRaisesRegexp( |
| 727 parser.ParseError, |
| 728 r"^my_file\.mojom:1: Error: Unexpected '\]':\n" |
| 729 r"\[MyAttribute=MyName,\] struct MyStruct {};$"): |
| 730 parser.Parse(source1, "my_file.mojom") |
| 731 |
| 732 # Missing value. |
| 733 source2 = "[MyAttribute=] struct MyStruct {};" |
| 734 with self.assertRaisesRegexp( |
| 735 parser.ParseError, |
| 736 r"^my_file\.mojom:1: Error: Unexpected '\]':\n" |
| 737 r"\[MyAttribute=\] struct MyStruct {};$"): |
| 738 parser.Parse(source2, "my_file.mojom") |
| 739 |
| 740 # Missing key. |
| 741 source3 = "[=MyName] struct MyStruct {};" |
| 742 with self.assertRaisesRegexp( |
| 743 parser.ParseError, |
| 744 r"^my_file\.mojom:1: Error: Unexpected '=':\n" |
| 745 r"\[=MyName\] struct MyStruct {};$"): |
| 746 parser.Parse(source3, "my_file.mojom") |
| 747 |
675 if __name__ == "__main__": | 748 if __name__ == "__main__": |
676 unittest.main() | 749 unittest.main() |
OLD | NEW |