Debian Codegraph @

tests/unit/text/test_caret.py

raw symbols SCIP
1 from collections import UserList
2 from unittest.mock import Mock, NonCallableMock
3
4 import pytest
5
6 from pyglet.graphics.shader import ShaderProgram
7 from pyglet.graphics.vertexdomain import IndexedVertexList
8 from pyglet.text import layout, caret
9
10
11 class ListSlicesAsTuple(UserList):
12 """
13 Mutable sequence which returns tuples when sliced like ctypes objects
14
15 It makes tests cleaner by allowing direct comparison to color tuples and
16 slices of them.
17 """
18
19 def __getitem__(self, item):
20 if isinstance(item, slice):
21 return tuple(super().__getitem__(item))
22 return super().__getitem__(item)
23
24
25 @pytest.fixture(autouse=True)
26 def disable_automatic_caret_blinking(monkeypatch):
27 monkeypatch.setattr(caret, 'clock', Mock(spec=caret.clock))
28
29
30 # Brittle tangle of mocks due to tightly coupled Caret/Layout design
31 @pytest.fixture
32 def mock_layout():
33
34 # Create layout mock
35 _layout = NonCallableMock(spec=layout.TextLayout)
36 _layout.foreground_decoration_group = NonCallableMock()
37 _layout.attach_mock(Mock(), 'push_handlers')
38
39 # Create mock shader program for it
40 program = NonCallableMock(spec=ShaderProgram)
41 _layout.foreground_decoration_group.attach_mock(program, 'program')
42
43 # Allow the shader program to create a mock vertex list on demand
44 def _fake_vertex_list_method(count, mode, batch=None, group=None, colors=None, visible=None):
45 vertex_list = NonCallableMock(spec=IndexedVertexList)
46 vertex_list.colors = ListSlicesAsTuple(colors[1])
47 vertex_list.visible = (1, 1)
48 return vertex_list
49
50 program.vertex_list = _fake_vertex_list_method
51
52 return _layout
53
54
55 # Color fixtures are defined in pyglet's tests/unit/conftest.py
56 @pytest.fixture
57 def rgba_caret(mock_layout, original_rgba_color):
58 return caret.Caret(layout=mock_layout, color=original_rgba_color)
59
60
61 @pytest.fixture
62 def rgb_caret(mock_layout, original_rgb_color):
63 return caret.Caret(layout=mock_layout, color=original_rgb_color)
64
65
66 @pytest.fixture
67 def rgb_or_rgba_caret(mock_layout, original_rgb_or_rgba_color):
68 return caret.Caret(layout=mock_layout, color=original_rgb_or_rgba_color)
69
70
71 def test_init_sets_opacity_to_255_when_rgb_color_argument(rgb_caret):
72 assert rgb_caret.color[3] == 255
73
74
75 def test_init_sets_opacity_from_rgba_value_as_color_argument(rgba_caret, original_rgba_color):
76 assert rgba_caret.color[3] == original_rgba_color[3]
77
78
79 def test_init_sets_rgb_channels_correctly(rgb_or_rgba_caret, original_rgb_or_rgba_color):
80 assert rgb_or_rgba_caret.color[:3] == original_rgb_or_rgba_color[:3]
81
82
83 def test_color_setter_sets_rgb_channels_correctly(rgb_or_rgba_caret, new_rgb_or_rgba_color):
84 rgb_or_rgba_caret.color = new_rgb_or_rgba_color
85 assert rgb_or_rgba_caret.color[:3] == new_rgb_or_rgba_color[:3]
86
87
88 def test_color_setter_preserves_alpha_channel_when_setting_rgb_colors(
89 rgb_or_rgba_caret,
90 original_rgb_or_rgba_expected_alpha,
91 new_rgb_color
92 ):
93 rgb_or_rgba_caret.color = new_rgb_color
94 assert rgb_or_rgba_caret.color[3] == original_rgb_or_rgba_expected_alpha
95
96
97 def test_color_setter_changes_alpha_channel_when_setting_rgba_colors(
98 rgb_or_rgba_caret,
99 new_rgba_color
100 ):
101 rgb_or_rgba_caret.color = new_rgba_color
102 assert rgb_or_rgba_caret.color[3] == new_rgba_color[3]