def assert_node_equal(self, first: libcst.CSTNode, second: libcst.CSTNode) -> None: """Check that two libcst nodes are equal.""" if not first.deep_equals(second): self.fail( f"Nodes are not equal\nGot: {utils.to_string(first)}\nExpected: {utils.to_string(second)}" )
def _cst_node_equality_func( a: cst.CSTNode, b: cst.CSTNode, msg: Optional[str] = None ) -> None: """ For use with addTypeEqualityFunc. """ if not a.deep_equals(b): suffix = "" if msg is None else f"\n{msg}" raise AssertionError(f"\n{a!r}\nis not deeply equal to \n{b!r}{suffix}")
def _leave(self, original: cst.CSTNode, updated: cst.CSTNode) -> cst.CSTNode: # TODO: if global scope query create a module tree from scratch? # NOTE: in the future can cache lib cst node comparisons for speed match = notFound if first_ref_index is not None: match = tryFind(lambda m: original.deep_equals( m.path[first_ref_index].node), matches) if match is not notFound: from_assert = first(astNodeFromAssertion(transform, match)) return from_assert elif original in transform.references: # TODO: replace references to anything destroyed by the transform pass elif find_attempt is notFound and isinstance(updated, cst.Module): module_match = Match( [CaptureExpr().contextualize(node=updated)]) return updated.with_changes(body=(*updated.body, *astNodeFromAssertion(transform, module_match))) else: return updated
def test_deep_equals_fails(self, a: cst.CSTNode, b: cst.CSTNode) -> None: self.assertFalse(a.deep_equals(b))
def test_deep_equals_success(self, a: cst.CSTNode, b: cst.CSTNode) -> None: self.assertTrue(a.deep_equals(b))