def testVersionPackage(self): gen = cpp_generator.CppGenerator(discovery={ 'name': 'dummy', 'version': 'v1', 'resources': {}, 'schemas': { 'Bar': { 'id': 'Bar', 'type': 'object', 'properties': { 'n32': { 'type': 'integer', 'format': 'int32', }, 'un64': { 'type': 'integer', 'format': 'uint64', }, } } } }, options={'version_package': True}) gen.AnnotateApiForLanguage(gen.api) self.assertEquals('google/dummy_api_v1', gen.api.module.path) self.assertEquals('google_dummy_api_v1', gen.api.module.name)
def testReservedWords(self): gen = cpp_generator.CppGenerator({ 'name': 'dummy', 'version': 'v1', 'schemas': { 'Bar': { 'id': 'Bar', 'type': 'object', 'properties': { 'n32': { 'type': 'integer', 'format': 'int32', }, 'class': { 'type': 'string', }, } } }, 'resources': { 'foo': { 'methods': { 'bar': { 'id': 'foo.bar', 'parameters': { 'aJavaStyleName': { 'location': 'path', 'required': True, 'type': 'int', }, 'namespace': { 'location': 'path', 'required': True, 'type': 'int', }, } } } } } }) gen.AnnotateApiForLanguage(gen.api) bar_schema = gen.api.SchemaByName('Bar') for p in bar_schema.values['properties']: wire_name = p.GetTemplateValue('wireName') if wire_name == 'class': self.assertEquals('class_', p.memberName) self.assertEquals('class__', p.parameterName) bar_method = gen.api.MethodByName('foo.bar') self.assertEquals(2, len(bar_method.required_parameters)) for p in bar_method.required_parameters: wire_name = p.GetTemplateValue('wireName') if wire_name == 'aJavaStyleName': self.assertEquals('a_java_style_name_', p.memberName) self.assertEquals('a_java_style_name', p.parameterName) if wire_name == 'namespace': self.assertEquals('namespace_', p.memberName) self.assertEquals('namespace__', p.parameterName)
def MakeGen(owner, the_name='fake', package_path=None): api_def = { 'name': the_name, 'version': 'v1', 'rootUrl': 'https://test.domain/X%sY' % owner, 'servicePath': 'fake/v1', 'domain': 'domain.com', 'owner': owner, } if package_path: api_def['packagePath'] = package_path gen = cpp_generator.CppGenerator(api_def) gen.AnnotateApiForLanguage(gen.api) return gen
def testDocumentation(self): """Test if we get the right documentation annotations. """ gen = cpp_generator.CppGenerator(discovery={ 'name': 'dummy', 'version': 'v1', 'resources': {}, 'schemas': {} }) # No DL case self.assertEqual(gen._DLifyDescription('simple text'), 'simple text') # No leading text before the <dl>. self.assertEqual( gen._DLifyDescription('A - description A\nB - description B'), '<dl>\n<dt>A\n<dd>description A.\n<dt>B\n<dd>description B.\n</dl>\n' ) # Leading text before the <dl>. self.assertEqual( gen._DLifyDescription('Text before:' '\nA - description A\nB - description B\n' 'Text after.'), 'Text before:\n<dl>\n<dt>A\n<dd>description A.\n' '<dt>B\n<dd>description B.\n</dl>\n' 'Text after.') # No line breaks after last element (short) self.assertEqual( gen._DLifyDescription('Text before:' '\nA - description A\n- B - description B. ' 'Text after.'), 'Text before:\n<dl>\n<dt>A\n<dd>description A.\n' '<dt>B\n<dd>description B. Text after.\n</dl>\n') # No line breaks after last element (long) blurb = ( 'This text is a bit long. At some point it will cross the threshold ' 'where we consider it runoff. That means we will step back and ' 'truncate the last element in the dl list so that it just includes ' 'the first sentence. All the rest of the sentences, such as these, ' 'will appear after we close the dl.') self.assertEqual( gen._DLifyDescription( 'Text before:' '\nA - description A\n- B - description B. %s' % blurb), 'Text before:\n<dl>\n<dt>A\n<dd>description A.\n' '<dt>B\n<dd>description B.\n</dl>\n %s' % blurb)
def testImports(self): gen = cpp_generator.CppGenerator({ 'name': 'dummy', 'version': 'v1', 'resources': {}, 'schemas': { 'Foo': { 'id': 'Foo', 'type': 'object', 'properties': { 's': { 'type': 'string', } } }, 'Bar': { 'id': 'Bar', 'type': 'object', 'properties': { 'n32': { 'type': 'integer', 'format': 'int32', }, 'un64': { 'type': 'integer', 'format': 'uint64', }, 'foo': { 'type': 'array', 'items': { '$ref': 'Foo' } }, } } } }) gen.AnnotateApiForLanguage(gen.api) foo_schema = gen.api._schemas['Foo'] self.assertEqual('Foo', foo_schema.values.get('id')) foo_import_manager = foo_schema.values.get('importManager') self.assertTrue('<string>' in foo_import_manager.platform_imports) bar_schema = gen.api._schemas['Bar'] self.assertEqual('Bar', bar_schema.values.get('id')) bar_import_manager = bar_schema.values.get('importManager') self.assertTrue('"googleapis/base/integral_types.h"' in bar_import_manager.google_imports)
def testImportsForArray(self): """Test if we get the right imports for an array. The goal is to see that an array of a primative type which requires an import really works. """ gen = cpp_generator.CppGenerator({ 'name': 'dummy', 'version': 'v1', 'resources': {}, 'schemas': { 'Bar': { 'id': 'Bar', 'type': 'object', 'properties': { 'p1': { 'type': 'array', 'items': { 'type': 'string', 'format': 'uint64' } }, 'p2': { 'type': 'array', 'items': { 'type': 'array', 'items': { 'type': 'string', 'format': 'date-time' } } } } } } }) gen.AnnotateApiForLanguage(gen.api)