]> granicus.if.org Git - python/commitdiff
Add tests for the writelines() method of file objects.
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 16 Oct 2012 20:57:11 +0000 (22:57 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 16 Oct 2012 20:57:11 +0000 (22:57 +0200)
Original patch by Felipe Cruz.

Lib/test/test_fileio.py
Lib/test/test_io.py

index c6863d6e7c1e6e2753ce0f757f0843e0340d2c1f..71ec44c31e74a03cf46e76f2ec5571a85592c019 100644 (file)
@@ -9,6 +9,7 @@ import unittest
 from array import array
 from weakref import proxy
 from functools import wraps
+from UserList import UserList
 
 from test.test_support import TESTFN, check_warnings, run_unittest, make_bad_fd
 from test.test_support import py3k_bytes as bytes
@@ -71,6 +72,26 @@ class AutoFileTests(unittest.TestCase):
         n = self.f.readinto(a)
         self.assertEqual(array(b'b', [1, 2]), a[:n])
 
+    def testWritelinesList(self):
+        l = [b'123', b'456']
+        self.f.writelines(l)
+        self.f.close()
+        self.f = _FileIO(TESTFN, 'rb')
+        buf = self.f.read()
+        self.assertEqual(buf, b'123456')
+
+    def testWritelinesUserList(self):
+        l = UserList([b'123', b'456'])
+        self.f.writelines(l)
+        self.f.close()
+        self.f = _FileIO(TESTFN, 'rb')
+        buf = self.f.read()
+        self.assertEqual(buf, b'123456')
+
+    def testWritelinesError(self):
+        self.assertRaises(TypeError, self.f.writelines, [1, 2, 3])
+        self.assertRaises(TypeError, self.f.writelines, None)
+
     def test_none_args(self):
         self.f.write(b"hi\nbye\nabc")
         self.f.close()
index e4507e444fc7d387bc6e3228f1db79661bf9f1c6..a5d87b9d1c5e86a16449ae9b10724e2bd15d4b34 100644 (file)
@@ -34,6 +34,7 @@ import signal
 import errno
 from itertools import cycle, count
 from collections import deque
+from UserList import UserList
 from test import test_support as support
 
 import codecs
@@ -1126,6 +1127,28 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
         bufio.flush()
         self.assertEqual(b"abc", writer._write_stack[0])
 
+    def test_writelines(self):
+        l = [b'ab', b'cd', b'ef']
+        writer = self.MockRawIO()
+        bufio = self.tp(writer, 8)
+        bufio.writelines(l)
+        bufio.flush()
+        self.assertEqual(b''.join(writer._write_stack), b'abcdef')
+
+    def test_writelines_userlist(self):
+        l = UserList([b'ab', b'cd', b'ef'])
+        writer = self.MockRawIO()
+        bufio = self.tp(writer, 8)
+        bufio.writelines(l)
+        bufio.flush()
+        self.assertEqual(b''.join(writer._write_stack), b'abcdef')
+
+    def test_writelines_error(self):
+        writer = self.MockRawIO()
+        bufio = self.tp(writer, 8)
+        self.assertRaises(TypeError, bufio.writelines, [1, 2, 3])
+        self.assertRaises(TypeError, bufio.writelines, None)
+
     def test_destructor(self):
         writer = self.MockRawIO()
         bufio = self.tp(writer, 8)