aboutsummaryrefslogtreecommitdiff
path: root/tests/extensions/test_confusing_elif.py
blob: 65fd9baddae7810b24ec4437a775a08e5310aace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# 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, Message

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(
            Message(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(
            Message(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(
            Message(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)