quicktex/tests/test_texture.py

64 lines
2.4 KiB
Python
Raw Normal View History

2021-03-30 05:15:53 +00:00
import unittest
import nose
2021-04-07 06:41:26 +00:00
import os.path
from tests.images import image_path
from quicktex import RawTexture
2021-03-30 05:15:53 +00:00
from PIL import Image
class TestRawTexture(unittest.TestCase):
2021-04-07 06:41:26 +00:00
boilerplate = Image.open(os.path.join(image_path, 'Boilerplate.png'))
boilerplate_bytes = boilerplate.tobytes('raw', 'RGBX')
width, height = boilerplate.size
size = width * height * 4
2021-03-30 09:36:27 +00:00
2021-03-30 05:15:53 +00:00
def setUp(self):
2021-04-07 06:41:26 +00:00
self.tex = RawTexture(self.width, self.height)
2021-03-30 05:15:53 +00:00
def test_size(self):
2021-03-30 09:36:27 +00:00
"""Test byte size and image dimensions"""
2021-04-08 23:04:21 +00:00
self.assertEqual(self.tex.nbytes, self.size, "incorrect texture byte size")
2021-04-07 06:41:26 +00:00
self.assertEqual(self.tex.width, self.width, "incorrect texture width")
self.assertEqual(self.tex.height, self.height, "incorrect texture height")
2021-04-08 23:04:21 +00:00
self.assertEqual(self.tex.size, (self.width, self.height), "incorrect texture dimensions")
2021-03-30 05:15:53 +00:00
def test_pixels(self):
2021-03-30 09:36:27 +00:00
"""Test getting and setting pixel values"""
2021-03-30 05:15:53 +00:00
color1 = (69, 13, 12, 0) # totally random color
color2 = (19, 142, 93, 44)
2021-04-07 06:41:26 +00:00
self.tex[0, 0] = color1
self.tex[-1, -1] = color2
data = self.tex.tobytes()
2021-03-30 05:15:53 +00:00
2021-04-07 06:41:26 +00:00
self.assertEqual(self.tex[0, 0], color1)
self.assertEqual(self.tex[-1, -1], color2)
2021-03-30 05:15:53 +00:00
self.assertEqual(tuple(data[0:4]), color1)
self.assertEqual(tuple(data[-4:]), color2)
2021-04-07 06:41:26 +00:00
2021-03-30 09:36:27 +00:00
with self.assertRaises(IndexError):
2021-04-07 06:41:26 +00:00
thing = self.tex[self.width, self.height]
2021-03-30 09:36:27 +00:00
with self.assertRaises(IndexError):
2021-04-07 06:41:26 +00:00
thing = self.tex[-1 - self.width, -1 - self.height]
2021-03-30 05:15:53 +00:00
def test_buffer(self):
"""Test the Buffer protocol implementation for RawTexture"""
2021-04-07 06:41:26 +00:00
mv = memoryview(self.tex)
self.assertFalse(mv.readonly, 'buffer is readonly')
self.assertTrue(mv.c_contiguous, 'buffer is not contiguous')
self.assertEqual(mv.nbytes, self.size, 'buffer is the wrong size')
self.assertEqual(mv.format, 'B', 'buffer has the wrong format')
2021-03-30 05:15:53 +00:00
2021-04-07 06:41:26 +00:00
mv[:] = self.boilerplate_bytes
self.assertEqual(mv.tobytes(), self.boilerplate_bytes, 'incorrect buffer data')
2021-03-30 05:15:53 +00:00
2021-03-30 09:36:27 +00:00
def test_frombytes(self):
2021-03-30 05:15:53 +00:00
"""Test the frombytes factory function"""
2021-04-07 06:41:26 +00:00
bytetex = RawTexture.frombytes(self.boilerplate_bytes, *self.boilerplate.size)
self.assertEqual(self.boilerplate_bytes, bytetex.tobytes(), 'Incorrect bytes after writing to buffer')
2021-03-30 05:15:53 +00:00
if __name__ == '__main__':
nose.main()