sphinx-autoapi/tests/test_domains.py

197 lines
7.3 KiB
Python
Raw Normal View History

2019-01-27 05:20:45 +00:00
"""Test .NET autoapi domain"""
import unittest
from mock import patch
2015-06-10 21:23:50 +00:00
from autoapi.mappers import dotnet
class DotNetSphinxMapperTests(unittest.TestCase):
2015-06-01 19:37:22 +00:00
def setUp(self):
2019-01-27 05:20:45 +00:00
"""Test setup"""
class _config(object):
def __getattr__(self, key):
attrs = {
2019-01-27 05:20:45 +00:00
"autoapi_dirs": ["/tmp/autoapi/tmp"],
"autoapi_root": "/tmp/autoapi/root",
}
return attrs.get(key, None)
class _application(object):
config = _config()
2015-06-01 19:37:22 +00:00
def warn(self, *args, **kwargs):
pass
self.application = _application()
def test_create_class(self):
2019-01-27 05:20:45 +00:00
"""Test .NET class instance creation helper"""
2015-06-10 21:23:50 +00:00
dom = dotnet.DotNetSphinxMapper(self.application)
2015-06-01 19:37:22 +00:00
def _create_class(data):
return list(dom.create_class(data))[0]
2019-01-27 05:20:45 +00:00
cls = _create_class({"id": "Foo.Bar", "type": "Namespace"})
self.assertIsInstance(cls, dotnet.DotNetNamespace)
2019-01-27 05:20:45 +00:00
cls = _create_class({"id": "Foo.Bar", "type": "Class"})
self.assertIsInstance(cls, dotnet.DotNetClass)
2019-01-27 05:20:45 +00:00
cls = _create_class({"id": "Foo.Bar", "type": "Property"})
self.assertIsInstance(cls, dotnet.DotNetProperty)
2019-01-27 05:20:45 +00:00
cls = _create_class({"id": "Foo.Bar", "type": "Method"})
self.assertIsInstance(cls, dotnet.DotNetMethod)
2019-01-27 05:20:45 +00:00
cls = _create_class({"id": "Foo.Bar", "type": "Enum"})
self.assertIsInstance(cls, dotnet.DotNetEnum)
2019-01-27 05:20:45 +00:00
cls = _create_class({"id": "Foo.Bar", "type": "Constructor"})
self.assertIsInstance(cls, dotnet.DotNetConstructor)
2019-01-27 05:20:45 +00:00
cls = _create_class({"id": "Foo.Bar", "type": "Struct"})
self.assertIsInstance(cls, dotnet.DotNetStruct)
2019-01-27 05:20:45 +00:00
cls = _create_class({"id": "Foo.Bar", "type": "Interface"})
self.assertIsInstance(cls, dotnet.DotNetInterface)
2019-01-27 05:20:45 +00:00
cls = _create_class({"id": "Foo.Bar", "type": "Delegate"})
self.assertIsInstance(cls, dotnet.DotNetDelegate)
2019-01-27 05:20:45 +00:00
cls = _create_class({"id": "Foo.Bar", "type": "Field"})
self.assertIsInstance(cls, dotnet.DotNetField)
2019-01-27 05:20:45 +00:00
cls = _create_class({"id": "Foo.Bar", "type": "Event"})
self.assertIsInstance(cls, dotnet.DotNetEvent)
def test_create_class_with_children(self):
2015-06-10 21:23:50 +00:00
dom = dotnet.DotNetSphinxMapper(self.application)
2015-06-01 19:37:22 +00:00
def _create_class(data):
return list(dom.create_class(data))[0]
2019-01-27 05:20:45 +00:00
cls = _create_class(
{
"id": "Foo.Bar",
"type": "Class",
"items": [{"id": "Foo.Bar.Baz", "type": "Method"}],
}
)
self.assertIsInstance(cls, dotnet.DotNetClass)
self.assertDictEqual(cls.item_map, {})
2019-01-27 05:20:45 +00:00
@patch("subprocess.check_output", lambda foo: foo)
def test_get_objects(self):
2019-01-27 05:20:45 +00:00
"""Test basic get objects"""
objs = []
2015-08-05 17:27:35 +00:00
def _mock_find(self, patterns, **kwargs):
2019-01-27 05:20:45 +00:00
return {"items": ["foo", "bar"]}
2015-08-05 17:27:35 +00:00
def _mock_read(self, path):
2019-01-27 05:20:45 +00:00
return {
"items": [
{"id": "Foo.Bar", "name": "Foo", "type": "property"},
{"id": "Foo.Bar2", "name": "Bar", "type": "property"},
],
"id": "Foo.Bar",
"type": "Class",
"summary": path,
}
2015-08-05 17:27:35 +00:00
2019-01-27 05:20:45 +00:00
with patch("autoapi.mappers.dotnet.DotNetSphinxMapper.find_files", _mock_find):
with patch(
"autoapi.mappers.dotnet.DotNetSphinxMapper.read_file", _mock_read
):
2015-08-05 17:27:35 +00:00
dom = dotnet.DotNetSphinxMapper(self.application)
2019-01-27 05:20:45 +00:00
dom.load("", "", "", raise_error=False)
2015-08-05 17:27:35 +00:00
dom.map()
objs = dom.objects
self.assertEqual(len(objs), 2)
2019-01-27 05:20:45 +00:00
self.assertEqual(objs["Foo.Bar"].id, "Foo.Bar")
self.assertEqual(objs["Foo.Bar"].name, "Foo.Bar")
self.assertEqual(objs["Foo.Bar2"].id, "Foo.Bar2")
self.assertEqual(objs["Foo.Bar2"].name, "Foo.Bar2")
class DotNetPythonMapperTests(unittest.TestCase):
def test_xml_parse(self):
"""XML doc comment parsing"""
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
2019-01-27 05:20:45 +00:00
'This is an example comment <see cref="FOO" />'
)
self.assertEqual(ret, "This is an example comment :any:`FOO`")
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
2019-01-27 05:20:45 +00:00
'This is an example comment <see cref="!:FOO" />'
)
self.assertEqual(ret, "This is an example comment FOO")
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
2019-01-27 05:20:45 +00:00
'This is an example comment <see cref="N:FOO">inner foo</see>'
)
self.assertEqual(ret, "This is an example comment :dn:ns:`FOO`")
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
2019-01-27 05:20:45 +00:00
'Test <see cref="P:FOO" /> and <see cref="E:BAR">Blah</see>'
)
self.assertEqual(ret, "Test :dn:prop:`FOO` and :dn:event:`BAR`")
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
2019-01-27 05:20:45 +00:00
'This is an example comment <paramref name="FOO" />'
)
self.assertEqual(ret, "This is an example comment ``FOO``")
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
2019-01-27 05:20:45 +00:00
'This is an example comment <typeparamref name="FOO" />'
)
self.assertEqual(ret, "This is an example comment ``FOO``")
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
2019-01-27 05:20:45 +00:00
'With surrounding characters s<see cref="FOO" />s'
)
self.assertEqual(ret, "With surrounding characters s :any:`FOO`\s")
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
2019-01-27 05:20:45 +00:00
'With surrounding characters s<paramref name="FOO" />s'
)
self.assertEqual(ret, "With surrounding characters s ``FOO``\s")
def test_xml_transform_escape(self):
"""XML transform escaping"""
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
2019-01-27 05:20:45 +00:00
'Foo <see cref="Foo`1" /> Bar'
)
self.assertEqual(ret, "Foo :any:`Foo\\`1` Bar")
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
2019-01-27 05:20:45 +00:00
'No space before<see cref="M:Foo`1" />or after'
)
self.assertEqual(ret, "No space before :dn:meth:`Foo\\`1`\\or after")
def test_parsing_obj(self):
"""Parse out object, test for transforms, etc"""
obj = {
2019-01-27 05:20:45 +00:00
"uid": "Foo`1",
"name": "Foo<TUser>",
"summary": 'Test parsing <see cref="Bar" />',
"syntax": {
"parameters": [
{
"id": "a",
"type": "{TUser}",
"description": 'Test <see cref="TUser" />',
}
],
2019-01-27 05:20:45 +00:00
"return": {
"type": "Bar",
"description": (
'Test references <see cref="Bar" /> '
'and paramrefs <paramref name="a" />'
),
},
},
}
mapped = dotnet.DotNetPythonMapper(obj)
self.assertEqual(
mapped.parameters[0],
2019-01-27 05:20:45 +00:00
{"name": "a", "type": "{TUser}", "desc": "Test :any:`TUser`"},
)
self.assertEqual(
2019-01-27 05:20:45 +00:00
mapped.returns["description"],
"Test references :any:`Bar` and paramrefs ``a``",
)