6Tests for docstring fixing.
10Verify that the formatter correctly fixes missing/incomplete module docstrings.
14- Test addition of complete docstring when missing.
15- Test addition of missing sections to partial docstrings.
16- Test preservation of existing docstring content.
17- Test correct positioning after header block.
21Domain: TESTS.FORMATTER
25 L4 — detailed docstring parsing and verification
29- New docstrings are inserted after the header block (after blank line).
30- Existing docstrings are never replaced, only extended.
31- All required sections are present in the result.
35from pathlib
import Path
41 """Test that complete docstring skeleton is added when missing."""
42 test_file = temp_project_root /
"test_no_docstring.py"
44 "#!/usr/bin/env python3\n"
45 "# -*- coding: utf-8 -*-\n"
51 test_file.write_text(content, encoding=
"utf-8")
53 changed, new_content, messages = fix_docstring(test_file, content, formatter_ctx_fix)
55 assert changed
is True
56 assert '"""' in new_content
57 assert "Purpose" in new_content
58 assert "Responsibilities" in new_content
59 assert "Diagnostics" in new_content
60 assert "Contracts" in new_content
64 """Test that missing Purpose section is added."""
65 test_file = temp_project_root /
"test_missing_purpose.py"
67 '"""\nResponsibilities\n----------------\nNone.\n"""'
70 changed, new_content, messages = fix_docstring(test_file, content, formatter_ctx_fix)
72 assert changed
is True
73 assert "Purpose" in new_content
74 assert "Responsibilities" in new_content
78 """Test that existing docstring content is preserved."""
79 test_file = temp_project_root /
"test_preserve_docstring.py"
80 original_docstring = (
84 'This is a very important module.\n'
87 content = original_docstring
89 changed, new_content, messages = fix_docstring(test_file, content, formatter_ctx_fix)
91 assert changed
is True
92 assert "This is a very important module." in new_content
96 """Test that dry-run mode doesn't modify files."""
97 test_file = temp_project_root /
"test_docstring_dryrun.py"
98 content =
"#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n# test.py\n\ndef foo():\n pass\n"
99 test_file.write_text(content, encoding=
"utf-8")
101 changed, _, messages = fix_docstring(test_file, content, formatter_ctx)
103 assert changed
is True
104 assert test_file.read_text(encoding=
"utf-8") == content
108 """Test that complete docstrings are not modified."""
109 test_file = temp_project_root /
"test_complete_docstring.py"
129 changed, _, messages = fix_docstring(test_file, content, formatter_ctx_fix)
131 assert changed
is False
132 assert len(messages) == 0