quicktex/tests/test_texture.py

62 lines
2.4 KiB
Python
Raw Normal View History

2021-03-30 05:15:53 +00:00
import unittest
import nose
import quicktex
2021-04-05 09:44:56 +00:00
import tests.images as images
2021-03-30 05:15:53 +00:00
import tempfile
from PIL import Image
class TestRawTexture(unittest.TestCase):
2021-03-30 09:36:27 +00:00
boilerplate = Image.open(images.image_path + '/Boilerplate.png')
bp_bytes = boilerplate.tobytes('raw', 'RGBX')
width = boilerplate.width
height = boilerplate.height
2021-03-30 05:15:53 +00:00
def setUp(self):
self.texture = quicktex.RawTexture(self.width, self.height)
def test_size(self):
2021-03-30 09:36:27 +00:00
"""Test byte size and image dimensions"""
2021-03-30 05:15:53 +00:00
self.assertEqual(self.texture.size, self.width * self.height * 4, "incorrect texture byte size")
self.assertEqual(self.texture.width, self.width, "incorrect texture width")
self.assertEqual(self.texture.height, self.height, "incorrect texture height")
self.assertEqual(self.texture.dimensions, (self.width, self.height), "incorrect texture dimension tuple")
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-03-30 05:56:25 +00:00
self.texture[0, 0] = color1
self.texture[-1, -1] = color2
2021-03-30 05:15:53 +00:00
data = self.texture.tobytes()
2021-03-30 05:56:25 +00:00
self.assertEqual(self.texture[0, 0], color1)
self.assertEqual(self.texture[-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-03-30 09:36:27 +00:00
with self.assertRaises(IndexError):
thing = self.texture[self.width, self.height]
with self.assertRaises(IndexError):
thing = self.texture[-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"""
with tempfile.TemporaryFile('r+b') as fp:
2021-03-30 09:36:27 +00:00
fp.write(self.bp_bytes)
2021-03-30 05:15:53 +00:00
fp.seek(0)
bytes_read = fp.readinto(self.texture)
2021-03-30 09:36:27 +00:00
self.assertEqual(bytes_read, self.texture.size, 'buffer over/underrun')
self.assertEqual(self.bp_bytes, self.texture.tobytes(), 'Incorrect bytes after writing to buffer')
self.assertEqual(self.bp_bytes, bytes(self.texture), "Incorrect bytes after reading from buffer")
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-03-30 09:36:27 +00:00
bytetex = quicktex.RawTexture.frombytes(self.bp_bytes, *self.boilerplate.size)
self.assertEqual(self.bp_bytes, bytetex.tobytes(), 'Incorrect bytes after writing to buffer')
2021-03-30 05:15:53 +00:00
if __name__ == '__main__':
nose.main()