File: test_node_list.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (113 lines) | stat: -rw-r--r-- 2,764 bytes parent folder | download | duplicates (2)
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
import pytest

from textual._node_list import NodeList
from textual.widget import Widget


def test_empty_list():
    """Does an empty node list report as being empty?"""
    assert len(NodeList()) == 0


def test_add_one():
    """Does adding a node to the node list report as having one item?"""
    nodes = NodeList()
    nodes._append(Widget())
    assert len(nodes) == 1


def test_length_hint():
    """Check length hint dunder method."""
    nodes = NodeList()
    assert nodes.__length_hint__() == 0
    nodes._append(Widget())
    nodes._append(Widget())
    nodes._append(Widget())
    assert nodes.__length_hint__() == 3


def test_repeat_add_one():
    """Does adding the same item to the node list ignore the additional adds?"""
    nodes = NodeList()
    widget = Widget()
    for _ in range(1000):
        nodes._append(widget)
    assert len(nodes) == 1


def test_insert():
    nodes = NodeList()
    widget1 = Widget()
    widget2 = Widget()
    widget3 = Widget()
    nodes._append(widget1)
    nodes._append(widget3)
    nodes._insert(1, widget2)
    assert list(nodes) == [widget1, widget2, widget3]


def test_truthy():
    """Does a node list act as a truthy object?"""
    nodes = NodeList()
    assert not bool(nodes)
    nodes._append(Widget())
    assert bool(nodes)


def test_contains():
    """Can we check if a widget is (not) within the list?"""
    widget = Widget()
    nodes = NodeList()
    assert widget not in nodes
    nodes._append(widget)
    assert widget in nodes
    assert Widget() not in nodes


def test_index():
    """Can we get the index of a widget in the list?"""
    widget = Widget()
    nodes = NodeList()
    with pytest.raises(ValueError):
        _ = nodes.index(widget)
    nodes._append(widget)
    assert nodes.index(widget) == 0


def test_remove():
    """Can we remove a widget we've added?"""
    widget = Widget()
    nodes = NodeList()
    nodes._append(widget)
    assert widget in nodes
    nodes._remove(widget)
    assert widget not in nodes


def test_clear():
    """Can we clear the list?"""
    nodes = NodeList()
    assert len(nodes) == 0
    widgets = [Widget() for _ in range(1000)]
    for widget in widgets:
        nodes._append(widget)
    assert len(nodes) == 1000
    for widget in widgets:
        assert widget in nodes
    nodes._clear()
    assert len(nodes) == 0
    for widget in widgets:
        assert widget not in nodes


def test_listy():
    nodes = NodeList()
    widget1 = Widget()
    widget2 = Widget()
    nodes._append(widget1)
    nodes._append(widget2)
    assert list(nodes) == [widget1, widget2]
    assert list(reversed(nodes)) == [widget2, widget1]
    assert nodes[0] == widget1
    assert nodes[1] == widget2
    assert nodes[0:2] == [widget1, widget2]