6Tests for func pattern fixing.
10Verify that the formatter correctly fixes missing func = "..." patterns.
14- Test addition of func variable to functions with _dbg() calls.
15- Test skipping of functions without _dbg() calls.
16- Test skipping of functions that already have the pattern.
17- Test proper indentation.
21Domain: TESTS.FORMATTER
25 L4 — AST parsing and pattern insertion
29- func variable must be first statement in function.
30- Pattern is skipped if already present.
31- Functions without _dbg() are never modified.
36from pathlib
import Path
46 """Pad content to be > 20 lines."""
47 lines = content.split(
'\n')
48 while len(lines) < 22:
49 lines.append(
"# padding")
50 return '\n'.join(lines)
54 """Test adding func pattern to a single function."""
55 test_file = temp_project_root /
"test_single_func.py"
57 "#!/usr/bin/env python3\n"
58 "# -*- coding: utf-8 -*-\n"
61 "from oc_diagnostics import _dbg\n"
64 " _dbg('TEST', 'start')\n"
67 test_file.write_text(content, encoding=
"utf-8")
69 changed, new_content, messages = fix_func_pattern(test_file, content, formatter_ctx_fix)
71 assert changed
is True
72 assert 'func = "my_func"' in new_content
76 """Test adding func pattern to multiple functions."""
77 test_file = temp_project_root /
"test_multi_func.py"
79 "#!/usr/bin/env python3\n"
80 "# -*- coding: utf-8 -*-\n"
83 "from oc_diagnostics import _dbg\n"
86 " _dbg('TEST', 'foo')\n"
89 " _dbg('TEST', 'bar')\n"
91 test_file.write_text(content, encoding=
"utf-8")
93 changed, new_content, messages = fix_func_pattern(test_file, content, formatter_ctx_fix)
95 assert changed
is True
96 assert 'func = "foo"' in new_content
97 assert 'func = "bar"' in new_content
101 """Test that functions without _dbg() are not modified."""
102 test_file = temp_project_root /
"test_no_dbg.py"
104 "#!/usr/bin/env python3\n"
105 "# -*- coding: utf-8 -*-\n"
108 "from oc_diagnostics import _dbg\n"
111 " '''No diagnostics.'''\n"
115 " _dbg('TEST', 'test')\n"
117 test_file.write_text(content, encoding=
"utf-8")
119 changed, new_content, messages = fix_func_pattern(test_file, content, formatter_ctx_fix)
121 assert changed
is True
122 assert 'func = "has_dbg"' in new_content
123 assert 'func = "no_dbg"' not in new_content
127 """Test that existing func patterns are not duplicated."""
128 test_file = temp_project_root /
"test_existing_pattern.py"
130 "#!/usr/bin/env python3\n"
131 "# -*- coding: utf-8 -*-\n"
134 "from oc_diagnostics import _dbg\n"
137 " func = 'my_func'\n"
138 " _dbg('TEST', func, 'start', 2)\n"
140 test_file.write_text(content, encoding=
"utf-8")
142 changed, _, messages = fix_func_pattern(test_file, content, formatter_ctx_fix)
144 assert changed
is False
148 """Test that trivial files are skipped."""
149 test_file = temp_project_root /
"test_trivial_func.py"
152 " _dbg('TEST', 'test')\n"
154 test_file.write_text(content, encoding=
"utf-8")
156 changed, _, messages = fix_func_pattern(test_file, content, formatter_ctx_fix)
158 assert changed
is False
167 """``_detect_indent_style`` returns '\\t' when tabs dominate."""
171 "\tfor i in range(3):",
175 assert _detect_indent_style(lines) ==
"\t"
179 """``_detect_indent_style`` returns ' ' when spaces dominate."""
183 " for i in range(3):",
186 assert _detect_indent_style(lines) ==
" "
190 """Blank lines must not affect the tally."""
191 lines = [
"",
"\n",
" ",
"\tcode",
"\tcode2"]
192 assert _detect_indent_style(lines) ==
"\t"
196 temp_project_root: Path, formatter_ctx_fix: FormatterContext,
198 """A tab-indented file receives a tab-indented ``func = "..."`` line."""
199 test_file = temp_project_root /
"test_tabs.py"
201 "#!/usr/bin/env python3\n"
202 "# -*- coding: utf-8 -*-\n"
205 "from oc_diagnostics import _dbg\n"
208 "\t_dbg('TEST', 'start')\n"
211 test_file.write_text(content, encoding=
"utf-8")
213 changed, new_content, _ = fix_func_pattern(
214 test_file, content, formatter_ctx_fix,
217 assert changed
is True
218 assert '\tfunc = "my_func"' in new_content
220 assert ' func = "my_func"' not in new_content
222 ast.parse(new_content)
226 temp_project_root: Path, formatter_ctx_fix: FormatterContext,
228 """Baseline: a space-indented file still receives space injection."""
229 test_file = temp_project_root /
"test_spaces.py"
231 "#!/usr/bin/env python3\n"
232 "# -*- coding: utf-8 -*-\n"
235 "from oc_diagnostics import _dbg\n"
238 " _dbg('TEST', 'start')\n"
241 test_file.write_text(content, encoding=
"utf-8")
243 changed, new_content, _ = fix_func_pattern(
244 test_file, content, formatter_ctx_fix,
247 assert changed
is True
248 assert ' func = "my_func"' in new_content
249 ast.parse(new_content)
253 temp_project_root: Path, formatter_ctx_fix: FormatterContext,
255 """A class method in a tab-indented file gets a ``\\t\\t`` injection."""
256 test_file = temp_project_root /
"test_tabs_class.py"
258 "#!/usr/bin/env python3\n"
259 "# -*- coding: utf-8 -*-\n"
262 "from oc_diagnostics import _dbg\n"
266 "\t\t_dbg('TEST', 'run')\n"
269 test_file.write_text(content, encoding=
"utf-8")
271 changed, new_content, _ = fix_func_pattern(
272 test_file, content, formatter_ctx_fix,
275 assert changed
is True
276 assert '\t\tfunc = "run"' in new_content
277 ast.parse(new_content)