Exemplo n.º 1
0
    def setUp(self):
        data = b"abcdefghijklmnopqrstuvwxyz"
        bytes_io = BytesIO(data)

        self.bytes_io = bytes_io
        self.isw = IStreamWrapper(bytes_io, len(data))
Exemplo n.º 2
0
class IStreamWrapperTestCase(TestCase):
    def setUp(self):
        data = b"abcdefghijklmnopqrstuvwxyz"
        bytes_io = BytesIO(data)

        self.bytes_io = bytes_io
        self.isw = IStreamWrapper(bytes_io, len(data))
    # end def setUp

    def test__init__(self):
        ae = self.assertEqual

        ae(self.isw.size, 26)
        ae(self.isw._stream, self.bytes_io)
    # end def test__init__

    def test_seek(self):
        ae = self.assertEqual
        ar = self.assertRaises
        isw = self.isw

        ae(isw.seek(10, SEEK_SET), 10)
        ae(isw._stream.tell(), 10)
        ar(ValueError, isw.seek, -10, SEEK_SET)

        isw.seek(3, SEEK_SET)
        ae(isw.seek(5, SEEK_CUR), 8)
        ae(isw._stream.tell(), 8)

        ae(isw.seek(-2, SEEK_CUR), 6)
        ae(isw._stream.tell(), 6)

        ae(isw.seek(-3, SEEK_END), 23)
        ae(isw._stream.tell(), 23)

        ae(isw.seek(3, SEEK_END), 29)
        ae(isw._stream.tell(), 29)
    # end def test_seek

    def test_tell(self):
        ae = self.assertEqual
        isw = self.isw
        bytes_io = self.bytes_io

        bytes_io.seek(0, SEEK_SET)
        ae(isw.tell(), 0)

        bytes_io.seek(2, SEEK_SET)
        ae(isw.tell(), 2)
    # end def test_tell

    def test_read(self):
        ae = self.assertEqual

        self.isw.seek(0, SEEK_SET)
        ae(self.isw.read(0), b"")
        ae(self.isw.read(1), b"a")
        ae(self.isw.read(2), b"bc")
        ae(self.isw.read(), b"defghijklmnopqrstuvwxyz")

        self.isw.seek(-3, SEEK_END)
        ae(self.isw.read(5), b"xyz")

        self.isw.seek(30, SEEK_SET)
        ae(self.isw.read(), b"")
    # end def test_read

    def test_readall(self):
        ae = self.assertEqual

        self.isw.seek(0, SEEK_SET)
        ae(self.isw.readall(), b"abcdefghijklmnopqrstuvwxyz")
        self.isw.seek(3, SEEK_SET)
        ae(self.isw.readall(), b"defghijklmnopqrstuvwxyz")
    # end def test_readall

    def test_readinto(self):
        ae = self.assertEqual
        isw = self.isw

        barray0 = bytearray(5)
        barray1 = bytearray(10)
        barray2 = bytearray(26)
        barray3 = bytearray(1)

        isw.seek(-12, SEEK_END)
        retval0 = isw.readinto(barray0)
        retval1 = isw.readinto(barray1)

        isw.seek(0, SEEK_SET)
        retval2 = isw.readinto(barray2)

        isw.seek(30, SEEK_SET)
        retval3 = isw.readinto(barray3)

        ae(retval0, 5)
        ae(retval1, 7)
        ae(retval2, 26)
        ae(retval3, 0)

        ae(barray0, b"opqrs")
        ae(barray1, b"tuvwxyz\x00\x00\x00")
        ae(barray2, b"abcdefghijklmnopqrstuvwxyz")
        ae(barray3, b"\x00")