Move names to properties in code and templates, add tests

go-templates
Anthony Johnson 10 years ago
parent c4c16d6bf4
commit 5a5ae1cf15

@ -55,12 +55,9 @@ class DotNetDomain(AutoAPIDomain):
# TODO this should recurse in the case we're getting back more complex
# argument listings
if 'items' in data:
obj.children = []
obj.item_map = defaultdict(list)
for item in data['items']:
child_obj = self.create_class(item)
obj.children.append(child_obj)
obj.item_map[item['type']].append(child_obj)
return obj
@ -96,7 +93,7 @@ class DotNetDomain(AutoAPIDomain):
'''Organize objects and namespaces'''
def _recurse_ns(obj):
namespace = obj.namespace()
namespace = obj.namespace
if namespace is not None:
ns_obj = None
for (n, search_obj) in enumerate(self.app.env.autoapi_data):
@ -136,7 +133,6 @@ class DotNetDomain(AutoAPIDomain):
# for obj in objs:
# TODO not here!
obj.item_map = defaultdict(list)
for child in obj.children:
obj.item_map[child.type].append(child)
@ -176,19 +172,13 @@ class DotNetBase(AutoAPIBase):
super(DotNetBase, self).__init__(obj)
# Always exist
self.id = obj['id']
self.type = obj['type']
# Use name or id
try:
self.name = obj['qualifiedName']['CSharp']
except:
self.name = self.id
self.short_name = self.name.split('.')[-1]
# Optional
self.summary = obj.get('summary', '')
self.parameters = []
self.items = []
self.items = obj.get('items', [])
self.children = []
self.item_map = defaultdict(list)
# Syntax example and parameter list
syntax = obj.get('syntax', None)
@ -209,24 +199,33 @@ class DotNetBase(AutoAPIBase):
'desc': param.get('description', '')
})
self.items = obj.get('items', [])
def __str__(self):
return '<{cls} {id}>'.format(cls=self.__class__.__name__,
id=self.id)
@property
def name(self):
'''Return short name for member id
Use C# qualified name from deserialized data first, falling back to the
member id minus the namespace prefix
'''
try:
return self.obj['qualifiedName']['CSharp']
except KeyError:
return self.id
@property
def short_name(self):
'''Shorten name property'''
return self.name.split('.')[-1]
@property
def namespace(self):
pieces = self.id.split('.')[:-1]
if pieces:
return '.'.join(pieces)
@property
def ref_type(self):
return self.type.lower().replace('class', 'cls').replace('interface', 'iface').replace('delegate', 'del')
def to_ref_type(self, _type):
return _type.lower().replace('class', 'cls').replace('interface', 'iface').replace('delegate', 'del')
class DotNetNamespace(DotNetBase):
type = 'namespace'

@ -1,11 +1,7 @@
.. {{ obj.type.lower() }}:: {{ obj.name }}
.. {{ object.type }}:: {{ object.name }}
{% if summary %}
{{ obj.summary }}
{{ object.obj.summary }}
{% endif %}
.. code-block:: csharp
{{ obj.syntax.content.CSharp }}

@ -1,9 +1,9 @@
{% block title %}
{{ short_name }} {{ object.type.title()}}
{{ "=" * (short_name|length + object.type|length + 1) }}
{{ object.short_name }} {{ object.type.title()}}
{{ "=" * (object.short_name|length + object.type|length + 1) }}
.. dn:{{ object.type }}:: {{ name }}
.. dn:{{ object.type }}:: {{ object.name }}
{% endblock %}

@ -1,4 +1,4 @@
.. dn:{{ object.type }}:: {{ name }}
.. dn:{{ object.type }}:: {{ object.name }}
{% if summary %}

@ -1,9 +1,9 @@
{% block title %}
{{ short_name }} {{ object.type.title()}}
{{ "=" * (short_name|length + object.type|length + 1) }}
{{ object.short_name }} {{ object.type.title()}}
{{ "=" * (object.short_name|length + object.type|length + 1) }}
.. dn:{{ object.type }}:: {{ name }}
.. dn:{{ object.type }}:: {{ object.name }}
{% endblock %}

@ -60,7 +60,7 @@ class DomainTests(unittest.TestCase):
'type': 'Method'}
]})
self.assertIsInstance(cls, dotnet.DotNetClass)
self.assertIsInstance(cls.item_map['Method'][0], dotnet.DotNetMethod)
self.assertDictEqual(cls.item_map, {})
def test_get_objects(self):
'''Test basic get objects'''

@ -8,26 +8,47 @@ from autoapi.domains import dotnet
class NamespaceTests(unittest.TestCase):
def test_type(self):
'''Test types of some of the objects'''
obj = dotnet.DotNetNamespace({'id': 'Foo.Bar'})
self.assertEqual(obj.type, 'namespace')
self.assertEqual(obj.ref_type, 'ns')
obj = dotnet.DotNetMethod({'id': 'Foo.Bar'})
self.assertEqual(obj.type, 'method')
self.assertEqual(obj.ref_type, 'meth')
obj = dotnet.DotNetClass({'id': 'Foo.Bar'})
self.assertEqual(obj.type, 'class')
self.assertEqual(obj.ref_type, 'cls')
def test_names(self):
'''Test names of objects'''
obj = dotnet.DotNetNamespace({'id': 'Foo.Bar'})
self.assertEqual(obj.name, 'Foo.Bar')
self.assertEqual(obj.short_name, 'Bar')
obj = dotnet.DotNetNamespace({'id': 'Foo.Bar.Something`1'})
self.assertEqual(obj.name, 'Foo.Bar.Something`1')
self.assertEqual(obj.short_name, 'Something`1')
def test_namespace_namespace(self):
'''Namespace parent resolution'''
ns = dotnet.DotNetNamespace(dict(id='Foo.Bar.Widgets',
type='namespace'))
self.assertEqual(ns.namespace(), 'Foo.Bar')
ns = dotnet.DotNetNamespace(dict(id='Foo.Bar',
type='namespace'))
self.assertEqual(ns.namespace(), 'Foo')
ns = dotnet.DotNetNamespace(dict(id='Foo',
type='namespace'))
self.assertIsNone(ns.namespace())
ns = dotnet.DotNetNamespace({'id': 'Foo.Bar.Widgets'})
self.assertEqual(ns.namespace, 'Foo.Bar')
ns = dotnet.DotNetNamespace({'id': 'Foo.Bar'})
self.assertEqual(ns.namespace, 'Foo')
ns = dotnet.DotNetNamespace({'id': 'Foo'})
self.assertIsNone(ns.namespace)
def test_class_namespace(self):
'''Class parent resolution'''
cls = dotnet.DotNetClass(dict(id='Foo.Bar.Widget',
type='class'))
self.assertEqual(cls.namespace(), 'Foo.Bar')
self.assertEqual(cls.namespace, 'Foo.Bar')
cls = dotnet.DotNetClass(dict(id='Foo.Bar',
type='class'))
self.assertEqual(cls.namespace(), 'Foo')
self.assertEqual(cls.namespace, 'Foo')
cls = dotnet.DotNetClass(dict(id='Foo',
type='class'))
self.assertIsNone(cls.namespace())
self.assertIsNone(cls.namespace)

Loading…
Cancel
Save