def setUp(self):
     self.packageless = descriptor.MessageDescriptor()
     self.packageless.name = 'Packageless'
     self.library = descriptor.DescriptorLibrary(
         descriptors={
             'not.real.Packageless': self.packageless,
             'Packageless': self.packageless,
         })
    def testMessages(self):
        """Test that messages are described."""
        module = self.LoadModule(
            'my.package', 'class Message1(messages.Message): pass\n'
            'class Message2(messages.Message): pass\n')

        message1 = descriptor.MessageDescriptor()
        message1.name = 'Message1'

        message2 = descriptor.MessageDescriptor()
        message2.name = 'Message2'

        expected = descriptor.FileDescriptor()
        expected.package = 'my.package'
        expected.message_types = [message1, message2]

        described = descriptor.describe_file(module)
        described.check_initialized()
        self.assertEquals(expected, described)
    def testEmptyDefinition(self):
        class MyMessage(messages.Message):
            pass

        expected = descriptor.MessageDescriptor()
        expected.name = 'MyMessage'

        described = descriptor.describe_message(MyMessage)
        described.check_initialized()
        self.assertEquals(expected, described)
    def testNestedMessage(self):
        class MessageWithMessage(messages.Message):
            class Nesty(messages.Message):
                pass

        expected = descriptor.MessageDescriptor()
        expected.name = 'MessageWithMessage'

        expected.message_types = [
            descriptor.describe_message(MessageWithMessage.Nesty)
        ]

        described = descriptor.describe_message(MessageWithMessage)
        described.check_initialized()
        self.assertEquals(expected, described)
    def testDefinitionWithFields(self):
        class MessageWithFields(messages.Message):
            field1 = messages.IntegerField(10)
            field2 = messages.StringField(30)
            field3 = messages.IntegerField(20)

        expected = descriptor.MessageDescriptor()
        expected.name = 'MessageWithFields'

        expected.fields = [
            descriptor.describe_field(
                MessageWithFields.field_by_name('field1')),
            descriptor.describe_field(
                MessageWithFields.field_by_name('field3')),
            descriptor.describe_field(
                MessageWithFields.field_by_name('field2')),
        ]

        described = descriptor.describe_message(MessageWithFields)
        described.check_initialized()
        self.assertEquals(expected, described)
Esempio n. 6
0
    def testNestedEnum(self):
        class MessageWithEnum(messages.Message):

            class Mood(messages.Enum):
                GOOD = 1
                BAD = 2
                UGLY = 3

            class Music(messages.Enum):
                CLASSIC = 1
                JAZZ = 2
                BLUES = 3

        expected = descriptor.MessageDescriptor()
        expected.name = 'MessageWithEnum'

        expected.enum_types = [descriptor.describe_enum(MessageWithEnum.Mood),
                               descriptor.describe_enum(MessageWithEnum.Music)]

        described = descriptor.describe_message(MessageWithEnum)
        described.check_initialized()
        self.assertEquals(expected, described)