Debian Codegraph @

tests/test_colortable.py

raw
1 from __future__ import annotations
2
3 import pytest
4 from test_prettytable import CITY_DATA, CITY_DATA_HEADER
5
6 from prettytable.colortable import RESET_CODE, ColorTable, Theme, Themes
7
8 TYPE_CHECKING = False
9 if TYPE_CHECKING:
10 from prettytable import PrettyTable
11
12
13 @pytest.fixture
14 def row_colortable() -> PrettyTable:
15 table = ColorTable()
16 table.field_names = CITY_DATA_HEADER
17 for row in CITY_DATA:
18 table.add_row(row)
19 return table
20
21
22 @pytest.fixture
23 def color_theme() -> Theme:
24 return Theme(
25 default_color="31",
26 vertical_color="32",
27 horizontal_color="33",
28 junction_color="34",
29 )
30
31
32 class TestColorTable:
33 def test_themeless(
34 self, row_prettytable: PrettyTable, row_colortable: ColorTable
35 ) -> None:
36 # Not worth the logic customizing the reset code
37 # For now we'll just get rid of it
38 assert (
39 row_colortable.get_string().replace(RESET_CODE, "")
40 == row_prettytable.get_string()
41 )
42
43 def test_theme_setter(self, color_theme: Theme) -> None:
44 table1 = ColorTable(theme=color_theme)
45
46 table2 = ColorTable()
47 table2.theme = color_theme
48
49 assert table1.theme == table2.theme
50
51 dict1 = table1.__dict__
52 dict2 = table2.__dict__
53
54 # So we don't compare functions
55 for func in ("_sort_key", "_row_filter"):
56 del dict1[func]
57 del dict2[func]
58
59 assert dict1 == dict2
60
61
62 class TestFormatCode:
63 def test_basic(self) -> None:
64 assert Theme.format_code("31") == "\x1b[31m"
65
66 def test_prefix(self) -> None:
67 assert Theme.format_code("\x1b[35m") == "\x1b[35m"
68
69 def test_escapes(self) -> None:
70 assert Theme.format_code("\033[41m") == "\x1b[41m"
71 assert Theme.format_code("\u001b[41m") == "\x1b[41m"
72
73 def test_empty(self) -> None:
74 assert Theme.format_code("") == ""
75
76 def test_stripped(self) -> None:
77 assert Theme.format_code("\t\t \t") == ""
78
79 def test_multiple(self) -> None:
80 assert Theme.format_code("30;42") == "\x1b[30;42m"
81 assert Theme.format_code("\x1b[30;42m") == "\x1b[30;42m"
82
83
84 class TestColorTableRendering:
85 """Tests for the rendering of the color table
86
87 Methods
88 -------
89 test_color_table_rendering
90 Tests the color table rendering using the default alignment (`'c'`)
91 """
92
93 @pytest.mark.parametrize(
94 ["with_title", "with_header"],
95 [
96 (False, True), # the default
97 (True, True), # titled
98 (True, False), # titled, no header
99 (True, True), # both title and header
100 ],
101 )
102 def test_color_table_rendering(self, with_title: bool, with_header: bool) -> None:
103 """Tests the color table rendering using the default alignment (`'c'`)"""
104 chars = {
105 "+": "\x1b[36m+\x1b[0m\x1b[96m",
106 "-": "\x1b[34m-\x1b[0m\x1b[96m",
107 "|": "\x1b[34m|\x1b[0m\x1b[96m",
108 " ": " ",
109 }
110
111 plus = chars.get("+")
112 minus = chars.get("-")
113 pipe = chars.get("|")
114 space = chars.get(" ")
115 assert isinstance(plus, str)
116 assert isinstance(minus, str)
117 assert isinstance(pipe, str)
118 assert isinstance(space, str)
119
120 # +-----------------------+
121 # | Efforts |
122 # +---+---+---+---+---+---+
123 # | A | B | C | D | E | f |
124 # +---+---+---+---+---+---+
125 # | 1 | 2 | 3 | 4 | 5 | 6 |
126 # +---+---+---+---+---+---+
127
128 header = (
129 plus + minus * 23 + plus,
130 pipe + space * 8 + "Efforts" + space * 8 + pipe,
131 (plus + minus * 3) * 6 + plus,
132 )
133
134 body = (
135 "".join(pipe + space + char + space for char in "ABCDEF") + pipe,
136 (plus + minus * 3) * 6 + plus,
137 "".join(pipe + space + char + space for char in "123456") + pipe,
138 (plus + minus * 3) * 6 + plus,
139 )
140
141 if with_title:
142 header_str = str("\n".join(header))
143 else:
144 header_str = str(header[2])
145 if with_header:
146 body_str = str("\n".join(body))
147 else:
148 body_str = str("\n".join(body[2:]))
149
150 table = ColorTable(
151 ("A", "B", "C", "D", "E", "F"),
152 theme=Themes.OCEAN,
153 )
154
155 if with_title:
156 table.title = "Efforts"
157 table.header = with_header
158 table.add_row([1, 2, 3, 4, 5, 6])
159
160 expected = header_str + "\n" + body_str + "\x1b[0m"
161 result = str(table)
162
163 assert expected == result
164
165 def test_all_themes(self) -> None:
166 """Tests rendering with all available themes"""
167 table = ColorTable(
168 ("A", "B", "C", "D", "E", "F"),
169 )
170 table.title = "Theme Test"
171 table.add_row([1, 2, 3, 4, 5, 6])
172
173 for theme_name, theme in vars(Themes).items():
174 if isinstance(theme, Theme):
175 table.theme = theme
176 result = str(table)
177 assert result # Simple check to ensure rendering doesn't fail