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

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

Issue 396733005: Mojo: Mojom: Add more parser tests for interfaces and structs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased 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 | « no previous file | 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 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 None, 169 None,
170 ast.ImportList(), 170 ast.ImportList(),
171 [ast.Struct( 171 [ast.Struct(
172 'MyStruct', 172 'MyStruct',
173 None, 173 None,
174 ast.StructBody( 174 ast.StructBody(
175 [ast.StructField('a', None, 'int32', None), 175 [ast.StructField('a', None, 'int32', None),
176 ast.StructField('b', None, 'double', None)]))]) 176 ast.StructField('b', None, 'double', None)]))])
177 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) 177 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
178 178
179 def testValidStructDefinitions(self):
180 """Tests all types of definitions that can occur in a struct."""
181
182 source = """\
183 struct MyStruct {
184 enum MyEnum { VALUE };
185 const double kMyConst = 1.23;
186 int32 a;
187 SomeOtherStruct b; // Invalidity detected at another stage.
188 };
189 """
190 expected = ast.Mojom(
191 None,
192 ast.ImportList(),
193 [ast.Struct(
194 'MyStruct',
195 None,
196 ast.StructBody(
197 [ast.Enum('MyEnum',
198 ast.EnumValueList(ast.EnumValue('VALUE', None))),
199 ast.Const('kMyConst', 'double', '1.23'),
200 ast.StructField('a', None, 'int32', None),
201 ast.StructField('b', None, 'SomeOtherStruct', None)]))])
202 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
203
204 def testInvalidStructDefinitions(self):
205 """Tests that definitions that aren't allowed in a struct are correctly
206 detected."""
207
208 source1 = """\
209 struct MyStruct {
210 MyMethod(int32 a);
211 };
212 """
213 with self.assertRaisesRegexp(
214 parser.ParseError,
215 r"^my_file\.mojom:2: Error: Unexpected '\(':\n"
216 r" *MyMethod\(int32 a\);$"):
217 parser.Parse(source1, "my_file.mojom")
218
219 source2 = """\
220 struct MyStruct {
221 struct MyInnerStruct {
222 int32 a;
223 };
224 };
225 """
226 with self.assertRaisesRegexp(
227 parser.ParseError,
228 r"^my_file\.mojom:2: Error: Unexpected 'struct':\n"
229 r" *struct MyInnerStruct {$"):
230 parser.Parse(source2, "my_file.mojom")
231
232 source3 = """\
233 struct MyStruct {
234 interface MyInterface {
235 MyMethod(int32 a);
236 };
237 };
238 """
239 with self.assertRaisesRegexp(
240 parser.ParseError,
241 r"^my_file\.mojom:2: Error: Unexpected 'interface':\n"
242 r" *interface MyInterface {$"):
243 parser.Parse(source3, "my_file.mojom")
244
179 def testMissingModuleName(self): 245 def testMissingModuleName(self):
180 """Tests an (invalid) .mojom with a missing module name.""" 246 """Tests an (invalid) .mojom with a missing module name."""
181 247
182 source1 = """\ 248 source1 = """\
183 // Missing module name. 249 // Missing module name.
184 module { 250 module {
185 struct MyStruct { 251 struct MyStruct {
186 int32 a; 252 int32 a;
187 }; 253 };
188 } 254 }
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 MyMethod1@0(int32 a@0, int64 b@1); 708 MyMethod1@0(int32 a@0, int64 b@1);
643 MyMethod2@1() => (); 709 MyMethod2@1() => ();
644 }; 710 };
645 """ 711 """
646 expected2 = ast.Mojom( 712 expected2 = ast.Mojom(
647 None, 713 None,
648 ast.ImportList(), 714 ast.ImportList(),
649 [ast.Interface( 715 [ast.Interface(
650 'MyInterface', 716 'MyInterface',
651 None, 717 None,
652 ast.InterfaceBody([ 718 ast.InterfaceBody(
653 ast.Method( 719 [ast.Method(
654 'MyMethod1', 720 'MyMethod1',
655 ast.Ordinal(0), 721 ast.Ordinal(0),
656 ast.ParameterList([ast.Parameter('a', ast.Ordinal(0), 722 ast.ParameterList([ast.Parameter('a', ast.Ordinal(0),
657 'int32'), 723 'int32'),
658 ast.Parameter('b', ast.Ordinal(1), 724 ast.Parameter('b', ast.Ordinal(1),
659 'int64')]), 725 'int64')]),
660 None), 726 None),
661 ast.Method( 727 ast.Method(
662 'MyMethod2', 728 'MyMethod2',
663 ast.Ordinal(1), 729 ast.Ordinal(1),
664 ast.ParameterList(), 730 ast.ParameterList(),
665 ast.ParameterList())]))]) 731 ast.ParameterList())]))])
666 self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) 732 self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2)
667 733
668 source3 = """\ 734 source3 = """\
669 interface MyInterface { 735 interface MyInterface {
670 MyMethod(string a) => (int32 a, bool b); 736 MyMethod(string a) => (int32 a, bool b);
671 }; 737 };
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 interface MyInterface { 771 interface MyInterface {
706 MyMethod(, string a); 772 MyMethod(, string a);
707 }; 773 };
708 """ 774 """
709 with self.assertRaisesRegexp( 775 with self.assertRaisesRegexp(
710 parser.ParseError, 776 parser.ParseError,
711 r"^my_file\.mojom:2: Error: Unexpected ',':\n" 777 r"^my_file\.mojom:2: Error: Unexpected ',':\n"
712 r" *MyMethod\(, string a\);$"): 778 r" *MyMethod\(, string a\);$"):
713 parser.Parse(source2, "my_file.mojom") 779 parser.Parse(source2, "my_file.mojom")
714 780
781 def testValidInterfaceDefinitions(self):
782 """Tests all types of definitions that can occur in an interface."""
783
784 source = """\
785 interface MyInterface {
786 enum MyEnum { VALUE };
787 const int32 kMyConst = 123;
788 MyMethod(int32 x) => (MyEnum y);
789 };
790 """
791 expected = ast.Mojom(
792 None,
793 ast.ImportList(),
794 [ast.Interface(
795 'MyInterface',
796 None,
797 ast.InterfaceBody(
798 [ast.Enum('MyEnum',
799 ast.EnumValueList(ast.EnumValue('VALUE', None))),
800 ast.Const('kMyConst', 'int32', '123'),
801 ast.Method(
802 'MyMethod',
803 None,
804 ast.ParameterList(ast.Parameter('x', None, 'int32')),
805 ast.ParameterList(ast.Parameter('y', None, 'MyEnum')))]))])
806 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
807
808 def testInvalidInterfaceDefinitions(self):
809 """Tests that definitions that aren't allowed in an interface are correctly
810 detected."""
811
812 source1 = """\
813 interface MyInterface {
814 struct MyStruct {
815 int32 a;
816 };
817 };
818 """
819 with self.assertRaisesRegexp(
820 parser.ParseError,
821 r"^my_file\.mojom:2: Error: Unexpected 'struct':\n"
822 r" *struct MyStruct {$"):
823 parser.Parse(source1, "my_file.mojom")
824
825 source2 = """\
826 interface MyInterface {
827 interface MyInnerInterface {
828 MyMethod(int32 x);
829 };
830 };
831 """
832 with self.assertRaisesRegexp(
833 parser.ParseError,
834 r"^my_file\.mojom:2: Error: Unexpected 'interface':\n"
835 r" *interface MyInnerInterface {$"):
836 parser.Parse(source2, "my_file.mojom")
837
838 source3 = """\
839 interface MyInterface {
840 int32 my_field;
841 };
842 """
843 # The parser thinks that "int32" is a plausible name for a method, so it's
844 # "my_field" that gives it away.
845 with self.assertRaisesRegexp(
846 parser.ParseError,
847 r"^my_file\.mojom:2: Error: Unexpected 'my_field':\n"
848 r" *int32 my_field;$"):
849 parser.Parse(source3, "my_file.mojom")
850
715 def testValidAttributes(self): 851 def testValidAttributes(self):
716 """Tests parsing attributes (and attribute lists).""" 852 """Tests parsing attributes (and attribute lists)."""
717 853
718 # Note: We use structs because they have (optional) attribute lists. 854 # Note: We use structs because they have (optional) attribute lists.
719 855
720 # Empty attribute list. 856 # Empty attribute list.
721 source1 = "[] struct MyStruct {};" 857 source1 = "[] struct MyStruct {};"
722 expected1 = ast.Mojom( 858 expected1 = ast.Mojom(
723 None, 859 None,
724 ast.ImportList(), 860 ast.ImportList(),
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 """ 969 """
834 with self.assertRaisesRegexp( 970 with self.assertRaisesRegexp(
835 parser.ParseError, 971 parser.ParseError,
836 r"^my_file\.mojom:2: Error: Unexpected 'module':\n" 972 r"^my_file\.mojom:2: Error: Unexpected 'module':\n"
837 r" *module {}$"): 973 r" *module {}$"):
838 parser.Parse(source2, "my_file.mojom") 974 parser.Parse(source2, "my_file.mojom")
839 975
840 976
841 if __name__ == "__main__": 977 if __name__ == "__main__":
842 unittest.main() 978 unittest.main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698