File: test_graphics.py

package info (click to toggle)
urwid 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,232 kB
  • sloc: python: 29,010; javascript: 382; sh: 34; makefile: 22
file content (171 lines) | stat: -rw-r--r-- 4,508 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from __future__ import annotations

import unittest

import urwid
from urwid.util import get_encoding
from urwid.widget import bar_graph


class BarGraphTest(unittest.TestCase):
    def bgtest(self, desc, data, top, widths, maxrow, exp):
        rval = bar_graph.calculate_bargraph_display(data, top, widths, maxrow)
        assert rval == exp, f"{desc} expected {exp!r}, got {rval!r}"

    def test1(self):
        self.bgtest("simplest", [[0]], 5, [1], 1, [(1, [(0, 1)])]),
        self.bgtest(
            "simpler",
            [[0], [0]],
            5,
            [1, 2],
            5,
            [(5, [(0, 3)])],
        )
        self.bgtest(
            "simple",
            [[5]],
            5,
            [1],
            1,
            [(1, [(1, 1)])],
        )
        self.bgtest(
            "2col-1",
            [[2], [0]],
            5,
            [1, 2],
            5,
            [(3, [(0, 3)]), (2, [(1, 1), (0, 2)])],
        )
        self.bgtest(
            "2col-2",
            [[0], [2]],
            5,
            [1, 2],
            5,
            [(3, [(0, 3)]), (2, [(0, 1), (1, 2)])],
        )
        self.bgtest(
            "2col-3",
            [[2], [3]],
            5,
            [1, 2],
            5,
            [(2, [(0, 3)]), (1, [(0, 1), (1, 2)]), (2, [(1, 3)])],
        )
        self.bgtest(
            "3col-1",
            [[5], [3], [0]],
            5,
            [2, 1, 1],
            5,
            [(2, [(1, 2), (0, 2)]), (3, [(1, 3), (0, 1)])],
        )
        self.bgtest(
            "3col-2",
            [[4], [4], [4]],
            5,
            [2, 1, 1],
            5,
            [(1, [(0, 4)]), (4, [(1, 4)])],
        )
        self.bgtest(
            "3col-3",
            [[1], [2], [3]],
            5,
            [2, 1, 1],
            5,
            [(2, [(0, 4)]), (1, [(0, 3), (1, 1)]), (1, [(0, 2), (1, 2)]), (1, [(1, 4)])],
        )
        self.bgtest(
            "3col-4",
            [[4], [2], [4]],
            5,
            [1, 2, 1],
            5,
            [(1, [(0, 4)]), (2, [(1, 1), (0, 2), (1, 1)]), (2, [(1, 4)])],
        )

    def test2(self):
        self.bgtest(
            "simple1a",
            [[2, 0], [2, 1]],
            2,
            [1, 1],
            2,
            [(1, [(1, 2)]), (1, [(1, 1), (2, 1)])],
        )
        self.bgtest(
            "simple1b",
            [[2, 1], [2, 0]],
            2,
            [1, 1],
            2,
            [(1, [(1, 2)]), (1, [(2, 1), (1, 1)])],
        )
        self.bgtest("cross1a", [[2, 2], [1, 2]], 2, [1, 1], 2, [(2, [(2, 2)])])
        self.bgtest("cross1b", [[1, 2], [2, 2]], 2, [1, 1], 2, [(2, [(2, 2)])])
        self.bgtest(
            "mix1a",
            [[3, 2, 1], [2, 2, 2], [1, 2, 3]],
            3,
            [1, 1, 1],
            3,
            [(1, [(1, 1), (0, 1), (3, 1)]), (1, [(2, 1), (3, 2)]), (1, [(3, 3)])],
        )
        self.bgtest(
            "mix1b",
            [[1, 2, 3], [2, 2, 2], [3, 2, 1]],
            3,
            [1, 1, 1],
            3,
            [(1, [(3, 1), (0, 1), (1, 1)]), (1, [(3, 2), (2, 1)]), (1, [(3, 3)])],
        )


class SmoothBarGraphTest(unittest.TestCase):
    def setUp(self) -> None:
        self.old_encoding = get_encoding()
        urwid.set_encoding("utf-8")

    def tearDown(self) -> None:
        urwid.set_encoding(self.old_encoding)

    def sbgtest(self, desc, data, top, exp):
        g = urwid.BarGraph(["black", "red", "blue"], None, {(1, 0): "red/black", (2, 1): "blue/red"})
        g.set_data(data, top)
        rval = g.calculate_display((5, 3))
        assert rval == exp, f"{desc} expected {exp!r}, got {rval!r}"

    def test1(self):
        self.sbgtest(
            "simple",
            [[3]],
            5,
            [(1, [(0, 5)]), (1, [((1, 0, 6), 5)]), (1, [(1, 5)])],
        )
        self.sbgtest(
            "boring",
            [[4, 2]],
            6,
            [(1, [(0, 5)]), (1, [(1, 5)]), (1, [(2, 5)])],
        )
        self.sbgtest(
            "two",
            [[4], [2]],
            6,
            [(1, [(0, 5)]), (1, [(1, 3), (0, 2)]), (1, [(1, 5)])],
        )
        self.sbgtest(
            "twos",
            [[3], [4]],
            6,
            [(1, [(0, 5)]), (1, [((1, 0, 4), 3), (1, 2)]), (1, [(1, 5)])],
        )
        self.sbgtest(
            "twof",
            [[4], [3]],
            6,
            [(1, [(0, 5)]), (1, [(1, 3), ((1, 0, 4), 2)]), (1, [(1, 5)])],
        )