Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 1 | # Tests universal newline support for both reading and parsing files. |
| 2 | import unittest |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 3 | import os |
| 4 | import sys |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 6 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 7 | if not hasattr(sys.stdin, 'newlines'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | raise support.TestSkipped( |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 9 | "This Python does not have universal newline support") |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 10 | |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 11 | FATX = 'x' * (2**14) |
| 12 | |
| 13 | DATA_TEMPLATE = [ |
| 14 | "line1=1", |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 15 | "line2='this is a very long line designed to go past any default " + |
| 16 | "buffer limits that exist in io.py but we also want to test " + |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 17 | "the uncommon case, naturally.'", |
| 18 | "def line3():pass", |
| 19 | "line4 = '%s'" % FATX, |
| 20 | ] |
| 21 | |
| 22 | DATA_LF = "\n".join(DATA_TEMPLATE) + "\n" |
| 23 | DATA_CR = "\r".join(DATA_TEMPLATE) + "\r" |
| 24 | DATA_CRLF = "\r\n".join(DATA_TEMPLATE) + "\r\n" |
| 25 | |
| 26 | # Note that DATA_MIXED also tests the ability to recognize a lone \r |
| 27 | # before end-of-file. |
| 28 | DATA_MIXED = "\n".join(DATA_TEMPLATE) + "\r" |
| 29 | DATA_SPLIT = [x + "\n" for x in DATA_TEMPLATE] |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 30 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 31 | class TestGenericUnivNewlines(unittest.TestCase): |
| 32 | # use a class variable DATA to define the data to write to the file |
| 33 | # and a class variable NEWLINE to set the expected newlines value |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 34 | READMODE = 'r' |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 35 | WRITEMODE = 'wb' |
| 36 | |
| 37 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 38 | fp = open(support.TESTFN, self.WRITEMODE) |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 39 | data = self.DATA |
| 40 | if "b" in self.WRITEMODE: |
| 41 | data = data.encode("ascii") |
| 42 | fp.write(data) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 43 | fp.close() |
| 44 | |
| 45 | def tearDown(self): |
| 46 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 47 | os.unlink(support.TESTFN) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 48 | except: |
| 49 | pass |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 50 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 51 | def test_read(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 52 | fp = open(support.TESTFN, self.READMODE) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 53 | data = fp.read() |
| 54 | self.assertEqual(data, DATA_LF) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 55 | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 56 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 57 | def test_readlines(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 58 | fp = open(support.TESTFN, self.READMODE) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 59 | data = fp.readlines() |
| 60 | self.assertEqual(data, DATA_SPLIT) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 61 | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 62 | |
| 63 | def test_readline(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 64 | fp = open(support.TESTFN, self.READMODE) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 65 | data = [] |
| 66 | d = fp.readline() |
| 67 | while d: |
| 68 | data.append(d) |
| 69 | d = fp.readline() |
| 70 | self.assertEqual(data, DATA_SPLIT) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 71 | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 72 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 73 | def test_seek(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 74 | fp = open(support.TESTFN, self.READMODE) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 75 | fp.readline() |
| 76 | pos = fp.tell() |
| 77 | data = fp.readlines() |
| 78 | self.assertEqual(data, DATA_SPLIT[1:]) |
| 79 | fp.seek(pos) |
| 80 | data = fp.readlines() |
| 81 | self.assertEqual(data, DATA_SPLIT[1:]) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 82 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 83 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 84 | class TestCRNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 85 | NEWLINE = '\r' |
| 86 | DATA = DATA_CR |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 87 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 88 | class TestLFNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 89 | NEWLINE = '\n' |
| 90 | DATA = DATA_LF |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 91 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 92 | class TestCRLFNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 93 | NEWLINE = '\r\n' |
| 94 | DATA = DATA_CRLF |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 96 | def test_tell(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 97 | fp = open(support.TESTFN, self.READMODE) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 98 | self.assertEqual(repr(fp.newlines), repr(None)) |
| 99 | data = fp.readline() |
| 100 | pos = fp.tell() |
| 101 | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
| 102 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 103 | class TestMixedNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 104 | NEWLINE = ('\r', '\n') |
| 105 | DATA = DATA_MIXED |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 106 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 107 | |
| 108 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 109 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 110 | TestCRNewlines, |
| 111 | TestLFNewlines, |
| 112 | TestCRLFNewlines, |
| 113 | TestMixedNewlines |
| 114 | ) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 115 | |
| 116 | if __name__ == '__main__': |
| 117 | test_main() |