File: test_dump_graphviz.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 (83 lines) | stat: -rw-r--r-- 2,799 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
# 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 __future__ import annotations

from textwrap import dedent
from typing import TYPE_CHECKING

from libcst import parse_module
from libcst.display import dump_graphviz
from libcst.testing.utils import UnitTest

if TYPE_CHECKING:
    from libcst import Module


class CSTDumpGraphvizTest(UnitTest):
    """Check dump_graphviz contains CST nodes."""

    source_code: str = dedent(
        r"""
        def foo(a: str) -> None:
            pass ;
            pass
            return
        """[
            1:
        ]
    )
    cst: Module

    @classmethod
    def setUpClass(cls) -> None:
        cls.cst = parse_module(cls.source_code)

    def _assert_node(self, node_name: str, graphviz_str: str) -> None:
        self.assertIn(
            node_name, graphviz_str, f"No node {node_name} found in graphviz_dump"
        )

    def _check_essential_nodes_in_tree(self, graphviz_str: str) -> None:
        # Check CST nodes are present in graphviz string
        self._assert_node("Module", graphviz_str)
        self._assert_node("FunctionDef", graphviz_str)
        self._assert_node("Name", graphviz_str)
        self._assert_node("Parameters", graphviz_str)
        self._assert_node("Param", graphviz_str)
        self._assert_node("Annotation", graphviz_str)
        self._assert_node("IndentedBlock", graphviz_str)
        self._assert_node("SimpleStatementLine", graphviz_str)
        self._assert_node("Pass", graphviz_str)
        self._assert_node("Return", graphviz_str)

        # Check CST values are present in graphviz string
        self._assert_node("<foo>", graphviz_str)
        self._assert_node("<a>", graphviz_str)
        self._assert_node("<str>", graphviz_str)
        self._assert_node("<None>", graphviz_str)

    def test_essential_tree(self) -> None:
        """Check essential nodes are present in the CST graphviz dump."""
        graphviz_str = dump_graphviz(self.cst)
        self._check_essential_nodes_in_tree(graphviz_str)

    def test_full_tree(self) -> None:
        """Check all nodes are present in the CST graphviz dump."""
        graphviz_str = dump_graphviz(
            self.cst,
            show_whitespace=True,
            show_defaults=True,
            show_syntax=True,
        )
        self._check_essential_nodes_in_tree(graphviz_str)

        self._assert_node("Semicolon", graphviz_str)
        self._assert_node("SimpleWhitespace", graphviz_str)
        self._assert_node("Newline", graphviz_str)
        self._assert_node("TrailingWhitespace", graphviz_str)

        self._assert_node("<>", graphviz_str)
        self._assert_node("< >", graphviz_str)