MCPcopy Index your code
hub / github.com/RustPython/RustPython / assertASTEqual

Method assertASTEqual

Lib/test/support/ast_helper.py:7–47  ·  view source on GitHub ↗
(self, ast1, ast2)

Source from the content-addressed store, hash-verified

5 """Test mixing to have basic assertions for AST nodes."""
6
7 def assertASTEqual(self, ast1, ast2):
8 # Ensure the comparisons start at an AST node
9 self.assertIsInstance(ast1, ast.AST)
10 self.assertIsInstance(ast2, ast.AST)
11
12 # An AST comparison routine modeled after ast.dump(), but
13 # instead of string building, it traverses the two trees
14 # in lock-step.
15 def traverse_compare(a, b, missing=object()):
16 if type(a) is not type(b):
17 self.fail(f"{type(a)!r} is not {type(b)!r}")
18 if isinstance(a, ast.AST):
19 for field in a._fields:
20 if isinstance(a, ast.Constant) and field == "kind":
21 # Skip the 'kind' field for ast.Constant
22 continue
23 value1 = getattr(a, field, missing)
24 value2 = getattr(b, field, missing)
25 # Singletons are equal by definition, so further
26 # testing can be skipped.
27 if value1 is not value2:
28 traverse_compare(value1, value2)
29 elif isinstance(a, list):
30 try:
31 for node1, node2 in zip(a, b, strict=True):
32 traverse_compare(node1, node2)
33 except ValueError:
34 # Attempt a "pretty" error ala assertSequenceEqual()
35 len1 = len(a)
36 len2 = len(b)
37 if len1 > len2:
38 what = "First"
39 diff = len1 - len2
40 else:
41 what = "Second"
42 diff = len2 - len1
43 msg = f"{what} list contains {diff} additional elements."
44 raise self.failureException(msg) from None
45 elif a != b:
46 self.fail(f"{a!r} != {b!r}")
47 traverse_compare(ast1, ast2)

Callers 4

check_ast_roundtripMethod · 0.80
test_nanMethod · 0.80
test_empty_setMethod · 0.80

Calls 1

assertIsInstanceMethod · 0.80

Tested by 4

check_ast_roundtripMethod · 0.64
test_nanMethod · 0.64
test_empty_setMethod · 0.64