def test_pack_fail(self): " Test packing data with invalid format" f1 = BufferFormat.from_string("(3f)[foo]") f2 = BufferFormat.from_string("(3f)[vertex](4B)[color]") with self.assertRaises(ValueError, msg="Assign succeed") as cm1: data = ((None, None, None), (None, None, None)) f1.pack(data) with self.assertRaises(IndexError, msg="Assign succeed") as cm2: data = ((4.0, 5.0, 6.0, 12.0), ) f1.pack(data) with self.assertRaises(ValueError, msg="Assign succeed") as cm3: f1.pack(()) with self.assertRaises(ValueError, msg="Assign succeed") as cm4: data = (((10.0, 20.0, 30.0), (10, 11, 12, 13)), (20.0, 30.0, 40.0)) f2.pack(data) self.assertEqual( 'Expected Sequence with format "3f", found "(None, None, None)"', str(cm1.exception), 'Exceptions do not match') self.assertEqual('invalid index', str(cm2.exception), 'Exceptions do not match') self.assertEqual('No data to pack', str(cm3.exception), 'Exceptions do not match') self.assertEqual('Expected Sequence with format "3f", found "20.0"', str(cm4.exception), 'Exceptions do not match')
def test_pack(self): " Test packing data into struct " f1 = BufferFormat.from_string('(3f)[foo]') f2 = BufferFormat.from_string('(3f)[vertex](4B)[color]') f3 = BufferFormat.from_string('(1d)[boo]') data1 = ((1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0)) f1pd = f1.pack(data1) data2 = (((1.0, 2.0, 3.0), (215, 200, 230, 255)), ((10.0, 8.0, 43.0), (100, 255, 50, 50))) f2pd = f2.pack(data2) data3 = [(x, ) for x in (2, 70, 900.0, 823.0)] f3pd = f3.pack(data3) for index, d1 in enumerate(f1pd): self.assertEqual(data1[index], tuple(d1.foo)) for index, d2 in enumerate(f2pd): self.assertEqual(data2[index][0], tuple(d2.vertex)) self.assertEqual(data2[index][1], tuple(d2.color)) for index, d3 in enumerate(f3pd): self.assertEqual(data3[index], tuple(d3.boo))
def test_pack(self): " Test packing data into struct " f1 = BufferFormat.from_string('(3f)[foo]') f2 = BufferFormat.from_string('(3f)[vertex](4B)[color]') f3 = BufferFormat.from_string('(1d)[boo]') data1 = ( (1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0) ) f1pd = f1.pack(data1) data2 = ( ((1.0, 2.0, 3.0), (215, 200, 230, 255)), ((10.0, 8.0, 43.0), (100, 255, 50, 50)) ) f2pd = f2.pack(data2) data3 = [(x,) for x in (2, 70, 900.0, 823.0)] f3pd = f3.pack(data3) for index, d1 in enumerate(f1pd): self.assertEqual(data1[index], tuple(d1.foo)) for index, d2 in enumerate(f2pd): self.assertEqual(data2[index][0], tuple(d2.vertex)) self.assertEqual(data2[index][1], tuple(d2.color)) for index, d3 in enumerate(f3pd): self.assertEqual(data3[index], tuple(d3.boo))
def test_fromstring_fail(self): "Test if invalid format string fails" with self.assertRaises(BufferFormatError) as cm1: BufferFormat.from_string("") with self.assertRaises(BufferFormatError) as cm2: BufferFormat.from_string("(4k)[lolwat](2f)[aaa]") with self.assertRaises(BufferFormatError) as cm3: BufferFormat.from_string("(-5f)[lolwat]") with self.assertRaises(BufferFormatError) as cm4: BufferFormat.from_string("(2f)[ ]") with self.assertRaises(ValueError) as cm5: BufferFormat.from_string("(2f)[23d]") self.assertEqual('Format must be present', str(cm1.exception), 'Exception do not match') self.assertEqual('Format string is not valid', str(cm2.exception), 'Exception do not match') self.assertEqual('Format string is not valid', str(cm3.exception), 'Exception do not match') self.assertEqual('Format string is not valid', str(cm4.exception), 'Exception do not match') self.assertEqual('"23d" is not a valid variable name', str(cm5.exception), 'Exception do not match')
def test_fromstring(self): f1 = BufferFormat.from_string("(3f)[foo]") f2 = BufferFormat.from_string("(3f)[vertex](4B)[color](3f)[normals]") f3 = BufferFormat.from_string( " (3 f)[b ar]( 2 B)[wa ll et] ") #Spaces are ignored self.assertIsInstance(f1, BufferFormat, "Buffer is not of type Buffer") self.assertIsInstance(f2, BufferFormat, "Buffer is not of type Buffer") self.assertIsInstance(f3, BufferFormat, "Buffer is not of type Buffer") f1_fields = f1.struct._fields_ f2_fields = f2.struct._fields_ f3_fields = f3.struct._fields_ self.assertEqual(1, len(f1.tokens), 'f1 should have 1 token') self.assertEqual(3, len(f2.tokens), 'f2 should have 3 tokens') self.assertEqual(2, len(f3.tokens), 'f3 should have 2 tokens') self.assertEqual([[3, GLfloat * 3, 'foo']], [list(i) for i in f1.tokens]) self.assertEqual( [[3, GLfloat * 3, 'vertex'], [4, GLubyte * 4, 'color'], [3, GLfloat * 3, 'normals']], [list(i) for i in f2.tokens]) self.assertEqual([[3, GLfloat * 3, 'bar'], [2, GLubyte * 2, 'wallet']], [list(i) for i in f3.tokens]) self.assertIn('foo', dir(f1.item), 'foo is not in the format items') self.assertIn('vertex', dir(f2.item), 'vertex is not in the format items') self.assertIn('color', dir(f2.item), 'color is not in the format items') self.assertIn('normals', dir(f2.item), 'normals is not in the format items') self.assertIn('bar', dir(f3.item), 'bar is not in the format items') self.assertIn('wallet', dir(f3.item), 'wallet is not in the format items') self.assertEqual(['foo'], [f[0] for f in f1_fields], 'fields names of the f1 struct do not match') self.assertEqual(['vertex', 'color', 'normals'], [f[0] for f in f2_fields], 'fields names of the f2 struct do not match') self.assertEqual(['bar', 'wallet'], [f[0] for f in f3_fields], 'fields names of the f3 struct do not match') self.assertEqual([GLfloat * 3], [f[1] for f in f1_fields], 'fields types of the f1 struct do not match') self.assertEqual([GLfloat * 3, GLubyte * 4, GLfloat * 3], [f[1] for f in f2_fields], 'fields types of the f2 struct do not match') self.assertEqual([GLfloat * 3, GLubyte * 2], [f[1] for f in f3_fields], 'fields types of the f3 struct do not match')
def test_unpack_fail(self): " Test unpacking bad data " f1 = BufferFormat.from_string('(3f)[foo]') f2 = BufferFormat.from_string('(3i)[foo]') data = ( (1,2,3), (4,5,6), (7,8,9) ) f2pd = f2.pack(data) with self.assertRaises(ValueError, msg='Unpack was successful') as cm1: f1.unpack(f2pd) self.assertEqual('Impossible to unpack data that was not packed by the formatter', str(cm1.exception), 'Exception do not match.')
def test_unpack_fail(self): " Test unpacking bad data " f1 = BufferFormat.from_string('(3f)[foo]') f2 = BufferFormat.from_string('(3i)[foo]') data = ((1, 2, 3), (4, 5, 6), (7, 8, 9)) f2pd = f2.pack(data) with self.assertRaises(ValueError, msg='Unpack was successful') as cm1: f1.unpack(f2pd) self.assertEqual( 'Impossible to unpack data that was not packed by the formatter', str(cm1.exception), 'Exception do not match.')
def test_unpack_single(self): " Test unpack single value" f1 = BufferFormat.from_string('(3f)[foo]') f2 = BufferFormat.from_string('(3f)[vertex](4B)[color]') f1pd = f1.pack_single((1.0, 2.0, 3.0)) f2pd = f2.pack_single(((1.0, 2.0, 3.0), (255, 100, 200, 140))) f2upd = f2.unpack_single(f2pd) f1upd = f1.unpack_single(f1pd) self.assertIsInstance(f1upd, f1.item) self.assertIsInstance(f2upd, f2.item) self.assertEqual((1.0, 2.0, 3.0), f1upd.foo) self.assertEqual((1.0, 2.0, 3.0), f2upd.vertex) self.assertEqual((255, 100, 200, 140), f2upd.color)
def test_unpack_single(self): " Test unpack single value" f1 = BufferFormat.from_string('(3f)[foo]') f2 = BufferFormat.from_string('(3f)[vertex](4B)[color]') f1pd = f1.pack_single((1.0, 2.0, 3.0)) f2pd = f2.pack_single( ((1.0, 2.0, 3.0), (255, 100, 200, 140)) ) f2upd = f2.unpack_single(f2pd) f1upd = f1.unpack_single(f1pd) self.assertIsInstance(f1upd, f1.item) self.assertIsInstance(f2upd, f2.item) self.assertEqual((1.0, 2.0, 3.0), f1upd.foo) self.assertEqual((1.0, 2.0, 3.0), f2upd.vertex) self.assertEqual((255, 100, 200, 140), f2upd.color)
def test_pack_single(self): " Test packing data into a struct " f1 = BufferFormat.from_string('(3f)[foo]') f2 = BufferFormat.from_string('(3f)[vertex](4B)[color]') f3 = BufferFormat.from_string('(1d)[boo]') f1pd = f1.pack_single((1.0, 2.0, 3.0)) f2pd = f2.pack_single(((1.0, 2.0, 3.0), (255, 100, 200, 140))) f3pd = f3.pack_single((6666.0, )) self.assertIsInstance(f1pd, f1.struct) self.assertIsInstance(f2pd, f2.struct) self.assertIsInstance(f3pd, f3.struct) self.assertEqual((1.0, 2.0, 3.0), tuple(f1pd.foo)) self.assertEqual((1.0, 2.0, 3.0), tuple(f2pd.vertex)) self.assertEqual((255, 100, 200, 140), tuple(f2pd.color)) self.assertEqual((6666.0, ), tuple(f3pd.boo))
def test_pack_single(self): " Test packing data into a struct " f1 = BufferFormat.from_string('(3f)[foo]') f2 = BufferFormat.from_string('(3f)[vertex](4B)[color]') f3 = BufferFormat.from_string('(1d)[boo]') f1pd = f1.pack_single((1.0, 2.0, 3.0)) f2pd = f2.pack_single( ((1.0, 2.0, 3.0), (255, 100, 200, 140)) ) f3pd = f3.pack_single((6666.0,)) self.assertIsInstance(f1pd, f1.struct) self.assertIsInstance(f2pd, f2.struct) self.assertIsInstance(f3pd, f3.struct) self.assertEqual((1.0, 2.0, 3.0), tuple(f1pd.foo)) self.assertEqual((1.0, 2.0, 3.0), tuple(f2pd.vertex)) self.assertEqual((255, 100, 200, 140), tuple(f2pd.color)) self.assertEqual((6666.0,), tuple(f3pd.boo))
def test_partial_pack(self): " Test pack with incomplete data" f1 = BufferFormat.from_string('(3f)[foo]') data1 = ( (1.0,), (4.0, 5.0, 6.0), (7.0, 8.0) ) f1pd = f1.pack(data1) self.assertEqual((1.0, 0.0, 0.0), tuple(f1pd[0].foo)) self.assertEqual((4.0, 5.0, 6.0), tuple(f1pd[1].foo)) self.assertEqual((7.0, 8.0, 0.0), tuple(f1pd[2].foo))
def test_partial_pack(self): " Test pack with incomplete data" f1 = BufferFormat.from_string('(3f)[foo]') data1 = ((1.0, ), (4.0, 5.0, 6.0), (7.0, 8.0)) f1pd = f1.pack(data1) self.assertEqual((1.0, 0.0, 0.0), tuple(f1pd[0].foo)) self.assertEqual((4.0, 5.0, 6.0), tuple(f1pd[1].foo)) self.assertEqual((7.0, 8.0, 0.0), tuple(f1pd[2].foo))
def test_unpack(self): " Test unpack multiple values " f1 = BufferFormat.from_string('(3f)[foo]') f2 = BufferFormat.from_string('(3f)[vertex](4B)[color]') data1 = ((1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0)) f1pd = f1.pack(data1) f1upd = f1.unpack(f1pd) data2 = (((1.0, 2.0, 3.0), (215, 200, 230, 255)), ((10.0, 8.0, 43.0), (100, 255, 50, 50))) f2pd = f2.pack(data2) f2upd = f2.unpack(f2pd) for index, d1 in enumerate(data1): self.assertEqual(d1, f1upd[index].foo) for index, d2 in enumerate(data2): self.assertEqual(d2[0], f2upd[index].vertex) self.assertEqual(d2[1], f2upd[index].color)
def test_unpack(self): " Test unpack multiple values " f1 = BufferFormat.from_string('(3f)[foo]') f2 = BufferFormat.from_string('(3f)[vertex](4B)[color]') data1 = ( (1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0) ) f1pd = f1.pack(data1) f1upd = f1.unpack(f1pd) data2 = ( ((1.0, 2.0, 3.0), (215, 200, 230, 255)), ((10.0, 8.0, 43.0), (100, 255, 50, 50)) ) f2pd = f2.pack(data2) f2upd = f2.unpack(f2pd) for index, d1 in enumerate(data1): self.assertEqual(d1, f1upd[index].foo) for index, d2 in enumerate(data2): self.assertEqual(d2[0], f2upd[index].vertex) self.assertEqual(d2[1], f2upd[index].color)
def test_fromstring(self): f1 = BufferFormat.from_string("(3f)[foo]") f2 = BufferFormat.from_string("(3f)[vertex](4B)[color](3f)[normals]") f3 = BufferFormat.from_string(" (3 f)[b ar]( 2 B)[wa ll et] ") #Spaces are ignored self.assertIsInstance(f1, BufferFormat, "Buffer is not of type Buffer") self.assertIsInstance(f2, BufferFormat, "Buffer is not of type Buffer") self.assertIsInstance(f3, BufferFormat, "Buffer is not of type Buffer") f1_fields = f1.struct._fields_ f2_fields = f2.struct._fields_ f3_fields = f3.struct._fields_ self.assertEqual(1, len(f1.tokens), 'f1 should have 1 token') self.assertEqual(3, len(f2.tokens), 'f2 should have 3 tokens') self.assertEqual(2, len(f3.tokens), 'f3 should have 2 tokens') self.assertEqual([[3, GLfloat*3, 'foo']], [list(i) for i in f1.tokens]) self.assertEqual([[3, GLfloat*3, 'vertex'], [4, GLubyte*4, 'color'], [3, GLfloat*3, 'normals']], [list(i) for i in f2.tokens]) self.assertEqual([[3, GLfloat*3, 'bar'], [2, GLubyte*2, 'wallet']], [list(i) for i in f3.tokens]) self.assertIn('foo', dir(f1.item), 'foo is not in the format items') self.assertIn('vertex', dir(f2.item), 'vertex is not in the format items') self.assertIn('color', dir(f2.item), 'color is not in the format items') self.assertIn('normals', dir(f2.item), 'normals is not in the format items') self.assertIn('bar', dir(f3.item), 'bar is not in the format items') self.assertIn('wallet', dir(f3.item), 'wallet is not in the format items') self.assertEqual(['foo'], [f[0] for f in f1_fields], 'fields names of the f1 struct do not match') self.assertEqual(['vertex', 'color', 'normals'], [f[0] for f in f2_fields], 'fields names of the f2 struct do not match') self.assertEqual(['bar', 'wallet'], [f[0] for f in f3_fields], 'fields names of the f3 struct do not match') self.assertEqual([GLfloat*3], [f[1] for f in f1_fields], 'fields types of the f1 struct do not match') self.assertEqual([GLfloat*3, GLubyte*4, GLfloat*3], [f[1] for f in f2_fields], 'fields types of the f2 struct do not match') self.assertEqual([GLfloat*3, GLubyte*2], [f[1] for f in f3_fields], 'fields types of the f3 struct do not match')
def test_pack_fail(self): " Test packing data with invalid format" f1 = BufferFormat.from_string("(3f)[foo]") f2 = BufferFormat.from_string("(3f)[vertex](4B)[color]") with self.assertRaises(ValueError, msg="Assign succeed") as cm1: data = ( (None,None,None), (None,None,None) ) f1.pack(data) with self.assertRaises(IndexError, msg="Assign succeed") as cm2: data = ( (4.0, 5.0, 6.0, 12.0), ) f1.pack(data) with self.assertRaises(ValueError, msg="Assign succeed") as cm3: f1.pack(()) with self.assertRaises(ValueError, msg="Assign succeed") as cm4: data = ( ((10.0, 20.0, 30.0), (10, 11, 12, 13)), (20.0, 30.0, 40.0) ) f2.pack(data) self.assertEqual('Expected Sequence with format "3f", found "(None, None, None)"', str(cm1.exception), 'Exceptions do not match') self.assertEqual('invalid index', str(cm2.exception), 'Exceptions do not match') self.assertEqual('No data to pack', str(cm3.exception), 'Exceptions do not match') self.assertEqual('Expected Sequence with format "3f", found "20.0"', str(cm4.exception), 'Exceptions do not match')
def test_fromstring_cache(self): " Returned format should be cached " f1 = BufferFormat.from_string("(3f)[foo]") f2 = BufferFormat.from_string("(3f)[foo]") self.assertIs(f1, f2)