Option C Tools
Loading...
Searching...
No Matches
test_format_async.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_formatter/test_format_async.py
4
5"""
6Purpose
7-------
8OI-520 regression tests — verify ``fix_func_pattern`` also handles
9``async def`` functions (previously only ``ast.FunctionDef`` was inspected,
10so async functions were silently skipped).
11
12Responsibilities
13----------------
14- Plain async function missing ``func = "..."`` gets one injected.
15- Async function that already has the pattern is unchanged.
16- Mixed sync + async file: both get fixed in the same pass.
17- Async method inside a class is also covered.
18
19Diagnostics
20-----------
21Domain: TESTS.FORMATTER
22Levels:
23 L2 — test lifecycle
24 L3 — assertion details
25 L4 — deep tracing
26
27Contracts
28---------
29- The formatter must treat ``async def`` bodies identically to ``def`` bodies.
30"""
31
32from pathlib import Path
33
34from oct.formatter.oct_format import FormatterContext, fix_func_pattern
35
36
37def _make_nontrivial(content: str) -> str:
38 lines = content.split("\n")
39 while len(lines) < 22:
40 lines.append("# padding")
41 return "\n".join(lines)
42
43
45 temp_project_root: Path, formatter_ctx_fix: FormatterContext
46):
47 test_file = temp_project_root / "async_single.py"
48 content = _make_nontrivial(
49 "#!/usr/bin/env python3\n"
50 "from oc_diagnostics import _dbg\n"
51 "\n"
52 "async def my_async():\n"
53 " _dbg('TEST', 'start')\n"
54 " return 42\n"
55 )
56 test_file.write_text(content, encoding="utf-8")
57
58 changed, new_content, _ = fix_func_pattern(test_file, content, formatter_ctx_fix)
59
60 assert changed is True
61 assert 'func = "my_async"' in new_content
62
63
65 temp_project_root: Path, formatter_ctx_fix: FormatterContext
66):
67 test_file = temp_project_root / "async_ok.py"
68 content = _make_nontrivial(
69 "#!/usr/bin/env python3\n"
70 "from oc_diagnostics import _dbg\n"
71 "\n"
72 "async def ok_async():\n"
73 ' func = "ok_async"\n'
74 " _dbg('TEST', func, 'msg', 2)\n"
75 " return 1\n"
76 )
77 test_file.write_text(content, encoding="utf-8")
78
79 changed, _, _ = fix_func_pattern(test_file, content, formatter_ctx_fix)
80
81 assert changed is False
82
83
85 temp_project_root: Path, formatter_ctx_fix: FormatterContext
86):
87 test_file = temp_project_root / "mixed.py"
88 content = _make_nontrivial(
89 "#!/usr/bin/env python3\n"
90 "from oc_diagnostics import _dbg\n"
91 "\n"
92 "def sync_func():\n"
93 " _dbg('TEST', 'a')\n"
94 " return 1\n"
95 "\n"
96 "async def async_func():\n"
97 " _dbg('TEST', 'b')\n"
98 " return 2\n"
99 )
100 test_file.write_text(content, encoding="utf-8")
101
102 changed, new_content, _ = fix_func_pattern(test_file, content, formatter_ctx_fix)
103
104 assert changed is True
105 assert 'func = "sync_func"' in new_content
106 assert 'func = "async_func"' in new_content
107
108
110 temp_project_root: Path, formatter_ctx_fix: FormatterContext
111):
112 test_file = temp_project_root / "async_method.py"
113 content = _make_nontrivial(
114 "#!/usr/bin/env python3\n"
115 "from oc_diagnostics import _dbg\n"
116 "\n"
117 "class Foo:\n"
118 " async def bar(self):\n"
119 " _dbg('TEST', 'method')\n"
120 " return 0\n"
121 )
122 test_file.write_text(content, encoding="utf-8")
123
124 changed, new_content, _ = fix_func_pattern(test_file, content, formatter_ctx_fix)
125
126 assert changed is True
127 assert 'func = "bar"' in new_content
test_mixed_sync_and_async_both_fixed(Path temp_project_root, FormatterContext formatter_ctx_fix)
test_async_function_with_pattern_unchanged(Path temp_project_root, FormatterContext formatter_ctx_fix)
test_async_method_in_class_gets_func_pattern(Path temp_project_root, FormatterContext formatter_ctx_fix)
test_async_function_gets_func_pattern(Path temp_project_root, FormatterContext formatter_ctx_fix)