From c2388622923c81b5f06b0c9a5ce821fc03c624b9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 26 Aug 2019 14:48:55 +0300 Subject: bpo-36917: Backport basic test for ast.NodeVisitor. (GH-15511) --- 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 72f8467..7614d40 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -4,6 +4,7 @@ import os import sys import unittest import weakref +from textwrap import dedent from test import support @@ -1123,6 +1124,44 @@ class ConstantTests(unittest.TestCase): 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 -- cgit v0.12