48 lines
2.2 KiB
Python
48 lines
2.2 KiB
Python
import struct
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'ops'))
|
|
from source_charsections import compare_sections, read_sections
|
|
|
|
|
|
class SourceCharSectionsTest(unittest.TestCase):
|
|
def table(self, directory, name, pool, rows):
|
|
path = Path(directory) / name
|
|
records = b''.join(struct.pack('<10I', *r) for r in rows)
|
|
path.write_bytes(struct.pack('<4s4I', b'WDBC', len(rows), 10, 40, len(pool)) + records + pool)
|
|
return path
|
|
|
|
def test_layout_slots_offsets_and_duplicate_selectors(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
base = read_sections(self.table(d, 'base', b'\0hair\0scalp\0', [
|
|
(575, 1, 1, 3, 0, 0, 1, 0, 6, 4), (576, 1, 1, 3, 0, 0, 0, 6, 0, 8)]))
|
|
patched = read_sections(self.table(d, 'patch', b'\0scalp\0hair\0', [
|
|
(575, 1, 1, 3, 0, 0, 7, 0, 1, 4), (577, 1, 1, 3, 0, 0, 0, 1, 0, 8),
|
|
(578, 1, 1, 3, 0, 0, 7, 0, 1, 2)]))
|
|
self.assertEqual(patched[0], dict(id=575, race=1, sex=1, section=3, variation=0,
|
|
color=0, flags=4, textures=['hair', '', 'scalp']))
|
|
diff = compare_sections(patched, base)
|
|
self.assertEqual([(r['id_state'],r['selector_state']) for r in diff['rows']],
|
|
[('unchanged','unchanged'),('added','unchanged'),('added','changed')])
|
|
self.assertEqual(diff['removed'], [base[1]])
|
|
|
|
def test_exact_member_boundaries_and_last_string(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
row = (575, 1, 1, 3, 2, 3, 0, 0, 1, 7)
|
|
path = self.table(d, 'member', b'\0end\0', [row])
|
|
self.assertEqual(read_sections(path)[0]['textures'][2], 'end')
|
|
original = path.read_bytes()
|
|
for blob in (original[:-1], original+b'\0', original[:-1]+b'x'):
|
|
with self.subTest(blob=blob), self.assertRaises(ValueError):
|
|
path.write_bytes(blob)
|
|
read_sections(path)
|
|
bad = self.table(d, 'offset', b'\0end\0', [(*row[:8],5,7)])
|
|
with self.assertRaises(ValueError): read_sections(bad)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|