Option C Tools
Loading...
Searching...
No Matches
test_docstring_fixing.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_formatter/test_docstring_fixing.py
4
5"""
6Tests for docstring fixing.
7
8Purpose
9-------
10Verify that the formatter correctly fixes missing/incomplete module docstrings.
11
12Responsibilities
13----------------
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.
18
19Diagnostics
20-----------
21Domain: TESTS.FORMATTER
22Levels:
23 L2 — test lifecycle
24 L3 — test case setup
25 L4 — detailed docstring parsing and verification
26
27Contracts
28---------
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.
32"""
33
34import pytest
35from pathlib import Path
36
37from oct.formatter.oct_format import fix_docstring, FormatterContext
38
39
40def test_add_missing_docstring(temp_project_root: Path, formatter_ctx_fix: FormatterContext):
41 """Test that complete docstring skeleton is added when missing."""
42 test_file = temp_project_root / "test_no_docstring.py"
43 content = (
44 "#!/usr/bin/env python3\n"
45 "# -*- coding: utf-8 -*-\n"
46 "# test.py\n"
47 "\n"
48 "def foo():\n"
49 " pass\n"
50 )
51 test_file.write_text(content, encoding="utf-8")
52
53 changed, new_content, messages = fix_docstring(test_file, content, formatter_ctx_fix)
54
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
61
62
63def test_add_missing_section_purpose(temp_project_root: Path, formatter_ctx_fix: FormatterContext):
64 """Test that missing Purpose section is added."""
65 test_file = temp_project_root / "test_missing_purpose.py"
66 content = (
67 '"""\nResponsibilities\n----------------\nNone.\n"""'
68 )
69
70 changed, new_content, messages = fix_docstring(test_file, content, formatter_ctx_fix)
71
72 assert changed is True
73 assert "Purpose" in new_content
74 assert "Responsibilities" in new_content
75
76
77def test_preserve_existing_docstring(temp_project_root: Path, formatter_ctx_fix: FormatterContext):
78 """Test that existing docstring content is preserved."""
79 test_file = temp_project_root / "test_preserve_docstring.py"
80 original_docstring = (
81 '"""\n'
82 'Purpose\n'
83 '-------\n'
84 'This is a very important module.\n'
85 '"""'
86 )
87 content = original_docstring
88
89 changed, new_content, messages = fix_docstring(test_file, content, formatter_ctx_fix)
90
91 assert changed is True
92 assert "This is a very important module." in new_content
93
94
95def test_docstring_dry_run(temp_project_root: Path, formatter_ctx: FormatterContext):
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")
100
101 changed, _, messages = fix_docstring(test_file, content, formatter_ctx)
102
103 assert changed is True # Change detected
104 assert test_file.read_text(encoding="utf-8") == content # But not applied
105
106
107def test_complete_docstring_no_change(temp_project_root: Path, formatter_ctx_fix: FormatterContext):
108 """Test that complete docstrings are not modified."""
109 test_file = temp_project_root / "test_complete_docstring.py"
110 content = (
111 '"""\n'
112 'Purpose\n'
113 '-------\n'
114 'Test.\n'
115 'Responsibilities\n'
116 '----------------\n'
117 'Test.\n'
118 'Diagnostics\n'
119 '-----------\n'
120 'Domain: TEST\n'
121 'Levels:\n'
122 ' L2 — test\n'
123 'Contracts\n'
124 '---------\n'
125 'Test.\n'
126 '"""'
127 )
128
129 changed, _, messages = fix_docstring(test_file, content, formatter_ctx_fix)
130
131 assert changed is False
132 assert len(messages) == 0
test_add_missing_section_purpose(Path temp_project_root, FormatterContext formatter_ctx_fix)
test_preserve_existing_docstring(Path temp_project_root, FormatterContext formatter_ctx_fix)
test_docstring_dry_run(Path temp_project_root, FormatterContext formatter_ctx)
test_add_missing_docstring(Path temp_project_root, FormatterContext formatter_ctx_fix)
test_complete_docstring_no_change(Path temp_project_root, FormatterContext formatter_ctx_fix)