def test_tree_subs(): tree = parse((thunk.fromexpr(1) + 2) - 3) assert tree.subs( {Call(Normal(op.add), (Normal(1), Normal(2)), {}): Normal(4)}) == Call(Normal(op.sub), (Normal(4), Normal(3)), {}) other_tree = parse(thunk.fromexpr(1) + 1 + 1) assert other_tree.subs({1: 2}) == parse(thunk.fromexpr(2) + 2 + 2)
def test_tree_subs(): tree = parse((thunk.fromexpr(1) + 2) - 3) assert tree.subs({ Call(Normal(op.add), (Normal(1), Normal(2)), {}): Normal(4) }) == Call(Normal(op.sub), (Normal(4), Normal(3)), {}) other_tree = parse(thunk.fromexpr(1) + 1 + 1) assert other_tree.subs({1: 2}) == parse(thunk.fromexpr(2) + 2 + 2)
def test_tree_leaves(): def f(a): return a tree = parse(thunk(f, (thunk.fromexpr(1) + 2) - 3)) assert set(tree.leaves()) == set(map(Normal, (f, op.add, op.sub, 1, 2, 3)))
def _box_literal(self, collection): """We are not using ``lazy_function`` to wrap the class body so literal values are not thunks. """ try: return self._literal_conversions[type(collection)](collection) except KeyError: return thunk.fromexpr(collection)
def test_tree_contains(): tree = parse((thunk.fromexpr(1) + 2) + 3) assert 1 in tree assert 2 in tree assert 3 in tree sub = Call(Normal(op.add), (Normal(1), Normal(2)), {}) assert sub in tree assert Call(Normal(op.add), (sub, Normal(3)), {}) in tree
def test_strict_thunk(): assert strict(thunk.fromexpr(5)) is 5 assert strict(thunk(lambda a: a, 5)) is 5
def test_fromexpr_of_thunk(type_): a = thunk.fromexpr(type_.fromexpr(1)) assert isinstance(a, type_) assert isinstance(strict(a), int) assert not isinstance(strict(a), type_)