Debian Codegraph @

tests/unit/shapes/test_shapes.py

raw symbols SCIP
1 from __future__ import annotations
2
3 from functools import partial
4 from unittest.mock import MagicMock
5
6 import pytest
7
8 from pyglet.graphics import Batch, Group
9 from pyglet.shapes import *
10
11
12 # The shapes are tested individually since their RGBA handling is
13 # inlined for maximum speed instead of encapsulated in their baseclass.
14 # A typo might break color functionality in one but not the others.
15 @pytest.fixture(scope="module", params=[
16 (Arc, (0, 0, 5)),
17 (Circle, (0, 0, 5)),
18 # Ellipse's a value below is nonsensical in normal use, but here it
19 # makes sure the value is not confused with the RGBA alpha channel
20 # internally.
21 (Ellipse, (0, 0, 0, 5)),
22 (Sector, (0, 0, 3)),
23 (Line, (0, 0, 7, 7)),
24 (Rectangle, (0, 0, 20, 20)),
25 (BorderedRectangle, (0, 0, 30, 10)),
26 (Triangle, (0, 0, 2, 2, 5, 5)),
27 (Star, (1, 1, 20, 11, 5)),
28 (Polygon, ((0, 0), (1, 1), (2, 2))),
29 (MultiLine, ((0, 0), (1, 1), (2, 2))),
30 ])
31 def shape_and_positionals(request):
32 return request.param
33
34
35 # Enable type-specific behavior; just Line + rotation at the moment
36 @pytest.fixture()
37 def shape_type(shape_and_positionals):
38 return shape_and_positionals[0]
39
40
41 @pytest.fixture()
42 def shape_keywords_only(shape_and_positionals):
43 class_, positional_args = shape_and_positionals
44 return partial(class_, *positional_args)
45
46
47 @pytest.fixture()
48 def rgb_or_rgba_shape(shape_keywords_only, original_rgb_or_rgba_color):
49 return shape_keywords_only(color=original_rgb_or_rgba_color)
50
51
52 @pytest.fixture()
53 def rgba_shape(shape_keywords_only):
54 return shape_keywords_only(color=(0, 255, 0, 37))
55
56
57 def test_init_sets_opacity_from_rgba_value_as_color_argument(rgba_shape):
58 assert rgba_shape.opacity == 37
59
60
61 def test_init_sets_opacity_to_255_for_rgb_value_as_color_argument(shape_keywords_only):
62 assert shape_keywords_only(color=(0, 0, 0)).opacity == 255
63
64
65 def test_init_sets_rotation_to_zero(rgb_or_rgba_shape, shape_type):
66 if shape_type is Line:
67 pytest.xfail("Rotation test not yet valid for line due to design ambiguity")
68 assert rgb_or_rgba_shape.rotation == 0
69
70
71 def test_rotation_prop_sets_rotation(rgb_or_rgba_shape, new_nonzero_rotation):
72 rgb_or_rgba_shape.rotation = new_nonzero_rotation
73 assert rgb_or_rgba_shape.rotation == new_nonzero_rotation
74
75
76 def test_setting_color_sets_color_rgb_channels(rgb_or_rgba_shape, new_rgb_or_rgba_color):
77 rgb_or_rgba_shape.color = new_rgb_or_rgba_color
78 assert rgb_or_rgba_shape.color[:3] == new_rgb_or_rgba_color[:3]
79
80
81 def test_setting_color_to_rgb_value_does_not_change_opacity(rgb_or_rgba_shape, new_rgb_color):
82 original_opacity = rgb_or_rgba_shape.opacity
83 rgb_or_rgba_shape.color = new_rgb_color
84 assert rgb_or_rgba_shape.opacity == original_opacity
85
86
87 def test_setting_color_to_rgba_value_changes_opacity(rgb_or_rgba_shape, new_rgba_color):
88 rgb_or_rgba_shape.color = new_rgba_color
89 assert rgb_or_rgba_shape.opacity == new_rgba_color[3]
90 assert rgb_or_rgba_shape.color[3] == new_rgba_color[3]
91
92
93 def test_setting_opacity_does_not_change_rgb_channels_on_color(rgb_or_rgba_shape):
94 original_color = rgb_or_rgba_shape.color[:3]
95 rgb_or_rgba_shape.opacity = 255
96 assert rgb_or_rgba_shape.color[:3] == original_color
97
98
99 def test_group_setter(shape_keywords_only):
100 shape = shape_keywords_only()
101
102 new_group = Group()
103 shape.group = new_group
104 assert shape.group is new_group
105
106
107 def test_batch_setter(shape_keywords_only):
108 shape = shape_keywords_only()
109
110 new_batch = Batch()
111 shape.batch = new_batch
112 assert shape.batch is new_batch
113
114
115 def test_program_setter(shape_keywords_only):
116 shape = shape_keywords_only()
117
118 program = MagicMock()
119 shape.program = program
120 assert shape.program == program
121
122
123 def test_blend_setter(shape_keywords_only):
124 shape = shape_keywords_only()
125
126 blend_mode = (1, 1)
127 shape.blend_mode = blend_mode
128 assert shape._group.blend_src == 1 # noqa: SLF001
129 assert shape._group.blend_dest == 1 # noqa: SLF001