Skip to content

Commit 5c6e1bd

Browse files
committed
move concolic related code to it's own file
1 parent b8bc645 commit 5c6e1bd

File tree

2 files changed

+93
-88
lines changed

2 files changed

+93
-88
lines changed

codeflash/code_utils/code_replacer.py

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -337,91 +337,3 @@ def function_to_optimize_original_worktree_fqn(
337337
+ "."
338338
+ function_to_optimize.qualified_name
339339
)
340-
341-
342-
class AssertCleanup:
343-
def transform_asserts(self, code: str) -> str:
344-
lines = code.splitlines()
345-
result_lines = []
346-
347-
for line in lines:
348-
transformed = self._transform_assert_line(line)
349-
if transformed is not None:
350-
result_lines.append(transformed)
351-
else:
352-
result_lines.append(line)
353-
354-
return "\n".join(result_lines)
355-
356-
def _transform_assert_line(self, line: str) -> Optional[str]:
357-
indent = line[: len(line) - len(line.lstrip())]
358-
359-
assert_match = re.match(r"\s*assert\s+(.*?)(?:\s*==\s*.*)?$", line)
360-
if assert_match:
361-
expression = assert_match.group(1).strip()
362-
if expression.startswith("not "):
363-
return f"{indent}{expression}"
364-
365-
expression = re.sub(r"[,;]\s*$", "", expression)
366-
return f"{indent}{expression}"
367-
368-
unittest_match = re.match(r"(\s*)self\.assert([A-Za-z]+)\((.*)\)$", line)
369-
if unittest_match:
370-
indent, assert_method, args = unittest_match.groups()
371-
372-
if args:
373-
arg_parts = self._split_top_level_args(args)
374-
if arg_parts and arg_parts[0]:
375-
return f"{indent}{arg_parts[0]}"
376-
377-
return None
378-
379-
def _split_top_level_args(self, args_str: str) -> list[str]:
380-
result = []
381-
current = []
382-
depth = 0
383-
384-
for char in args_str:
385-
if char in "([{":
386-
depth += 1
387-
current.append(char)
388-
elif char in ")]}":
389-
depth -= 1
390-
current.append(char)
391-
elif char == "," and depth == 0:
392-
result.append("".join(current).strip())
393-
current = []
394-
else:
395-
current.append(char)
396-
397-
if current:
398-
result.append("".join(current).strip())
399-
400-
return result
401-
402-
403-
def clean_concolic_tests(test_suite_code: str) -> str:
404-
try:
405-
can_parse = True
406-
tree = ast.parse(test_suite_code)
407-
except SyntaxError:
408-
can_parse = False
409-
410-
if not can_parse:
411-
return AssertCleanup().transform_asserts(test_suite_code)
412-
413-
for node in ast.walk(tree):
414-
if isinstance(node, ast.FunctionDef) and node.name.startswith("test_"):
415-
new_body = []
416-
for stmt in node.body:
417-
if isinstance(stmt, ast.Assert):
418-
if isinstance(stmt.test, ast.Compare) and isinstance(stmt.test.left, ast.Call):
419-
new_body.append(ast.Expr(value=stmt.test.left))
420-
else:
421-
new_body.append(stmt)
422-
423-
else:
424-
new_body.append(stmt)
425-
node.body = new_body
426-
427-
return ast.unparse(tree).strip()
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from __future__ import annotations
2+
3+
import ast
4+
import re
5+
from typing import Optional
6+
7+
8+
class AssertCleanup:
9+
def transform_asserts(self, code: str) -> str:
10+
lines = code.splitlines()
11+
result_lines = []
12+
13+
for line in lines:
14+
transformed = self._transform_assert_line(line)
15+
if transformed is not None:
16+
result_lines.append(transformed)
17+
else:
18+
result_lines.append(line)
19+
20+
return "\n".join(result_lines)
21+
22+
def _transform_assert_line(self, line: str) -> Optional[str]:
23+
indent = line[: len(line) - len(line.lstrip())]
24+
25+
assert_match = re.match(r"\s*assert\s+(.*?)(?:\s*==\s*.*)?$", line)
26+
if assert_match:
27+
expression = assert_match.group(1).strip()
28+
if expression.startswith("not "):
29+
return f"{indent}{expression}"
30+
31+
expression = re.sub(r"[,;]\s*$", "", expression)
32+
return f"{indent}{expression}"
33+
34+
unittest_match = re.match(r"(\s*)self\.assert([A-Za-z]+)\((.*)\)$", line)
35+
if unittest_match:
36+
indent, assert_method, args = unittest_match.groups()
37+
38+
if args:
39+
arg_parts = self._split_top_level_args(args)
40+
if arg_parts and arg_parts[0]:
41+
return f"{indent}{arg_parts[0]}"
42+
43+
return None
44+
45+
def _split_top_level_args(self, args_str: str) -> list[str]:
46+
result = []
47+
current = []
48+
depth = 0
49+
50+
for char in args_str:
51+
if char in "([{":
52+
depth += 1
53+
current.append(char)
54+
elif char in ")]}":
55+
depth -= 1
56+
current.append(char)
57+
elif char == "," and depth == 0:
58+
result.append("".join(current).strip())
59+
current = []
60+
else:
61+
current.append(char)
62+
63+
if current:
64+
result.append("".join(current).strip())
65+
66+
return result
67+
68+
69+
def clean_concolic_tests(test_suite_code: str) -> str:
70+
try:
71+
can_parse = True
72+
tree = ast.parse(test_suite_code)
73+
except SyntaxError:
74+
can_parse = False
75+
76+
if not can_parse:
77+
return AssertCleanup().transform_asserts(test_suite_code)
78+
79+
for node in ast.walk(tree):
80+
if isinstance(node, ast.FunctionDef) and node.name.startswith("test_"):
81+
new_body = []
82+
for stmt in node.body:
83+
if isinstance(stmt, ast.Assert):
84+
if isinstance(stmt.test, ast.Compare) and isinstance(stmt.test.left, ast.Call):
85+
new_body.append(ast.Expr(value=stmt.test.left))
86+
else:
87+
new_body.append(stmt)
88+
89+
else:
90+
new_body.append(stmt)
91+
node.body = new_body
92+
93+
return ast.unparse(tree).strip()

0 commit comments

Comments
 (0)