From cf7da1843c45a4c2df7a749f7886a2d2ba0ee92a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 19:25:40 +0200 Subject: Adding upstream version 7.2.6. Signed-off-by: Daniel Baumann --- tests/test_pycode_ast.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/test_pycode_ast.py (limited to 'tests/test_pycode_ast.py') diff --git a/tests/test_pycode_ast.py b/tests/test_pycode_ast.py new file mode 100644 index 0000000..5efd0cb --- /dev/null +++ b/tests/test_pycode_ast.py @@ -0,0 +1,62 @@ +"""Test pycode.ast""" + +import ast + +import pytest + +from sphinx.pycode.ast import unparse as ast_unparse + + +@pytest.mark.parametrize(('source', 'expected'), [ + ("a + b", "a + b"), # Add + ("a and b", "a and b"), # And + ("os.path", "os.path"), # Attribute + ("1 * 2", "1 * 2"), # BinOp + ("a & b", "a & b"), # BitAnd + ("a | b", "a | b"), # BitOr + ("a ^ b", "a ^ b"), # BitXor + ("a and b and c", "a and b and c"), # BoolOp + ("b'bytes'", "b'bytes'"), # Bytes + ("object()", "object()"), # Call + ("1234", "1234"), # Constant + ("{'key1': 'value1', 'key2': 'value2'}", + "{'key1': 'value1', 'key2': 'value2'}"), # Dict + ("a / b", "a / b"), # Div + ("...", "..."), # Ellipsis + ("a // b", "a // b"), # FloorDiv + ("Tuple[int, int]", "Tuple[int, int]"), # Index, Subscript + ("~1", "~1"), # Invert + ("lambda x, y: x + y", + "lambda x, y: ..."), # Lambda + ("[1, 2, 3]", "[1, 2, 3]"), # List + ("a << b", "a << b"), # LShift + ("a @ b", "a @ b"), # MatMult + ("a % b", "a % b"), # Mod + ("a * b", "a * b"), # Mult + ("sys", "sys"), # Name, NameConstant + ("1234", "1234"), # Num + ("not a", "not a"), # Not + ("a or b", "a or b"), # Or + ("a**b", "a**b"), # Pow + ("a >> b", "a >> b"), # RShift + ("{1, 2, 3}", "{1, 2, 3}"), # Set + ("a - b", "a - b"), # Sub + ("'str'", "'str'"), # Str + ("+a", "+a"), # UAdd + ("-1", "-1"), # UnaryOp + ("-a", "-a"), # USub + ("(1, 2, 3)", "(1, 2, 3)"), # Tuple + ("()", "()"), # Tuple (empty) + ("(1,)", "(1,)"), # Tuple (single item) + ("lambda x=0, /, y=1, *args, z, **kwargs: x + y + z", + "lambda x=0, /, y=1, *args, z, **kwargs: ..."), # posonlyargs + ("0x1234", "0x1234"), # Constant + ("1_000_000", "1_000_000"), # Constant +]) +def test_unparse(source, expected): + module = ast.parse(source) + assert ast_unparse(module.body[0].value, source) == expected + + +def test_unparse_None(): + assert ast_unparse(None) is None -- cgit v1.2.3