Coverage for tests/test_objects.py: 100.00%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Tests for [the `objects` module][pytkdocs.objects]."""
2import os
4from pytkdocs.loader import Loader
5from pytkdocs.objects import Attribute, Class, Function, Method, Module, Object
6from tests import FIXTURES_DIR
9def test_creating_module():
10 """Create a Module."""
11 assert Module(name="my_object", path="my.dotted.path", file_path="/my/absolute/path.py")
14def test_creating_class():
15 """Create a Class."""
16 assert Class(name="my_object", path="my.dotted.path", file_path="/my/absolute/path.py")
19def test_creating_method():
20 """Create a Method."""
21 assert Method(name="my_object", path="my.dotted.path", file_path="/my/absolute/path.py")
24def test_creating_function():
25 """Create a Function."""
26 assert Function(name="my_object", path="my.dotted.path", file_path="/my/absolute/path.py")
29def test_creating_attribute():
30 """Create an Attribute."""
31 assert Attribute(name="my_object", path="my.dotted.path", file_path="/my/absolute/path.py")
34def test_add_child():
35 """Add a child."""
36 parent = Module(name="my_module", path="my.dotted.path", file_path="/my/absolute/path.py")
37 child = Attribute(name="my_attribute", path="my.dotted.path.my_attribute", file_path="/my/absolute/path.py")
38 parent.add_child(child)
39 assert parent.children[0] is child
40 assert parent.attributes[0] is child
43def test_do_not_add_child_if_parent_is_not_self():
44 """Don't add a child the parent is not the right one."""
45 parent = Module(name="my_module", path="my.dotted.path", file_path="/my/absolute/path.py")
46 child = Attribute(name="my_attribute", path="my.other.path.my_attribute", file_path="/my/absolute/path.py")
47 parent.add_child(child)
48 assert not parent.children
49 assert not parent.attributes
52def test_get_root():
53 """Get the root object."""
54 root = Module(name="my_module", path="my.dotted.path", file_path="")
55 node1 = Class(name="my_class1", path="my.dotted.path.my_class1", file_path="")
56 node2 = Class(name="my_class2", path="my.dotted.path.my_class2", file_path="")
57 leaf = Method(name="my_method", path="my.dotted.path.my_class1.my_method", file_path="")
59 root.add_children([node1, node2])
60 node1.add_child(leaf)
62 assert root.root is root
63 assert node1.root is root
64 assert node2.root is root
65 assert leaf.root is root
68def test_relative_file_path_for_root():
69 """Get the relative file of a shallow object."""
70 obj = Object(
71 name="nested_class", path="tests.fixtures.nested_class", file_path=str(FIXTURES_DIR / "nested_class.py")
72 )
73 assert obj.relative_file_path == os.path.join("tests", "fixtures", "nested_class.py")
76def test_relative_file_path_for_leaf():
77 """Get the relative file path of a deep object."""
78 obj = Loader().get_object_documentation("tests.fixtures.pkg1")
79 leaf = obj.children[0].children[0].children[0].children[0]
80 assert leaf.relative_file_path == os.path.join(
81 "tests", "fixtures", "pkg1", "pkg2", "pkg3", "pkg4", "pkg5", "__init__.py"
82 )
85def test_no_relative_file_path_for_non_existent_package():
86 """Cannot find relative file path."""
87 obj = Object(name="o", path="a.b.o", file_path="/some/non_existent/path/a/b/o.py")
88 assert not obj.relative_file_path
91def test_no_relative_file_path_for_wrong_path():
92 """Cannot find relative file path with wrong dotted path."""
93 obj = Object(name="o", path="wrong.dotted.path", file_path=str(FIXTURES_DIR / "nested_class.py"))
94 assert not obj.relative_file_path
97def test_no_relative_file_path_for_wrong_file_path():
98 """Cannot find relative file path with wrong file path."""
99 obj = Object(name="o", path="tests.fixtures.nested_class", file_path="/wrong/module/path.py")
100 assert not obj.relative_file_path
103def test_add_children():
104 """Add multiple children at once."""
105 root = Object(name="o", path="o", file_path="o.py")
107 class_ = Class(name="c", path="o.c", file_path="o.py")
108 attribute = Attribute(name="a", path="o.c.a", file_path="o.py")
109 class_.add_child(attribute)
111 root.add_children(
112 [
113 # class has wrong path
114 Class(name="w", path="wrong.path.w", file_path="/wrong/path/w.py"),
115 # class OK
116 class_,
117 # not a direct child,
118 attribute,
119 # function OK
120 Function(name="f", path="o.f", file_path="o.py"),
121 # not a direct child, not even a child of known child
122 Method(name="missing_node", path="o.mn.missing_node", file_path="o.py"),
123 ]
124 )
126 assert len(root.children) == 2
127 assert root.classes and root.classes[0] is class_
128 assert root.functions and root.functions[0].name == "f"
131def test_has_contents():
132 """Check if an object has contents."""
133 obj = Loader().get_object_documentation("tests.fixtures.pkg1")
134 assert obj.has_contents()
136 obj = Loader().get_object_documentation("tests.fixtures.__init__")
137 assert not obj.children
138 assert obj.has_contents() # we specified that the root always 'has contents'
140 obj = Loader().get_object_documentation("tests.fixtures.no_contents")
141 assert obj.children
142 assert obj.has_contents
143 assert not obj.children[0].has_contents()
146def test_has_no_contents():
147 """Check that an object has no contents."""
148 pass # TODO