def test_can_copy_a_wildcarded_path_to_another_location_in_the_same_tree(example_tree): pre_exisiting = list(walk(example_tree)) Path(example_tree).minky['%y'].mookey['%x'] = Path(example_tree)['%x'].bar['%y'] assert sorted(walk(example_tree)) == sorted(pre_exisiting + [(('minky', 'baz', 'mookey', 'foo'), sentinel.foobarbaz), (('minky', 'bang', 'mookey', 'foo'), sentinel.foobarbang), (('minky', 'baz', 'mookey', 'moo'), sentinel.moobarbaz),])
def test_adding_to_an_existing_tree(example_tree): pre_exisiting = list(walk(example_tree)) p = Path(example_tree) p.hello = sentinel.hello p.animals.badger = sentinel.badger p.animals.weasel = sentinel.weasel p.animals.stoat = sentinel.stoat assert sorted(walk(example_tree)) == sorted(pre_exisiting + [(('hello',), sentinel.hello), (('animals', 'badger'), sentinel.badger), (('animals', 'weasel'), sentinel.weasel), (('animals', 'stoat'), sentinel.stoat), ])
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 test_changing_an_existing_tree(example_tree): p = Path(example_tree) p.foo.bar.baz = sentinel.new_value assert sorted(walk(example_tree)) == sorted([(('foo', 'bar', 'baz',), sentinel.new_value), (('foo', 'bar', 'bang',), sentinel.foobarbang), (('foo', 'lar', 'baz',), sentinel.foolarbaz), (('moo', 'bar', 'baz',), sentinel.moobarbaz),])
def test_adding_to_an_empty_tree(): tree = Tree() p = Path(tree) p.hello = sentinel.hello p.animals.badger = sentinel.badger p.animals.weasel = sentinel.weasel p.animals.stoat = sentinel.stoat assert sorted(walk(tree)) == sorted([(('hello',), sentinel.hello), (('animals', 'badger'), sentinel.badger), (('animals', 'weasel'), sentinel.weasel), (('animals', 'stoat'), sentinel.stoat), ])
def test_structural_copy_copies_branches_but_not_leaves(example_tree): result = structural_copy(example_tree) for path, value in walk(example_tree): assert get_at(result, path[:-1]) is not get_at(example_tree, path[:-1]) assert get_at(result, path) is value
def test_del_at_can_remove_a_subtree(example_tree): pre_exisiting_paths = {path for path, _ in walk(example_tree)} del_at(example_tree, ('foo', 'bar')) assert {path for path, _ in walk(example_tree)} == pre_exisiting_paths - {('foo', 'bar', 'baz'), ('foo', 'bar', 'bang')}
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_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), ])
def test_changing_an_existing_tree_using_wildcards(example_tree): p = Path(example_tree) p.foo.bar['%x'] = sentinel.new_value assert sorted(walk(example_tree)) == sorted([(('foo', 'bar', 'baz',), sentinel.new_value), (('foo', 'bar', 'bang',), sentinel.new_value), ])