1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2024-11-09 01:29:01 +00:00
dupeguru/hscommon/tests/tree_test.py

124 lines
3.4 KiB
Python
Raw Normal View History

2019-09-10 00:54:28 +00:00
# Created By: Virgil Dupras
# Created On: 2010-02-12
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
2019-09-10 00:54:28 +00:00
# http://www.gnu.org/licenses/gpl-3.0.html
2022-05-09 06:40:08 +00:00
from hscommon.testutil import eq_
from hscommon.gui.tree import Tree, Node
2019-09-10 00:54:28 +00:00
2019-09-10 00:54:28 +00:00
def tree_with_some_nodes():
t = Tree()
t.append(Node("foo"))
t.append(Node("bar"))
t.append(Node("baz"))
t[0].append(Node("sub1"))
t[0].append(Node("sub2"))
2019-09-10 00:54:28 +00:00
return t
2019-09-10 00:54:28 +00:00
def test_selection():
t = tree_with_some_nodes()
assert t.selected_node is None
eq_(t.selected_nodes, [])
assert t.selected_path is None
eq_(t.selected_paths, [])
2019-09-10 00:54:28 +00:00
def test_select_one_node():
t = tree_with_some_nodes()
t.selected_node = t[0][0]
assert t.selected_node is t[0][0]
eq_(t.selected_nodes, [t[0][0]])
eq_(t.selected_path, [0, 0])
eq_(t.selected_paths, [[0, 0]])
2019-09-10 00:54:28 +00:00
def test_select_one_path():
t = tree_with_some_nodes()
t.selected_path = [0, 1]
assert t.selected_node is t[0][1]
2019-09-10 00:54:28 +00:00
def test_select_multiple_nodes():
t = tree_with_some_nodes()
t.selected_nodes = [t[0], t[1]]
eq_(t.selected_paths, [[0], [1]])
2019-09-10 00:54:28 +00:00
def test_select_multiple_paths():
t = tree_with_some_nodes()
t.selected_paths = [[0], [1]]
eq_(t.selected_nodes, [t[0], t[1]])
2019-09-10 00:54:28 +00:00
def test_select_none_path():
# setting selected_path to None clears the selection
t = Tree()
t.selected_path = None
assert t.selected_path is None
2019-09-10 00:54:28 +00:00
def test_select_none_node():
# setting selected_node to None clears the selection
t = Tree()
t.selected_node = None
eq_(t.selected_nodes, [])
2019-09-10 00:54:28 +00:00
def test_clear_removes_selection():
# When clearing a tree, we want to clear the selection as well or else we end up with a crash
# when calling selected_paths.
t = tree_with_some_nodes()
t.selected_path = [0]
t.clear()
assert t.selected_node is None
2019-09-10 00:54:28 +00:00
def test_selection_override():
# All selection changed pass through the _select_node() method so it's easy for subclasses to
# customize the tree's behavior.
class MyTree(Tree):
called = False
2019-09-10 00:54:28 +00:00
def _select_nodes(self, nodes):
self.called = True
2019-09-10 00:54:28 +00:00
t = MyTree()
t.selected_paths = []
assert t.called
t.called = False
t.selected_node = None
assert t.called
2019-09-10 00:54:28 +00:00
def test_findall():
t = tree_with_some_nodes()
r = t.findall(lambda n: n.name.startswith("sub"))
2022-04-28 01:53:12 +00:00
eq_(set(r), {t[0][0], t[0][1]})
2019-09-10 00:54:28 +00:00
2019-09-10 00:54:28 +00:00
def test_findall_dont_include_self():
# When calling findall with include_self=False, the node itself is never evaluated.
t = tree_with_some_nodes()
del t._name # so that if the predicate is called on `t`, we crash
r = t.findall(lambda n: not n.name.startswith("sub"), include_self=False) # no crash
2022-04-28 01:53:12 +00:00
eq_(set(r), {t[0], t[1], t[2]})
2019-09-10 00:54:28 +00:00
2019-09-10 00:54:28 +00:00
def test_find_dont_include_self():
# When calling find with include_self=False, the node itself is never evaluated.
t = tree_with_some_nodes()
del t._name # so that if the predicate is called on `t`, we crash
r = t.find(lambda n: not n.name.startswith("sub"), include_self=False) # no crash
2019-09-10 00:54:28 +00:00
assert r is t[0]
2019-09-10 00:54:28 +00:00
def test_find_none():
# when find() yields no result, return None
t = Tree()
assert t.find(lambda n: False) is None # no StopIteration exception