summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/test/testmock
diff options
context:
space:
mode:
authorTony Flury <anthony.flury@btinternet.com>2018-09-12 22:21:16 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2018-09-12 22:21:16 (GMT)
commit2087023fdec2c89070bd14f384a3c308c548a94a (patch)
treeadc1cfdebd6ed15525044cb09d83188d511831c9 /Lib/unittest/test/testmock
parentc7042224b8a67748f125c22836862483f81a87a6 (diff)
downloadcpython-2087023fdec2c89070bd14f384a3c308c548a94a.zip
cpython-2087023fdec2c89070bd14f384a3c308c548a94a.tar.gz
cpython-2087023fdec2c89070bd14f384a3c308c548a94a.tar.bz2
bpo-32933: Implement __iter__ method on mock_open() (GH-5974)
Diffstat (limited to 'Lib/unittest/test/testmock')
-rw-r--r--Lib/unittest/test/testmock/testmock.py10
-rw-r--r--Lib/unittest/test/testmock/testwith.py15
2 files changed, 25 insertions, 0 deletions
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index b64c866..c7bfa27 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1450,6 +1450,16 @@ class MockTest(unittest.TestCase):
f2_data = f2.read()
self.assertEqual(f1_data, f2_data)
+ def test_mock_open_dunder_iter_issue(self):
+ # Test dunder_iter method generates the expected result and
+ # consumes the iterator.
+ mocked_open = mock.mock_open(read_data='Remarkable\nNorwegian Blue')
+ f1 = mocked_open('a-name')
+ lines = [line for line in f1]
+ self.assertEqual(lines[0], 'Remarkable\n')
+ self.assertEqual(lines[1], 'Norwegian Blue')
+ self.assertEqual(list(f1), [])
+
def test_mock_open_write(self):
# Test exception in file writing write()
mock_namedtemp = mock.mock_open(mock.MagicMock(name='JLV'))
diff --git a/Lib/unittest/test/testmock/testwith.py b/Lib/unittest/test/testmock/testwith.py
index a7bee73..43b36a1 100644
--- a/Lib/unittest/test/testmock/testwith.py
+++ b/Lib/unittest/test/testmock/testwith.py
@@ -188,6 +188,7 @@ class TestMockOpen(unittest.TestCase):
def test_readline_data(self):
# Check that readline will return all the lines from the fake file
+ # And that once fully consumed, readline will return an empty string.
mock = mock_open(read_data='foo\nbar\nbaz\n')
with patch('%s.open' % __name__, mock, create=True):
h = open('bar')
@@ -197,6 +198,7 @@ class TestMockOpen(unittest.TestCase):
self.assertEqual(line1, 'foo\n')
self.assertEqual(line2, 'bar\n')
self.assertEqual(line3, 'baz\n')
+ self.assertEqual(h.readline(), '')
# Check that we properly emulate a file that doesn't end in a newline
mock = mock_open(read_data='foo')
@@ -204,6 +206,19 @@ class TestMockOpen(unittest.TestCase):
h = open('bar')
result = h.readline()
self.assertEqual(result, 'foo')
+ self.assertEqual(h.readline(), '')
+
+
+ def test_dunder_iter_data(self):
+ # Check that dunder_iter will return all the lines from the fake file.
+ mock = mock_open(read_data='foo\nbar\nbaz\n')
+ with patch('%s.open' % __name__, mock, create=True):
+ h = open('bar')
+ lines = [l for l in h]
+ self.assertEqual(lines[0], 'foo\n')
+ self.assertEqual(lines[1], 'bar\n')
+ self.assertEqual(lines[2], 'baz\n')
+ self.assertEqual(h.readline(), '')
def test_readlines_data(self):