File: tree_test.py

package info (click to toggle)
dupeguru 4.3.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,604 kB
  • sloc: python: 16,846; ansic: 424; makefile: 123
file content (123 lines) | stat: -rw-r--r-- 3,443 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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
# http://www.gnu.org/licenses/gpl-3.0.html

from hscommon.testutil import eq_
from hscommon.gui.tree import Tree, Node


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"))
    return t


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, [])


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]])


def test_select_one_path():
    t = tree_with_some_nodes()
    t.selected_path = [0, 1]
    assert t.selected_node is t[0][1]


def test_select_multiple_nodes():
    t = tree_with_some_nodes()
    t.selected_nodes = [t[0], t[1]]
    eq_(t.selected_paths, [[0], [1]])


def test_select_multiple_paths():
    t = tree_with_some_nodes()
    t.selected_paths = [[0], [1]]
    eq_(t.selected_nodes, [t[0], t[1]])


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


def test_select_none_node():
    # setting selected_node to None clears the selection
    t = Tree()
    t.selected_node = None
    eq_(t.selected_nodes, [])


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


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

        def _select_nodes(self, nodes):
            self.called = True

    t = MyTree()
    t.selected_paths = []
    assert t.called
    t.called = False
    t.selected_node = None
    assert t.called


def test_findall():
    t = tree_with_some_nodes()
    r = t.findall(lambda n: n.name.startswith("sub"))
    eq_(set(r), {t[0][0], t[0][1]})


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
    eq_(set(r), {t[0], t[1], t[2]})


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
    assert r is t[0]


def test_find_none():
    # when find() yields no result, return None
    t = Tree()
    assert t.find(lambda n: False) is None  # no StopIteration exception