From d2943e5636b21bb1aab2c95f12f6a5ddaf5ab2ee Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 26 Aug 2019 10:50:31 +0300 Subject: [PATCH] bpo-36917: Backport basic test for ast.NodeVisitor. --- Lib/test/test_ast.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 72f8467847e8dc..7614d40e3e5c78 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -4,6 +4,7 @@ import sys import unittest import weakref +from textwrap import dedent from test import support @@ -1123,6 +1124,44 @@ def test_literal_eval(self): self.assertEqual(ast.literal_eval(binop), 10+20j) +class NodeVisitorTests(unittest.TestCase): + def test_old_constant_nodes(self): + class Visitor(ast.NodeVisitor): + def visit_Num(self, node): + log.append((node.lineno, 'Num', node.n)) + def visit_Str(self, node): + log.append((node.lineno, 'Str', node.s)) + def visit_Bytes(self, node): + log.append((node.lineno, 'Bytes', node.s)) + def visit_NameConstant(self, node): + log.append((node.lineno, 'NameConstant', node.value)) + def visit_Ellipsis(self, node): + log.append((node.lineno, 'Ellipsis', ...)) + mod = ast.parse(dedent('''\ + i = 42 + f = 4.25 + c = 4.25j + s = 'string' + b = b'bytes' + t = True + n = None + e = ... + ''')) + visitor = Visitor() + log = [] + visitor.visit(mod) + self.assertEqual(log, [ + (1, 'Num', 42), + (2, 'Num', 4.25), + (3, 'Num', 4.25j), + (4, 'Str', 'string'), + (5, 'Bytes', b'bytes'), + (6, 'NameConstant', True), + (7, 'NameConstant', None), + (8, 'Ellipsis', ...), + ]) + + def main(): if __name__ != '__main__': return