File: test_cst_node.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 (197 lines) | stat: -rw-r--r-- 7,323 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
# 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 textwrap import dedent
from typing import Union

import libcst as cst
from libcst._removal_sentinel import RemovalSentinel
from libcst._types import CSTNodeT
from libcst._visitors import CSTTransformer
from libcst.testing.utils import data_provider, none_throws, UnitTest

_EMPTY_SIMPLE_WHITESPACE = cst.SimpleWhitespace("")


class _TestVisitor(CSTTransformer):
    def __init__(self, test: UnitTest) -> None:
        self.counter = 0
        self.test = test

    def assert_counter(self, expected: int) -> None:
        self.test.assertEqual(self.counter, expected)
        self.counter += 1

    def on_visit(self, node: cst.CSTNode) -> bool:
        if isinstance(node, cst.Module):
            self.assert_counter(0)
        elif isinstance(node, cst.SimpleStatementLine):
            self.assert_counter(1)
        elif isinstance(node, cst.Pass):
            self.assert_counter(2)
        elif isinstance(node, cst.Newline):
            self.assert_counter(4)
        return True

    def on_leave(
        self, original_node: CSTNodeT, updated_node: CSTNodeT
    ) -> Union[CSTNodeT, RemovalSentinel]:
        self.test.assertTrue(original_node.deep_equals(updated_node))
        # Don't allow type checkers to accidentally refine our return type.
        return_node = updated_node
        if isinstance(updated_node, cst.Pass):
            self.assert_counter(3)
        elif isinstance(updated_node, cst.Newline):
            self.assert_counter(5)
        elif isinstance(updated_node, cst.SimpleStatementLine):
            self.assert_counter(6)
        elif isinstance(updated_node, cst.Module):
            self.assert_counter(7)
        return return_node


class CSTNodeTest(UnitTest):
    def test_with_changes(self) -> None:
        initial = cst.TrailingWhitespace(
            whitespace=cst.SimpleWhitespace("  \\\n  "),
            comment=cst.Comment("# initial"),
            newline=cst.Newline("\r\n"),
        )
        changed = initial.with_changes(comment=cst.Comment("# new comment"))

        # see that we have the updated fields
        self.assertEqual(none_throws(changed.comment).value, "# new comment")
        # and that the old fields are still there
        self.assertEqual(changed.whitespace.value, "  \\\n  ")
        self.assertEqual(changed.newline.value, "\r\n")

        # ensure no mutation actually happened
        self.assertEqual(none_throws(initial.comment).value, "# initial")

    def test_default_eq(self) -> None:
        sw1 = cst.SimpleWhitespace("")
        sw2 = cst.SimpleWhitespace("")
        self.assertNotEqual(sw1, sw2)
        self.assertEqual(sw1, sw1)
        self.assertEqual(sw2, sw2)
        self.assertTrue(sw1.deep_equals(sw2))
        self.assertTrue(sw2.deep_equals(sw1))

    def test_hash(self) -> None:
        sw1 = cst.SimpleWhitespace("")
        sw2 = cst.SimpleWhitespace("")
        self.assertNotEqual(hash(sw1), hash(sw2))
        self.assertEqual(hash(sw1), hash(sw1))
        self.assertEqual(hash(sw2), hash(sw2))

    @data_provider(
        {
            "simple": (cst.SimpleWhitespace(""), cst.SimpleWhitespace("")),
            "identity": (_EMPTY_SIMPLE_WHITESPACE, _EMPTY_SIMPLE_WHITESPACE),
            "nested": (
                cst.EmptyLine(whitespace=cst.SimpleWhitespace("")),
                cst.EmptyLine(whitespace=cst.SimpleWhitespace("")),
            ),
            "tuple_versus_list": (
                cst.SimpleStatementLine(body=[cst.Pass()]),
                cst.SimpleStatementLine(body=(cst.Pass(),)),
            ),
        }
    )
    def test_deep_equals_success(self, a: cst.CSTNode, b: cst.CSTNode) -> None:
        self.assertTrue(a.deep_equals(b))

    @data_provider(
        {
            "simple": (cst.SimpleWhitespace(" "), cst.SimpleWhitespace("     ")),
            "nested": (
                cst.EmptyLine(whitespace=cst.SimpleWhitespace(" ")),
                cst.EmptyLine(whitespace=cst.SimpleWhitespace("       ")),
            ),
            "list": (
                cst.SimpleStatementLine(body=[cst.Pass(semicolon=cst.Semicolon())]),
                cst.SimpleStatementLine(body=[cst.Pass(semicolon=cst.Semicolon())] * 2),
            ),
        }
    )
    def test_deep_equals_fails(self, a: cst.CSTNode, b: cst.CSTNode) -> None:
        self.assertFalse(a.deep_equals(b))

    def test_repr(self) -> None:
        self.assertEqual(
            repr(
                cst.SimpleStatementLine(
                    body=[cst.Pass()],
                    # tuple with multiple items
                    leading_lines=(
                        cst.EmptyLine(
                            indent=True,
                            whitespace=cst.SimpleWhitespace(""),
                            comment=None,
                            newline=cst.Newline(),
                        ),
                        cst.EmptyLine(
                            indent=True,
                            whitespace=cst.SimpleWhitespace(""),
                            comment=None,
                            newline=cst.Newline(),
                        ),
                    ),
                    trailing_whitespace=cst.TrailingWhitespace(
                        whitespace=cst.SimpleWhitespace(" "),
                        comment=cst.Comment("# comment"),
                        newline=cst.Newline(),
                    ),
                )
            ),
            dedent(
                """
                SimpleStatementLine(
                    body=[
                        Pass(
                            semicolon=MaybeSentinel.DEFAULT,
                        ),
                    ],
                    leading_lines=[
                        EmptyLine(
                            indent=True,
                            whitespace=SimpleWhitespace(
                                value='',
                            ),
                            comment=None,
                            newline=Newline(
                                value=None,
                            ),
                        ),
                        EmptyLine(
                            indent=True,
                            whitespace=SimpleWhitespace(
                                value='',
                            ),
                            comment=None,
                            newline=Newline(
                                value=None,
                            ),
                        ),
                    ],
                    trailing_whitespace=TrailingWhitespace(
                        whitespace=SimpleWhitespace(
                            value=' ',
                        ),
                        comment=Comment(
                            value='# comment',
                        ),
                        newline=Newline(
                            value=None,
                        ),
                    ),
                )
                """
            ).strip(),
        )

    def test_visit(self) -> None:
        tree = cst.Module((cst.SimpleStatementLine((cst.Pass(),)),))
        tree.visit(_TestVisitor(self))