Skip to content

Commit 04a94f2

Browse files
committed
test: update tests for refactored language support
- Update discover_functions calls to new (source, file_path) signature - Use language-specific FunctionOptimizer subclasses in tests - Add explicit utf-8 encoding to read_text()/write_text() for Windows - Fix pytest fixture in TestTsJestSkipsConversion (was __init__) - Update nonexistent file tests for source-based discover_functions - Remove unused imports
1 parent 3ebaf10 commit 04a94f2

22 files changed

Lines changed: 474 additions & 474 deletions

tests/test_code_deduplication.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from codeflash.code_utils.deduplicate_code import are_codes_duplicate, normalize_code
1+
from codeflash.languages.python.normalizer import normalize_python_code as normalize_code
22

33

44
def test_deduplicate1():
@@ -23,7 +23,7 @@ def compute_sum(numbers):
2323
"""
2424

2525
assert normalize_code(code1) == normalize_code(code2)
26-
assert are_codes_duplicate(code1, code2)
26+
assert normalize_code(code1) == normalize_code(code2)
2727

2828
# Example 3: Same function and parameter names, different local variables (should match)
2929
code3 = """
@@ -43,7 +43,7 @@ def calculate_sum(numbers):
4343
"""
4444

4545
assert normalize_code(code3) == normalize_code(code4)
46-
assert are_codes_duplicate(code3, code4)
46+
assert normalize_code(code3) == normalize_code(code4)
4747

4848
# Example 4: Nested functions and classes (preserving names)
4949
code5 = """

tests/test_code_replacement.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
2020
from codeflash.models.models import CodeOptimizationContext, CodeStringsMarkdown, FunctionParent, FunctionSource
21-
from codeflash.optimization.function_optimizer import FunctionOptimizer
21+
from codeflash.languages.python.function_optimizer import PythonFunctionOptimizer
2222
from codeflash.verification.verification_utils import TestConfig
2323

2424
os.environ["CODEFLASH_API_KEY"] = "cf-test-key"
@@ -54,7 +54,7 @@ def sorter(arr):
5454
test_framework="pytest",
5555
pytest_cmd="pytest",
5656
)
57-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
57+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
5858
code_context: CodeOptimizationContext = func_optimizer.get_code_optimization_context().unwrap()
5959
original_helper_code: dict[Path, str] = {}
6060
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
@@ -834,7 +834,7 @@ def main_method(self):
834834
test_framework="pytest",
835835
pytest_cmd="pytest",
836836
)
837-
func_optimizer = FunctionOptimizer(function_to_optimize=func_top_optimize, test_cfg=test_config)
837+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func_top_optimize, test_cfg=test_config)
838838
code_context = func_optimizer.get_code_optimization_context().unwrap()
839839
assert code_context.testgen_context.flat.rstrip() == get_code_output.rstrip()
840840

@@ -1745,7 +1745,7 @@ def new_function2(value):
17451745
test_framework="pytest",
17461746
pytest_cmd="pytest",
17471747
)
1748-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
1748+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
17491749
code_context: CodeOptimizationContext = func_optimizer.get_code_optimization_context().unwrap()
17501750
original_helper_code: dict[Path, str] = {}
17511751
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
@@ -1824,7 +1824,7 @@ def new_function2(value):
18241824
test_framework="pytest",
18251825
pytest_cmd="pytest",
18261826
)
1827-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
1827+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
18281828
code_context: CodeOptimizationContext = func_optimizer.get_code_optimization_context().unwrap()
18291829
original_helper_code: dict[Path, str] = {}
18301830
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
@@ -1904,7 +1904,7 @@ def new_function2(value):
19041904
test_framework="pytest",
19051905
pytest_cmd="pytest",
19061906
)
1907-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
1907+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
19081908
code_context: CodeOptimizationContext = func_optimizer.get_code_optimization_context().unwrap()
19091909
original_helper_code: dict[Path, str] = {}
19101910
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
@@ -1983,7 +1983,7 @@ def new_function2(value):
19831983
test_framework="pytest",
19841984
pytest_cmd="pytest",
19851985
)
1986-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
1986+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
19871987
code_context: CodeOptimizationContext = func_optimizer.get_code_optimization_context().unwrap()
19881988
original_helper_code: dict[Path, str] = {}
19891989
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
@@ -2063,7 +2063,7 @@ def new_function2(value):
20632063
test_framework="pytest",
20642064
pytest_cmd="pytest",
20652065
)
2066-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
2066+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
20672067
code_context: CodeOptimizationContext = func_optimizer.get_code_optimization_context().unwrap()
20682068
original_helper_code: dict[Path, str] = {}
20692069
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
@@ -2153,7 +2153,7 @@ def new_function2(value):
21532153
test_framework="pytest",
21542154
pytest_cmd="pytest",
21552155
)
2156-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
2156+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
21572157
code_context: CodeOptimizationContext = func_optimizer.get_code_optimization_context().unwrap()
21582158
original_helper_code: dict[Path, str] = {}
21592159
helper_function_paths = {hf.file_path for hf in code_context.helper_functions}
@@ -3453,7 +3453,7 @@ def hydrate_input_text_actions_with_field_names(
34533453
test_framework="pytest",
34543454
pytest_cmd="pytest",
34553455
)
3456-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
3456+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
34573457
code_context: CodeOptimizationContext = func_optimizer.get_code_optimization_context().unwrap()
34583458

34593459
original_helper_code: dict[Path, str] = {}

tests/test_function_dependencies.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
66
from codeflash.either import is_successful
77
from codeflash.models.models import FunctionParent
8-
from codeflash.optimization.function_optimizer import FunctionOptimizer
8+
from codeflash.languages.python.function_optimizer import PythonFunctionOptimizer
99
from codeflash.verification.verification_utils import TestConfig
1010

1111

@@ -132,7 +132,7 @@ def test_class_method_dependencies() -> None:
132132
starting_line=None,
133133
ending_line=None,
134134
)
135-
func_optimizer = FunctionOptimizer(
135+
func_optimizer = PythonFunctionOptimizer(
136136
function_to_optimize=function_to_optimize,
137137
test_cfg=TestConfig(
138138
tests_root=file_path,
@@ -202,7 +202,7 @@ def test_recursive_function_context() -> None:
202202
starting_line=None,
203203
ending_line=None,
204204
)
205-
func_optimizer = FunctionOptimizer(
205+
func_optimizer = PythonFunctionOptimizer(
206206
function_to_optimize=function_to_optimize,
207207
test_cfg=TestConfig(
208208
tests_root=file_path,

tests/test_function_discovery.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,14 @@ def test_in_dunder_tests():
680680

681681
# Combine all discovered functions
682682
all_functions = {}
683-
for discovered in [discovered_source, discovered_test, discovered_test_underscore,
684-
discovered_spec, discovered_tests_dir, discovered_dunder_tests]:
683+
for discovered in [
684+
discovered_source,
685+
discovered_test,
686+
discovered_test_underscore,
687+
discovered_spec,
688+
discovered_tests_dir,
689+
discovered_dunder_tests,
690+
]:
685691
all_functions.update(discovered)
686692

687693
# Test Case 1: tests_root == module_root (overlapping case)
@@ -781,9 +787,7 @@ def test_filter_functions_strict_string_matching():
781787

782788
# Strict check: exactly these 3 files should remain (those with 'test' as substring only)
783789
expected_files = {contest_file, latest_file, attestation_file}
784-
assert set(filtered.keys()) == expected_files, (
785-
f"Expected files {expected_files}, got {set(filtered.keys())}"
786-
)
790+
assert set(filtered.keys()) == expected_files, f"Expected files {expected_files}, got {set(filtered.keys())}"
787791

788792
# Strict check: each file should have exactly 1 function with the expected name
789793
assert [fn.function_name for fn in filtered[contest_file]] == ["run_contest"], (
@@ -871,9 +875,7 @@ def test_filter_functions_test_directory_patterns():
871875

872876
# Strict check: exactly these 2 files should remain (those in non-test directories)
873877
expected_files = {contest_file, latest_file}
874-
assert set(filtered.keys()) == expected_files, (
875-
f"Expected files {expected_files}, got {set(filtered.keys())}"
876-
)
878+
assert set(filtered.keys()) == expected_files, f"Expected files {expected_files}, got {set(filtered.keys())}"
877879

878880
# Strict check: each file should have exactly 1 function with the expected name
879881
assert [fn.function_name for fn in filtered[contest_file]] == ["get_scores"], (
@@ -936,9 +938,7 @@ def test_filter_functions_non_overlapping_tests_root():
936938

937939
# Strict check: exactly these 2 files should remain (both in src/, not in tests/)
938940
expected_files = {source_file, test_in_src}
939-
assert set(filtered.keys()) == expected_files, (
940-
f"Expected files {expected_files}, got {set(filtered.keys())}"
941-
)
941+
assert set(filtered.keys()) == expected_files, f"Expected files {expected_files}, got {set(filtered.keys())}"
942942

943943
# Strict check: each file should have exactly 1 function with the expected name
944944
assert [fn.function_name for fn in filtered[source_file]] == ["process"], (
@@ -1047,20 +1047,15 @@ def test_deep_copy():
10471047
)
10481048

10491049
root_functions = [fn.function_name for fn in filtered.get(root_source_file, [])]
1050-
assert root_functions == ["main"], (
1051-
f"Expected ['main'], got {root_functions}"
1052-
)
1050+
assert root_functions == ["main"], f"Expected ['main'], got {root_functions}"
10531051

10541052
# Strict check: exactly 3 functions (2 from utils.py + 1 from main.py)
10551053
assert count == 3, (
1056-
f"Expected exactly 3 functions, got {count}. "
1057-
f"Some source files may have been incorrectly filtered."
1054+
f"Expected exactly 3 functions, got {count}. Some source files may have been incorrectly filtered."
10581055
)
10591056

10601057
# Verify test file was properly filtered (should not be in results)
1061-
assert test_file not in filtered, (
1062-
f"Test file {test_file} should have been filtered but wasn't"
1063-
)
1058+
assert test_file not in filtered, f"Test file {test_file} should have been filtered but wasn't"
10641059

10651060

10661061
def test_filter_functions_typescript_project_in_tests_folder():
@@ -1214,9 +1209,7 @@ def sample_data():
12141209
# source_file and file_in_test_dir should remain
12151210
# test_prefix_file, conftest_file, and test_in_subdir should be filtered
12161211
expected_files = {source_file, file_in_test_dir}
1217-
assert set(filtered.keys()) == expected_files, (
1218-
f"Expected {expected_files}, got {set(filtered.keys())}"
1219-
)
1212+
assert set(filtered.keys()) == expected_files, f"Expected {expected_files}, got {set(filtered.keys())}"
12201213
assert count == 2, f"Expected exactly 2 functions, got {count}"
12211214

12221215

@@ -1266,7 +1259,8 @@ def helper_method(self):
12661259
""")
12671260

12681261
support = PythonSupport()
1269-
functions = support.discover_functions(fixture_file)
1262+
source = fixture_file.read_text(encoding="utf-8")
1263+
functions = support.discover_functions(source, fixture_file)
12701264
function_names = [fn.function_name for fn in functions]
12711265

12721266
assert "regular_function" in function_names

tests/test_get_helper_code.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
88
from codeflash.either import is_successful
99
from codeflash.models.models import FunctionParent, get_code_block_splitter
10-
from codeflash.optimization.function_optimizer import FunctionOptimizer
10+
from codeflash.languages.python.function_optimizer import PythonFunctionOptimizer
1111
from codeflash.optimization.optimizer import Optimizer
1212
from codeflash.verification.verification_utils import TestConfig
1313

@@ -233,7 +233,7 @@ def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R:
233233
test_framework="pytest",
234234
pytest_cmd="pytest",
235235
)
236-
func_optimizer = FunctionOptimizer(function_to_optimize=function_to_optimize, test_cfg=test_config)
236+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=function_to_optimize, test_cfg=test_config)
237237
with open(file_path) as f:
238238
original_code = f.read()
239239
ctx_result = func_optimizer.get_code_optimization_context()
@@ -404,7 +404,7 @@ def test_bubble_sort_deps() -> None:
404404
test_framework="pytest",
405405
pytest_cmd="pytest",
406406
)
407-
func_optimizer = FunctionOptimizer(function_to_optimize=function_to_optimize, test_cfg=test_config)
407+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=function_to_optimize, test_cfg=test_config)
408408
with open(file_path) as f:
409409
original_code = f.read()
410410
ctx_result = func_optimizer.get_code_optimization_context()

tests/test_instrument_line_profiler.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from codeflash.languages.python.static_analysis.line_profile_utils import add_decorator_imports, contains_jit_decorator
66
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
77
from codeflash.models.models import CodeOptimizationContext
8-
from codeflash.optimization.function_optimizer import FunctionOptimizer
8+
from codeflash.languages.python.function_optimizer import PythonFunctionOptimizer
99
from codeflash.verification.verification_utils import TestConfig
1010

1111

@@ -22,7 +22,7 @@ def test_add_decorator_imports_helper_in_class():
2222
pytest_cmd="pytest",
2323
)
2424
func = FunctionToOptimize(function_name="sort_classmethod", parents=[], file_path=code_path)
25-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
25+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
2626
os.chdir(run_cwd)
2727
# func_optimizer = pass
2828
try:
@@ -94,7 +94,7 @@ def test_add_decorator_imports_helper_in_nested_class():
9494
pytest_cmd="pytest",
9595
)
9696
func = FunctionToOptimize(function_name="sort_classmethod", parents=[], file_path=code_path)
97-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
97+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
9898
os.chdir(run_cwd)
9999
# func_optimizer = pass
100100
try:
@@ -143,7 +143,7 @@ def test_add_decorator_imports_nodeps():
143143
pytest_cmd="pytest",
144144
)
145145
func = FunctionToOptimize(function_name="sorter", parents=[], file_path=code_path)
146-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
146+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
147147
os.chdir(run_cwd)
148148
# func_optimizer = pass
149149
try:
@@ -194,7 +194,7 @@ def test_add_decorator_imports_helper_outside():
194194
pytest_cmd="pytest",
195195
)
196196
func = FunctionToOptimize(function_name="sorter_deps", parents=[], file_path=code_path)
197-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
197+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
198198
os.chdir(run_cwd)
199199
# func_optimizer = pass
200200
try:
@@ -271,7 +271,7 @@ def __init__(self, arr):
271271
pytest_cmd="pytest",
272272
)
273273
func = FunctionToOptimize(function_name="sorter", parents=[], file_path=code_write_path)
274-
func_optimizer = FunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
274+
func_optimizer = PythonFunctionOptimizer(function_to_optimize=func, test_cfg=test_config)
275275
os.chdir(run_cwd)
276276
# func_optimizer = pass
277277
try:

0 commit comments

Comments
 (0)