def test_create(self): " Test ShaderObject.vertex and ShaderObject.fragment " vert_obj = ShaderObject.vertex() frag_obj = ShaderObject.fragment() self.assertIs(type(vert_obj), ShaderObject, 'vertex did not returned a ShaderObject.') self.assertIs(type(frag_obj), ShaderObject, 'fragment did not returned a ShaderObject.')
def test_shader_type(self): " Test descriptors " vert_obj = ShaderObject.vertex() frag_obj = ShaderObject.fragment() self.assertEqual(GL_VERTEX_SHADER, vert_obj.type, 'type is not vertex') self.assertEqual(GL_FRAGMENT_SHADER, frag_obj.type, 'type is not fragment')
def test_valid(self): " Test if generated objects are valid" vert_obj = ShaderObject.vertex() frag_obj = ShaderObject.fragment() bad_obj = ShaderObject(8000, owned=True) borrowed_obj = ShaderObject(glCreateShader(GL_VERTEX_SHADER), owned=True) self.assertTrue(vert_obj.valid(), 'vertex shader is not valid') self.assertTrue(frag_obj.valid(), 'fragment shader is not valid') self.assertTrue(borrowed_obj.valid(), 'other vertex shader is not valid') self.assertFalse(bad_obj.valid(), 'bad shader is valid') self.assertTrue(vert_obj, 'vertex shader is not valid') self.assertFalse(bad_obj, 'bad shader is valid')
def get_objects(self, name='shader1'): vert = ShaderObject.vertex() frag = ShaderObject.fragment() with open(vert_path(name), 'r') as f: vert.source = f.read() self.assertEqual(True, vert.compile(), 'vert compilation failed') with open(frag_path(name), 'r') as f: frag.source = f.read() self.assertEqual(True, frag.compile(), 'frag compilation failed') return vert, frag
def test_link(self): "Test if a shader program links correctly" prog = ShaderProgram.new_program() vert, frag = self.get_objects('shader2') lib = ShaderObject.fragment() with open(frag_path('frag_lib')) as f: lib.source = f.read() self.assertTrue(lib.compile(), 'lib did not compile successfully') prog.attach(vert, frag, lib) self.assertEqual(GL_FALSE, prog.link_status, 'link status is not false') self.assertTrue(prog.link(), 'link was not successful') self.assertEqual(GL_TRUE, prog.link_status, 'link status is not true')
def test_fail_compile(self): " Test fail compile and logs " srcf = open(frag_path('shader_bad'), 'r') src = srcf.read() frag_obj = ShaderObject.fragment() frag_obj.source = src self.assertFalse(frag_obj.compile(), 'shader was compiled successfully') self.assertFalse(frag_obj.compiled, 'compiled returned True') self.assertNotEqual(0, frag_obj.log_length, 'Log length is 0') logs = frag_obj.logs self.assertIn('Undeclared identifier: x', logs, 'Error not found') self.assertIn('Undeclared identifier: y', logs, 'Error not found') self.assertIn('Syntax error: "}" parse error', logs, 'Error not found') self.assertIn('3 compilation errors. No code generated', logs, 'Error not found') srcf.close()