Skip to content

Commit 8039ec4

Browse files
committed
coverage utils
1 parent 4fac1e2 commit 8039ec4

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

codeflash/code_utils/coverage_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def extract_dependent_function(main_function: str, code_context: CodeOptimizatio
1414
"""Extract the single dependent function from the code context excluding the main function."""
1515
ast_tree = ast.parse(code_context.testgen_context_code)
1616

17-
dependent_functions = {node.name for node in ast_tree.body if isinstance(node, ast.FunctionDef)}
17+
dependent_functions = {node.name for node in ast_tree.body if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))}
1818

1919
if main_function in dependent_functions:
2020
dependent_functions.discard(main_function)

tests/test_code_utils.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
validate_python_code,
2121
)
2222
from codeflash.code_utils.concolic_utils import clean_concolic_tests
23-
from codeflash.code_utils.coverage_utils import generate_candidates, prepare_coverage_files
23+
from codeflash.code_utils.coverage_utils import extract_dependent_function, generate_candidates, prepare_coverage_files
2424

2525

2626
@pytest.fixture
@@ -308,6 +308,86 @@ def my_function():
308308
assert is_class_defined_in_file("MyClass", test_file) is False
309309

310310

311+
@pytest.fixture
312+
def mock_code_context():
313+
"""Mock CodeOptimizationContext for testing extract_dependent_function."""
314+
from unittest.mock import MagicMock
315+
from codeflash.models.models import CodeOptimizationContext
316+
317+
context = MagicMock(spec=CodeOptimizationContext)
318+
context.preexisting_objects = []
319+
return context
320+
321+
322+
def test_extract_dependent_function_sync_and_async(mock_code_context):
323+
"""Test extract_dependent_function with both sync and async functions."""
324+
# Test sync function extraction
325+
mock_code_context.testgen_context_code = """
326+
def main_function():
327+
pass
328+
329+
def helper_function():
330+
pass
331+
"""
332+
assert extract_dependent_function("main_function", mock_code_context) == "helper_function"
333+
334+
# Test async function extraction
335+
mock_code_context.testgen_context_code = """
336+
def main_function():
337+
pass
338+
339+
async def async_helper_function():
340+
pass
341+
"""
342+
assert extract_dependent_function("main_function", mock_code_context) == "async_helper_function"
343+
344+
345+
def test_extract_dependent_function_edge_cases(mock_code_context):
346+
"""Test extract_dependent_function edge cases."""
347+
# No dependent functions
348+
mock_code_context.testgen_context_code = """
349+
def main_function():
350+
pass
351+
"""
352+
assert extract_dependent_function("main_function", mock_code_context) is False
353+
354+
# Multiple dependent functions
355+
mock_code_context.testgen_context_code = """
356+
def main_function():
357+
pass
358+
359+
def helper1():
360+
pass
361+
362+
async def helper2():
363+
pass
364+
"""
365+
assert extract_dependent_function("main_function", mock_code_context) is False
366+
367+
368+
def test_extract_dependent_function_mixed_scenarios(mock_code_context):
369+
"""Test extract_dependent_function with mixed sync/async scenarios."""
370+
# Async main with sync helper
371+
mock_code_context.testgen_context_code = """
372+
async def async_main():
373+
pass
374+
375+
def sync_helper():
376+
pass
377+
"""
378+
assert extract_dependent_function("async_main", mock_code_context) == "sync_helper"
379+
380+
# Only async functions
381+
mock_code_context.testgen_context_code = """
382+
async def async_main():
383+
pass
384+
385+
async def async_helper():
386+
pass
387+
"""
388+
assert extract_dependent_function("async_main", mock_code_context) == "async_helper"
389+
390+
311391
def test_is_class_defined_in_file_with_non_existing_file() -> None:
312392
non_existing_file = Path("/non/existing/file.py")
313393

0 commit comments

Comments
 (0)