mojo/public/tools/mojom/mojom/parse/parser_unittest.py
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] import "my_import";

        [Attr2=2] struct MyStruct {
          [Attr3=3] int32 a;
        };
        [Attr4=4] union MyUnion {
          [Attr5=5] int32 a;
        };
        [Attr6=6] enum MyEnum {
          [Attr7=7] a
        };
        [Attr8=8] interface MyInterface {
          [Attr9=9] MyMethod([Attr10=10] int32 a) => ([Attr11=11] bool b);
        };
        [Attr12=12] const double kMyConst = 1.23;
        """
    expected4 = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'),
                   ast.AttributeList([ast.Attribute("Attr0", 0)])),
        ast.ImportList(
            ast.Import(
                ast.AttributeList([ast.Attribute("Attr1", 1)]), "my_import")),
        [
            ast.Struct(
                'MyStruct', ast.AttributeList(ast.Attribute("Attr2", 2)),
                ast.StructBody(
                    ast.StructField(
                        'a', ast.AttributeList([ast.Attribute("Attr3", 3)]),
                        None, 'int32', None))),
            ast.Union(
                'MyUnion', ast.AttributeList(ast.Attribute("Attr4", 4)),
                ast.UnionBody(
                    ast.UnionField(
                        'a', ast.AttributeList([ast.Attribute("Attr5", 5)]),
                        None, 'int32'))),
            ast.Enum(
                'MyEnum', ast.AttributeList(ast.Attribute("Attr6", 6)),
                ast.EnumValueList(
                    ast.EnumValue(
                        'VALUE', ast.AttributeList([ast.Attribute("Attr7", 7)]),
                        None))),
            ast.Interface(
                'MyInterface', ast.AttributeList(ast.Attribute("Attr8", 8)),
                ast.InterfaceBody(
                    ast.Method(
                        'MyMethod', ast.AttributeList(
                            ast.Attribute("Attr9", 9)), None,
                        ast.ParameterList(
                            ast.Parameter(
                                'a',
                                ast.AttributeList([ast.Attribute("Attr10", 10)
                                                   ]), None, 'int32')),
                        ast.ParameterList(
                            ast.Parameter(
                                'b',
                                ast.AttributeList([ast.Attribute("Attr11", 11)
                                                   ]), None, 'bool'))))),
            ast.Const('kMyConst', ast.AttributeList(
                ast.Attribute("Attr12", 12)), 'double', '1.23')
        ])
    self.assertEquals(parser.Parse(source4, "my_file.mojom"), expected4)

    # TODO(vtl): Boolean attributes don't work yet. (In fact, we just |eval()|
    # literal (non-name) values, which is extremely dubious.)
Tests parsing attributes (and attribute lists).
Tests parsing attributes (and attribute lists).
Tests parsing attributes ( and attribute lists ) .
def testValidAttributes(self):
    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)
    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)
    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)
    source4 = """\
        [Attr0=0] module my_module;
        [Attr1=1] import "my_import";
        [Attr2=2] struct MyStruct {
          [Attr3=3] int32 a;
        };
        [Attr4=4] union MyUnion {
          [Attr5=5] int32 a;
        };
        [Attr6=6] enum MyEnum {
          [Attr7=7] a
        };
        [Attr8=8] interface MyInterface {
          [Attr9=9] MyMethod([Attr10=10] int32 a) => ([Attr11=11] bool b);
        };
        [Attr12=12] const double kMyConst = 1.23;
        """
    expected4 = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'),
                   ast.AttributeList([ast.Attribute("Attr0", 0)])),
        ast.ImportList(
            ast.Import(
                ast.AttributeList([ast.Attribute("Attr1", 1)]), "my_import")),
        [
            ast.Struct(
                'MyStruct', ast.AttributeList(ast.Attribute("Attr2", 2)),
                ast.StructBody(
                    ast.StructField(
                        'a', ast.AttributeList([ast.Attribute("Attr3", 3)]),
                        None, 'int32', None))),
            ast.Union(
                'MyUnion', ast.AttributeList(ast.Attribute("Attr4", 4)),
                ast.UnionBody(
                    ast.UnionField(
                        'a', ast.AttributeList([ast.Attribute("Attr5", 5)]),
                        None, 'int32'))),
            ast.Enum(
                'MyEnum', ast.AttributeList(ast.Attribute("Attr6", 6)),
                ast.EnumValueList(
                    ast.EnumValue(
                        'VALUE', ast.AttributeList([ast.Attribute("Attr7", 7)]),
                        None))),
            ast.Interface(
                'MyInterface', ast.AttributeList(ast.Attribute("Attr8", 8)),
                ast.InterfaceBody(
                    ast.Method(
                        'MyMethod', ast.AttributeList(
                            ast.Attribute("Attr9", 9)), None,
                        ast.ParameterList(
                            ast.Parameter(
                                'a',
                                ast.AttributeList([ast.Attribute("Attr10", 10)
                                                   ]), None, 'int32')),
                        ast.ParameterList(
                            ast.Parameter(
                                'b',
                                ast.AttributeList([ast.Attribute("Attr11", 11)
                                                   ]), None, 'bool'))))),
            ast.Const('kMyConst', ast.AttributeList(
                ast.Attribute("Attr12", 12)), 'double', '1.23')
        ])
    self.assertEquals(parser.Parse(source4, "my_file.mojom"), expected4)
def testValidAttributes ( self ) : 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 ) 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 ) 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 ) source4 = """\
        [Attr0=0] module my_module;

        [Attr1=1] import "my_import";

        [Attr2=2] struct MyStruct {
          [Attr3=3] int32 a;
        };
        [Attr4=4] union MyUnion {
          [Attr5=5] int32 a;
        };
        [Attr6=6] enum MyEnum {
          [Attr7=7] a
        };
        [Attr8=8] interface MyInterface {
          [Attr9=9] MyMethod([Attr10=10] int32 a) => ([Attr11=11] bool b);
        };
        [Attr12=12] const double kMyConst = 1.23;
        """ expected4 = ast . Mojom ( ast . Module ( ( 'IDENTIFIER' , 'my_module' ) , ast . AttributeList ( [ ast . Attribute ( "Attr0" , 0 ) ] ) ) , ast . ImportList ( ast . Import ( ast . AttributeList ( [ ast . Attribute ( "Attr1" , 1 ) ] ) , "my_import" ) ) , [ ast . Struct ( 'MyStruct' , ast . AttributeList ( ast . Attribute ( "Attr2" , 2 ) ) , ast . StructBody ( ast . StructField ( 'a' , ast . AttributeList ( [ ast . Attribute ( "Attr3" , 3 ) ] ) , None , 'int32' , None ) ) ) , ast . Union ( 'MyUnion' , ast . AttributeList ( ast . Attribute ( "Attr4" , 4 ) ) , ast . UnionBody ( ast . UnionField ( 'a' , ast . AttributeList ( [ ast . Attribute ( "Attr5" , 5 ) ] ) , None , 'int32' ) ) ) , ast . Enum ( 'MyEnum' , ast . AttributeList ( ast . Attribute ( "Attr6" , 6 ) ) , ast . EnumValueList ( ast . EnumValue ( 'VALUE' , ast . AttributeList ( [ ast . Attribute ( "Attr7" , 7 ) ] ) , None ) ) ) , ast . Interface ( 'MyInterface' , ast . AttributeList ( ast . Attribute ( "Attr8" , 8 ) ) , ast . InterfaceBody ( ast . Method ( 'MyMethod' , ast . AttributeList ( ast . Attribute ( "Attr9" , 9 ) ) , None , ast . ParameterList ( ast . Parameter ( 'a' , ast . AttributeList ( [ ast . Attribute ( "Attr10" , 10 ) ] ) , None , 'int32' ) ) , ast . ParameterList ( ast . Parameter ( 'b' , ast . AttributeList ( [ ast . Attribute ( "Attr11" , 11 ) ] ) , None , 'bool' ) ) ) ) ) , ast . Const ( 'kMyConst' , ast . AttributeList ( ast . Attribute ( "Attr12" , 12 ) ) , 'double' , '1.23' ) ] ) self . assertEquals ( parser . Parse ( source4 , "my_file.mojom" ) , expected4 )
Tests parsing attributes (and attribute lists).
Tests parsing attributes ( and attribute lists ) .
"""Tests parsing attributes (and attribute lists).""" # Note: We use structs because they have (optional) attribute lists. # Empty attribute list. # One-element attribute list, with name value. # Two-element attribute list, with one string value and one integer value. # Various places that attribute list is allowed. # TODO(vtl): Boolean attributes don't work yet. (In fact, we just |eval()| # literal (non-name) values, which is extremely dubious.)
self