import unittest

from xpdt import NameSpace, StructDecl, MemberDecl, BaseType

class Test_Stringy(unittest.TestCase):
    def setUp(self):
        self.code = NameSpace.from_decls(
            (
                StructDecl(
                    struct_name='Item',
                    members=[
                        MemberDecl('id', 'u32'),
                        MemberDecl('name', 'blob'),
                    ],
                ),
                StructDecl(
                    struct_name='Entity',
                    members=[
                        MemberDecl('id', 'u32'),
                        MemberDecl('a', 'Item'),
                        MemberDecl('b', 'Item'),
                    ],
                ),
            ),
            name=None,
        ).gen_dynamic_python()

    def test_entity(self):
        obj_a = self.code.Item(1, b'Item A')
        obj_b = self.code.Item(2, b'Item B')
        orig = self.code.Entity(0xdeadbeef, obj_a, obj_b)
        b = bytes(orig)
        clone = orig._frombytes(b)
        self.assertEqual(orig, clone)

    def test_entity_list(self):
        obj_a = self.code.Item(1, b'Item A')
        obj_b = self.code.Item(2, b'Item B')
        obj_c = self.code.Item(3, b'Item C')
        ent_a = self.code.Entity(0xdeadbeef, obj_a, obj_b)
        ent_b = self.code.Entity(0xfeedface, obj_c, obj_b)

        orig = [ent_a, ent_b]

        clone = b''.join((bytes(ent) for ent in orig))

        tot_len = len(clone)
        decoded = []
        off = 0
        while off < tot_len:
            item_len, ent = self.code.Entity._frombuf(clone, off)
            off += item_len
            decoded.append(ent)

        self.assertListEqual(orig, decoded)
