Debian Codegraph @

tests/test_win32_console.py

raw
1 import dataclasses
2 import sys
3 from unittest import mock
4 from unittest.mock import patch
5
6 import pytest
7
8 from rich.style import Style
9
10 if sys.platform == "win32":
11 from rich import _win32_console
12 from rich._win32_console import COORD, LegacyWindowsTerm, WindowsCoordinates
13
14 CURSOR_X = 1
15 CURSOR_Y = 2
16 CURSOR_POSITION = WindowsCoordinates(row=CURSOR_Y, col=CURSOR_X)
17 SCREEN_WIDTH = 20
18 SCREEN_HEIGHT = 30
19 DEFAULT_STYLE_ATTRIBUTE = 16
20 CURSOR_SIZE = 25
21
22 @dataclasses.dataclass
23 class StubScreenBufferInfo:
24 dwCursorPosition: COORD = COORD(CURSOR_X, CURSOR_Y)
25 dwSize: COORD = COORD(SCREEN_WIDTH, SCREEN_HEIGHT)
26 wAttributes: int = DEFAULT_STYLE_ATTRIBUTE
27
28 pytestmark = pytest.mark.skipif(sys.platform != "win32", reason="windows only")
29
30 def test_windows_coordinates_to_ctype():
31 coord = WindowsCoordinates.from_param(WindowsCoordinates(row=1, col=2))
32 assert coord.X == 2
33 assert coord.Y == 1
34
35 @pytest.fixture
36 def win32_handle():
37 handle = mock.sentinel
38 with mock.patch.object(_win32_console, "GetStdHandle", return_value=handle):
39 yield handle
40
41 @pytest.fixture
42 def win32_console_getters():
43 def stub_console_cursor_info(std_handle, cursor_info):
44 cursor_info.dwSize = CURSOR_SIZE
45 cursor_info.bVisible = True
46
47 with mock.patch.object(
48 _win32_console,
49 "GetConsoleScreenBufferInfo",
50 return_value=StubScreenBufferInfo,
51 ) as GetConsoleScreenBufferInfo, mock.patch.object(
52 _win32_console, "GetConsoleCursorInfo", side_effect=stub_console_cursor_info
53 ) as GetConsoleCursorInfo:
54 yield {
55 "GetConsoleScreenBufferInfo": GetConsoleScreenBufferInfo,
56 "GetConsoleCursorInfo": GetConsoleCursorInfo,
57 }
58
59 def test_cursor_position(win32_console_getters):
60 term = LegacyWindowsTerm(sys.stdout)
61 assert term.cursor_position == WindowsCoordinates(row=CURSOR_Y, col=CURSOR_X)
62
63 def test_screen_size(win32_console_getters):
64 term = LegacyWindowsTerm(sys.stdout)
65 assert term.screen_size == WindowsCoordinates(
66 row=SCREEN_HEIGHT, col=SCREEN_WIDTH
67 )
68
69 def test_write_text(win32_console_getters, win32_handle, capsys):
70 text = "Hello, world!"
71 term = LegacyWindowsTerm(sys.stdout)
72
73 term.write_text(text)
74
75 captured = capsys.readouterr()
76 assert captured.out == text
77
78 @patch.object(_win32_console, "SetConsoleTextAttribute")
79 def test_write_styled(
80 SetConsoleTextAttribute,
81 win32_console_getters,
82 win32_handle,
83 capsys,
84 ):
85 style = Style.parse("black on red")
86 text = "Hello, world!"
87 term = LegacyWindowsTerm(sys.stdout)
88
89 term.write_styled(text, style)
90
91 captured = capsys.readouterr()
92 assert captured.out == text
93
94 # Ensure we set the text attributes and then reset them after writing styled text
95 call_args = SetConsoleTextAttribute.call_args_list
96 assert len(call_args) == 2
97 first_args, first_kwargs = call_args[0]
98 second_args, second_kwargs = call_args[1]
99
100 assert first_args == (win32_handle,)
101 assert first_kwargs["attributes"].value == 64
102 assert second_args == (win32_handle,)
103 assert second_kwargs["attributes"] == DEFAULT_STYLE_ATTRIBUTE
104
105 @patch.object(_win32_console, "SetConsoleTextAttribute")
106 def test_write_styled_bold(
107 SetConsoleTextAttribute, win32_console_getters, win32_handle
108 ):
109 style = Style.parse("bold black on red")
110 text = "Hello, world!"
111 term = LegacyWindowsTerm(sys.stdout)
112
113 term.write_styled(text, style)
114
115 call_args = SetConsoleTextAttribute.call_args_list
116 first_args, first_kwargs = call_args[0]
117
118 expected_attr = 64 + 8 # 64 for red bg, +8 for bright black
119 assert first_args == (win32_handle,)
120 assert first_kwargs["attributes"].value == expected_attr
121
122 @patch.object(_win32_console, "SetConsoleTextAttribute")
123 def test_write_styled_reverse(
124 SetConsoleTextAttribute, win32_console_getters, win32_handle
125 ):
126 style = Style.parse("reverse red on blue")
127 text = "Hello, world!"
128 term = LegacyWindowsTerm(sys.stdout)
129
130 term.write_styled(text, style)
131
132 call_args = SetConsoleTextAttribute.call_args_list
133 first_args, first_kwargs = call_args[0]
134
135 expected_attr = 64 + 1 # 64 for red bg (after reverse), +1 for blue fg
136 assert first_args == (win32_handle,)
137 assert first_kwargs["attributes"].value == expected_attr
138
139 @patch.object(_win32_console, "SetConsoleTextAttribute")
140 def test_write_styled_reverse(
141 SetConsoleTextAttribute, win32_console_getters, win32_handle
142 ):
143 style = Style.parse("dim bright_red on blue")
144 text = "Hello, world!"
145 term = LegacyWindowsTerm(sys.stdout)
146
147 term.write_styled(text, style)
148
149 call_args = SetConsoleTextAttribute.call_args_list
150 first_args, first_kwargs = call_args[0]
151
152 expected_attr = 4 + 16 # 4 for red text (after dim), +16 for blue bg
153 assert first_args == (win32_handle,)
154 assert first_kwargs["attributes"].value == expected_attr
155
156 @patch.object(_win32_console, "SetConsoleTextAttribute")
157 def test_write_styled_no_foreground_color(
158 SetConsoleTextAttribute, win32_console_getters, win32_handle
159 ):
160 style = Style.parse("on blue")
161 text = "Hello, world!"
162 term = LegacyWindowsTerm(sys.stdout)
163
164 term.write_styled(text, style)
165
166 call_args = SetConsoleTextAttribute.call_args_list
167 first_args, first_kwargs = call_args[0]
168
169 expected_attr = 16 | term._default_fore # 16 for blue bg, plus default fg color
170 assert first_args == (win32_handle,)
171 assert first_kwargs["attributes"].value == expected_attr
172
173 @patch.object(_win32_console, "SetConsoleTextAttribute")
174 def test_write_styled_no_background_color(
175 SetConsoleTextAttribute, win32_console_getters, win32_handle
176 ):
177 style = Style.parse("blue")
178 text = "Hello, world!"
179 term = LegacyWindowsTerm(sys.stdout)
180
181 term.write_styled(text, style)
182
183 call_args = SetConsoleTextAttribute.call_args_list
184 first_args, first_kwargs = call_args[0]
185
186 expected_attr = (
187 16 | term._default_back
188 ) # 16 for blue foreground, plus default bg color
189 assert first_args == (win32_handle,)
190 assert first_kwargs["attributes"].value == expected_attr
191
192 @patch.object(_win32_console, "FillConsoleOutputCharacter", return_value=None)
193 @patch.object(_win32_console, "FillConsoleOutputAttribute", return_value=None)
194 def test_erase_line(
195 FillConsoleOutputAttribute,
196 FillConsoleOutputCharacter,
197 win32_console_getters,
198 win32_handle,
199 ):
200 term = LegacyWindowsTerm(sys.stdout)
201 term.erase_line()
202 start = WindowsCoordinates(row=CURSOR_Y, col=0)
203 FillConsoleOutputCharacter.assert_called_once_with(
204 win32_handle, " ", length=SCREEN_WIDTH, start=start
205 )
206 FillConsoleOutputAttribute.assert_called_once_with(
207 win32_handle, DEFAULT_STYLE_ATTRIBUTE, length=SCREEN_WIDTH, start=start
208 )
209
210 @patch.object(_win32_console, "FillConsoleOutputCharacter", return_value=None)
211 @patch.object(_win32_console, "FillConsoleOutputAttribute", return_value=None)
212 def test_erase_end_of_line(
213 FillConsoleOutputAttribute,
214 FillConsoleOutputCharacter,
215 win32_console_getters,
216 win32_handle,
217 ):
218 term = LegacyWindowsTerm(sys.stdout)
219 term.erase_end_of_line()
220
221 FillConsoleOutputCharacter.assert_called_once_with(
222 win32_handle, " ", length=SCREEN_WIDTH - CURSOR_X, start=CURSOR_POSITION
223 )
224 FillConsoleOutputAttribute.assert_called_once_with(
225 win32_handle,
226 DEFAULT_STYLE_ATTRIBUTE,
227 length=SCREEN_WIDTH - CURSOR_X,
228 start=CURSOR_POSITION,
229 )
230
231 @patch.object(_win32_console, "FillConsoleOutputCharacter", return_value=None)
232 @patch.object(_win32_console, "FillConsoleOutputAttribute", return_value=None)
233 def test_erase_start_of_line(
234 FillConsoleOutputAttribute,
235 FillConsoleOutputCharacter,
236 win32_console_getters,
237 win32_handle,
238 ):
239 term = LegacyWindowsTerm(sys.stdout)
240 term.erase_start_of_line()
241
242 start = WindowsCoordinates(CURSOR_Y, 0)
243
244 FillConsoleOutputCharacter.assert_called_once_with(
245 win32_handle, " ", length=CURSOR_X, start=start
246 )
247 FillConsoleOutputAttribute.assert_called_once_with(
248 win32_handle, DEFAULT_STYLE_ATTRIBUTE, length=CURSOR_X, start=start
249 )
250
251 @patch.object(_win32_console, "SetConsoleCursorPosition", return_value=None)
252 def test_move_cursor_to(
253 SetConsoleCursorPosition, win32_console_getters, win32_handle
254 ):
255 coords = WindowsCoordinates(row=4, col=5)
256 term = LegacyWindowsTerm(sys.stdout)
257
258 term.move_cursor_to(coords)
259
260 SetConsoleCursorPosition.assert_called_once_with(win32_handle, coords=coords)
261
262 @patch.object(_win32_console, "SetConsoleCursorPosition", return_value=None)
263 def test_move_cursor_to_out_of_bounds_row(
264 SetConsoleCursorPosition, win32_console_getters, win32_handle
265 ):
266 coords = WindowsCoordinates(row=-1, col=4)
267 term = LegacyWindowsTerm(sys.stdout)
268
269 term.move_cursor_to(coords)
270
271 assert not SetConsoleCursorPosition.called
272
273 @patch.object(_win32_console, "SetConsoleCursorPosition", return_value=None)
274 def test_move_cursor_to_out_of_bounds_col(
275 SetConsoleCursorPosition, win32_console_getters, win32_handle
276 ):
277 coords = WindowsCoordinates(row=10, col=-4)
278 term = LegacyWindowsTerm(sys.stdout)
279
280 term.move_cursor_to(coords)
281
282 assert not SetConsoleCursorPosition.called
283
284 @patch.object(_win32_console, "SetConsoleCursorPosition", return_value=None)
285 def test_move_cursor_up(
286 SetConsoleCursorPosition, win32_console_getters, win32_handle
287 ):
288 term = LegacyWindowsTerm(sys.stdout)
289
290 term.move_cursor_up()
291
292 SetConsoleCursorPosition.assert_called_once_with(
293 win32_handle, coords=WindowsCoordinates(row=CURSOR_Y - 1, col=CURSOR_X)
294 )
295
296 @patch.object(_win32_console, "SetConsoleCursorPosition", return_value=None)
297 def test_move_cursor_down(
298 SetConsoleCursorPosition, win32_console_getters, win32_handle
299 ):
300 term = LegacyWindowsTerm(sys.stdout)
301
302 term.move_cursor_down()
303
304 SetConsoleCursorPosition.assert_called_once_with(
305 win32_handle, coords=WindowsCoordinates(row=CURSOR_Y + 1, col=CURSOR_X)
306 )
307
308 @patch.object(_win32_console, "SetConsoleCursorPosition", return_value=None)
309 def test_move_cursor_forward(
310 SetConsoleCursorPosition, win32_console_getters, win32_handle
311 ):
312 term = LegacyWindowsTerm(sys.stdout)
313
314 term.move_cursor_forward()
315
316 SetConsoleCursorPosition.assert_called_once_with(
317 win32_handle, coords=WindowsCoordinates(row=CURSOR_Y, col=CURSOR_X + 1)
318 )
319
320 @patch.object(_win32_console, "SetConsoleCursorPosition", return_value=None)
321 def test_move_cursor_forward_newline_wrap(
322 SetConsoleCursorPosition, win32_console_getters, win32_handle
323 ):
324 cursor_at_end_of_line = StubScreenBufferInfo(
325 dwCursorPosition=COORD(SCREEN_WIDTH - 1, CURSOR_Y)
326 )
327 win32_console_getters[
328 "GetConsoleScreenBufferInfo"
329 ].return_value = cursor_at_end_of_line
330 term = LegacyWindowsTerm(sys.stdout)
331 term.move_cursor_forward()
332
333 SetConsoleCursorPosition.assert_called_once_with(
334 win32_handle, coords=WindowsCoordinates(row=CURSOR_Y + 1, col=0)
335 )
336
337 @patch.object(_win32_console, "SetConsoleCursorPosition", return_value=None)
338 def test_move_cursor_to_column(
339 SetConsoleCursorPosition, win32_console_getters, win32_handle
340 ):
341 term = LegacyWindowsTerm(sys.stdout)
342 term.move_cursor_to_column(5)
343 SetConsoleCursorPosition.assert_called_once_with(
344 win32_handle, coords=WindowsCoordinates(CURSOR_Y, 5)
345 )
346
347 @patch.object(_win32_console, "SetConsoleCursorPosition", return_value=None)
348 def test_move_cursor_backward(
349 SetConsoleCursorPosition, win32_console_getters, win32_handle
350 ):
351 term = LegacyWindowsTerm(sys.stdout)
352 term.move_cursor_backward()
353 SetConsoleCursorPosition.assert_called_once_with(
354 win32_handle, coords=WindowsCoordinates(row=CURSOR_Y, col=CURSOR_X - 1)
355 )
356
357 @patch.object(_win32_console, "SetConsoleCursorPosition", return_value=None)
358 def test_move_cursor_backward_prev_line_wrap(
359 SetConsoleCursorPosition, win32_console_getters, win32_handle
360 ):
361 cursor_at_start_of_line = StubScreenBufferInfo(
362 dwCursorPosition=COORD(0, CURSOR_Y)
363 )
364 win32_console_getters[
365 "GetConsoleScreenBufferInfo"
366 ].return_value = cursor_at_start_of_line
367 term = LegacyWindowsTerm(sys.stdout)
368 term.move_cursor_backward()
369 SetConsoleCursorPosition.assert_called_once_with(
370 win32_handle,
371 coords=WindowsCoordinates(row=CURSOR_Y - 1, col=SCREEN_WIDTH - 1),
372 )
373
374 @patch.object(_win32_console, "SetConsoleCursorInfo", return_value=None)
375 def test_hide_cursor(SetConsoleCursorInfo, win32_console_getters, win32_handle):
376 term = LegacyWindowsTerm(sys.stdout)
377 term.hide_cursor()
378
379 call_args = SetConsoleCursorInfo.call_args_list
380
381 assert len(call_args) == 1
382
383 args, kwargs = call_args[0]
384 assert kwargs["cursor_info"].bVisible == 0
385 assert kwargs["cursor_info"].dwSize == CURSOR_SIZE
386
387 @patch.object(_win32_console, "SetConsoleCursorInfo", return_value=None)
388 def test_show_cursor(SetConsoleCursorInfo, win32_console_getters, win32_handle):
389 term = LegacyWindowsTerm(sys.stdout)
390 term.show_cursor()
391
392 call_args = SetConsoleCursorInfo.call_args_list
393
394 assert len(call_args) == 1
395
396 args, kwargs = call_args[0]
397 assert kwargs["cursor_info"].bVisible == 1
398 assert kwargs["cursor_info"].dwSize == CURSOR_SIZE
399
400 @patch.object(_win32_console, "SetConsoleTitle", return_value=None)
401 def test_set_title(SetConsoleTitle, win32_console_getters):
402 term = LegacyWindowsTerm(sys.stdout)
403 term.set_title("title")
404
405 SetConsoleTitle.assert_called_once_with("title")
406
407 @patch.object(_win32_console, "SetConsoleTitle", return_value=None)
408 def test_set_title_too_long(_, win32_console_getters):
409 term = LegacyWindowsTerm(sys.stdout)
410
411 with pytest.raises(AssertionError):
412 term.set_title("a" * 255)