# Path: rlp/exceptions.py
# class DeserializationError(RLPException):
#     """Exception raised if deserialization fails.
# 
#     :ivar serial: the decoded RLP string that could not be deserialized
#     """
# 
#     def __init__(self, message, serial):
#         super(DeserializationError, self).__init__(message)
#         self.serial = serial
# 
# Path: rlp/sedes/big_endian_int.py
# class BigEndianInt(object):
#     def __init__(self, l=None):
#     def serialize(self, obj):
#     def deserialize(self, serial):
# 
# Path: rlp/sedes/lists.py
# class CountableList(object):
# 
#     """A sedes for lists of arbitrary length.
# 
#     :param element_sedes: when (de-)serializing a list, this sedes will be
#                           applied to all of its elements
#     :param max_length: maximum number of allowed elements, or `None` for no limit
#     """
# 
#     def __init__(self, element_sedes, max_length=None):
#         self.element_sedes = element_sedes
#         self.max_length = max_length
# 
#     @to_list
#     def serialize(self, obj):
#         if not is_sequence(obj):
#             raise ListSerializationError('Can only serialize sequences', obj)
# 
#         if self.max_length is not None and len(obj) > self.max_length:
#             raise ListSerializationError(
#                 'Too many elements ({}, allowed {})'.format(
#                     len(obj),
#                     self.max_length,
#                 ),
#                 obj=obj,
#             )
# 
#         for index, element in enumerate(obj):
#             try:
#                 yield self.element_sedes.serialize(element)
#             except SerializationError as e:
#                 raise ListSerializationError(obj=obj, element_exception=e, index=index)
# 
#     @to_tuple
#     def deserialize(self, serial):
#         if not is_sequence(serial):
#             raise ListDeserializationError('Can only deserialize sequences', serial=serial)
#         for index, element in enumerate(serial):
#             if self.max_length is not None and index >= self.max_length:
#                 raise ListDeserializationError(
#                     'Too many elements (more than {})'.format(self.max_length),
#                     serial=serial,
#                 )
# 
#             try:
#                 yield self.element_sedes.deserialize(element)
#             except DeserializationError as e:
#                 raise ListDeserializationError(serial=serial, element_exception=e, index=index)


from collections.abc import Sequence
from rlp import DeserializationError
from rlp.sedes import big_endian_int, CountableList
import pytest
import rlp

@pytest.mark.parametrize(
    'value',
    (
        (),
        (1,),
        (3, 2, 1),
    ),
)
def test_evaluation_of_lazy_decode_with_simple_value_sedes(value):
    assert evaluate(rlp.decode_lazy(rlp.encode(value), big_endian_int)) == value


def test_evaluation_of_lazy_decode_with_list_sedes_and_invalid_value():
    sedes = CountableList(big_endian_int)
    value = [(), (1, 2), b'asdf', (3)]
    invalid_lazy = rlp.decode_lazy(rlp.encode(value), sedes)
    assert invalid_lazy[0] == value[0]
    assert invalid_lazy[1] == value[1]
    with pytest.raises(DeserializationError):
        invalid_lazy[2]


def test_peek():
    assert rlp.peek(rlp.encode(b''), []) == b''
    nested = rlp.encode([0, 1, [2, 3]])
    assert rlp.peek(nested, [2, 0], big_endian_int) == 2
    for index in [3, [3], [0, 0], [2, 2], [2, 1, 0]]:
        with pytest.raises(IndexError):
            rlp.peek(nested, index)

# Path: rlp/exceptions.py
# class DeserializationError(RLPException):
#     """Exception raised if deserialization fails.
# 
#     :ivar serial: the decoded RLP string that could not be deserialized
#     """
# 
#     def __init__(self, message, serial):
#         super(DeserializationError, self).__init__(message)
#         self.serial = serial
# 
# Path: rlp/sedes/big_endian_int.py
# class BigEndianInt(object):
#     def __init__(self, l=None):
#     def serialize(self, obj):
#     def deserialize(self, serial):
# 
# Path: rlp/sedes/lists.py
# class CountableList(object):
# 
#     """A sedes for lists of arbitrary length.
# 
#     :param element_sedes: when (de-)serializing a list, this sedes will be
#                           applied to all of its elements
#     :param max_length: maximum number of allowed elements, or `None` for no limit
#     """
# 
#     def __init__(self, element_sedes, max_length=None):
#         self.element_sedes = element_sedes
#         self.max_length = max_length
# 
#     @to_list
#     def serialize(self, obj):
#         if not is_sequence(obj):
#             raise ListSerializationError('Can only serialize sequences', obj)
# 
#         if self.max_length is not None and len(obj) > self.max_length:
#             raise ListSerializationError(
#                 'Too many elements ({}, allowed {})'.format(
#                     len(obj),
#                     self.max_length,
#                 ),
#                 obj=obj,
#             )
# 
#         for index, element in enumerate(obj):
#             try:
#                 yield self.element_sedes.serialize(element)
#             except SerializationError as e:
#                 raise ListSerializationError(obj=obj, element_exception=e, index=index)
# 
#     @to_tuple
#     def deserialize(self, serial):
#         if not is_sequence(serial):
#             raise ListDeserializationError('Can only deserialize sequences', serial=serial)
#         for index, element in enumerate(serial):
#             if self.max_length is not None and index >= self.max_length:
#                 raise ListDeserializationError(
#                     'Too many elements (more than {})'.format(self.max_length),
#                     serial=serial,
#                 )
# 
#             try:
#                 yield self.element_sedes.deserialize(element)
#             except DeserializationError as e:
#                 raise ListDeserializationError(serial=serial, element_exception=e, index=index)


# Path: tests/test_lazy.py
from collections.abc import Sequence
from rlp import DeserializationError
from rlp.sedes import big_endian_int, CountableList
import pytest
import rlp


@pytest.mark.parametrize(
    'value',
    (
        (),
        (1,),
        (3, 2, 1),
    ),
)
def test_evaluation_of_lazy_decode_with_simple_value_sedes(value):
    assert evaluate(rlp.decode_lazy(rlp.encode(value), big_endian_int)) == value


def test_evaluation_of_lazy_decode_with_list_sedes_and_invalid_value():
    sedes = CountableList(big_endian_int)
    value = [(), (1, 2), b'asdf', (3)]
    invalid_lazy = rlp.decode_lazy(rlp.encode(value), sedes)
    assert invalid_lazy[0] == value[0]
    assert invalid_lazy[1] == value[1]
    with pytest.raises(DeserializationError):
        invalid_lazy[2]


def test_peek():
    assert rlp.peek(rlp.encode(b''), []) == b''
    nested = rlp.encode([0, 1, [2, 3]])
    assert rlp.peek(nested, [2, 0], big_endian_int) == 2
    for index in [3, [3], [0, 0], [2, 2], [2, 1, 0]]:
        with pytest.raises(IndexError):
            rlp.peek(nested, index)

    assert rlp.peek(nested, 2, CountableList(big_endian_int)) == (2, 3)