Skip to content

Commit 18ac4a6

Browse files
author
tbrar06
committed
Added test for LPL multi file
1 parent db72019 commit 18ac4a6

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

tests/refactorers/test_long_parameter_list_refactor.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,3 +924,134 @@ def process_complex_data(self, data_params):
924924
# cleanup after test
925925
test_file.unlink()
926926
test_dir.rmdir()
927+
928+
929+
def test_lpl_multi_file_refactor(refactorer, source_files):
930+
"""Test refactoring a function that is called from another file"""
931+
932+
test_dir = source_files / "temp_test_lpl"
933+
test_dir.mkdir(exist_ok=True)
934+
935+
# Create the main file with function definition
936+
main_file = test_dir / "main.py"
937+
main_code = textwrap.dedent("""\
938+
def process_user_data(user_id, username, email, preferences, timezone_config, language, notification_settings, theme):
939+
result = {
940+
'id': user_id,
941+
'name': username,
942+
'email': email,
943+
'prefs': preferences,
944+
'tz': timezone_config,
945+
'lang': language,
946+
'notif': notification_settings,
947+
'theme': theme
948+
}
949+
return result
950+
""")
951+
main_file.write_text(main_code)
952+
953+
# Create another file that uses this function
954+
user_file = test_dir / "user_processor.py"
955+
user_code = textwrap.dedent("""\
956+
from main import process_user_data
957+
958+
def handle_user():
959+
# Call with positional args
960+
result1 = process_user_data(
961+
1,
962+
"john",
963+
964+
{"theme": "light"},
965+
"PST",
966+
"en",
967+
False,
968+
"dark"
969+
)
970+
971+
# Call with keyword args
972+
result2 = process_user_data(
973+
user_id=2,
974+
username="jane",
975+
976+
preferences={"theme": "dark"},
977+
timezone_config="UTC",
978+
language="fr",
979+
notification_settings=True,
980+
theme="light"
981+
)
982+
983+
return result1, result2
984+
""")
985+
user_file.write_text(user_code)
986+
987+
# Expected output for main.py
988+
expected_main_code = textwrap.dedent("""\
989+
class DataParams_process_user_data_1:
990+
def __init__(self, user_id, username, email, preferences, language, theme):
991+
self.user_id = user_id
992+
self.username = username
993+
self.email = email
994+
self.preferences = preferences
995+
self.language = language
996+
self.theme = theme
997+
class ConfigParams_process_user_data_1:
998+
def __init__(self, timezone_config, notification_settings):
999+
self.timezone_config = timezone_config
1000+
self.notification_settings = notification_settings
1001+
def process_user_data(data_params, config_params):
1002+
result = {
1003+
'id': data_params.user_id,
1004+
'name': data_params.username,
1005+
'email': data_params.email,
1006+
'prefs': data_params.preferences,
1007+
'tz': config_params.timezone_config,
1008+
'lang': data_params.language,
1009+
'notif': config_params.notification_settings,
1010+
'theme': data_params.theme
1011+
}
1012+
return result
1013+
""")
1014+
1015+
# Expected output for user_processor.py
1016+
expected_user_code = textwrap.dedent("""\
1017+
from main import process_user_data
1018+
class DataParams_process_user_data_1:
1019+
def __init__(self, user_id, username, email, preferences, language, theme):
1020+
self.user_id = user_id
1021+
self.username = username
1022+
self.email = email
1023+
self.preferences = preferences
1024+
self.language = language
1025+
self.theme = theme
1026+
class ConfigParams_process_user_data_1:
1027+
def __init__(self, timezone_config, notification_settings):
1028+
self.timezone_config = timezone_config
1029+
self.notification_settings = notification_settings
1030+
1031+
def handle_user():
1032+
# Call with positional args
1033+
result1 = process_user_data(
1034+
DataParams_process_user_data_1(1, "john", "[email protected]", {"theme": "light"}, "en", "dark"), ConfigParams_process_user_data_1("PST", False))
1035+
1036+
# Call with keyword args
1037+
result2 = process_user_data(
1038+
DataParams_process_user_data_1(user_id = 2, username = "jane", email = "[email protected]", preferences = {"theme": "dark"}, language = "fr", theme = "light"), ConfigParams_process_user_data_1(timezone_config = "UTC", notification_settings = True))
1039+
1040+
return result1, result2
1041+
""")
1042+
1043+
# Apply the refactoring
1044+
smell = create_smell([1])()
1045+
refactorer.refactor(main_file, test_dir, smell, main_file)
1046+
1047+
# Verify both files were updated correctly
1048+
modified_main_code = main_file.read_text()
1049+
modified_user_code = user_file.read_text()
1050+
1051+
assert modified_main_code.strip() == expected_main_code.strip()
1052+
assert modified_user_code.strip() == expected_user_code.strip()
1053+
1054+
# cleanup after test
1055+
main_file.unlink()
1056+
user_file.unlink()
1057+
test_dir.rmdir()

0 commit comments

Comments
 (0)