Debian Codegraph @

tests/unit/shader/test_shader.py

raw symbols SCIP
1 import unittest
2 from unittest.mock import patch, MagicMock
3
4 GL_TRIANGLES = 4
5
6 # The default shader attributes.
7 # Update this if you change the default shader.
8 default_shader_attributes = {
9 'colors': {'type': 35666, 'size': 1, 'location': 1, 'count': 4, 'format': 'f', 'instance': False},
10 'position': {'type': 35665, 'size': 1, 'location': 0, 'count': 3, 'format': 'f', 'instance': False},
11 'tex_coords': {'type': 35665, 'size': 1, 'location': 2, 'count': 3, 'format': 'f', 'instance': False},
12 }
13
14
15 class TestDefaultShader(unittest.TestCase):
16
17 def setUp(self):
18 """Setup the fake ShaderProgram for all of the tests."""
19 patcher = patch.multiple(
20 'pyglet.graphics.shader',
21 _link_program=patch('pyglet.graphics.shader._link_program', return_value=1).start(),
22 _introspect_attributes=patch('pyglet.graphics.shader._introspect_attributes',
23 return_value=default_shader_attributes).start(),
24 _introspect_uniforms=patch('pyglet.graphics.shader._introspect_uniforms',
25 return_value={}).start(),
26 _introspect_uniform_blocks=patch('pyglet.graphics.shader._introspect_uniform_blocks',
27 return_value={}).start(),
28 glEnableVertexAttribArray=patch('pyglet.graphics.shader.glEnableVertexAttribArray', new_callable=MagicMock()).start(),
29 glVertexAttribPointer=patch('pyglet.graphics.shader.glVertexAttribPointer',
30 new_callable=MagicMock()).start(),
31 )
32 self.addCleanup(patcher.stop)
33
34 patcher_gl = patch.multiple(
35 'pyglet.gl',
36 current_context=patch('pyglet.gl.current_context', create=True).start(),
37 glUseProgram=patch('pyglet.gl.glUseProgram').start(),
38 glDeleteProgram=patch('pyglet.gl.glDeleteProgram').start(),
39 glGenVertexArrays=patch('pyglet.gl.glGenVertexArrays').start(),
40 )
41 self.addCleanup(patcher_gl.stop)
42
43 # These are imported already before this test runs, so we need to override them. Imports fixed.
44 patcher_vertexbuffer = patch.multiple(
45 'pyglet.graphics.vertexbuffer',
46 glBindBuffer=MagicMock(),
47 glBufferData=MagicMock(),
48 glBufferSubData=MagicMock(),
49 glGenBuffers=MagicMock(),
50 )
51 self.addCleanup(patcher_vertexbuffer.stop)
52 patcher_vertexbuffer.start()
53
54 patcher_vertexarray = patch.multiple(
55 'pyglet.graphics.vertexarray',
56 glGenVertexArrays=MagicMock(),
57 glBindVertexArray=MagicMock(),
58 )
59 self.addCleanup(patcher_vertexarray.stop)
60 patcher_vertexarray.start()
61
62 # Create mock shaders
63 mock_shader1 = MagicMock()
64 mock_shader2 = MagicMock()
65
66 from pyglet.graphics import Batch
67 from pyglet.graphics.shader import ShaderProgram
68
69 # Patch default batch.
70 patch('pyglet.graphics.get_default_batch', return_value=Batch()).start()
71
72 self.program = ShaderProgram(mock_shader1, mock_shader2)
73
74 self.batch = Batch()
75
76 def test_index_vertex_list_create_no_batch_no_group(self):
77 vlist = self.program._vertex_list_create( # noqa: SLF001
78 count=4,
79 mode=GL_TRIANGLES,
80 indices=[0, 1, 2, 0, 2, 3],
81 instances=None,
82 batch=None,
83 group=None,
84 position=('f', (400, 400, 0, 400 + 50, 400, 0, 400 + 50, 400 + 50, 0, 400, 400 + 50, 0)),
85 colors=('f', (1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1)),
86 )
87
88 assert vlist is not None
89 assert vlist.count == 4
90
91 def test_index_vertex_list_create_batch_no_group(self):
92 vlist = self.program._vertex_list_create( # noqa: SLF001
93 count=4,
94 mode=GL_TRIANGLES,
95 indices=[0, 1, 2, 0, 2, 3],
96 instances=None,
97 batch=self.batch,
98 group=None,
99 position=('f', (400, 400, 0, 400 + 50, 400, 0, 400 + 50, 400 + 50, 0, 400, 400 + 50, 0)),
100 colors=('f', (1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1)),
101 )
102
103 assert vlist is not None
104 assert vlist.count == 4
105
106 def test_index_vertex_list_create_batch_group(self):
107 from pyglet.graphics import Group
108 vlist = self.program._vertex_list_create( # noqa: SLF001
109 count=4,
110 mode=GL_TRIANGLES,
111 indices=[0, 1, 2, 0, 2, 3],
112 instances=None,
113 batch=self.batch,
114 group=Group(),
115 position=('f', (400, 400, 0, 400 + 50, 400, 0, 400 + 50, 400 + 50, 0, 400, 400 + 50, 0)),
116 colors=('f', (1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1)),
117 )
118
119 assert vlist is not None
120 assert vlist.count == 4
121
122 def test_index_vertex_list_create_no_batch_group(self):
123 from pyglet.graphics import Group
124 vlist = self.program._vertex_list_create( # noqa: SLF001
125 count=4,
126 mode=GL_TRIANGLES,
127 indices=[0, 1, 2, 0, 2, 3],
128 instances=None,
129 batch=None,
130 group=Group(),
131 position=('f', (400, 400, 0, 400 + 50, 400, 0, 400 + 50, 400 + 50, 0, 400, 400 + 50, 0)),
132 colors=('f', (1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1)),
133 )
134
135 assert vlist is not None
136 assert vlist.count == 4
137
138 if __name__ == '__main__':
139 unittest.main()