]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36917: Backport basic test for ast.NodeVisitor. (GH-15511)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 26 Aug 2019 11:48:55 +0000 (14:48 +0300)
committerGitHub <noreply@github.com>
Mon, 26 Aug 2019 11:48:55 +0000 (14:48 +0300)
Lib/test/test_ast.py

index 72f8467847e8dc468a8c419b6c5d0647b6bf01a8..7614d40e3e5c78f38a15f6a5efbf072d9207a406 100644 (file)
@@ -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