diff options
author | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-12-13 15:59:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-13 15:59:54 +0100 |
commit | e3e5aebae7a96eb02b0f32e14f07c873f8d2519f (patch) | |
tree | f879e52a100a30ee30f9785a40da6c86061edbf7 | |
parent | bf5217819adbb71bc2c873efa2efb298d12f4731 (diff) | |
download | pylint-git-e3e5aebae7a96eb02b0f32e14f07c873f8d2519f.tar.gz |
Move tests from ``TestConfusingConsecutiveElifChecker`` to functional tests (#5517)
* Remove some redundant test
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r-- | tests/extensions/test_confusing_elif.py | 198 | ||||
-rw-r--r-- | tests/functional/ext/confusing_elif/confusing_elif.py | 114 | ||||
-rw-r--r-- | tests/functional/ext/confusing_elif/confusing_elif.txt | 4 |
3 files changed, 111 insertions, 205 deletions
diff --git a/tests/extensions/test_confusing_elif.py b/tests/extensions/test_confusing_elif.py deleted file mode 100644 index 236b0c6e6..000000000 --- a/tests/extensions/test_confusing_elif.py +++ /dev/null @@ -1,198 +0,0 @@ -# Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> -# Copyright (c) 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com> -# Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com> -# Copyright (c) 2021 Andreas Finkler <andi.finkler@gmail.com> - -# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html -# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE - -"""Tests for the pylint checker in :mod:`pylint.extensions.confusing_elif -""" - -import astroid - -from pylint.extensions.confusing_elif import ConfusingConsecutiveElifChecker -from pylint.testutils import CheckerTestCase, MessageTest - -MSG_ID_CONFUSING_CONSECUTIVE_ELIF = "confusing-consecutive-elif" - - -class TestConfusingConsecutiveElifChecker(CheckerTestCase): - """Tests for pylint.extensions.confusing_elif.ConfusingConsecutiveElifChecker""" - - CHECKER_CLASS = ConfusingConsecutiveElifChecker - - def test_triggered_if_if_block_ends_with_elif(self) -> None: - """ - Given an if-elif construct - When the body of the if ends with an elif - Then the message confusing-consecutive-elif must be triggered. - """ - example_code = """ - def foo(a, b, c): - if a > b: #@ - if a > 0: - return a - elif a < 0: - return 0 - elif a > c: #@ - return c - """ - if_node_to_test, elif_node = astroid.extract_node(example_code) - with self.assertAddsMessages( - MessageTest(msg_id=MSG_ID_CONFUSING_CONSECUTIVE_ELIF, node=elif_node) - ): - self.checker.visit_if(if_node_to_test) - - def test_triggered_if_elif_block_ends_with_elif(self) -> None: - """ - Given an if-elif-elif construct - When the body of the first elif ends with an elif - Then the message confusing-consecutive-elif must be triggered. - """ - example_code = """ - def foo(a, b, c): - if a < b: - return b - elif a > b: #@ - if a > 0: - return a - elif a < 0: - return 0 - elif a > c: #@ - return c - """ - if_node_to_test, elif_node = astroid.extract_node(example_code) - with self.assertAddsMessages( - MessageTest(msg_id=MSG_ID_CONFUSING_CONSECUTIVE_ELIF, node=elif_node) - ): - self.checker.visit_if(if_node_to_test) - - def test_triggered_if_block_ends_with_if(self) -> None: - """ - Given an if-elif construct - When the body of the if ends with an if - Then the message confusing-consecutive-elif must be triggered. - """ - example_code = """ - def foo(a, b, c): - if a > b: #@ - if a > 0: - return a - elif a > c: #@ - return c - """ - if_node_to_test, elif_node = astroid.extract_node(example_code) - with self.assertAddsMessages( - MessageTest(msg_id=MSG_ID_CONFUSING_CONSECUTIVE_ELIF, node=elif_node) - ): - self.checker.visit_if(if_node_to_test) - - def test_not_triggered_if_indented_block_ends_with_else(self) -> None: - """ - Given an if-elif construct - When the body of the if ends with an else - Then no message shall be triggered. - """ - example_code = """ - def foo(a, b, c): - if a > b: #@ - if a > 0: - return a - else: - return 0 - elif a > c: - return c - """ - if_node_to_test = astroid.extract_node(example_code) - with self.assertNoMessages(): - self.checker.visit_if(if_node_to_test) - - def test_not_triggered_if_indentend_block_ends_with_call(self) -> None: - """ - Given an if-elif construct - When the body of the if ends with a function call - Then no message shall be triggered. - - Note: There is nothing special about the body ending with a function call. This is just taken as a - representative value for the equivalence class of "every node class unrelated to if/elif/else". - """ - example_code = """ - def foo(a, b, c): - result = None - if a > b: #@ - if a > 0: - result = a - print("result is", result) - elif a > c: - result = c - return result - """ - if_node_to_test = astroid.extract_node(example_code) - with self.assertNoMessages(): - self.checker.visit_if(if_node_to_test) - - def test_not_triggered_if_indented_block_ends_with_ifexp(self) -> None: - """ - Given an if-elif construct - When the body of the if ends with an if expression - Then no message shall be triggered. - """ - example_code = """ - def foo(a, b, c): - result = None - if a > b: #@ - if a > 0: - result = a - result = b if b > c else c - elif a > c: - result = c - return result - """ - if_node_to_test = astroid.extract_node(example_code) - with self.assertNoMessages(): - self.checker.visit_if(if_node_to_test) - - def test_not_triggered_if_outer_block_does_not_have_elif(self) -> None: - """ - Given an if construct without an elif - When the body of the if ends with an if - Then no message shall be triggered. - """ - example_code = """ - def foo(a, b, c): - result = None - if a > b: #@ - if a > 0: - result = a - elif a < 0: - result = 0 - else: - result = c - return result - """ - if_node_to_test = astroid.extract_node(example_code) - with self.assertNoMessages(): - self.checker.visit_if(if_node_to_test) - - def test_not_triggered_if_outer_block_continues_with_if(self) -> None: - """ - Given an if construct which continues with a new if construct - When the body of the first if ends with an if expression - Then no message shall be triggered. - """ - example_code = """ - def foo(a, b, c): - result = None - if a > b: #@ - if a > 0: - result = a - elif a < 0: - result = 0 - if b > c: - result = c - return result - """ - if_node_to_test = astroid.extract_node(example_code) - with self.assertNoMessages(): - self.checker.visit_if(if_node_to_test) diff --git a/tests/functional/ext/confusing_elif/confusing_elif.py b/tests/functional/ext/confusing_elif/confusing_elif.py index 9f1d1ffe9..002931de3 100644 --- a/tests/functional/ext/confusing_elif/confusing_elif.py +++ b/tests/functional/ext/confusing_elif/confusing_elif.py @@ -1,7 +1,13 @@ # pylint: disable=missing-module-docstring, missing-function-docstring -def check_config(machine, old_conf, new_conf): - """Example code that will trigger the message""" + +def triggered_if_if_block_ends_with_elif(machine, old_conf, new_conf): + """Example code that will trigger the message + + Given an if-elif construct + When the body of the if ends with an elif + Then the message confusing-consecutive-elif must be triggered. + """ if old_conf: if not new_conf: machine.disable() @@ -12,8 +18,13 @@ def check_config(machine, old_conf, new_conf): machine.enable(new_conf.value) -def check_config_2(machine, old_conf, new_conf): - """Example code must not trigger the message, because the inner block ends with else.""" +def not_triggered_if_indented_block_ends_with_else(machine, old_conf, new_conf): + """Example code must not trigger the message, because the inner block ends with else. + + Given an if-elif construct + When the body of the if ends with an else + Then no message shall be triggered. + """ if old_conf: if not new_conf: machine.disable() @@ -25,10 +36,18 @@ def check_config_2(machine, old_conf, new_conf): elif new_conf: machine.enable(new_conf.value) -def check_config_3(machine, old_conf, new_conf): + +def not_triggered_if_indentend_block_ends_with_call(machine, old_conf, new_conf): """ Example code must not trigger the message, - because the inner if is not the final node of the body. + + Given an if-elif construct + When the body of the if ends with a function call + Then no message shall be triggered. + + Note: There is nothing special about the body ending with a function call. + This is just taken as a representative value for the equivalence class of + "every node class unrelated to if/elif/else". """ if old_conf: if not new_conf: @@ -39,3 +58,86 @@ def check_config_3(machine, old_conf, new_conf): print("Processed old configuration...") elif new_conf: machine.enable(new_conf.value) + + +def triggered_if_elif_block_ends_with_elif(machine, old_conf, new_conf, new_new_conf): + """Example code that will trigger the message + + Given an if-elif-elif construct + When the body of the first elif ends with an elif + Then the message confusing-consecutive-elif must be triggered. + """ + if old_conf: + machine.disable() + elif not new_conf: + if new_new_conf: + machine.disable() + elif old_conf.value != new_conf.value: + machine.disable() + machine.enable(new_conf.value) + elif new_conf: # [confusing-consecutive-elif] + machine.enable(new_conf.value) + + +def triggered_if_block_ends_with_if(machine, old_conf, new_conf, new_new_conf): + """Example code that will trigger the message + + Given an if-elif construct + When the body of the if ends with an if + Then the message confusing-consecutive-elif must be triggered. + """ + if old_conf: + if new_new_conf: + machine.disable() + elif new_conf: # [confusing-consecutive-elif] + machine.enable(new_conf.value) + + +def not_triggered_if_indented_block_ends_with_ifexp(machine, old_conf, new_conf): + """ + Example code must not trigger the message, + + Given an if-elif construct + When the body of the if ends with an if expression + Then no message shall be triggered. + """ + if old_conf: + if not new_conf: + machine.disable() + print("Processed old configuration...") + elif new_conf: + machine.enable(new_conf.value) + + +def not_triggered_if_outer_block_does_not_have_elif(machine, old_conf, new_conf): + """Example code must not trigger the message + + Given an if construct without an elif + When the body of the if ends with an if + Then no message shall be triggered. + """ + if old_conf: + if not new_conf: + machine.disable() + elif old_conf.value != new_conf.value: + machine.disable() + machine.enable(new_conf.value) + else: + pass + + +def not_triggered_if_outer_block_continues_with_if(machine, old_conf, new_conf, new_new_conf): + """Example code that will trigger the message + + Given an if construct which continues with a new if construct + When the body of the first if ends with an if expression + Then no message shall be triggered. + """ + if old_conf: + if new_new_conf: + machine.disable() + elif old_conf.value != new_conf.value: + machine.disable() + machine.enable(new_conf.value) + if new_conf: + machine.enable(new_conf.value) diff --git a/tests/functional/ext/confusing_elif/confusing_elif.txt b/tests/functional/ext/confusing_elif/confusing_elif.txt index 17fd18eb5..35487e9df 100644 --- a/tests/functional/ext/confusing_elif/confusing_elif.txt +++ b/tests/functional/ext/confusing_elif/confusing_elif.txt @@ -1 +1,3 @@ -confusing-consecutive-elif:11:4:12:38:check_config:Consecutive elif with differing indentation level, consider creating a function to separate the inner elif:UNDEFINED +confusing-consecutive-elif:17:4:18:38:triggered_if_if_block_ends_with_elif:Consecutive elif with differing indentation level, consider creating a function to separate the inner elif:UNDEFINED +confusing-consecutive-elif:78:4:79:38:triggered_if_elif_block_ends_with_elif:Consecutive elif with differing indentation level, consider creating a function to separate the inner elif:UNDEFINED +confusing-consecutive-elif:92:4:93:38:triggered_if_block_ends_with_if:Consecutive elif with differing indentation level, consider creating a function to separate the inner elif:UNDEFINED |