From 131a489903cd03706c3cc571b94ea702e0f29c2e Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Tue, 16 Oct 2012 22:57:11 +0200 Subject: Add tests for the writelines() method of file objects. Original patch by Felipe Cruz. --- Lib/test/test_fileio.py | 22 ++++++++++++++++++++++ Lib/test/test_io.py | 25 ++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 5504ea3..b5bd8d4 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -9,6 +9,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 @@ -67,6 +68,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 0c3e0b8..6fc3c73 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -32,7 +32,7 @@ import errno import warnings import pickle from itertools import cycle, count -from collections import deque +from collections import deque, UserList from test import support import codecs @@ -1177,6 +1177,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) -- cgit v0.12