Skip to content

Commit 03487d7

Browse files
committed
Create test_directives.py
1 parent e3f369e commit 03487d7

1 file changed

Lines changed: 345 additions & 0 deletions

File tree

tests/test_directives.py

Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
from great_docs.directives import (
2+
DocDirectives,
3+
extract_directives,
4+
has_directives,
5+
strip_directives,
6+
)
7+
8+
9+
class TestExtractDirectives:
10+
"""Tests for extract_directives function."""
11+
12+
def test_extract_family(self):
13+
"""Test extracting @family directive."""
14+
docstring = """
15+
Short description.
16+
17+
@family: Family Name
18+
19+
Parameters
20+
----------
21+
x : int
22+
"""
23+
directives = extract_directives(docstring)
24+
assert directives.family == "Family Name"
25+
26+
def test_extract_order(self):
27+
"""Test extracting @order directive."""
28+
docstring = """
29+
Short description.
30+
31+
@order: 42
32+
33+
Parameters
34+
----------
35+
x : int
36+
"""
37+
directives = extract_directives(docstring)
38+
assert directives.order == 42
39+
40+
def test_extract_seealso(self):
41+
"""Test extracting @seealso directive."""
42+
docstring = """
43+
Short description.
44+
45+
@seealso: func_a, func_b, ClassC
46+
47+
Parameters
48+
----------
49+
x : int
50+
"""
51+
directives = extract_directives(docstring)
52+
assert directives.seealso == ["func_a", "func_b", "ClassC"]
53+
54+
def test_extract_nodoc(self):
55+
"""Test extracting @nodoc directive."""
56+
docstring = """
57+
Short description.
58+
59+
@nodoc: true
60+
"""
61+
directives = extract_directives(docstring)
62+
assert directives.nodoc is True
63+
64+
def test_extract_nodoc_without_value(self):
65+
"""Test extracting @nodoc directive without explicit value."""
66+
docstring = """
67+
Short description.
68+
69+
@nodoc:
70+
"""
71+
directives = extract_directives(docstring)
72+
assert directives.nodoc is True
73+
74+
def test_extract_multiple_directives(self):
75+
"""Test extracting multiple directives from one docstring."""
76+
docstring = """
77+
Validate column values are greater than a threshold.
78+
79+
@family: Family Name
80+
@order: 1
81+
@seealso: func_1, func_2
82+
83+
Parameters
84+
----------
85+
columns : str
86+
Target columns.
87+
"""
88+
directives = extract_directives(docstring)
89+
assert directives.family == "Family Name"
90+
assert directives.order == 1
91+
assert directives.seealso == ["func_1", "func_2"]
92+
assert directives.nodoc is False
93+
94+
def test_extract_from_none(self):
95+
"""Test extracting from None returns empty directives."""
96+
directives = extract_directives(None)
97+
assert directives.family is None
98+
assert directives.order is None
99+
assert directives.seealso == []
100+
assert directives.nodoc is False
101+
102+
def test_extract_from_empty_string(self):
103+
"""Test extracting from empty string returns empty directives."""
104+
directives = extract_directives("")
105+
assert directives.family is None
106+
assert directives.order is None
107+
assert directives.seealso == []
108+
assert directives.nodoc is False
109+
110+
def test_extract_no_directives(self):
111+
"""Test docstring without directives."""
112+
docstring = """
113+
Short description.
114+
115+
Parameters
116+
----------
117+
x : int
118+
The input value.
119+
"""
120+
directives = extract_directives(docstring)
121+
assert directives.family is None
122+
assert directives.order is None
123+
assert directives.seealso == []
124+
assert directives.nodoc is False
125+
126+
def test_directive_with_indentation(self):
127+
"""Test that indented directives are extracted."""
128+
docstring = """
129+
Short description.
130+
131+
@family: Family Name
132+
@order: 5
133+
"""
134+
directives = extract_directives(docstring)
135+
assert directives.family == "Family Name"
136+
assert directives.order == 5
137+
138+
def test_family_with_special_characters(self):
139+
"""Test family names with special characters."""
140+
docstring = """
141+
@family: Data Processing & Transformation
142+
"""
143+
directives = extract_directives(docstring)
144+
assert directives.family == "Data Processing & Transformation"
145+
146+
147+
class TestStripDirectives:
148+
"""Tests for strip_directives function."""
149+
150+
def test_strip_family(self):
151+
"""Test stripping @family directive."""
152+
docstring = """Short description.
153+
154+
@family: Family Name
155+
156+
Parameters
157+
----------
158+
x : int"""
159+
result = strip_directives(docstring)
160+
assert "@family" not in result
161+
assert "Family Name" not in result
162+
assert "Short description." in result
163+
assert "Parameters" in result
164+
165+
def test_strip_multiple_directives(self):
166+
"""Test stripping multiple directives."""
167+
docstring = """Short description.
168+
169+
@family: Family Name
170+
@order: 1
171+
@seealso: func_a, func_b
172+
173+
Parameters
174+
----------
175+
x : int"""
176+
result = strip_directives(docstring)
177+
assert "@family" not in result
178+
assert "@order" not in result
179+
assert "@seealso" not in result
180+
assert "Short description." in result
181+
assert "Parameters" in result
182+
183+
def test_strip_preserves_content(self):
184+
"""Test that stripping preserves non-directive content."""
185+
docstring = """Short description.
186+
187+
This is a longer explanation of what the function does.
188+
It spans multiple lines.
189+
190+
@family: Family Name
191+
192+
Parameters
193+
----------
194+
x : int
195+
The input value.
196+
197+
Returns
198+
-------
199+
str
200+
The output value."""
201+
result = strip_directives(docstring)
202+
assert "Short description." in result
203+
assert "longer explanation" in result
204+
assert "Parameters" in result
205+
assert "Returns" in result
206+
assert "@family" not in result
207+
208+
def test_strip_cleans_blank_lines(self):
209+
"""Test that stripping removes extra blank lines."""
210+
docstring = """Short description.
211+
212+
@family: Test
213+
214+
Parameters
215+
----------"""
216+
result = strip_directives(docstring)
217+
# Should not have more than 2 consecutive newlines
218+
assert "\n\n\n" not in result
219+
220+
def test_strip_from_none(self):
221+
"""Test stripping from None returns empty string."""
222+
result = strip_directives(None)
223+
assert result == ""
224+
225+
def test_strip_from_empty(self):
226+
"""Test stripping from empty string returns empty string."""
227+
result = strip_directives("")
228+
assert result == ""
229+
230+
def test_strip_indented_directives(self):
231+
"""Test that indented directives are stripped."""
232+
docstring = """Short description.
233+
234+
@family: Family Name
235+
@order: 5
236+
237+
Parameters
238+
----------"""
239+
result = strip_directives(docstring)
240+
assert "@family" not in result
241+
assert "@order" not in result
242+
243+
244+
class TestHasDirectives:
245+
"""Tests for has_directives function."""
246+
247+
def test_has_family(self):
248+
"""Test detecting @family directive."""
249+
assert has_directives("@family: Test")
250+
251+
def test_has_order(self):
252+
"""Test detecting @order directive."""
253+
assert has_directives("@order: 1")
254+
255+
def test_has_seealso(self):
256+
"""Test detecting @seealso directive."""
257+
assert has_directives("@seealso: func_a")
258+
259+
def test_has_nodoc(self):
260+
"""Test detecting @nodoc directive."""
261+
assert has_directives("@nodoc: true")
262+
263+
def test_no_directives(self):
264+
"""Test docstring without directives."""
265+
assert not has_directives("Just a regular docstring.")
266+
267+
def test_none_input(self):
268+
"""Test None input returns False."""
269+
assert not has_directives(None)
270+
271+
def test_empty_input(self):
272+
"""Test empty string returns False."""
273+
assert not has_directives("")
274+
275+
276+
class TestDocDirectives:
277+
"""Tests for DocDirectives dataclass."""
278+
279+
def test_bool_false_when_empty(self):
280+
"""Test that empty DocDirectives is falsy."""
281+
directives = DocDirectives()
282+
assert not directives
283+
284+
def test_bool_true_with_family(self):
285+
"""Test that DocDirectives with family is truthy."""
286+
directives = DocDirectives(family="Test")
287+
assert directives
288+
289+
def test_bool_true_with_order(self):
290+
"""Test that DocDirectives with order is truthy."""
291+
directives = DocDirectives(order=1)
292+
assert directives
293+
294+
def test_bool_true_with_seealso(self):
295+
"""Test that DocDirectives with seealso is truthy."""
296+
directives = DocDirectives(seealso=["func_a"])
297+
assert directives
298+
299+
def test_bool_true_with_nodoc(self):
300+
"""Test that DocDirectives with nodoc is truthy."""
301+
directives = DocDirectives(nodoc=True)
302+
assert directives
303+
304+
305+
class TestIntegration:
306+
"""Integration tests combining extract and strip."""
307+
308+
def test_roundtrip(self):
309+
"""Test extracting directives then stripping produces clean docstring."""
310+
original = """
311+
Validate column values are greater than a threshold.
312+
313+
This validation step checks values against a threshold.
314+
315+
@family: Family Name
316+
@order: 1
317+
@seealso: func_1, func_2
318+
319+
Parameters
320+
----------
321+
columns : str
322+
Target columns.
323+
value : numeric
324+
The threshold.
325+
326+
Returns
327+
-------
328+
Validate
329+
Self for chaining.
330+
"""
331+
332+
# Extract directives
333+
directives = extract_directives(original)
334+
assert directives.family == "Family Name"
335+
assert directives.order == 1
336+
assert directives.seealso == ["func_1", "func_2"]
337+
338+
# Strip directives
339+
cleaned = strip_directives(original)
340+
assert "@family" not in cleaned
341+
assert "@order" not in cleaned
342+
assert "@seealso" not in cleaned
343+
assert "Validate column values" in cleaned
344+
assert "Parameters" in cleaned
345+
assert "Returns" in cleaned

0 commit comments

Comments
 (0)