Beispiel #1
0
 def test_readonly(self):
     buffer = self.make_buffer(b"abc")
     cursor = Cursor(buffer, readonly=True)
     self.assertFalse(cursor.writable())
     self.assertRaises(io.UnsupportedOperation, cursor.write, b"blah\n")
     self.assertRaises(io.UnsupportedOperation, cursor.writelines,
                       [b"blah\n"])
Beispiel #2
0
    def test_writelines_overflow(self):
        buffer = self.make_buffer(bytearray(8))
        cursor = Cursor(buffer)

        cursor.writelines([b"abc\n", b"def\n"])
        self.assertEqual(bytes(buffer), b"abc\ndef\n")
        self.assertEqual(cursor.seek(0), 0)
        self.assertRaises(BufferError, cursor.writelines,
                          [b"123\n", b"456\n", b"789\n"])
        self.assertEqual(bytes(buffer), b"123\n456\n")
Beispiel #3
0
 def test_tell(self):
     cursor = Cursor(self.make_buffer(b"abcdefghijkl"))
     self.assertEqual(cursor.tell(), 0)
     cursor.read(4)
     self.assertEqual(cursor.tell(), 4)
     cursor.read()
     self.assertEqual(cursor.tell(), 12)
Beispiel #4
0
 def test_seek_overflow(self):
     buffer = self.make_buffer(b"abcd")
     cursor = Cursor(buffer, readonly=True)
     self.assertEqual(cursor.seek(2), 2)
     self.assertRaises(OverflowError,
                       cursor.seek,
                       sys.maxsize - 1,
                       whence=os.SEEK_CUR)
     self.assertRaises(OverflowError,
                       cursor.seek,
                       sys.maxsize - 1,
                       whence=os.SEEK_END)
Beispiel #5
0
    def test_write_overflow(self):
        buffer = self.make_buffer(bytearray(10))
        cursor = Cursor(buffer)

        self.assertEqual(cursor.write(b"0123456789"), 10)
        self.assertEqual(cursor.write(b""), 0)
        self.assertRaises(BufferError, cursor.write, b"abc")
        cursor.seek(-2, whence=os.SEEK_END)
        self.assertRaises(BufferError, cursor.write, b"abc")
        cursor.seek(-3, whence=os.SEEK_END)
        self.assertEqual(cursor.write(b"abc"), 3)
        self.assertEqual(bytes(buffer), b"0123456abc")
Beispiel #6
0
 def test_context(self):
     buffer = self.make_buffer(b"abcd")
     with Cursor(buffer) as cursor:
         self.assertEqual(cursor.read(), b"abcd")
         self.assertFalse(cursor.closed)
     self.assertTrue(cursor.closed)
     self.assertRaises(ValueError, cursor.read)
Beispiel #7
0
 def test_read(self):
     cursor = Cursor(self.make_buffer(b"abcdefghijkl"))
     self.assertEqual(cursor.read(2), b"ab")
     self.assertEqual(cursor.read(3), b"cde")
     self.assertEqual(cursor.read(0), b"")
     self.assertEqual(cursor.read(), b"fghijkl")
     self.assertEqual(cursor.read(2), b"")
     self.assertEqual(cursor.read(), b"")
Beispiel #8
0
 def test_close(self):
     cursor = Cursor(self.make_buffer(b"abcd"))
     self.assertFalse(cursor.closed)
     cursor.close()
     self.assertTrue(cursor.closed)
     cursor.close()
     self.assertTrue(cursor.closed)
Beispiel #9
0
 def test_getvalue(self):
     buffer = self.make_buffer(b"abc")
     cursor = Cursor(buffer)
     self.assertIs(cursor.getvalue(), buffer)
     self.assertIsInstance(cursor.getvalue(), type(buffer))
Beispiel #10
0
    def test_flags(self):
        buf = self.make_buffer(b"abc")
        cursor = Cursor(buf)
        self.assertEqual(cursor.writable(), False)
        self.assertEqual(cursor.readable(), True)
        self.assertEqual(cursor.seekable(), True)
        self.assertEqual(cursor.isatty(), False)
        self.assertEqual(cursor.closed, False)

        buf = self.make_buffer(b"abc")
        cursor = Cursor(buf, readonly=True)
        self.assertEqual(cursor.writable(), False)
        self.assertEqual(cursor.readable(), True)
        self.assertEqual(cursor.seekable(), True)
        self.assertEqual(cursor.isatty(), False)
        self.assertEqual(cursor.closed, False)
Beispiel #11
0
 def test_repr_readonly(self):
     buffer = self.make_buffer(b"abcd")
     cursor = Cursor(buffer, readonly=True)
     self.assertEqual(repr(cursor), "Cursor({!r})".format(buffer))
Beispiel #12
0
 def test_interface(self):
     cursor = Cursor(self.make_buffer(b"abcd"))
     self.assertIsInstance(cursor, io.IOBase)
     self.assertIsInstance(cursor, io.BufferedIOBase)
Beispiel #13
0
    def test_write(self):
        buffer = self.make_buffer(bytearray(256))
        cursor = Cursor(buffer)

        self.assertEqual(cursor.write(b"blah."), 5)
        self.assertEqual(cursor.seek(0), 0)
        self.assertEqual(cursor.read(5), b"blah.")
        self.assertEqual(cursor.seek(0), 0)
        self.assertEqual(cursor.write(b"Hello."), 6)
        self.assertEqual(cursor.tell(), 6)
        self.assertEqual(cursor.seek(5), 5)
        self.assertEqual(cursor.tell(), 5)
        self.assertEqual(cursor.write(b" world\n\n\n"), 9)
        self.assertEqual(cursor.seek(0), 0)
        self.assertEqual(cursor.write(b"h"), 1)
        self.assertRaises(io.UnsupportedOperation, cursor.truncate, 12)
        self.assertEqual(cursor.tell(), 1)

        value = cursor.getvalue()
        self.assertIs(value, buffer)
        self.assertEqual(value[:12], self.make_buffer(b"hello world\n"))
Beispiel #14
0
 def test_repr_write(self):
     buffer = self.make_buffer(b"abcd")
     cursor = Cursor(buffer)
     self.assertEqual(repr(cursor), "Cursor({!r})".format(buffer))