Debian Codegraph @

tests/unit/conftest.py

raw symbols SCIP
1 from typing import Tuple
2 from unittest import mock
3
4 from pytest import fixture
5
6
7 @fixture
8 def get_dummy_shader_program():
9 """
10 Provide a dummy getter to monkeypatch getters for default shaders.
11
12 By default, batchable objects create or reuse a default shader
13 program. This is usually done through a ``get_default_shader``
14 function on their implementing module. If no GL context exists,
15 calling that function creates one, which risks non-drawing tests
16 failing or running slower than optimal.
17
18 Avoid that by passing this fixture to local monkey patching fixtures
19 in module-specific single test files or conftest.py instances for
20 test modules::
21
22 # Example from ./shapes/conftest.py
23
24 @fixture(autouse=True) # Force this to be used for every test in the module
25 def monkeypatch_default_shape_shader(monkeypatch, get_dummy_shader_program):
26 monkeypatch.setattr(
27 'pyglet.shapes.get_default_shader',
28 get_dummy_shader_program)
29
30 """
31 # A named function instead of a lambda for clarity in debugger views.
32 def _get_dummy_shader_program(*args, **kwargs):
33 return mock.MagicMock()
34
35 return _get_dummy_shader_program
36
37
38 # Color constants & fixtures for use with Shapes, UI elements, etc.
39 ORIGINAL_RGB_COLOR = 253, 254, 255
40 ORIGINAL_RGBA_COLOR = ORIGINAL_RGB_COLOR + (37,)
41 NEW_RGB_COLOR = 1, 2, 3
42 NEW_RGBA_COLOR = 5, 6, 7, 59
43
44
45 @fixture(scope="session")
46 def original_rgb_color():
47 return ORIGINAL_RGB_COLOR
48
49
50 @fixture(scope="session")
51 def original_rgba_color():
52 return ORIGINAL_RGBA_COLOR
53
54
55 @fixture(params=[ORIGINAL_RGB_COLOR, ORIGINAL_RGBA_COLOR])
56 def original_rgb_or_rgba_color(request):
57 return request.param
58
59
60 def expected_alpha_for_color(color: Tuple[int, ...]):
61 """
62 Slow but readable color helper with validation.
63
64 This uses more readable logic than the main library and will raise
65 clear ValueErrors as part of validation.
66
67 Args:
68 color: An RGB or RGBA color
69
70 Returns:
71
72 """
73 num_channels = len(color)
74
75 if num_channels == 4:
76 return color[3]
77 elif num_channels == 3:
78 return 255
79
80 raise ValueError(
81 f"Expected color tuple with 3 or 4 elements, but got {color!r}.")
82
83
84 @fixture
85 def original_rgb_or_rgba_expected_alpha(original_rgb_or_rgba_color):
86 return expected_alpha_for_color(original_rgb_or_rgba_color)
87
88
89 @fixture(scope="session")
90 def new_rgb_color():
91 return NEW_RGB_COLOR
92
93
94 @fixture(scope="session")
95 def new_rgba_color():
96 return NEW_RGBA_COLOR
97
98
99 @fixture(params=[NEW_RGB_COLOR, NEW_RGBA_COLOR])
100 def new_rgb_or_rgba_color(request):
101 return request.param
102
103
104 @fixture(params=[180, -180])
105 def new_nonzero_rotation(request) -> float:
106 return request.param
107
108
109 def new_color_expected_alpha(new_rgb_or_rgba_color):
110 return expected_alpha_for_color(new_rgb_or_rgba_color)
111
112
113 @fixture(params=[0, 128, 254])
114 def new_opacity_set_opacity_alone(request):
115 return request.param
116