summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_unparse.py
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2023-01-21 21:44:41 (GMT)
committerGitHub <noreply@github.com>2023-01-21 21:44:41 (GMT)
commitc1c5882359a2899b74c1685a0d4e61d6e232161f (patch)
tree25735ca47f51c618a5acad9de4917588f56e2498 /Lib/test/test_unparse.py
parentf63f525e161204970418ebc132efc542daaa24ed (diff)
downloadcpython-c1c5882359a2899b74c1685a0d4e61d6e232161f.zip
cpython-c1c5882359a2899b74c1685a0d4e61d6e232161f.tar.gz
cpython-c1c5882359a2899b74c1685a0d4e61d6e232161f.tar.bz2
gh-100518: Add tests for `ast.NodeTransformer` (#100521)
Diffstat (limited to 'Lib/test/test_unparse.py')
-rw-r--r--Lib/test/test_unparse.py42
1 files changed, 2 insertions, 40 deletions
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py
index f1f1dd5..88c7c3a 100644
--- a/Lib/test/test_unparse.py
+++ b/Lib/test/test_unparse.py
@@ -6,6 +6,7 @@ import pathlib
import random
import tokenize
import ast
+from test.support.ast_helper import ASTTestMixin
def read_pyfile(filename):
@@ -128,46 +129,7 @@ docstring_prefixes = (
"async def foo():\n ",
)
-class ASTTestCase(unittest.TestCase):
- def assertASTEqual(self, ast1, ast2):
- # Ensure the comparisons start at an AST node
- self.assertIsInstance(ast1, ast.AST)
- self.assertIsInstance(ast2, ast.AST)
-
- # An AST comparison routine modeled after ast.dump(), but
- # instead of string building, it traverses the two trees
- # in lock-step.
- def traverse_compare(a, b, missing=object()):
- if type(a) is not type(b):
- self.fail(f"{type(a)!r} is not {type(b)!r}")
- if isinstance(a, ast.AST):
- for field in a._fields:
- value1 = getattr(a, field, missing)
- value2 = getattr(b, field, missing)
- # Singletons are equal by definition, so further
- # testing can be skipped.
- if value1 is not value2:
- traverse_compare(value1, value2)
- elif isinstance(a, list):
- try:
- for node1, node2 in zip(a, b, strict=True):
- traverse_compare(node1, node2)
- except ValueError:
- # Attempt a "pretty" error ala assertSequenceEqual()
- len1 = len(a)
- len2 = len(b)
- if len1 > len2:
- what = "First"
- diff = len1 - len2
- else:
- what = "Second"
- diff = len2 - len1
- msg = f"{what} list contains {diff} additional elements."
- raise self.failureException(msg) from None
- elif a != b:
- self.fail(f"{a!r} != {b!r}")
- traverse_compare(ast1, ast2)
-
+class ASTTestCase(ASTTestMixin, unittest.TestCase):
def check_ast_roundtrip(self, code1, **kwargs):
with self.subTest(code1=code1, ast_parse_kwargs=kwargs):
ast1 = ast.parse(code1, **kwargs)