# Path: rlp/exceptions.py
# class SerializationError(RLPException):
#     """Exception raised if serialization fails.
# 
#     :ivar obj: the object that could not be serialized
#     """
# 
#     def __init__(self, message, obj):
#         super(SerializationError, self).__init__(message)
#         self.obj = obj
# 
# Path: rlp/sedes/binary.py
# class Binary(object):
#     """A sedes object for binary data of certain length.
# 
#     :param min_length: the minimal length in bytes or `None` for no lower limit
#     :param max_length: the maximal length in bytes or `None` for no upper limit
#     :param allow_empty: if true, empty strings are considered valid even if
#                         a minimum length is required otherwise
#     """
# 
#     def __init__(self, min_length=None, max_length=None, allow_empty=False):
#         self.min_length = min_length or 0
#         if max_length is None:
#             self.max_length = float('inf')
#         else:
#             self.max_length = max_length
#         self.allow_empty = allow_empty
# 
#     @classmethod
#     def fixed_length(cls, l, allow_empty=False):
#         """Create a sedes for binary data with exactly `l` bytes."""
#         return cls(l, l, allow_empty=allow_empty)
# 
#     @classmethod
#     def is_valid_type(cls, obj):
#         return isinstance(obj, (bytes, bytearray))
# 
#     def is_valid_length(self, l):
#         return any((self.min_length <= l <= self.max_length,
#                     self.allow_empty and l == 0))
# 
#     def serialize(self, obj):
#         if not Binary.is_valid_type(obj):
#             raise SerializationError('Object is not a serializable ({})'.format(type(obj)), obj)
# 
#         if not self.is_valid_length(len(obj)):
#             raise SerializationError('Object has invalid length', obj)
# 
#         return obj
# 
#     def deserialize(self, serial):
#         if not isinstance(serial, Atomic):
#             m = 'Objects of type {} cannot be deserialized'
#             raise DeserializationError(m.format(type(serial).__name__), serial)
# 
#         if self.is_valid_length(len(serial)):
#             return serial
#         else:
#             raise DeserializationError('{} has invalid length'.format(type(serial)), serial)


import pytest
from rlp import SerializationError
from rlp.sedes import Binary


@pytest.mark.parametrize(
    'value',
    (b'ab', b'', b'a', b'xy'),
)
def test_binary_min_length_serialization_wrong_length(value):
    sedes = Binary(min_length=3)
    with pytest.raises(SerializationError):
        sedes.serialize(value)


@pytest.mark.parametrize(
    'value,expected',
    (
        (b'', b''),
        (b'ab', b'ab'),
        (b'abc', b'abc'),
    )
)
def test_binary_max_length_serialization(value, expected):
    sedes = Binary(max_length=3)
    assert sedes.serialize(value) == expected


@pytest.mark.parametrize(
    'value',
    (b'abcd', b'vwxyz', b'a' * 32),
)
def test_binary_max_length_serialization_wrong_length(value):

# Path: rlp/exceptions.py
# class SerializationError(RLPException):
#     """Exception raised if serialization fails.
# 
#     :ivar obj: the object that could not be serialized
#     """
# 
#     def __init__(self, message, obj):
#         super(SerializationError, self).__init__(message)
#         self.obj = obj
# 
# Path: rlp/sedes/binary.py
# class Binary(object):
#     """A sedes object for binary data of certain length.
# 
#     :param min_length: the minimal length in bytes or `None` for no lower limit
#     :param max_length: the maximal length in bytes or `None` for no upper limit
#     :param allow_empty: if true, empty strings are considered valid even if
#                         a minimum length is required otherwise
#     """
# 
#     def __init__(self, min_length=None, max_length=None, allow_empty=False):
#         self.min_length = min_length or 0
#         if max_length is None:
#             self.max_length = float('inf')
#         else:
#             self.max_length = max_length
#         self.allow_empty = allow_empty
# 
#     @classmethod
#     def fixed_length(cls, l, allow_empty=False):
#         """Create a sedes for binary data with exactly `l` bytes."""
#         return cls(l, l, allow_empty=allow_empty)
# 
#     @classmethod
#     def is_valid_type(cls, obj):
#         return isinstance(obj, (bytes, bytearray))
# 
#     def is_valid_length(self, l):
#         return any((self.min_length <= l <= self.max_length,
#                     self.allow_empty and l == 0))
# 
#     def serialize(self, obj):
#         if not Binary.is_valid_type(obj):
#             raise SerializationError('Object is not a serializable ({})'.format(type(obj)), obj)
# 
#         if not self.is_valid_length(len(obj)):
#             raise SerializationError('Object has invalid length', obj)
# 
#         return obj
# 
#     def deserialize(self, serial):
#         if not isinstance(serial, Atomic):
#             m = 'Objects of type {} cannot be deserialized'
#             raise DeserializationError(m.format(type(serial).__name__), serial)
# 
#         if self.is_valid_length(len(serial)):
#             return serial
#         else:
#             raise DeserializationError('{} has invalid length'.format(type(serial)), serial)


# Path: tests/test_binary_sedes.py
import pytest
from rlp import SerializationError
from rlp.sedes import Binary



@pytest.mark.parametrize(
    'value',
    (b'ab', b'', b'a', b'xy'),
)
def test_binary_min_length_serialization_wrong_length(value):
    sedes = Binary(min_length=3)
    with pytest.raises(SerializationError):
        sedes.serialize(value)


@pytest.mark.parametrize(
    'value,expected',
    (
        (b'', b''),
        (b'ab', b'ab'),
        (b'abc', b'abc'),
    )
)
def test_binary_max_length_serialization(value, expected):
    sedes = Binary(max_length=3)
    assert sedes.serialize(value) == expected


@pytest.mark.parametrize(
    'value',
    (b'abcd', b'vwxyz', b'a' * 32),
)
def test_binary_max_length_serialization_wrong_length(value):

    sedes = Binary(max_length=3)