# 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 typing import List
from unittest import TestCase

from hypothesis import given
from hypothesis.strategies import lists

from keks import dumps
from keks import loads
from keks import NotEnoughData
from tests.strategies import any_st
from tests.strategies import everything_st
from tests.strategies import junk_st


class TestList(TestCase):
    @given(junk_st)
    def test_empty(self, junk: bytes) -> None:
        encoded = dumps([])
        self.assertSequenceEqual(encoded, b"\x08\x00")
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded, [])
        self.assertSequenceEqual(tail, junk)

    @given(lists(everything_st, max_size=4), junk_st)
    def test_symmetric(self, test_list: List, junk: bytes) -> None:
        encoded = dumps(test_list)
        self.assertSequenceEqual(
            encoded,
            b"\x08" + b"".join(dumps(i) for i in test_list) + b"\x00",
        )
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded, test_list)
        self.assertSequenceEqual(tail, junk)

    @given(lists(any_st, max_size=2))
    def test_no_eoc(self, test_list: List) -> None:
        encoded = dumps(test_list)[:-1]
        with self.assertRaises(NotEnoughData) as err:
            loads(encoded)
        self.assertEqual(err.exception.n, 1)
