2019-01-27 05:20:45 +00:00
|
|
|
"""Test .NET autoapi domain"""
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2021-04-04 02:01:27 +00:00
|
|
|
from unittest import mock
|
|
|
|
from unittest.mock import patch
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
from autoapi.mappers import dotnet
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2021-04-04 02:01:27 +00:00
|
|
|
class MockConfig:
|
|
|
|
def __getattr__(self, key):
|
|
|
|
attrs = {
|
|
|
|
"autoapi_dirs": ["/tmp/autoapi/tmp"],
|
|
|
|
"autoapi_root": "/tmp/autoapi/root",
|
|
|
|
}
|
|
|
|
return attrs.get(key, None)
|
2019-01-27 05:20:45 +00:00
|
|
|
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2021-04-04 02:01:27 +00:00
|
|
|
class MockApplication:
|
|
|
|
config = MockConfig()
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2021-04-04 02:01:27 +00:00
|
|
|
def warn(self, *args, **kwargs):
|
|
|
|
pass
|
2015-06-01 19:37:22 +00:00
|
|
|
|
|
|
|
|
2021-04-04 02:01:27 +00:00
|
|
|
class TestDotNetSphinxMapper:
|
2015-04-14 22:59:09 +00:00
|
|
|
def test_create_class(self):
|
2019-01-27 05:20:45 +00:00
|
|
|
"""Test .NET class instance creation helper"""
|
2021-04-04 02:01:27 +00:00
|
|
|
dom = dotnet.DotNetSphinxMapper(MockApplication())
|
2015-06-10 18:48:15 +00:00
|
|
|
|
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"})
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetNamespace)
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = _create_class({"id": "Foo.Bar", "type": "Class"})
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetClass)
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = _create_class({"id": "Foo.Bar", "type": "Property"})
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetProperty)
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = _create_class({"id": "Foo.Bar", "type": "Method"})
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetMethod)
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = _create_class({"id": "Foo.Bar", "type": "Enum"})
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetEnum)
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = _create_class({"id": "Foo.Bar", "type": "Constructor"})
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetConstructor)
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = _create_class({"id": "Foo.Bar", "type": "Struct"})
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetStruct)
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = _create_class({"id": "Foo.Bar", "type": "Interface"})
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetInterface)
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = _create_class({"id": "Foo.Bar", "type": "Delegate"})
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetDelegate)
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = _create_class({"id": "Foo.Bar", "type": "Field"})
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetField)
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = _create_class({"id": "Foo.Bar", "type": "Event"})
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetEvent)
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
def test_create_class_with_children(self):
|
2021-04-04 02:01:27 +00:00
|
|
|
dom = dotnet.DotNetSphinxMapper(MockApplication())
|
2015-06-10 18:48:15 +00:00
|
|
|
|
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"}],
|
|
|
|
}
|
|
|
|
)
|
2021-04-04 02:01:27 +00:00
|
|
|
assert isinstance(cls, dotnet.DotNetClass)
|
|
|
|
assert cls.item_map == {}
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2019-01-27 05:20:45 +00:00
|
|
|
@patch("subprocess.check_output", lambda foo: foo)
|
2015-04-14 22:59:09 +00:00
|
|
|
def test_get_objects(self):
|
2019-01-27 05:20:45 +00:00
|
|
|
"""Test basic get objects"""
|
2015-04-14 22:59:09 +00:00
|
|
|
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
|
|
|
|
):
|
2021-04-04 02:01:27 +00:00
|
|
|
dom = dotnet.DotNetSphinxMapper(MockApplication())
|
2021-04-13 03:00:35 +00:00
|
|
|
dom.load("", "", "")
|
2015-08-05 17:27:35 +00:00
|
|
|
dom.map()
|
|
|
|
objs = dom.objects
|
2021-04-04 02:01:27 +00:00
|
|
|
assert len(objs) == 2
|
|
|
|
assert objs["Foo.Bar"].id == "Foo.Bar"
|
|
|
|
assert objs["Foo.Bar"].name == "Foo.Bar"
|
|
|
|
assert objs["Foo.Bar2"].id == "Foo.Bar2"
|
|
|
|
assert objs["Foo.Bar2"].name == "Foo.Bar2"
|
2015-08-17 21:04:07 +00:00
|
|
|
|
2016-02-24 19:44:15 +00:00
|
|
|
|
2021-04-04 02:01:27 +00:00
|
|
|
class TestDotNetPythonMapper:
|
2015-08-17 21:04:07 +00:00
|
|
|
def test_xml_parse(self):
|
2016-02-24 19:44:15 +00:00
|
|
|
"""XML doc comment parsing"""
|
2015-08-17 21:04:07 +00:00
|
|
|
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
|
2019-01-27 05:20:45 +00:00
|
|
|
'This is an example comment <see cref="FOO" />'
|
|
|
|
)
|
2021-04-04 02:01:27 +00:00
|
|
|
assert ret == "This is an example comment :any:`FOO`"
|
2015-08-17 21:04:07 +00:00
|
|
|
|
|
|
|
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
|
2019-01-27 05:20:45 +00:00
|
|
|
'This is an example comment <see cref="!:FOO" />'
|
|
|
|
)
|
2021-04-04 02:01:27 +00:00
|
|
|
assert ret == "This is an example comment FOO"
|
2015-08-18 21:30:08 +00:00
|
|
|
|
|
|
|
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>'
|
|
|
|
)
|
2021-04-04 02:01:27 +00:00
|
|
|
assert ret == "This is an example comment :dn:ns:`FOO`"
|
2015-08-17 21:04:07 +00:00
|
|
|
|
|
|
|
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>'
|
|
|
|
)
|
2021-04-04 02:01:27 +00:00
|
|
|
assert ret == "Test :dn:prop:`FOO` and :dn:event:`BAR`"
|
2015-08-17 21:04:07 +00:00
|
|
|
|
|
|
|
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
|
2019-01-27 05:20:45 +00:00
|
|
|
'This is an example comment <paramref name="FOO" />'
|
|
|
|
)
|
2021-04-04 02:01:27 +00:00
|
|
|
assert ret == "This is an example comment ``FOO``"
|
2015-08-17 21:04:07 +00:00
|
|
|
|
|
|
|
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
|
2019-01-27 05:20:45 +00:00
|
|
|
'This is an example comment <typeparamref name="FOO" />'
|
|
|
|
)
|
2021-04-04 02:01:27 +00:00
|
|
|
assert ret == "This is an example comment ``FOO``"
|
2015-10-27 07:46:59 +00:00
|
|
|
|
2016-02-24 19:44:15 +00:00
|
|
|
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
|
2019-01-27 05:20:45 +00:00
|
|
|
'With surrounding characters s<see cref="FOO" />s'
|
|
|
|
)
|
2021-04-04 02:01:27 +00:00
|
|
|
assert ret == r"With surrounding characters s :any:`FOO`\s"
|
2016-02-24 19:44:15 +00:00
|
|
|
|
|
|
|
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
|
2019-01-27 05:20:45 +00:00
|
|
|
'With surrounding characters s<paramref name="FOO" />s'
|
|
|
|
)
|
2021-04-04 02:01:27 +00:00
|
|
|
assert ret == r"With surrounding characters s ``FOO``\s"
|
2016-02-24 19:44:15 +00:00
|
|
|
|
2015-10-27 07:46:59 +00:00
|
|
|
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'
|
|
|
|
)
|
2021-04-04 02:01:27 +00:00
|
|
|
assert ret == "Foo :any:`Foo\\`1` Bar"
|
2015-10-27 07:46:59 +00:00
|
|
|
|
|
|
|
ret = dotnet.DotNetPythonMapper.transform_doc_comments(
|
2019-01-27 05:20:45 +00:00
|
|
|
'No space before<see cref="M:Foo`1" />or after'
|
|
|
|
)
|
2021-04-04 02:01:27 +00:00
|
|
|
assert ret == "No space before :dn:meth:`Foo\\`1`\\or after"
|
2016-02-24 19:44:15 +00:00
|
|
|
|
|
|
|
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" />',
|
|
|
|
}
|
2016-02-24 19:44:15 +00:00
|
|
|
],
|
2019-01-27 05:20:45 +00:00
|
|
|
"return": {
|
|
|
|
"type": "Bar",
|
|
|
|
"description": (
|
|
|
|
'Test references <see cref="Bar" /> '
|
|
|
|
'and paramrefs <paramref name="a" />'
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
2016-02-24 19:44:15 +00:00
|
|
|
}
|
2020-01-25 22:50:35 +00:00
|
|
|
mapped = dotnet.DotNetPythonMapper(obj, app=mock.MagicMock(), jinja_env=None)
|
2021-04-04 02:01:27 +00:00
|
|
|
expected = {"name": "a", "type": "{TUser}", "desc": "Test :any:`TUser`"}
|
|
|
|
assert mapped.parameters[0] == expected
|
|
|
|
assert (
|
|
|
|
mapped.returns["description"]
|
|
|
|
== "Test references :any:`Bar` and paramrefs ``a``"
|
2016-02-24 19:44:15 +00:00
|
|
|
)
|