@@ -3139,3 +3139,149 @@ def test_sleepfunc_sequence_short(self, n, expected_total_sleep_time):
31393139
31403140 finally :
31413141 test_path .unlink (missing_ok = True )
3142+
3143+ def test_add_decorator_imports_helper_in_class ():
3144+ code_path = (Path (__file__ ).parent .resolve () / "../code_to_optimize/bubble_sort_classmethod.py" ).resolve ()
3145+ tests_root = Path (__file__ ).parent .resolve () / "../code_to_optimize/tests/pytest/"
3146+ project_root_path = (Path (__file__ ).parent / ".." ).resolve ()
3147+ original_cwd = Path .cwd ()
3148+ run_cwd = Path (__file__ ).parent .parent .resolve ()
3149+ test_config = TestConfig (
3150+ tests_root = tests_root ,
3151+ tests_project_rootdir = project_root_path ,
3152+ project_root_path = project_root_path ,
3153+ test_framework = "pytest" ,
3154+ pytest_cmd = "pytest" ,
3155+ )
3156+ func = FunctionToOptimize (function_name = "sort_classmethod" , parents = [], file_path = code_path )
3157+ func_optimizer = FunctionOptimizer (function_to_optimize = func , test_cfg = test_config )
3158+ #func_optimizer = pass
3159+ try :
3160+ ctx_result = func_optimizer .get_code_optimization_context ()
3161+ code_context : CodeOptimizationContext = ctx_result .unwrap ()
3162+ original_helper_code : dict [Path , str ] = {}
3163+ helper_function_paths = {hf .file_path for hf in code_context .helper_functions }
3164+ for helper_function_path in helper_function_paths :
3165+ with helper_function_path .open (encoding = "utf8" ) as f :
3166+ helper_code = f .read ()
3167+ original_helper_code [helper_function_path ] = helper_code
3168+ computed_fn_opt = True
3169+ line_profiler_output_file = add_decorator_imports (
3170+ func_optimizer .function_to_optimize , code_context )
3171+ expected_code_main = f"""from code_to_optimize.bubble_sort_in_class import BubbleSortClass
3172+ from line_profiler import profile as codeflash_line_profile
3173+ codeflash_line_profile.enable(output_prefix='{ line_profiler_output_file } ')
3174+
3175+
3176+ @codeflash_line_profile
3177+ def sort_classmethod(x):
3178+ y = BubbleSortClass()
3179+ return y.sorter(x)
3180+ """
3181+ expected_code_helper = """from line_profiler import profile as codeflash_line_profile
3182+
3183+
3184+ def hi():
3185+ pass
3186+
3187+
3188+ class BubbleSortClass:
3189+ def __init__(self):
3190+ pass
3191+
3192+ @codeflash_line_profile
3193+ def sorter(self, arr):
3194+ n = len(arr)
3195+ for i in range(n):
3196+ for j in range(0, n - i - 1):
3197+ if arr[j] > arr[j + 1]:
3198+ arr[j], arr[j + 1] = arr[j + 1], arr[j]
3199+ return arr
3200+
3201+ def helper(self, arr, j):
3202+ return arr[j] > arr[j + 1]
3203+ """
3204+ assert code_path .read_text ("utf-8" ) == expected_code_main
3205+ assert code_context .helper_functions [0 ].file_path .read_text ("utf-8" ) == expected_code_helper
3206+ finally :
3207+ #if computed_fn_opt:
3208+ func_optimizer .write_code_and_helpers (
3209+ func_optimizer .function_to_optimize_source_code , original_helper_code , func_optimizer .function_to_optimize .file_path
3210+ )
3211+
3212+ def test_add_decorator_imports_helper_in_nested_class ():
3213+ code_path = (Path (__file__ ).parent .resolve () / "../code_to_optimize/bubble_sort_nested_classmethod.py" ).resolve ()
3214+ tests_root = Path (__file__ ).parent .resolve () / "../code_to_optimize/tests/pytest/"
3215+ project_root_path = (Path (__file__ ).parent / ".." ).resolve ()
3216+ original_cwd = Path .cwd ()
3217+ run_cwd = Path (__file__ ).parent .parent .resolve ()
3218+ test_config = TestConfig (
3219+ tests_root = tests_root ,
3220+ tests_project_rootdir = project_root_path ,
3221+ project_root_path = project_root_path ,
3222+ test_framework = "pytest" ,
3223+ pytest_cmd = "pytest" ,
3224+ )
3225+ func = FunctionToOptimize (function_name = "sort_classmethod" , parents = [], file_path = code_path )
3226+ func_optimizer = FunctionOptimizer (function_to_optimize = func , test_cfg = test_config )
3227+ #func_optimizer = pass
3228+ try :
3229+ ctx_result = func_optimizer .get_code_optimization_context ()
3230+ code_context : CodeOptimizationContext = ctx_result .unwrap ()
3231+ original_helper_code : dict [Path , str ] = {}
3232+ helper_function_paths = {hf .file_path for hf in code_context .helper_functions }
3233+ for helper_function_path in helper_function_paths :
3234+ with helper_function_path .open (encoding = "utf8" ) as f :
3235+ helper_code = f .read ()
3236+ original_helper_code [helper_function_path ] = helper_code
3237+ computed_fn_opt = True
3238+ line_profiler_output_file = add_decorator_imports (
3239+ func_optimizer .function_to_optimize , code_context )
3240+ expected_code_main = f"""from code_to_optimize.bubble_sort_in_nested_class import WrapperClass
3241+ from line_profiler import profile as codeflash_line_profile
3242+ codeflash_line_profile.enable(output_prefix='{ line_profiler_output_file } ')
3243+
3244+
3245+ @codeflash_line_profile
3246+ def sort_classmethod(x):
3247+ y = WrapperClass.BubbleSortClass()
3248+ return y.sorter(x)
3249+ """
3250+ expected_code_helper = """from line_profiler import profile as codeflash_line_profile
3251+
3252+
3253+ def hi():
3254+ pass
3255+
3256+
3257+ class WrapperClass:
3258+ def __init__(self):
3259+ pass
3260+
3261+ class BubbleSortClass:
3262+ def __init__(self):
3263+ pass
3264+
3265+ @codeflash_line_profile
3266+ def sorter(self, arr):
3267+ def inner_helper(arr, j):
3268+ return arr[j] > arr[j + 1]
3269+
3270+ for i in range(len(arr)):
3271+ for j in range(len(arr) - 1):
3272+ if arr[j] > arr[j + 1]:
3273+ temp = arr[j]
3274+ arr[j] = arr[j + 1]
3275+ arr[j + 1] = temp
3276+ return arr
3277+
3278+ def helper(self, arr, j):
3279+ return arr[j] > arr[j + 1]
3280+ """
3281+ assert code_path .read_text ("utf-8" ) == expected_code_main
3282+ assert code_context .helper_functions [0 ].file_path .read_text ("utf-8" ) == expected_code_helper
3283+ finally :
3284+ #if computed_fn_opt:
3285+ func_optimizer .write_code_and_helpers (
3286+ func_optimizer .function_to_optimize_source_code , original_helper_code , func_optimizer .function_to_optimize .file_path
3287+ )
0 commit comments