Skip to content

Commit f220d60

Browse files
author
tbrar06
committed
Finalized LPL refactorer for variadic args and kwargs #387
1 parent 3a807db commit f220d60

File tree

1 file changed

+192
-11
lines changed

1 file changed

+192
-11
lines changed

tests/refactorers/test_long_parameter_list_refactor.py

Lines changed: 192 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,6 @@ def __init__(self, data_params, config_params):
144144
refactorer.refactor(test_file, test_dir, smell, test_file)
145145

146146
modified_code = test_file.read_text()
147-
print("***************************************")
148-
print(modified_code.strip())
149-
print("***************************************")
150-
print(expected_modified_code.strip())
151-
print("***************************************")
152147
assert modified_code.strip() == expected_modified_code.strip()
153148

154149
# cleanup after test
@@ -306,11 +301,6 @@ def generate_report_partial(data_params, config_params):
306301
refactorer.refactor(test_file, test_dir, smell, test_file)
307302

308303
modified_code = test_file.read_text()
309-
print("***************************************")
310-
print(modified_code.strip())
311-
print("***************************************")
312-
print(expected_modified_code.strip())
313-
print("***************************************")
314304
assert modified_code.strip() == expected_modified_code.strip()
315305

316306
# cleanup after test
@@ -410,7 +400,6 @@ def create_partial_report(user_id, username):
410400
refactorer.refactor(test_file, test_dir, smell, test_file)
411401

412402
modified_code = test_file.read_text()
413-
print(modified_code.strip())
414403
assert modified_code.strip() == expected_modified_code.strip()
415404

416405

@@ -624,3 +613,195 @@ def process_data(self, data_params, config_params):
624613
# cleanup after test
625614
test_file.unlink()
626615
test_dir.rmdir()
616+
617+
618+
def test_lpl_with_args_kwargs(refactorer, source_files):
619+
"""Test for function with *args and **kwargs"""
620+
621+
test_dir = source_files / "temp_test_lpl"
622+
test_dir.mkdir(parents=True, exist_ok=True)
623+
624+
test_file = test_dir / "fake.py"
625+
626+
code = textwrap.dedent("""\
627+
def process_data(user_id, username, email, preferences, timezone_config, language, notification_settings, *args, **kwargs):
628+
report = {}
629+
# Use all regular parameters
630+
report.user_id = user_id
631+
report.username = username
632+
report.email = email
633+
report.preferences = preferences
634+
report.timezone = timezone_config
635+
report.language = language
636+
report.notifications = notification_settings
637+
638+
# Use *args
639+
for arg in args:
640+
report.setdefault("extra_data", []).append(arg)
641+
642+
# Use **kwargs
643+
for key, value in kwargs.items():
644+
report[key] = value
645+
646+
return report
647+
648+
# Test call with various argument types
649+
result = process_data(
650+
2,
651+
"janedoe",
652+
653+
{"theme": "light"},
654+
"PST",
655+
"en",
656+
False,
657+
"extra1",
658+
"extra2",
659+
custom_field="custom_value",
660+
another_field=123
661+
)
662+
""")
663+
664+
expected_modified_code = textwrap.dedent("""\
665+
class DataParams_process_data_1:
666+
def __init__(self, user_id, username, email, preferences, language):
667+
self.user_id = user_id
668+
self.username = username
669+
self.email = email
670+
self.preferences = preferences
671+
self.language = language
672+
class ConfigParams_process_data_1:
673+
def __init__(self, timezone_config, notification_settings):
674+
self.timezone_config = timezone_config
675+
self.notification_settings = notification_settings
676+
def process_data(data_params, config_params, *args, **kwargs):
677+
report = {}
678+
# Use all regular parameters
679+
report.user_id = data_params.user_id
680+
report.username = data_params.username
681+
report.email = data_params.email
682+
report.preferences = data_params.preferences
683+
report.timezone = config_params.timezone_config
684+
report.language = data_params.language
685+
report.notifications = config_params.notification_settings
686+
687+
# Use *args
688+
for arg in args:
689+
report.setdefault("extra_data", []).append(arg)
690+
691+
# Use **kwargs
692+
for key, value in kwargs.items():
693+
report[key] = value
694+
695+
return report
696+
697+
# Test call with various argument types
698+
result = process_data(
699+
DataParams_process_data_1(2, "janedoe", "[email protected]", {"theme": "light"}, "en"), ConfigParams_process_data_1("PST", False), "extra1", "extra2", custom_field = "custom_value", another_field = 123)""")
700+
test_file.write_text(code)
701+
smell = create_smell([1])()
702+
refactorer.refactor(test_file, test_dir, smell, test_file)
703+
704+
modified_code = test_file.read_text()
705+
assert modified_code.strip() == expected_modified_code.strip()
706+
707+
# cleanup after test
708+
test_file.unlink()
709+
test_dir.rmdir()
710+
711+
712+
def test_lpl_with_kwargs_only(refactorer, source_files):
713+
"""Test for function with **kwargs"""
714+
715+
test_dir = source_files / "temp_test_lpl"
716+
test_dir.mkdir(parents=True, exist_ok=True)
717+
718+
test_file = test_dir / "fake.py"
719+
720+
code = textwrap.dedent("""\
721+
def process_data_2(user_id, username, email, preferences, timezone_config, language, notification_settings, **kwargs):
722+
report = {}
723+
# Use all regular parameters
724+
report.user_id = user_id
725+
report.username = username
726+
report.email = email
727+
report.preferences.update(preferences)
728+
report.timezone = timezone_config
729+
report.language = language
730+
report.notifications = notification_settings
731+
732+
# Use **kwargs
733+
for key, value in kwargs.items():
734+
report[key] = value # kwargs used
735+
736+
# Additional processing using the parameters
737+
if notification_settings:
738+
report.timezone = f"{timezone_config}_notified"
739+
740+
if "theme" in preferences:
741+
report.language = f"{language}_{preferences['theme']}"
742+
743+
return report
744+
745+
# Test call with various argument types
746+
result = process_data_2(
747+
2,
748+
"janedoe",
749+
750+
{"theme": "light"},
751+
"PST",
752+
"en",
753+
False,
754+
custom_field="custom_value",
755+
another_field=123
756+
)
757+
""")
758+
759+
expected_modified_code = textwrap.dedent("""\
760+
class DataParams_process_data_2_1:
761+
def __init__(self, user_id, username, email, preferences, language):
762+
self.user_id = user_id
763+
self.username = username
764+
self.email = email
765+
self.preferences = preferences
766+
self.language = language
767+
class ConfigParams_process_data_2_1:
768+
def __init__(self, timezone_config, notification_settings):
769+
self.timezone_config = timezone_config
770+
self.notification_settings = notification_settings
771+
def process_data_2(data_params, config_params, **kwargs):
772+
report = {}
773+
# Use all regular parameters
774+
report.user_id = data_params.user_id
775+
report.username = data_params.username
776+
report.email = data_params.email
777+
report.preferences.update(data_params.preferences)
778+
report.timezone = config_params.timezone_config
779+
report.language = data_params.language
780+
report.notifications = config_params.notification_settings
781+
782+
# Use **kwargs
783+
for key, value in kwargs.items():
784+
report[key] = value # kwargs used
785+
786+
# Additional processing using the parameters
787+
if config_params.notification_settings:
788+
report.timezone = f"{config_params.timezone_config}_notified"
789+
790+
if "theme" in data_params.preferences:
791+
report.language = f"{data_params.language}_{data_params.preferences['theme']}"
792+
793+
return report
794+
795+
# Test call with various argument types
796+
result = process_data_2(
797+
DataParams_process_data_2_1(2, "janedoe", "[email protected]", {"theme": "light"}, "en"), ConfigParams_process_data_2_1("PST", False), custom_field = "custom_value", another_field = 123)""")
798+
test_file.write_text(code)
799+
smell = create_smell([1])()
800+
refactorer.refactor(test_file, test_dir, smell, test_file)
801+
802+
modified_code = test_file.read_text()
803+
assert modified_code.strip() == expected_modified_code.strip()
804+
805+
# cleanup after test
806+
test_file.unlink()
807+
test_dir.rmdir()

0 commit comments

Comments
 (0)