# KEKS -- Python KEKS implementation
# Copyright (C) 2024-2025 Anton Rudenko <rudenko.ad@phystech.edu>
#               2024-2025 Sergey Matveev <stargrave@stargrave.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.

from unittest import TestCase

from hypothesis import given
from hypothesis.strategies import binary

from keks import DecodeError
from keks import dumps
from keks import loads
from keks import NotEnoughData
from keks import Raw


class TestUnknownType(TestCase):
    def runTest(self) -> None:
        with self.assertRaises(NotImplementedError) as err:
            class A:
                pass
            dumps(A())
        self.assertEqual(str(err.exception), "unsupported type")


class TestUnknownTag(TestCase):
    def runTest(self) -> None:
        with self.assertRaises(DecodeError) as err:
            loads(b"\x05")
        self.assertEqual(str(err.exception), "unknown tag")


class TestEmptyData(TestCase):
    def runTest(self) -> None:
        with self.assertRaises(NotEnoughData) as err:
            loads(b"")
        self.assertEqual(err.exception.n, 1)


class TestRaw(TestCase):
    @given(binary(max_size=8))
    def runTest(self, body: bytes) -> None:
        self.assertSequenceEqual(dumps(Raw(body)), body)


class TestLonelyEOC(TestCase):
    def runTest(self) -> None:
        with self.assertRaises(DecodeError) as err:
            loads(b"\x00")
        self.assertEqual(str(err.exception), "unexpected EOC")
