summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-10-16 21:06:17 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2012-10-16 21:06:17 (GMT)
commit8261f838ff40b1235180feda58dbe7eafa67586a (patch)
tree81e2574194cea2514484e773997cb5999f2141da
parentd0ad0b3ae2820eb681ae035a3e764d99b01e88da (diff)
parent0c7081aff471cca50fb068094d8cae11bf705cc6 (diff)
downloadcpython-8261f838ff40b1235180feda58dbe7eafa67586a.zip
cpython-8261f838ff40b1235180feda58dbe7eafa67586a.tar.gz
cpython-8261f838ff40b1235180feda58dbe7eafa67586a.tar.bz2
Merge for issue #15744: add tests for the writelines() method of file objects.
-rw-r--r--Lib/test/test_fileio.py22
-rw-r--r--Lib/test/test_io.py47
2 files changed, 68 insertions, 1 deletions
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
index 906952c..0847961 100644
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -10,6 +10,7 @@ from weakref import proxy
from functools import wraps
from test.support import TESTFN, check_warnings, run_unittest, make_bad_fd
+from collections import UserList
from _io import FileIO as _FileIO
@@ -68,6 +69,27 @@ class AutoFileTests(unittest.TestCase):
n = self.f.readinto(a)
self.assertEqual(array('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)
+ self.assertRaises(TypeError, self.f.writelines, "abc")
+
def test_none_args(self):
self.f.write(b"hi\nbye\nabc")
self.f.close()
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index ec0501e..a412469 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -32,7 +32,7 @@ import time
import unittest
import warnings
import weakref
-from collections import deque
+from collections import deque, UserList
from itertools import cycle, count
from test import support
@@ -1201,6 +1201,29 @@ 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)
+ self.assertRaises(TypeError, bufio.writelines, 'abc')
+
def test_destructor(self):
writer = self.MockRawIO()
bufio = self.tp(writer, 8)
@@ -2304,6 +2327,28 @@ class TextIOWrapperTest(unittest.TestCase):
reads += c
self.assertEqual(reads, "A"*127+"\nB")
+ def test_writelines(self):
+ l = ['ab', 'cd', 'ef']
+ buf = self.BytesIO()
+ txt = self.TextIOWrapper(buf)
+ txt.writelines(l)
+ txt.flush()
+ self.assertEqual(buf.getvalue(), b'abcdef')
+
+ def test_writelines_userlist(self):
+ l = UserList(['ab', 'cd', 'ef'])
+ buf = self.BytesIO()
+ txt = self.TextIOWrapper(buf)
+ txt.writelines(l)
+ txt.flush()
+ self.assertEqual(buf.getvalue(), b'abcdef')
+
+ def test_writelines_error(self):
+ txt = self.TextIOWrapper(self.BytesIO())
+ self.assertRaises(TypeError, txt.writelines, [1, 2, 3])
+ self.assertRaises(TypeError, txt.writelines, None)
+ self.assertRaises(TypeError, txt.writelines, b'abc')
+
def test_issue1395_1(self):
txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")