def testTranslateSimpleUnions(self): """Makes sure that a simple union is translated correctly.""" tree = ast.Mojom(None, ast.ImportList(), [ ast.Union( "SomeUnion", ast.UnionBody([ ast.UnionField("a", None, "int32"), ast.UnionField("b", None, "string") ])) ]) expected = [{ "name": "SomeUnion", "fields": [{ "kind": "i32", "name": "a", "ordinal": None }, { "kind": "s", "name": "b", "ordinal": None }] }] actual = translate.Translate(tree, "mojom_tree") self.assertEquals(actual["union"], expected)
def testSelfRecursiveUnions(self): """Verifies _UnionField() raises when a union is self-recursive.""" tree = ast.Mojom(None, ast.ImportList(), [ ast.Union( "SomeUnion", None, ast.UnionBody([ast.UnionField("a", None, None, "SomeUnion")])) ]) with self.assertRaises(Exception): translate.OrderedModule(tree, "mojom_tree", []) tree = ast.Mojom(None, ast.ImportList(), [ ast.Union( "SomeUnion", None, ast.UnionBody([ast.UnionField("a", None, None, "SomeUnion?")])) ]) with self.assertRaises(Exception): translate.OrderedModule(tree, "mojom_tree", [])
def testUnionWithMapMember(self): """Test that map members are accepted.""" source = """\ module my_module; union MyUnion { map<int32, string> m; }; """ expected = ast.Mojom( ast.Module(('IDENTIFIER', 'my_module'), None), ast.ImportList(), [ast.Union( 'MyUnion', None, ast.UnionBody([ ast.UnionField('m', None, None, 'string{int32}') ]))]) actual = parser.Parse(source, "my_file.mojom") self.assertEquals(actual, expected)
def testUnionWithArrayMember(self): """Test that array members are accepted.""" source = """\ module my_module; union MyUnion { array<int32> a; }; """ expected = ast.Mojom( ast.Module(('IDENTIFIER', 'my_module'), None), ast.ImportList(), [ast.Union( 'MyUnion', None, ast.UnionBody([ ast.UnionField('a', None, None, 'int32[]') ]))]) actual = parser.Parse(source, "my_file.mojom") self.assertEquals(actual, expected)
def testUnionWithStructMembers(self): """Test that struct members are accepted.""" source = """\ module my_module; union MyUnion { SomeStruct s; }; """ expected = ast.Mojom( ast.Module(('IDENTIFIER', 'my_module'), None), ast.ImportList(), [ast.Union( 'MyUnion', None, ast.UnionBody([ ast.UnionField('s', None, None, 'SomeStruct') ]))]) actual = parser.Parse(source, "my_file.mojom") self.assertEquals(actual, expected)
def testUnionWithOrdinals(self): """Test that ordinals are assigned to fields.""" source = """\ module my_module; union MyUnion { int32 a @10; double b @30; }; """ expected = ast.Mojom( ast.Module(('IDENTIFIER', 'my_module'), None), ast.ImportList(), [ast.Union( 'MyUnion', None, ast.UnionBody([ ast.UnionField('a', None, ast.Ordinal(10), 'int32'), ast.UnionField('b', None, ast.Ordinal(30), 'double') ]))]) actual = parser.Parse(source, "my_file.mojom") self.assertEquals(actual, expected)
def testSimpleUnion(self): """Tests a simple .mojom source that just defines a union.""" source = """\ module my_module; union MyUnion { int32 a; double b; }; """ expected = ast.Mojom( ast.Module(('IDENTIFIER', 'my_module'), None), ast.ImportList(), [ast.Union( 'MyUnion', None, ast.UnionBody([ ast.UnionField('a', None, None, 'int32'), ast.UnionField('b', None, None, 'double') ]))]) actual = parser.Parse(source, "my_file.mojom") self.assertEquals(actual, expected)
def testTranslateSimpleUnions(self): """Makes sure that a simple union is translated correctly.""" tree = ast.Mojom(None, ast.ImportList(), [ ast.Union( "SomeUnion", None, ast.UnionBody([ ast.UnionField("a", None, None, "int32"), ast.UnionField("b", None, None, "string") ])) ]) translation = translate.OrderedModule(tree, "mojom_tree", []) self.assertEqual(1, len(translation.unions)) union = translation.unions[0] self.assertTrue(isinstance(union, mojom.Union)) self.assertEqual("SomeUnion", union.mojom_name) self.assertEqual(2, len(union.fields)) self.assertEqual("a", union.fields[0].mojom_name) self.assertEqual(mojom.INT32.spec, union.fields[0].kind.spec) self.assertEqual("b", union.fields[1].mojom_name) self.assertEqual(mojom.STRING.spec, union.fields[1].kind.spec)
def p_union(self, p): """union : attribute_section UNION NAME LBRACE union_body RBRACE SEMI""" p[0] = ast.Union(p[3], p[1], p[5])
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)