summaryrefslogtreecommitdiff
path: root/tests/test_pycode_ast.py
blob: d195e5c6f6786cee433faf10a51dfc79ae4dbc47 (plain)
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
"""
    test_pycode_ast
    ~~~~~~~~~~~~~~~

    Test pycode.ast

    :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import sys

import pytest

from sphinx.pycode import ast


@pytest.mark.parametrize('source,expected', [
    ("os.path", "os.path"),                     # Attribute
    ("b'bytes'", "b'bytes'"),                   # Bytes
    ("object()", "object()"),                   # Call
    ("1234", "1234"),                           # Constant
    ("{'key1': 'value1', 'key2': 'value2'}",
     "{'key1': 'value1', 'key2': 'value2'}"),   # Dict
    ("...", "..."),                             # Ellipsis
    ("Tuple[int, int]", "Tuple[int, int]"),     # Index, Subscript
    ("lambda x, y: x + y",
     "lambda x, y: ..."),                       # Lambda
    ("[1, 2, 3]", "[1, 2, 3]"),                 # List
    ("sys", "sys"),                             # Name, NameConstant
    ("1234", "1234"),                           # Num
    ("{1, 2, 3}", "{1, 2, 3}"),                 # Set
    ("'str'", "'str'"),                         # Str
    ("(1, 2, 3)", "1, 2, 3"),                   # Tuple
])
def test_unparse(source, expected):
    module = ast.parse(source)
    assert ast.unparse(module.body[0].value) == expected


def test_unparse_None():
    assert ast.unparse(None) is None


@pytest.mark.skipif(sys.version_info < (3, 8), reason='python 3.8+ is required.')
def test_unparse_py38():
    source = "lambda x=0, /, y=1, *args, z, **kwargs: x + y + z"
    expected = "lambda x=0, /, y=1, *args, z, **kwargs: ..."
    module = ast.parse(source)
    assert ast.unparse(module.body[0].value) == expected