1# coding=utf8 2 3"""Test .NET autoapi objects""" 4 5from collections import namedtuple 6from unittest import mock 7import os 8 9from jinja2 import Environment, FileSystemLoader 10 11from autoapi.mappers import dotnet 12from autoapi.mappers import python 13from autoapi.settings import TEMPLATE_DIR 14 15 16class TestDotNetObject: 17 def test_type(self): 18 """Test types of some of the objects""" 19 obj = dotnet.DotNetNamespace({"id": "Foo.Bar"}, jinja_env=None, app=None) 20 assert obj.type == "namespace" 21 assert obj.ref_type == "namespace" 22 assert obj.ref_directive == "ns" 23 24 obj = dotnet.DotNetMethod({"id": "Foo.Bar"}, jinja_env=None, app=None) 25 assert obj.type == "method" 26 assert obj.ref_type == "method" 27 assert obj.ref_directive == "meth" 28 29 obj = dotnet.DotNetProperty({"id": "Foo.Bar"}, jinja_env=None, app=None) 30 assert obj.type == "property" 31 assert obj.ref_type == "property" 32 assert obj.ref_directive == "prop" 33 34 obj = dotnet.DotNetEnum({"id": "Foo.Bar"}, jinja_env=None, app=None) 35 assert obj.type == "enum" 36 assert obj.ref_type == "enumeration" 37 assert obj.ref_directive == "enum" 38 39 obj = dotnet.DotNetStruct({"id": "Foo.Bar"}, jinja_env=None, app=None) 40 assert obj.type == "struct" 41 assert obj.ref_type == "structure" 42 assert obj.ref_directive == "struct" 43 44 obj = dotnet.DotNetConstructor({"id": "Foo.Bar"}, jinja_env=None, app=None) 45 assert obj.type == "constructor" 46 assert obj.ref_type == "constructor" 47 assert obj.ref_directive == "ctor" 48 49 obj = dotnet.DotNetInterface({"id": "Foo.Bar"}, jinja_env=None, app=None) 50 assert obj.type == "interface" 51 assert obj.ref_type == "interface" 52 assert obj.ref_directive == "iface" 53 54 obj = dotnet.DotNetDelegate({"id": "Foo.Bar"}, jinja_env=None, app=None) 55 assert obj.type == "delegate" 56 assert obj.ref_type == "delegate" 57 assert obj.ref_directive == "del" 58 59 obj = dotnet.DotNetClass({"id": "Foo.Bar"}, jinja_env=None, app=None) 60 assert obj.type == "class" 61 assert obj.ref_type == "class" 62 assert obj.ref_directive == "cls" 63 64 obj = dotnet.DotNetField({"id": "Foo.Bar"}, jinja_env=None, app=None) 65 assert obj.type == "field" 66 assert obj.ref_type == "field" 67 assert obj.ref_directive == "field" 68 69 obj = dotnet.DotNetEvent({"id": "Foo.Bar"}, jinja_env=None, app=None) 70 assert obj.type == "event" 71 assert obj.ref_type == "event" 72 assert obj.ref_directive == "event" 73 74 def test_names(self): 75 """Test names of objects""" 76 obj = dotnet.DotNetNamespace({"id": "Foo.Bar"}, jinja_env=None, app=None) 77 assert obj.name == "Foo.Bar" 78 assert obj.short_name == "Bar" 79 80 obj = dotnet.DotNetNamespace( 81 {"id": "Foo.Bar.Something`1"}, jinja_env=None, app=None 82 ) 83 assert obj.name == "Foo.Bar.Something`1" 84 assert obj.short_name == "Something`1" 85 86 def test_namespace_namespace(self): 87 """Namespace parent resolution""" 88 ns = dotnet.DotNetNamespace({"id": "Foo.Bar.Widgets"}, jinja_env=None, app=None) 89 assert ns.namespace == "Foo.Bar" 90 ns = dotnet.DotNetNamespace({"id": "Foo.Bar"}, jinja_env=None, app=None) 91 assert ns.namespace == "Foo" 92 ns = dotnet.DotNetNamespace({"id": "Foo"}, jinja_env=None, app=None) 93 assert ns.namespace is None 94 95 def test_class_namespace(self): 96 """Class parent resolution""" 97 cls = dotnet.DotNetClass( 98 dict(id="Foo.Bar.Widget", type="class"), 99 jinja_env=None, 100 app=None, 101 ) 102 assert cls.namespace == "Foo.Bar" 103 cls = dotnet.DotNetClass( 104 dict(id="Foo.Bar", type="class"), jinja_env=None, app=None 105 ) 106 assert cls.namespace == "Foo" 107 cls = dotnet.DotNetClass(dict(id="Foo", type="class"), jinja_env=None, app=None) 108 assert cls.namespace is None 109 110 def test_filename(self): 111 """Object file name""" 112 cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget"}, jinja_env=None, app=None) 113 assert cls.pathname == os.path.join("Foo", "Bar", "Widget") 114 cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget<T>"}, jinja_env=None, app=None) 115 assert cls.pathname == os.path.join("Foo", "Bar", "Widget-T") 116 cls = dotnet.DotNetClass( 117 {"id": "Foo.Bar.Widget<T>(TFoo)"}, jinja_env=None, app=None 118 ) 119 assert cls.pathname == os.path.join("Foo", "Bar", "Widget-T") 120 cls = dotnet.DotNetClass( 121 {"id": "Foo.Foo-Bar.Widget<T>(TFoo)"}, jinja_env=None, app=None 122 ) 123 assert cls.pathname == os.path.join("Foo", "FooBar", "Widget-T") 124 cls = dotnet.DotNetClass({"id": u"Foo.Bär"}, jinja_env=None, app=None) 125 assert cls.pathname == os.path.join("Foo", "Bar") 126 cls = dotnet.DotNetClass({"id": u"Ащщ.юИфк"}, jinja_env=None, app=None) 127 assert cls.pathname == os.path.join("Ashchshch", "iuIfk") 128 129 def test_rendered_class_escaping(self): 130 """Rendered class escaping""" 131 jinja_env = Environment(loader=FileSystemLoader([TEMPLATE_DIR])) 132 cls = dotnet.DotNetClass( 133 {"id": "Foo.Bar`1", "inheritance": ["Foo.Baz`1"]}, 134 jinja_env=jinja_env, 135 app=mock.MagicMock(), 136 ) 137 assert "* :dn:cls:`Foo.Baz\\`1`\n" in cls.render() 138 139 def test_include_path(self): 140 """Include path""" 141 cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget"}, jinja_env=None, app=None) 142 assert cls.include_path == "/autoapi/Foo/Bar/Widget/index" 143 cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget"}, jinja_env=None, app=None) 144 cls.url_root = "/autofoo" 145 assert cls.include_path == "/autofoo/Foo/Bar/Widget/index" 146