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_compile(self): " Test compile " srcf = open(vert_path('shader1'), 'r') src = srcf.read() vert_obj = ShaderObject.vertex() vert_obj.source = src self.assertEqual(0, vert_obj.log_length, 'Log length is not zero') self.assertFalse(vert_obj.compiled, 'compiled returned True') self.assertTrue(vert_obj.compile(), 'compilation failed') self.assertTrue(vert_obj.compiled, 'compiled returned False') self.assertEqual(0, vert_obj.log_length, 'Log length is not zero') self.assertEqual('', vert_obj.logs, 'Logs are not empty') srcf.close()
def test_source(self): " Test shader set/get source & source_length descriptor " srcf = open(vert_path('shader1'), 'r') src = srcf.read() vert_obj = ShaderObject.vertex() self.assertEqual("", vert_obj.source) self.assertEqual(0, vert_obj.source_length) vert_obj.source = src self.assertEqual(len(src)+1, vert_obj.source_length) # +1 because source_length includes a null byte src2 = vert_obj.source[0:-1] # Remove the null byte self.assertIsNot(src, src2) self.assertEqual(src, src2) srcf.close()
def test_freeing(self): " Test if the shaders are freed correctly" vert_obj = ShaderObject.vertex() # Warning, borrowed_obj do not own the underlying shader so it will not be freed automatically borrowed_obj = ShaderObject(glCreateShader(GL_VERTEX_SHADER), owned=False) borrowed_sid = borrowed_obj.sid sid = vert_obj.sid del vert_obj del borrowed_obj gc.collect() self.assertEqual(GL_FALSE, glIsShader(sid), 'shader object is still valid') self.assertEqual(GL_TRUE, glIsShader(borrowed_sid), 'shader object was deleted') #Free the shader object glDeleteShader(borrowed_sid)
def test_eq(self): vert_obj = ShaderObject.vertex() vert_obj_clone = ShaderObject(vert_obj.sid) self.assertEqual(vert_obj, vert_obj_clone, 'vert and vert clone are not equal')
def test_repr(self): vert_obj = ShaderObject.vertex() self.assertEqual('ShaderObject {}'.format(vert_obj.sid), repr(vert_obj), "Repr do not match")