def testMapTreeForTypeRaisesWithDuplicate(self): """Verifies _MapTreeForType() raises when passed two values with the same name.""" methods = [ ast.Method('dup', None, None, ast.ParameterList(), None), ast.Method('dup', None, None, ast.ParameterList(), None) ] self.assertRaises(Exception, translate._MapTreeForType, (lambda x: x, methods, '', 'scope'))
def testMapKindRaisesWithDuplicate(self): """Verifies _MapTreeForType() raises when passed two values with the same name.""" methods = [ ast.Method('dup', None, None, ast.ParameterList(), None), ast.Method('dup', None, None, ast.ParameterList(), None) ] with self.assertRaises(Exception): translate._ElemsOfType(methods, ast.Method, 'scope')
def testValidMethod(self): """Tests parsing method declarations.""" source1 = "interface MyInterface { MyMethod(int32 a); };" expected1 = ast.Mojom(None, ast.ImportList(), [ ast.Interface( 'MyInterface', None, ast.InterfaceBody( ast.Method( 'MyMethod', None, ast.ParameterList(ast.Parameter('a', None, 'int32')), None))) ]) self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) source2 = """\ interface MyInterface { MyMethod1@0(int32 a@0, int64 b@1); MyMethod2@1() => (); }; """ expected2 = ast.Mojom(None, ast.ImportList(), [ ast.Interface( 'MyInterface', None, ast.InterfaceBody([ ast.Method( 'MyMethod1', ast.Ordinal(0), ast.ParameterList([ ast.Parameter('a', ast.Ordinal(0), 'int32'), ast.Parameter('b', ast.Ordinal(1), 'int64') ]), None), ast.Method('MyMethod2', ast.Ordinal(1), ast.ParameterList(), ast.ParameterList()) ])) ]) self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) source3 = """\ interface MyInterface { MyMethod(string a) => (int32 a, bool b); }; """ expected3 = ast.Mojom(None, ast.ImportList(), [ ast.Interface( 'MyInterface', None, ast.InterfaceBody( ast.Method( 'MyMethod', None, ast.ParameterList(ast.Parameter('a', None, 'string')), ast.ParameterList([ ast.Parameter('a', None, 'int32'), ast.Parameter('b', None, 'bool') ])))) ]) self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3)
def testValidInterfaceDefinitions(self): """Tests all types of definitions that can occur in an interface.""" source = """\ interface MyInterface { enum MyEnum { VALUE }; const int32 kMyConst = 123; MyMethod(int32 x) => (MyEnum y); }; """ expected = ast.Mojom( None, ast.ImportList(), [ast.Interface( 'MyInterface', None, ast.InterfaceBody( [ast.Enum('MyEnum', None, ast.EnumValueList( ast.EnumValue('VALUE', None, None))), ast.Const('kMyConst', 'int32', '123'), ast.Method( 'MyMethod', None, None, ast.ParameterList(ast.Parameter('x', None, None, 'int32')), ast.ParameterList(ast.Parameter('y', None, None, 'MyEnum')))]))]) self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
def testValidAssociativeArrays(self): """Tests that we can parse valid associative array structures.""" source1 = "struct MyStruct { map<string, uint8> data; };" expected1 = ast.Mojom(None, ast.ImportList(), [ ast.Struct( 'MyStruct', None, ast.StructBody( [ast.StructField('data', None, 'uint8{string}', None)])) ]) self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) source2 = "interface MyInterface { MyMethod(map<string, uint8> a); };" expected2 = ast.Mojom(None, ast.ImportList(), [ ast.Interface( 'MyInterface', None, ast.InterfaceBody( ast.Method( 'MyMethod', None, ast.ParameterList( ast.Parameter('a', None, 'uint8{string}')), None))) ]) self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) source3 = "struct MyStruct { map<string, array<uint8>> data; };" expected3 = ast.Mojom(None, ast.ImportList(), [ ast.Struct( 'MyStruct', None, ast.StructBody( [ast.StructField('data', None, 'uint8[]{string}', None)])) ]) self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3)
def testValidAssociatedKinds(self): """Tests parsing associated interfaces and requests.""" source1 = """\ struct MyStruct { associated MyInterface a; associated MyInterface& b; associated MyInterface? c; associated MyInterface&? d; }; """ expected1 = ast.Mojom( None, ast.ImportList(), [ast.Struct( 'MyStruct', None, ast.StructBody( [ast.StructField('a', None, None,'asso<MyInterface>', None), ast.StructField('b', None, None,'asso<MyInterface&>', None), ast.StructField('c', None, None,'asso<MyInterface>?', None), ast.StructField('d', None, None,'asso<MyInterface&>?', None)]))]) self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) source2 = """\ interface MyInterface { MyMethod(associated A a) =>(associated B& b); };""" expected2 = ast.Mojom( None, ast.ImportList(), [ast.Interface( 'MyInterface', None, ast.InterfaceBody( ast.Method( 'MyMethod', None, None, ast.ParameterList( ast.Parameter('a', None, None, 'asso<A>')), ast.ParameterList( ast.Parameter('b', None, None, 'asso<B&>')))))]) self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2)
def p_method(self, p): """method : attribute_section NAME ordinal LPAREN parameter_list RPAREN \ response SEMI""" p[0] = ast.Method(p[2], p[1], p[3], p[5], p[7])
def testValidAttributes(self): """Tests parsing attributes (and attribute lists).""" # Note: We use structs because they have (optional) attribute lists. # Empty attribute list. source1 = "[] struct MyStruct {};" expected1 = ast.Mojom( None, ast.ImportList(), [ast.Struct('MyStruct', ast.AttributeList(), ast.StructBody())]) self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1) # One-element attribute list, with name value. source2 = "[MyAttribute=MyName] struct MyStruct {};" expected2 = ast.Mojom( None, ast.ImportList(), [ast.Struct( 'MyStruct', ast.AttributeList(ast.Attribute("MyAttribute", "MyName")), ast.StructBody())]) self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2) # Two-element attribute list, with one string value and one integer value. source3 = "[MyAttribute1 = \"hello\", MyAttribute2 = 5] struct MyStruct {};" expected3 = ast.Mojom( None, ast.ImportList(), [ast.Struct( 'MyStruct', ast.AttributeList([ast.Attribute("MyAttribute1", "hello"), ast.Attribute("MyAttribute2", 5)]), ast.StructBody())]) self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3) # Various places that attribute list is allowed. source4 = """\ [Attr0=0] module my_module; [Attr1=1] struct MyStruct { [Attr2=2] int32 a; }; [Attr3=3] union MyUnion { [Attr4=4] int32 a; }; [Attr5=5] enum MyEnum { [Attr6=6] a }; [Attr7=7] interface MyInterface { [Attr8=8] MyMethod([Attr9=9] int32 a) => ([Attr10=10] bool b); }; """ expected4 = ast.Mojom( ast.Module(('IDENTIFIER', 'my_module'), ast.AttributeList([ast.Attribute("Attr0", 0)])), ast.ImportList(), [ast.Struct( 'MyStruct', ast.AttributeList(ast.Attribute("Attr1", 1)), ast.StructBody( ast.StructField( 'a', ast.AttributeList([ast.Attribute("Attr2", 2)]), None, 'int32', None))), ast.Union( 'MyUnion', ast.AttributeList(ast.Attribute("Attr3", 3)), ast.UnionBody( ast.UnionField( 'a', ast.AttributeList([ast.Attribute("Attr4", 4)]), None, 'int32'))), ast.Enum( 'MyEnum', ast.AttributeList(ast.Attribute("Attr5", 5)), ast.EnumValueList( ast.EnumValue( 'VALUE', ast.AttributeList([ast.Attribute("Attr6", 6)]), None))), ast.Interface( 'MyInterface', ast.AttributeList(ast.Attribute("Attr7", 7)), ast.InterfaceBody( ast.Method( 'MyMethod', ast.AttributeList(ast.Attribute("Attr8", 8)), None, ast.ParameterList( ast.Parameter( 'a', ast.AttributeList([ast.Attribute("Attr9", 9)]), None, 'int32')), ast.ParameterList( ast.Parameter( 'b', ast.AttributeList([ast.Attribute("Attr10", 10)]), None, 'bool')))))]) self.assertEquals(parser.Parse(source4, "my_file.mojom"), expected4)