def test_set_at_can_set_a_leaf(example_tree): set_at(example_tree, ('foo', 'bar', 'baz'), sentinel.new_value) for path, value in walk(example_tree): if path == ('foo', 'bar', 'baz'): assert value == sentinel.new_value else: assert value != sentinel.new_value
def structural_copy(tree): result = Tree() r = Path(result) for path, leaf in walk(tree): set_at(r, path, leaf) return result
def test_set_at_non_existent_path(example_tree): with pytest.raises(AttributeError): set_at(example_tree, ('does', 'not', 'exist'), sentinel.new_value)
def test_set_at_non_existent_leaf(example_tree): pre_exisiting = list(walk(example_tree)) set_at(example_tree, ('foo', 'bar', 'new_node'), sentinel.new_value) assert sorted(walk(example_tree)) == sorted(pre_exisiting + [(('foo', 'bar', 'new_node'), sentinel.new_value)])
def test_set_at_empty_path_in_empty_tree(): with pytest.raises(ValueError): set_at(Tree(), [], sentinel.new_value)
def test_set_at_empty_path(example_tree): with pytest.raises(ValueError): set_at(example_tree, [], sentinel.new_value)
def test_set_at_can_overwrite_a_subtree(example_tree): set_at(example_tree, ('foo', 'bar'), sentinel.new_value) assert sorted(walk(example_tree)) == sorted([(('foo', 'bar',), sentinel.new_value), (('foo', 'lar', 'baz',), sentinel.foolarbaz), (('moo', 'bar', 'baz',), sentinel.moobarbaz), ])