Skip to content

Commit 50e89be

Browse files
committed
Merge the line terminator tests back together
test_write_lineterminator_in_field was a strict superset of test_write_lineterminator -- same rows, same assertion plus one line, and the original five terminators were all in its list -- so the older test asserted nothing the newer one did not. Apply the reviewer's suggestion in place, as it was written, and keep only the cases the merged test cannot reach in a second test: every character of a multi-character terminator rather than just the last, non-members bracketing each terminator in code point order, and the empty terminator, which cannot join the first loop because lineterminator[-1] raises IndexError on ''. Verified by mutating Modules/_csv.c: the two tests catch an off-by-one on the cached maximum (c <= -> c <), a maximum that is never populated, and a dropped membership scan. They also still pass against _csv.c as it stood before the optimization.
1 parent aeb530f commit 50e89be

1 file changed

Lines changed: 3 additions & 22 deletions

File tree

Lib/test/test_csv.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -260,19 +260,6 @@ def test_write_escape(self):
260260
escapechar='\\', quoting=csv.QUOTE_MINIMAL)
261261

262262
def test_write_lineterminator(self):
263-
for lineterminator in '\r\n', '\n', '\r', '!@#', '\0':
264-
with self.subTest(lineterminator=lineterminator):
265-
with StringIO() as sio:
266-
writer = csv.writer(sio, lineterminator=lineterminator)
267-
writer.writerow(['a', 'b'])
268-
writer.writerow([1, 2])
269-
writer.writerow(['\r', '\n'])
270-
self.assertEqual(sio.getvalue(),
271-
f'a,b{lineterminator}'
272-
f'1,2{lineterminator}'
273-
f'"\r","\n"{lineterminator}')
274-
275-
def test_write_lineterminator_in_field(self):
276263
for lineterminator in ('\r\n', '\n', '\r', '!@#', '\0', '\x85',
277264
'\u2028', '\U0001f600'):
278265
with self.subTest(lineterminator=lineterminator):
@@ -289,22 +276,16 @@ def test_write_lineterminator_in_field(self):
289276
f'"a{lineterminator[-1]}b",c{lineterminator}')
290277

291278
def test_write_lineterminator_quoting(self):
292-
for lineterminator, plain in ('!@#', ' ?A'), ('\u2028', '\u2027\u2029'):
279+
for lineterminator, plain in (('!@#', ' ?A'),
280+
('\u2028', '\u2027\u2029'),
281+
('', '\0')):
293282
with self.subTest(lineterminator=lineterminator):
294283
for c in lineterminator:
295284
self._write_test([f'a{c}b', 'c'], f'"a{c}b",c',
296285
lineterminator=lineterminator)
297286
self._write_test([f'a{plain}b', 'c'], f'a{plain}b,c',
298287
lineterminator=lineterminator)
299288

300-
def test_write_empty_lineterminator(self):
301-
with StringIO() as sio:
302-
writer = csv.writer(sio, lineterminator='')
303-
writer.writerow(['a', 'b'])
304-
writer.writerow(['\0', 'c'])
305-
writer.writerow(['\r', '\n'])
306-
self.assertEqual(sio.getvalue(), 'a,b\0,c"\r","\n"')
307-
308289
def test_write_iterable(self):
309290
self._write_test(iter(['a', 1, 'p,q']), 'a,1,"p,q"')
310291
self._write_test(iter(['a', 1, None]), 'a,1,')

0 commit comments

Comments
 (0)