Coverage for tests/test_parsers/test_attributes.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 `parsers.attributes` module][pytkdocs.parsers.attributes]."""
3from pytkdocs.parsers.attributes import get_class_attributes, get_instance_attributes, get_module_attributes
4from tests.fixtures.parsing import attributes as attr_module
7class TestParsing:
8 """Test the parser in general."""
10 def setup(self):
11 """Setup reusable attributes."""
12 self.attributes = get_module_attributes(attr_module)
14 def test_parse_tuple_target(self):
15 """Assert can parse `a, b, c = 0, 0, 0`."""
16 assert "OK" in self.attributes
17 assert "WARNING" in self.attributes
18 assert "CRITICAL" in self.attributes
19 assert "UNKNOWN" in self.attributes
22class TestModuleAttributes:
23 """Test the parser for module attributes."""
25 def setup(self):
26 """Setup reusable attributes."""
27 self.attributes = get_module_attributes(attr_module)
29 def test_pick_up_attribute_without_docstring(self):
30 """Don't pick attributes without docstrings."""
31 assert "NO_DOC_NO_TYPE" in self.attributes
32 assert "NO_DOC_NO_VALUE" in self.attributes
33 assert "NO_DOC" in self.attributes
35 def test_pick_up_attribute_without_type(self):
36 """Pick up attribute without a type."""
37 assert "NO_TYPE" in self.attributes
38 assert self.attributes["NO_TYPE"]["docstring"] == "No type."
40 def test_pick_up_attribute_without_value(self):
41 """Pick up attribute without a value."""
42 assert "NO_VALUE" in self.attributes
43 assert self.attributes["NO_VALUE"]["docstring"] == "No value."
45 def test_pick_up_attribute_with_type_and_value(self):
46 """Pick up attribute with type and value."""
47 assert "FULL" in self.attributes
48 assert self.attributes["FULL"]["docstring"] == "Full."
50 def test_pick_up_attribute_with_complex_type(self):
51 """Pick up attribute with complex type."""
52 assert "COMPLEX_TYPE" in self.attributes
53 assert self.attributes["COMPLEX_TYPE"]["docstring"] == "Complex type."
55 def test_pick_up_attribute_in_if(self):
56 """Pick attribute in `if` and `else`."""
57 assert "IN_IF" in self.attributes
58 assert self.attributes["IN_IF"]["docstring"] == "In if."
60 assert "IN_ELSE" in self.attributes
61 assert self.attributes["IN_ELSE"]["docstring"] == "In else."
63 def test_pick_up_attribute_in_try_except(self):
64 """Pick attribute in `try`, `except`, `else` and `finally`.."""
65 assert "IN_TRY" in self.attributes
66 assert self.attributes["IN_TRY"]["docstring"] == "In try."
68 assert "IN_EXCEPT" in self.attributes
69 assert self.attributes["IN_EXCEPT"]["docstring"] == "In except."
71 assert "IN_TRY_ELSE" in self.attributes
72 assert self.attributes["IN_TRY_ELSE"]["docstring"] == "In try else."
74 assert "IN_FINALLY" in self.attributes
75 assert self.attributes["IN_FINALLY"]["docstring"] == "In finally."
77 def test_docstring_is_correctly_dedented(self):
78 assert "\n " not in self.attributes["DEDENT"]["docstring"]
81class TestClassAttributes:
82 """Test the parser for module attributes."""
84 def setup(self):
85 """Setup reusable attributes."""
86 self.attributes = get_class_attributes(attr_module.E)
88 def test_pick_up_attribute_in_class(self):
89 """Pick up class attribute."""
90 assert "IN_CLASS" in self.attributes
91 assert self.attributes["IN_CLASS"]["docstring"] == "In class."
93 def test_docstring_is_correctly_dedented(self):
94 assert "\n " not in self.attributes["DEDENT"]["docstring"]
97class TestInstanceAttributes:
98 """Test the parser for module attributes."""
100 def setup(self):
101 """Setup reusable attributes."""
102 self.attributes = get_instance_attributes(attr_module.E.__init__)
104 def test_pick_up_attribute_in_init_method(self):
105 """Pick up instance attribute."""
106 assert "in_init" in self.attributes
107 assert self.attributes["in_init"]["docstring"] == "In init."
109 def test_do_not_pick_up_non_attributes(self):
110 """Don't pick documented variables in functions."""
111 assert "non_attribute" not in self.attributes
112 assert "non_attribute2" not in self.attributes
113 assert "non_self_attribute" not in self.attributes
114 assert "non_self_attribute2" not in self.attributes
116 def test_do_not_pick_up_subscript_attribute(self):
117 """Don't pick documented variables in functions."""
118 assert "d" not in self.attributes
119 assert "d.subscript" not in self.attributes
120 assert "subscript" not in self.attributes
122 def test_docstring_is_correctly_dedented(self):
123 assert "\n " not in self.attributes["dedent"]["docstring"]
126class TestPydanticFields:
127 """Test the parser for module attributes."""
129 def setup(self):
130 """Setup reusable attributes."""
131 self.attributes = get_class_attributes(attr_module.Model)
133 def test_pick_up_attribute_in_pydantic_model(self):
134 """Pick up attribute in Pydantic model."""
135 assert "in_pydantic_model" in self.attributes
136 assert self.attributes["in_pydantic_model"]["docstring"] == "In Pydantic model."
138 assert "model_field" in self.attributes
139 assert self.attributes["model_field"]["docstring"] == "A model field."
142class TestMarshmallowFields:
143 """Test the parser for module attributes."""
145 def setup(self):
146 """Setup reusable attributes."""
147 self.attributes = get_class_attributes(attr_module.MarshmallowSchema)
149 def test_pick_up_attribute_in_pydantic_model(self):
150 """Pick up attribute in Marshmallow model."""
151 assert "in_marshmallow_model" in self.attributes
152 assert self.attributes["in_marshmallow_model"]["docstring"] == "In Marshmallow model."
154 assert "model_field" in self.attributes
155 assert self.attributes["model_field"]["docstring"] == "A model field."