File: test_matchers.py

package info (click to toggle)
python-libcst 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,240 kB
  • sloc: python: 78,096; makefile: 15; sh: 2
file content (53 lines) | stat: -rw-r--r-- 1,562 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
# 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 libcst import parse_expression, parse_statement
from libcst.helpers.matchers import node_to_matcher
from libcst.matchers import matches
from libcst.testing.utils import data_provider, UnitTest


class MatchersTest(UnitTest):
    @data_provider(
        (
            ('"some string"',),
            ("call(some, **kwargs)",),
            ("a[b.c]",),
            ("[1 for _ in range(99) if False]",),
        )
    )
    def test_reflexive_expressions(self, code: str) -> None:
        node = parse_expression(code)
        matcher = node_to_matcher(node)
        self.assertTrue(matches(node, matcher))

    @data_provider(
        (
            ("def foo(a) -> None: pass",),
            ("class F: ...",),
            ("foo: bar",),
        )
    )
    def test_reflexive_statements(self, code: str) -> None:
        node = parse_statement(code)
        matcher = node_to_matcher(node)
        self.assertTrue(matches(node, matcher))

    def test_whitespace(self) -> None:
        code_ws = parse_expression("(foo  ,   bar  )")
        code = parse_expression("(foo,bar)")
        self.assertTrue(
            matches(
                code,
                node_to_matcher(code_ws),
            )
        )
        self.assertFalse(
            matches(
                code,
                node_to_matcher(code_ws, match_syntactic_trivia=True),
            )
        )