diff --git a/integration_tests/test_use_walrus_if.py b/integration_tests/test_use_walrus_if.py new file mode 100644 index 000000000..702c8c26b --- /dev/null +++ b/integration_tests/test_use_walrus_if.py @@ -0,0 +1,32 @@ +from codemodder.codemods.use_walrus_if import UseWalrusIf +from integration_tests.base_test import ( + BaseIntegrationTest, + original_and_expected_from_code_path, +) + + +class TestUseWalrusIf(BaseIntegrationTest): + codemod = UseWalrusIf + code_path = "tests/samples/use_walrus_if.py" + original_code, _ = original_and_expected_from_code_path(code_path, []) + expected_new_code = """ +if (x := foo()) is not None: + print(x) + +if y := bar(): + print(y) + +z = baz() +print(z) + + +def whatever(): + if (b := biz()) == 10: + print(b) +""".lstrip() + + expected_diff = "--- \n+++ \n@@ -1,9 +1,7 @@\n-x = foo()\n-if x is not None:\n+if (x := foo()) is not None:\n print(x)\n \n-y = bar()\n-if y:\n+if y := bar():\n print(y)\n \n z = baz()\n@@ -11,6 +9,5 @@\n \n \n def whatever():\n- b = biz()\n- if b == 10:\n+ if (b := biz()) == 10:\n print(b)\n" + + num_changes = 3 + expected_line_change = 1 + change_description = UseWalrusIf.CHANGE_DESCRIPTION diff --git a/tests/samples/unnecessary_f_str.py b/tests/samples/unnecessary_f_str.py index 7b91fa31b..ec6fa6439 100644 --- a/tests/samples/unnecessary_f_str.py +++ b/tests/samples/unnecessary_f_str.py @@ -1,2 +1,2 @@ -bad = "hello" +bad = f"hello" good = f"{2+3}" diff --git a/tests/samples/use_walrus_if.py b/tests/samples/use_walrus_if.py new file mode 100644 index 000000000..df591dfd9 --- /dev/null +++ b/tests/samples/use_walrus_if.py @@ -0,0 +1,16 @@ +x = foo() +if x is not None: + print(x) + +y = bar() +if y: + print(y) + +z = baz() +print(z) + + +def whatever(): + b = biz() + if b == 10: + print(b)