コード例 #1
0
ファイル: definition_test.py プロジェクト: Alattack/catapult
 def testImportFile(self):
   """Test importing FileDescriptor in to module space."""
   modules = {}
   file_descriptor = self.MakeFileDescriptor('standalone')
   definition.import_file(file_descriptor, modules=modules)
   self.assertEquals(file_descriptor,
                     descriptor.describe_file(modules['standalone']))
コード例 #2
0
ファイル: definition_test.py プロジェクト: Alattack/catapult
  def testImportBuiltInProtorpcClasses(self):
    """Test that built in Protorpc classes are skipped."""
    file_set = descriptor.FileSet()
    file_set.files = [self.MakeFileDescriptor(u'standalone'),
                      self.MakeFileDescriptor(u'root.nested'),
                      self.MakeFileDescriptor(u'root.nested.nested'),
                      descriptor.describe_file(descriptor),
    ]

    root = types.ModuleType('root')
    nested = types.ModuleType('root.nested')
    root.nested = nested
    modules = {
        'root': root,
        'root.nested': nested,
        'protorpc.descriptor': descriptor,
    }

    definition.import_file_set(file_set, modules=modules)

    self.assertEquals(root, modules['root'])
    self.assertEquals(nested, modules['root.nested'])
    self.assertEquals(nested.nested, modules['root.nested.nested'])
    self.assertEquals(descriptor, modules['protorpc.descriptor'])

    self.assertEquals(file_set,
                      descriptor.describe_file_set(
                          [modules['standalone'],
                           modules['root.nested'],
                           modules['root.nested.nested'],
                           modules['protorpc.descriptor'],
                          ]))
コード例 #3
0
 def testImportFile(self):
     """Test importing FileDescriptor in to module space."""
     modules = {}
     file_descriptor = self.MakeFileDescriptor('standalone')
     definition.import_file(file_descriptor, modules=modules)
     self.assertEquals(file_descriptor,
                       descriptor.describe_file(modules['standalone']))
コード例 #4
0
    def testImportBuiltInProtorpcClasses(self):
        """Test that built in Protorpc classes are skipped."""
        file_set = descriptor.FileSet()
        file_set.files = [
            self.MakeFileDescriptor(u'standalone'),
            self.MakeFileDescriptor(u'root.nested'),
            self.MakeFileDescriptor(u'root.nested.nested'),
            descriptor.describe_file(descriptor),
        ]

        root = types.ModuleType('root')
        nested = types.ModuleType('root.nested')
        root.nested = nested
        modules = {
            'root': root,
            'root.nested': nested,
            'protorpc.descriptor': descriptor,
        }

        definition.import_file_set(file_set, modules=modules)

        self.assertEquals(root, modules['root'])
        self.assertEquals(nested, modules['root.nested'])
        self.assertEquals(nested.nested, modules['root.nested.nested'])
        self.assertEquals(descriptor, modules['protorpc.descriptor'])

        self.assertEquals(
            file_set,
            descriptor.describe_file_set([
                modules['standalone'],
                modules['root.nested'],
                modules['root.nested.nested'],
                modules['protorpc.descriptor'],
            ]))
コード例 #5
0
ファイル: definition_test.py プロジェクト: Alattack/catapult
 def testImportFile_Nested(self):
   """Test importing FileDescriptor in to existing nested module."""
   modules = {}
   file_descriptor = self.MakeFileDescriptor('root.nested')
   definition.import_file(file_descriptor, modules=modules)
   self.assertEquals(modules['root'].nested, modules['root.nested'])
   self.assertEquals(file_descriptor,
                     descriptor.describe_file(modules['root.nested']))
コード例 #6
0
 def testImportFile_Nested(self):
     """Test importing FileDescriptor in to existing nested module."""
     modules = {}
     file_descriptor = self.MakeFileDescriptor('root.nested')
     definition.import_file(file_descriptor, modules=modules)
     self.assertEquals(modules['root'].nested, modules['root.nested'])
     self.assertEquals(file_descriptor,
                       descriptor.describe_file(modules['root.nested']))
コード例 #7
0
    def testNoPackageName(self):
        """Test describing a module with no module name."""
        module = types.ModuleType('')

        expected = descriptor.FileDescriptor()

        described = descriptor.describe_file(module)
        described.check_initialized()
        self.assertEquals(expected, described)
コード例 #8
0
  def testNoPackageName(self):
    """Test describing a module with no module name."""
    module = new.module('')

    expected = descriptor.FileDescriptor()

    described = descriptor.describe_file(module)
    described.check_initialized()
    self.assertEquals(expected, described)
コード例 #9
0
ファイル: definition_test.py プロジェクト: Alattack/catapult
 def testImportFile_InToExisting(self):
   """Test importing FileDescriptor in to existing module."""
   module = types.ModuleType('standalone')
   modules = {'standalone': module}
   file_descriptor = self.MakeFileDescriptor('standalone')
   definition.import_file(file_descriptor, modules=modules)
   self.assertEquals(module, modules['standalone'])
   self.assertEquals(file_descriptor,
                     descriptor.describe_file(modules['standalone']))
コード例 #10
0
 def testImportFile_InToExisting(self):
     """Test importing FileDescriptor in to existing module."""
     module = types.ModuleType('standalone')
     modules = {'standalone': module}
     file_descriptor = self.MakeFileDescriptor('standalone')
     definition.import_file(file_descriptor, modules=modules)
     self.assertEquals(module, modules['standalone'])
     self.assertEquals(file_descriptor,
                       descriptor.describe_file(modules['standalone']))
コード例 #11
0
  def testEmptyModule(self):
    """Test describing an empty file."""
    module = new.module('my.package.name')

    expected = descriptor.FileDescriptor()
    expected.package = 'my.package.name'

    described = descriptor.describe_file(module)
    described.check_initialized()
    self.assertEquals(expected, described)
コード例 #12
0
    def testEmptyModule(self):
        """Test describing an empty file."""
        module = types.ModuleType('my.package.name')

        expected = descriptor.FileDescriptor()
        expected.package = 'my.package.name'

        described = descriptor.describe_file(module)
        described.check_initialized()
        self.assertEquals(expected, described)
コード例 #13
0
  def testPackageName(self):
    """Test using the 'package' module attribute."""
    module = new.module('my.module.name')
    module.package = 'my.package.name'

    expected = descriptor.FileDescriptor()
    expected.package = 'my.package.name'

    described = descriptor.describe_file(module)
    described.check_initialized()
    self.assertEquals(expected, described)
コード例 #14
0
    def testMain(self):
        """Test using the 'package' module attribute."""
        module = types.ModuleType('__main__')
        module.__file__ = '/blim/blam/bloom/my_package.py'

        expected = descriptor.FileDescriptor()
        expected.package = 'my_package'

        described = descriptor.describe_file(module)
        described.check_initialized()
        self.assertEquals(expected, described)
コード例 #15
0
  def testMain(self):
    """Test using the 'package' module attribute."""
    module = new.module('__main__')
    module.__file__ = '/blim/blam/bloom/my_package.py'

    expected = descriptor.FileDescriptor()
    expected.package = 'my_package'

    described = descriptor.describe_file(module)
    described.check_initialized()
    self.assertEquals(expected, described)
コード例 #16
0
    def testPackageName(self):
        """Test using the 'package' module attribute."""
        module = types.ModuleType('my.module.name')
        module.package = 'my.package.name'

        expected = descriptor.FileDescriptor()
        expected.package = 'my.package.name'

        described = descriptor.describe_file(module)
        described.check_initialized()
        self.assertEquals(expected, described)
コード例 #17
0
ファイル: definition_test.py プロジェクト: Alattack/catapult
  def testDefineModule(self):
    """Test define_module function."""
    file_descriptor = self.MakeFileDescriptor('my.package')

    module = definition.define_file(file_descriptor)

    self.assertEquals('my.package', module.__name__)
    self.assertEquals('my.package', module.MyEnum.__module__)
    self.assertEquals('my.package', module.MyMessage.__module__)
    self.assertEquals('my.package', module.MyService.__module__)

    self.assertEquals(file_descriptor, descriptor.describe_file(module))
コード例 #18
0
    def testDefineModule(self):
        """Test define_module function."""
        file_descriptor = self.MakeFileDescriptor('my.package')

        module = definition.define_file(file_descriptor)

        self.assertEquals('my.package', module.__name__)
        self.assertEquals('my.package', module.MyEnum.__module__)
        self.assertEquals('my.package', module.MyMessage.__module__)
        self.assertEquals('my.package', module.MyService.__module__)

        self.assertEquals(file_descriptor, descriptor.describe_file(module))
コード例 #19
0
ファイル: definition_test.py プロジェクト: Alattack/catapult
 def testImportFile_InToGlobalModules(self):
   """Test importing FileDescriptor in to global modules."""
   original_modules = sys.modules
   try:
     sys.modules = dict(sys.modules)
     if 'standalone' in sys.modules:
       del sys.modules['standalone']
     file_descriptor = self.MakeFileDescriptor('standalone')
     definition.import_file(file_descriptor)
     self.assertEquals(file_descriptor,
                       descriptor.describe_file(sys.modules['standalone']))
   finally:
     sys.modules = original_modules
コード例 #20
0
 def testImportFile_InToGlobalModules(self):
     """Test importing FileDescriptor in to global modules."""
     original_modules = sys.modules
     try:
         sys.modules = dict(sys.modules)
         if 'standalone' in sys.modules:
             del sys.modules['standalone']
         file_descriptor = self.MakeFileDescriptor('standalone')
         definition.import_file(file_descriptor)
         self.assertEquals(
             file_descriptor,
             descriptor.describe_file(sys.modules['standalone']))
     finally:
         sys.modules = original_modules
コード例 #21
0
ファイル: definition_test.py プロジェクト: Alattack/catapult
  def testDefineModule_ReuseModule(self):
    """Test updating module with additional definitions."""
    file_descriptor = self.MakeFileDescriptor('my.package')

    module = types.ModuleType('override')
    self.assertEquals(module, definition.define_file(file_descriptor, module))

    self.assertEquals('override', module.MyEnum.__module__)
    self.assertEquals('override', module.MyMessage.__module__)
    self.assertEquals('override', module.MyService.__module__)

    # One thing is different between original descriptor and new.
    file_descriptor.package = 'override'
    self.assertEquals(file_descriptor, descriptor.describe_file(module))
コード例 #22
0
    def testDefineModule_ReuseModule(self):
        """Test updating module with additional definitions."""
        file_descriptor = self.MakeFileDescriptor('my.package')

        module = types.ModuleType('override')
        self.assertEquals(module,
                          definition.define_file(file_descriptor, module))

        self.assertEquals('override', module.MyEnum.__module__)
        self.assertEquals('override', module.MyMessage.__module__)
        self.assertEquals('override', module.MyService.__module__)

        # One thing is different between original descriptor and new.
        file_descriptor.package = 'override'
        self.assertEquals(file_descriptor, descriptor.describe_file(module))
コード例 #23
0
    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)
コード例 #24
0
    def testServices(self):
        """Test that services are described."""
        module = self.LoadModule(
            'my.package', 'class Service1(remote.Service): pass\n'
            'class Service2(remote.Service): pass\n')

        service1 = descriptor.ServiceDescriptor()
        service1.name = 'Service1'

        service2 = descriptor.ServiceDescriptor()
        service2.name = 'Service2'

        expected = descriptor.FileDescriptor()
        expected.package = 'my.package'
        expected.service_types = [service1, service2]

        described = descriptor.describe_file(module)
        described.check_initialized()
        self.assertEquals(expected, described)
コード例 #25
0
  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)
コード例 #26
0
    def testEnums(self):
        """Test that enums are described."""
        module = self.LoadModule(
            'my.package', 'class Enum1(messages.Enum): pass\n'
            'class Enum2(messages.Enum): pass\n')

        enum1 = descriptor.EnumDescriptor()
        enum1.name = 'Enum1'

        enum2 = descriptor.EnumDescriptor()
        enum2.name = 'Enum2'

        expected = descriptor.FileDescriptor()
        expected.package = 'my.package'
        expected.enum_types = [enum1, enum2]

        described = descriptor.describe_file(module)
        described.check_initialized()
        self.assertEquals(expected, described)
コード例 #27
0
  def testEnums(self):
    """Test that enums are described."""
    module = self.LoadModule('my.package',
                             'class Enum1(messages.Enum): pass\n'
                             'class Enum2(messages.Enum): pass\n')

    enum1 = descriptor.EnumDescriptor()
    enum1.name = 'Enum1'

    enum2 = descriptor.EnumDescriptor()
    enum2.name = 'Enum2'

    expected = descriptor.FileDescriptor()
    expected.package = 'my.package'
    expected.enum_types = [enum1, enum2]

    described = descriptor.describe_file(module)
    described.check_initialized()
    self.assertEquals(expected, described)
コード例 #28
0
  def testServices(self):
    """Test that services are described."""
    module = self.LoadModule('my.package',
                             'class Service1(remote.Service): pass\n'
                             'class Service2(remote.Service): pass\n')

    service1 = descriptor.ServiceDescriptor()
    service1.name = 'Service1'

    service2 = descriptor.ServiceDescriptor()
    service2.name = 'Service2'

    expected = descriptor.FileDescriptor()
    expected.package = 'my.package'
    expected.service_types = [service1, service2]

    described = descriptor.describe_file(module)
    described.check_initialized()
    self.assertEquals(expected, described)
コード例 #29
0
    def DoPythonTest(self, file_descriptor):
        """Execute python test based on a FileDescriptor object.

    The full test of the Python code generation is to generate a Python source
    code file, import the module and regenerate the FileDescriptor from it.
    If the generated FileDescriptor is the same as the original, it means that
    the generated source code correctly implements the actual FileDescriptor.
    """
        file_name = os.path.join(
            self.temp_dir, '%s.py' % (file_descriptor.package or 'blank', ))
        source_file = open(file_name, 'wt')
        try:
            generate_python.format_python_file(file_descriptor, source_file)
        finally:
            source_file.close()

        module_to_import = file_descriptor.package or 'blank'
        module = __import__(module_to_import)

        if not file_descriptor.package:
            self.assertFalse(hasattr(module, 'package'))
            module.package = ''  # Create package name so that comparison will work.

        reloaded_descriptor = descriptor.describe_file(module)

        # Need to sort both message_types fields because document order is never
        # Ensured.
        # TODO(rafek): Ensure document order.
        if reloaded_descriptor.message_types:
            reloaded_descriptor.message_types = sorted(
                reloaded_descriptor.message_types, key=lambda v: v.name)

        if file_descriptor.message_types:
            file_descriptor.message_types = sorted(
                file_descriptor.message_types, key=lambda v: v.name)

        self.assertEquals(file_descriptor, reloaded_descriptor)
コード例 #30
0
  def DoPythonTest(self, file_descriptor):
    """Execute python test based on a FileDescriptor object.

    The full test of the Python code generation is to generate a Python source
    code file, import the module and regenerate the FileDescriptor from it.
    If the generated FileDescriptor is the same as the original, it means that
    the generated source code correctly implements the actual FileDescriptor.
    """
    file_name = os.path.join(self.temp_dir,
                             '%s.py' % (file_descriptor.package or 'blank',))
    source_file = open(file_name, 'wt')
    try:
      generate_python.format_python_file(file_descriptor, source_file)
    finally:
      source_file.close()

    module_to_import = file_descriptor.package or 'blank'
    module = __import__(module_to_import)

    if not file_descriptor.package:
      self.assertFalse(hasattr(module, 'package'))
      module.package = ''  # Create package name so that comparison will work.

    reloaded_descriptor = descriptor.describe_file(module)

    # Need to sort both message_types fields because document order is never
    # Ensured.
    # TODO(rafek): Ensure document order.
    if reloaded_descriptor.message_types:
      reloaded_descriptor.message_types = sorted(
        reloaded_descriptor.message_types, key=lambda v: v.name)

    if file_descriptor.message_types:
      file_descriptor.message_types = sorted(
        file_descriptor.message_types, key=lambda v: v.name)

    self.assertEquals(file_descriptor, reloaded_descriptor)
コード例 #31
0
 def testModule(self):
     self.assertEquals(descriptor.describe_file(test_util),
                       descriptor.describe(test_util))
コード例 #32
0
 def testFindModule(self):
     self.assertEquals(
         descriptor.describe_file(registry),
         descriptor.import_descriptor_loader('protorpc.registry'))
コード例 #33
0
 def testFindModule(self):
   self.assertEquals(descriptor.describe_file(registry),
                     descriptor.import_descriptor_loader('protorpc.registry'))
コード例 #34
0
 def testModule(self):
   self.assertEquals(descriptor.describe_file(test_util),
                     descriptor.describe(test_util))
コード例 #35
0
ファイル: snippet.py プロジェクト: someburner/GistsHub
def format_proto_module(module, output, indent_space=2):
    file_descriptor = descriptor.describe_file(module)
    format_proto_file(file_descriptor, output, indent_space=indent_space)