File: test_expression.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 (85 lines) | stat: -rw-r--r-- 3,507 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
# 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 ast import literal_eval
from typing import Optional, Union

import libcst as cst
from libcst.helpers import (
    ensure_type,
    get_full_name_for_node,
    get_full_name_for_node_or_raise,
)
from libcst.testing.utils import data_provider, UnitTest


class ExpressionTest(UnitTest):
    @data_provider(
        (
            ("a string", "a string"),
            (cst.Name("a_name"), "a_name"),
            (cst.parse_expression("a.b.c"), "a.b.c"),
            (cst.parse_expression("a.b()"), "a.b"),
            (cst.parse_expression("a.b.c[i]"), "a.b.c"),
            (cst.parse_statement("def fun():  pass"), "fun"),
            (cst.parse_statement("class cls:  pass"), "cls"),
            (
                cst.Decorator(
                    ensure_type(cst.parse_expression("a.b.c.d"), cst.Attribute)
                ),
                "a.b.c.d",
            ),
            (cst.parse_statement("(a.b()).c()"), None),  # not a supported Node type
        )
    )
    def test_get_full_name_for_expression(
        self,
        input: Union[str, cst.CSTNode],
        output: Optional[str],
    ) -> None:
        self.assertEqual(get_full_name_for_node(input), output)
        if output is None:
            with self.assertRaises(Exception):
                get_full_name_for_node_or_raise(input)
        else:
            self.assertEqual(get_full_name_for_node_or_raise(input), output)

    def test_simplestring_evaluated_value(self) -> None:
        raw_string = '"a string."'
        node = ensure_type(cst.parse_expression(raw_string), cst.SimpleString)
        self.assertEqual(node.value, raw_string)
        self.assertEqual(node.evaluated_value, literal_eval(raw_string))

    def test_integer_evaluated_value(self) -> None:
        raw_value = "5"
        node = ensure_type(cst.parse_expression(raw_value), cst.Integer)
        self.assertEqual(node.value, raw_value)
        self.assertEqual(node.evaluated_value, literal_eval(raw_value))

    def test_float_evaluated_value(self) -> None:
        raw_value = "5.5"
        node = ensure_type(cst.parse_expression(raw_value), cst.Float)
        self.assertEqual(node.value, raw_value)
        self.assertEqual(node.evaluated_value, literal_eval(raw_value))

    def test_complex_evaluated_value(self) -> None:
        raw_value = "5j"
        node = ensure_type(cst.parse_expression(raw_value), cst.Imaginary)
        self.assertEqual(node.value, raw_value)
        self.assertEqual(node.evaluated_value, literal_eval(raw_value))

    def test_concatenated_string_evaluated_value(self) -> None:
        code = '"This " "is " "a " "concatenated " "string."'
        node = ensure_type(cst.parse_expression(code), cst.ConcatenatedString)
        self.assertEqual(node.evaluated_value, "This is a concatenated string.")
        code = 'b"A concatenated" b" byte."'
        node = ensure_type(cst.parse_expression(code), cst.ConcatenatedString)
        self.assertEqual(node.evaluated_value, b"A concatenated byte.")
        code = '"var=" f" {var}"'
        node = ensure_type(cst.parse_expression(code), cst.ConcatenatedString)
        self.assertEqual(node.evaluated_value, None)
        code = '"var" "=" f" {var}"'
        node = ensure_type(cst.parse_expression(code), cst.ConcatenatedString)
        self.assertEqual(node.evaluated_value, None)