2023-12-30 02:37:49 +00:00
|
|
|
import os
|
|
|
|
|
|
2024-02-07 15:26:45 +00:00
|
|
|
from codeflash.code_utils.code_replacer import replace_functions_in_file
|
2023-12-30 02:37:49 +00:00
|
|
|
|
2024-02-07 01:35:13 +00:00
|
|
|
os.environ["CODEFLASH_API_KEY"] = "cf-test-key"
|
|
|
|
|
|
2023-12-30 02:37:49 +00:00
|
|
|
|
|
|
|
|
def test_test_libcst_code_replacement():
|
|
|
|
|
optim_code = """import libcst as cst
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
def totally_new_function(value):
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
class NewClass:
|
|
|
|
|
def __init__(self, name):
|
|
|
|
|
self.name = name
|
|
|
|
|
def new_function(self, value):
|
|
|
|
|
return self.name
|
|
|
|
|
def new_function2(value):
|
|
|
|
|
return value
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
original_code = """class NewClass:
|
|
|
|
|
def __init__(self, name):
|
|
|
|
|
self.name = name
|
|
|
|
|
def new_function(self, value):
|
|
|
|
|
return "I am still old"
|
|
|
|
|
|
|
|
|
|
print("Hello world")
|
|
|
|
|
"""
|
|
|
|
|
expected = """import libcst as cst
|
|
|
|
|
from typing import Optional
|
|
|
|
|
class NewClass:
|
|
|
|
|
def __init__(self, name):
|
|
|
|
|
self.name = name
|
|
|
|
|
def new_function(self, value):
|
|
|
|
|
return self.name
|
|
|
|
|
def new_function2(value):
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def totally_new_function(value):
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
print("Hello world")
|
|
|
|
|
"""
|
|
|
|
|
|
2024-02-07 01:35:13 +00:00
|
|
|
function_name: str = "NewClass.new_function"
|
|
|
|
|
preexisting_functions: list[str] = ["NewClass.new_function"]
|
2024-02-07 15:26:45 +00:00
|
|
|
new_code: str = replace_functions_in_file(
|
2024-02-08 19:25:49 +00:00
|
|
|
original_code, [function_name], optim_code, preexisting_functions, "module"
|
2024-02-07 15:26:45 +00:00
|
|
|
)
|
|
|
|
|
assert new_code == expected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_test_libcst_code_replacement2():
|
|
|
|
|
optim_code = """import libcst as cst
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
def totally_new_function(value):
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def other_function(st):
|
|
|
|
|
return(st * 2)
|
|
|
|
|
|
|
|
|
|
class NewClass:
|
|
|
|
|
def __init__(self, name):
|
|
|
|
|
self.name = name
|
|
|
|
|
def new_function(self, value):
|
|
|
|
|
return other_function(self.name)
|
|
|
|
|
def new_function2(value):
|
|
|
|
|
return value
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
original_code = """from OtherModule import other_function
|
|
|
|
|
|
|
|
|
|
class NewClass:
|
|
|
|
|
def __init__(self, name):
|
|
|
|
|
self.name = name
|
|
|
|
|
def new_function(self, value):
|
|
|
|
|
return other_function("I am still old")
|
|
|
|
|
|
|
|
|
|
print("Hello world")
|
|
|
|
|
"""
|
|
|
|
|
expected = """import libcst as cst
|
|
|
|
|
from typing import Optional
|
|
|
|
|
from OtherModule import other_function
|
|
|
|
|
|
|
|
|
|
class NewClass:
|
|
|
|
|
def __init__(self, name):
|
|
|
|
|
self.name = name
|
|
|
|
|
def new_function(self, value):
|
|
|
|
|
return other_function(self.name)
|
|
|
|
|
def new_function2(value):
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def totally_new_function(value):
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
print("Hello world")
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
function_name: str = "NewClass.new_function"
|
|
|
|
|
preexisting_functions: list[str] = ["NewClass.new_function", "other_function"]
|
|
|
|
|
new_code: str = replace_functions_in_file(
|
2024-02-08 19:25:49 +00:00
|
|
|
original_code, [function_name], optim_code, preexisting_functions, "module"
|
2024-02-07 15:26:45 +00:00
|
|
|
)
|
|
|
|
|
assert new_code == expected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_test_libcst_code_replacement3():
|
|
|
|
|
optim_code = """import libcst as cst
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
def totally_new_function(value):
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def other_function(st):
|
|
|
|
|
return(st * 2)
|
|
|
|
|
|
|
|
|
|
class NewClass:
|
|
|
|
|
def __init__(self, name):
|
|
|
|
|
self.name = name
|
|
|
|
|
def new_function(self, value):
|
|
|
|
|
return other_function(self.name)
|
|
|
|
|
def new_function2(value):
|
|
|
|
|
return value
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
original_code = """import libcst as cst
|
|
|
|
|
from typing import Mandatory
|
|
|
|
|
|
|
|
|
|
print("Au revoir")
|
|
|
|
|
|
|
|
|
|
def yet_another_function(values):
|
|
|
|
|
return len(values)
|
|
|
|
|
|
|
|
|
|
def other_function(st):
|
|
|
|
|
return(st + st)
|
|
|
|
|
|
|
|
|
|
print("Salut monde")
|
|
|
|
|
"""
|
|
|
|
|
expected = """import libcst as cst
|
|
|
|
|
from typing import Optional
|
|
|
|
|
import libcst as cst
|
|
|
|
|
from typing import Mandatory
|
|
|
|
|
|
|
|
|
|
print("Au revoir")
|
|
|
|
|
|
|
|
|
|
def yet_another_function(values):
|
|
|
|
|
return len(values)
|
|
|
|
|
|
|
|
|
|
def other_function(st):
|
|
|
|
|
return(st * 2)
|
|
|
|
|
|
|
|
|
|
print("Salut monde")
|
|
|
|
|
"""
|
|
|
|
|
|
2024-02-08 19:25:49 +00:00
|
|
|
function_names: list[str] = ["module.other_function"]
|
2024-02-07 15:26:45 +00:00
|
|
|
preexisting_functions: list[str] = []
|
|
|
|
|
new_code: str = replace_functions_in_file(
|
2024-02-08 19:25:49 +00:00
|
|
|
original_code, function_names, optim_code, preexisting_functions, "module"
|
2024-02-07 15:26:45 +00:00
|
|
|
)
|
|
|
|
|
assert new_code == expected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_test_libcst_code_replacement4():
|
|
|
|
|
optim_code = """import libcst as cst
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
def totally_new_function(value):
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def yet_another_function(values):
|
|
|
|
|
return len(values) + 2
|
|
|
|
|
|
|
|
|
|
def other_function(st):
|
|
|
|
|
return(st * 2)
|
|
|
|
|
|
|
|
|
|
class NewClass:
|
|
|
|
|
def __init__(self, name):
|
|
|
|
|
self.name = name
|
|
|
|
|
def new_function(self, value):
|
|
|
|
|
return other_function(self.name)
|
|
|
|
|
def new_function2(value):
|
|
|
|
|
return value
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
original_code = """import libcst as cst
|
|
|
|
|
from typing import Mandatory
|
|
|
|
|
|
|
|
|
|
print("Au revoir")
|
|
|
|
|
|
|
|
|
|
def yet_another_function(values):
|
|
|
|
|
return len(values)
|
|
|
|
|
|
|
|
|
|
def other_function(st):
|
|
|
|
|
return(st + st)
|
|
|
|
|
|
|
|
|
|
print("Salut monde")
|
|
|
|
|
"""
|
|
|
|
|
expected = """import libcst as cst
|
|
|
|
|
from typing import Optional
|
|
|
|
|
import libcst as cst
|
|
|
|
|
from typing import Mandatory
|
|
|
|
|
|
|
|
|
|
print("Au revoir")
|
|
|
|
|
def yet_another_function(values):
|
|
|
|
|
return len(values) + 2
|
|
|
|
|
|
|
|
|
|
def other_function(st):
|
|
|
|
|
return(st * 2)
|
|
|
|
|
|
|
|
|
|
print("Salut monde")
|
|
|
|
|
"""
|
|
|
|
|
|
2024-02-08 19:25:49 +00:00
|
|
|
function_names: list[str] = ["module.yet_another_function", "module.other_function"]
|
2024-02-07 15:26:45 +00:00
|
|
|
preexisting_functions: list[str] = []
|
|
|
|
|
new_code: str = replace_functions_in_file(
|
2024-02-08 19:25:49 +00:00
|
|
|
original_code, function_names, optim_code, preexisting_functions, "module"
|
2024-02-07 01:35:13 +00:00
|
|
|
)
|
|
|
|
|
assert new_code == expected
|
2024-02-08 23:52:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_test_libcst_code_replacement5():
|
|
|
|
|
optim_code = """def sorter_deps(arr):
|
|
|
|
|
supersort(badsort(arr))
|
|
|
|
|
return arr
|
|
|
|
|
|
|
|
|
|
def badsort(ploc):
|
|
|
|
|
donothing(ploc)
|
|
|
|
|
|
|
|
|
|
def supersort(doink):
|
|
|
|
|
for i in range(len(doink)):
|
|
|
|
|
fix(doink, i)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
original_code = """from code_to_optimize.bubble_sort_dep1_helper import dep1_comparer
|
|
|
|
|
from code_to_optimize.bubble_sort_dep2_swap import dep2_swap
|
|
|
|
|
|
|
|
|
|
def sorter_deps(arr):
|
|
|
|
|
for i in range(len(arr)):
|
|
|
|
|
for j in range(len(arr) - 1):
|
|
|
|
|
if dep1_comparer(arr, j):
|
|
|
|
|
dep2_swap(arr, j)
|
|
|
|
|
return arr
|
|
|
|
|
"""
|
|
|
|
|
expected = """from code_to_optimize.bubble_sort_dep1_helper import dep1_comparer
|
|
|
|
|
from code_to_optimize.bubble_sort_dep2_swap import dep2_swap
|
|
|
|
|
def sorter_deps(arr):
|
|
|
|
|
supersort(badsort(arr))
|
|
|
|
|
return arr
|
|
|
|
|
|
|
|
|
|
def badsort(ploc):
|
|
|
|
|
donothing(ploc)
|
|
|
|
|
|
|
|
|
|
def supersort(doink):
|
|
|
|
|
for i in range(len(doink)):
|
|
|
|
|
fix(doink, i)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
function_names: list[str] = ["sorter_deps"]
|
|
|
|
|
preexisting_functions: list[str] = ["sorter_deps"]
|
|
|
|
|
new_code: str = replace_functions_in_file(
|
|
|
|
|
original_code, function_names, optim_code, preexisting_functions, "module"
|
|
|
|
|
)
|
|
|
|
|
assert new_code == expected
|