File: test_comparison.py

package info (click to toggle)
python-libcst 1.4.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,928 kB
  • sloc: python: 76,235; makefile: 10; sh: 2
file content (353 lines) | stat: -rw-r--r-- 13,640 bytes parent folder | download
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from typing import Callable, Optional

import libcst as cst
from libcst import parse_expression
from libcst._nodes.tests.base import CSTNodeTest
from libcst.metadata import CodeRange
from libcst.testing.utils import data_provider


class ComparisonTest(CSTNodeTest):
    @data_provider(
        (
            # Simple comparison statements
            (
                cst.Comparison(
                    cst.Name("foo"),
                    (cst.ComparisonTarget(cst.LessThan(), cst.Integer("5")),),
                ),
                "foo < 5",
            ),
            (
                cst.Comparison(
                    cst.Name("foo"),
                    (cst.ComparisonTarget(cst.NotEqual(), cst.Integer("5")),),
                ),
                "foo != 5",
            ),
            (
                cst.Comparison(
                    cst.Name("foo"), (cst.ComparisonTarget(cst.Is(), cst.Name("True")),)
                ),
                "foo is True",
            ),
            (
                cst.Comparison(
                    cst.Name("foo"),
                    (cst.ComparisonTarget(cst.IsNot(), cst.Name("False")),),
                ),
                "foo is not False",
            ),
            (
                cst.Comparison(
                    cst.Name("foo"), (cst.ComparisonTarget(cst.In(), cst.Name("bar")),)
                ),
                "foo in bar",
            ),
            (
                cst.Comparison(
                    cst.Name("foo"),
                    (cst.ComparisonTarget(cst.NotIn(), cst.Name("bar")),),
                ),
                "foo not in bar",
            ),
            # Comparison with parens
            (
                cst.Comparison(
                    lpar=(cst.LeftParen(),),
                    left=cst.Name("foo"),
                    comparisons=(
                        cst.ComparisonTarget(
                            operator=cst.NotIn(), comparator=cst.Name("bar")
                        ),
                    ),
                    rpar=(cst.RightParen(),),
                ),
                "(foo not in bar)",
            ),
            (
                cst.Comparison(
                    left=cst.Name(
                        "a", lpar=(cst.LeftParen(),), rpar=(cst.RightParen(),)
                    ),
                    comparisons=(
                        cst.ComparisonTarget(
                            operator=cst.Is(
                                whitespace_before=cst.SimpleWhitespace(""),
                                whitespace_after=cst.SimpleWhitespace(""),
                            ),
                            comparator=cst.Name(
                                "b", lpar=(cst.LeftParen(),), rpar=(cst.RightParen(),)
                            ),
                        ),
                        cst.ComparisonTarget(
                            operator=cst.Is(
                                whitespace_before=cst.SimpleWhitespace(""),
                                whitespace_after=cst.SimpleWhitespace(""),
                            ),
                            comparator=cst.Name(
                                "c", lpar=(cst.LeftParen(),), rpar=(cst.RightParen(),)
                            ),
                        ),
                    ),
                ),
                "(a)is(b)is(c)",
            ),
            # Valid expressions that look like they shouldn't parse
            (
                cst.Comparison(
                    left=cst.Integer("5"),
                    comparisons=(
                        cst.ComparisonTarget(
                            operator=cst.NotIn(
                                whitespace_before=cst.SimpleWhitespace("")
                            ),
                            comparator=cst.Name("bar"),
                        ),
                    ),
                ),
                "5not in bar",
            ),
            # Validate that spacing works properly
            (
                cst.Comparison(
                    lpar=(cst.LeftParen(whitespace_after=cst.SimpleWhitespace(" ")),),
                    left=cst.Name("foo"),
                    comparisons=(
                        cst.ComparisonTarget(
                            operator=cst.NotIn(
                                whitespace_before=cst.SimpleWhitespace("  "),
                                whitespace_between=cst.SimpleWhitespace("  "),
                                whitespace_after=cst.SimpleWhitespace("  "),
                            ),
                            comparator=cst.Name("bar"),
                        ),
                    ),
                    rpar=(cst.RightParen(whitespace_before=cst.SimpleWhitespace(" ")),),
                ),
                "( foo  not  in  bar )",
            ),
            # Do some complex nodes
            (
                cst.Comparison(
                    left=cst.Name("baz"),
                    comparisons=(
                        cst.ComparisonTarget(
                            operator=cst.Equal(),
                            comparator=cst.Comparison(
                                lpar=(cst.LeftParen(),),
                                left=cst.Name("foo"),
                                comparisons=(
                                    cst.ComparisonTarget(
                                        operator=cst.NotIn(), comparator=cst.Name("bar")
                                    ),
                                ),
                                rpar=(cst.RightParen(),),
                            ),
                        ),
                    ),
                ),
                "baz == (foo not in bar)",
                CodeRange((1, 0), (1, 23)),
            ),
            (
                cst.Comparison(
                    left=cst.Name("a"),
                    comparisons=(
                        cst.ComparisonTarget(
                            operator=cst.GreaterThan(), comparator=cst.Name("b")
                        ),
                        cst.ComparisonTarget(
                            operator=cst.GreaterThan(), comparator=cst.Name("c")
                        ),
                    ),
                ),
                "a > b > c",
                CodeRange((1, 0), (1, 9)),
            ),
            # Is safe to use with word operators if it's leading/trailing children are
            (
                cst.IfExp(
                    body=cst.Comparison(
                        left=cst.Name("a"),
                        comparisons=(
                            cst.ComparisonTarget(
                                operator=cst.GreaterThan(),
                                comparator=cst.Name(
                                    "b",
                                    lpar=(cst.LeftParen(),),
                                    rpar=(cst.RightParen(),),
                                ),
                            ),
                        ),
                    ),
                    test=cst.Comparison(
                        left=cst.Name(
                            "c", lpar=(cst.LeftParen(),), rpar=(cst.RightParen(),)
                        ),
                        comparisons=(
                            cst.ComparisonTarget(
                                operator=cst.GreaterThan(), comparator=cst.Name("d")
                            ),
                        ),
                    ),
                    orelse=cst.Name("e"),
                    whitespace_before_if=cst.SimpleWhitespace(""),
                    whitespace_after_if=cst.SimpleWhitespace(""),
                ),
                "a > (b)if(c) > d else e",
            ),
            # is safe to use with word operators if entirely surrounded in parenthesis
            (
                cst.IfExp(
                    body=cst.Name("a"),
                    test=cst.Comparison(
                        left=cst.Name("b"),
                        comparisons=(
                            cst.ComparisonTarget(
                                operator=cst.GreaterThan(), comparator=cst.Name("c")
                            ),
                        ),
                        lpar=(cst.LeftParen(),),
                        rpar=(cst.RightParen(),),
                    ),
                    orelse=cst.Name("d"),
                    whitespace_after_if=cst.SimpleWhitespace(""),
                    whitespace_before_else=cst.SimpleWhitespace(""),
                ),
                "a if(b > c)else d",
            ),
        )
    )
    def test_valid(
        self, node: cst.CSTNode, code: str, position: Optional[CodeRange] = None
    ) -> None:
        self.validate_node(node, code, parse_expression, expected_position=position)

    @data_provider(
        (
            (
                lambda: cst.Comparison(
                    cst.Name("foo"),
                    (cst.ComparisonTarget(cst.LessThan(), cst.Integer("5")),),
                    lpar=(cst.LeftParen(),),
                ),
                "left paren without right paren",
            ),
            (
                lambda: cst.Comparison(
                    cst.Name("foo"),
                    (cst.ComparisonTarget(cst.LessThan(), cst.Integer("5")),),
                    rpar=(cst.RightParen(),),
                ),
                "right paren without left paren",
            ),
            (
                lambda: cst.Comparison(cst.Name("foo"), ()),
                "at least one ComparisonTarget",
            ),
            (
                lambda: cst.Comparison(
                    left=cst.Name("foo"),
                    comparisons=(
                        cst.ComparisonTarget(
                            operator=cst.NotIn(
                                whitespace_before=cst.SimpleWhitespace("")
                            ),
                            comparator=cst.Name("bar"),
                        ),
                    ),
                ),
                "at least one space around comparison operator",
            ),
            (
                lambda: cst.Comparison(
                    left=cst.Name("foo"),
                    comparisons=(
                        cst.ComparisonTarget(
                            operator=cst.NotIn(
                                whitespace_after=cst.SimpleWhitespace("")
                            ),
                            comparator=cst.Name("bar"),
                        ),
                    ),
                ),
                "at least one space around comparison operator",
            ),
            # multi-target comparisons
            (
                lambda: cst.Comparison(
                    left=cst.Name("a"),
                    comparisons=(
                        cst.ComparisonTarget(
                            operator=cst.Is(), comparator=cst.Name("b")
                        ),
                        cst.ComparisonTarget(
                            operator=cst.Is(whitespace_before=cst.SimpleWhitespace("")),
                            comparator=cst.Name("c"),
                        ),
                    ),
                ),
                "at least one space around comparison operator",
            ),
            (
                lambda: cst.Comparison(
                    left=cst.Name("a"),
                    comparisons=(
                        cst.ComparisonTarget(
                            operator=cst.Is(), comparator=cst.Name("b")
                        ),
                        cst.ComparisonTarget(
                            operator=cst.Is(whitespace_after=cst.SimpleWhitespace("")),
                            comparator=cst.Name("c"),
                        ),
                    ),
                ),
                "at least one space around comparison operator",
            ),
            # whitespace around the comparision itself
            # a ifb > c else d
            (
                lambda: cst.IfExp(
                    body=cst.Name("a"),
                    test=cst.Comparison(
                        left=cst.Name("b"),
                        comparisons=(
                            cst.ComparisonTarget(
                                operator=cst.GreaterThan(), comparator=cst.Name("c")
                            ),
                        ),
                    ),
                    orelse=cst.Name("d"),
                    whitespace_after_if=cst.SimpleWhitespace(""),
                ),
                "Must have at least one space after 'if' keyword.",
            ),
            # a if b > celse d
            (
                lambda: cst.IfExp(
                    body=cst.Name("a"),
                    test=cst.Comparison(
                        left=cst.Name("b"),
                        comparisons=(
                            cst.ComparisonTarget(
                                operator=cst.GreaterThan(), comparator=cst.Name("c")
                            ),
                        ),
                    ),
                    orelse=cst.Name("d"),
                    whitespace_before_else=cst.SimpleWhitespace(""),
                ),
                "Must have at least one space before 'else' keyword.",
            ),
        )
    )
    def test_invalid(
        self, get_node: Callable[[], cst.CSTNode], expected_re: str
    ) -> None:
        self.assert_invalid(get_node, expected_re)