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 |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame^] | 5 | from test import test_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'): |
| 8 | raise test_support.TestSkipped, \ |
| 9 | "This Python does not have universal newline support" |
| 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", |
| 15 | "line2='this is a very long line designed to go past the magic " + |
| 16 | "hundred character limit that is inside fileobject.c and which " + |
| 17 | "is meant to speed up the common case, but we also want to test " + |
| 18 | "the uncommon case, naturally.'", |
| 19 | "def line3():pass", |
| 20 | "line4 = '%s'" % FATX, |
| 21 | ] |
| 22 | |
| 23 | DATA_LF = "\n".join(DATA_TEMPLATE) + "\n" |
| 24 | DATA_CR = "\r".join(DATA_TEMPLATE) + "\r" |
| 25 | DATA_CRLF = "\r\n".join(DATA_TEMPLATE) + "\r\n" |
| 26 | |
| 27 | # Note that DATA_MIXED also tests the ability to recognize a lone \r |
| 28 | # before end-of-file. |
| 29 | DATA_MIXED = "\n".join(DATA_TEMPLATE) + "\r" |
| 30 | DATA_SPLIT = [x + "\n" for x in DATA_TEMPLATE] |
| 31 | del x |
| 32 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 33 | class TestGenericUnivNewlines(unittest.TestCase): |
| 34 | # use a class variable DATA to define the data to write to the file |
| 35 | # and a class variable NEWLINE to set the expected newlines value |
| 36 | READMODE = 'U' |
| 37 | WRITEMODE = 'wb' |
| 38 | |
| 39 | def setUp(self): |
| 40 | fp = open(test_support.TESTFN, self.WRITEMODE) |
| 41 | fp.write(self.DATA) |
| 42 | fp.close() |
| 43 | |
| 44 | def tearDown(self): |
| 45 | try: |
| 46 | os.unlink(test_support.TESTFN) |
| 47 | except: |
| 48 | pass |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 49 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 50 | def test_read(self): |
| 51 | fp = open(test_support.TESTFN, self.READMODE) |
| 52 | data = fp.read() |
| 53 | self.assertEqual(data, DATA_LF) |
| 54 | self.assertEqual(`fp.newlines`, `self.NEWLINE`) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 55 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 56 | def test_readlines(self): |
| 57 | fp = open(test_support.TESTFN, self.READMODE) |
| 58 | data = fp.readlines() |
| 59 | self.assertEqual(data, DATA_SPLIT) |
| 60 | self.assertEqual(`fp.newlines`, `self.NEWLINE`) |
| 61 | |
| 62 | def test_readline(self): |
| 63 | fp = open(test_support.TESTFN, self.READMODE) |
| 64 | data = [] |
| 65 | d = fp.readline() |
| 66 | while d: |
| 67 | data.append(d) |
| 68 | d = fp.readline() |
| 69 | self.assertEqual(data, DATA_SPLIT) |
| 70 | self.assertEqual(`fp.newlines`, `self.NEWLINE`) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 71 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 72 | def test_seek(self): |
| 73 | fp = open(test_support.TESTFN, self.READMODE) |
| 74 | fp.readline() |
| 75 | pos = fp.tell() |
| 76 | data = fp.readlines() |
| 77 | self.assertEqual(data, DATA_SPLIT[1:]) |
| 78 | fp.seek(pos) |
| 79 | data = fp.readlines() |
| 80 | self.assertEqual(data, DATA_SPLIT[1:]) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 81 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 82 | def test_execfile(self): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 83 | namespace = {} |
| 84 | execfile(test_support.TESTFN, namespace) |
| 85 | func = namespace['line3'] |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 86 | self.assertEqual(func.func_code.co_firstlineno, 3) |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 87 | self.assertEqual(namespace['line4'], FATX) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 88 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 89 | |
| 90 | class TestNativeNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 91 | NEWLINE = None |
| 92 | DATA = DATA_LF |
| 93 | READMODE = 'r' |
| 94 | WRITEMODE = 'w' |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 95 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 96 | class TestCRNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 97 | NEWLINE = '\r' |
| 98 | DATA = DATA_CR |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 99 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 100 | class TestLFNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 101 | NEWLINE = '\n' |
| 102 | DATA = DATA_LF |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 103 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 104 | class TestCRLFNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 105 | NEWLINE = '\r\n' |
| 106 | DATA = DATA_CRLF |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 107 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 108 | class TestMixedNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 109 | NEWLINE = ('\r', '\n') |
| 110 | DATA = DATA_MIXED |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 111 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 112 | |
| 113 | def test_main(): |
| 114 | test_support.run_unittest(TestNativeNewlines) |
| 115 | test_support.run_unittest(TestCRNewlines) |
| 116 | test_support.run_unittest(TestLFNewlines) |
| 117 | test_support.run_unittest(TestCRLFNewlines) |
| 118 | test_support.run_unittest(TestMixedNewlines) |
| 119 | |
| 120 | if __name__ == '__main__': |
| 121 | test_main() |