blob: c4e4a3fbbb710a0cc487b5c32b0f429987027d95 [file] [log] [blame]
Jack Jansen518d2612002-04-14 20:17:18 +00001# Tests universal newline support for both reading and parsing files.
2import unittest
Jack Jansen518d2612002-04-14 20:17:18 +00003import os
4import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Jack Jansen518d2612002-04-14 20:17:18 +00006
Jack Jansen518d2612002-04-14 20:17:18 +00007if not hasattr(sys.stdin, 'newlines'):
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008 raise support.TestSkipped(
Collin Winter3add4d72007-08-29 23:37:32 +00009 "This Python does not have universal newline support")
Jack Jansen518d2612002-04-14 20:17:18 +000010
Tim Petersea572b22002-04-21 06:12:02 +000011FATX = 'x' * (2**14)
12
13DATA_TEMPLATE = [
14 "line1=1",
Guido van Rossum8358db22007-08-18 21:39:55 +000015 "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 Petersea572b22002-04-21 06:12:02 +000017 "the uncommon case, naturally.'",
18 "def line3():pass",
19 "line4 = '%s'" % FATX,
20 ]
21
22DATA_LF = "\n".join(DATA_TEMPLATE) + "\n"
23DATA_CR = "\r".join(DATA_TEMPLATE) + "\r"
24DATA_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.
28DATA_MIXED = "\n".join(DATA_TEMPLATE) + "\r"
29DATA_SPLIT = [x + "\n" for x in DATA_TEMPLATE]
Tim Petersea572b22002-04-21 06:12:02 +000030
Jack Jansen518d2612002-04-14 20:17:18 +000031class 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 Rossum8358db22007-08-18 21:39:55 +000034 READMODE = 'r'
Jack Jansen518d2612002-04-14 20:17:18 +000035 WRITEMODE = 'wb'
36
37 def setUp(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000038 fp = open(support.TESTFN, self.WRITEMODE)
Guido van Rossum9c627722007-08-27 18:31:48 +000039 data = self.DATA
40 if "b" in self.WRITEMODE:
41 data = data.encode("ascii")
42 fp.write(data)
Jack Jansen518d2612002-04-14 20:17:18 +000043 fp.close()
44
45 def tearDown(self):
46 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000047 os.unlink(support.TESTFN)
Jack Jansen518d2612002-04-14 20:17:18 +000048 except:
49 pass
Tim Peters863ac442002-04-16 01:38:40 +000050
Jack Jansen518d2612002-04-14 20:17:18 +000051 def test_read(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000052 fp = open(support.TESTFN, self.READMODE)
Jack Jansen518d2612002-04-14 20:17:18 +000053 data = fp.read()
54 self.assertEqual(data, DATA_LF)
Walter Dörwald70a6b492004-02-12 17:35:32 +000055 self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
Tim Peters863ac442002-04-16 01:38:40 +000056
Jack Jansen518d2612002-04-14 20:17:18 +000057 def test_readlines(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000058 fp = open(support.TESTFN, self.READMODE)
Jack Jansen518d2612002-04-14 20:17:18 +000059 data = fp.readlines()
60 self.assertEqual(data, DATA_SPLIT)
Walter Dörwald70a6b492004-02-12 17:35:32 +000061 self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
Jack Jansen518d2612002-04-14 20:17:18 +000062
63 def test_readline(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000064 fp = open(support.TESTFN, self.READMODE)
Jack Jansen518d2612002-04-14 20:17:18 +000065 data = []
66 d = fp.readline()
67 while d:
68 data.append(d)
69 d = fp.readline()
70 self.assertEqual(data, DATA_SPLIT)
Walter Dörwald70a6b492004-02-12 17:35:32 +000071 self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
Tim Peters863ac442002-04-16 01:38:40 +000072
Jack Jansen518d2612002-04-14 20:17:18 +000073 def test_seek(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000074 fp = open(support.TESTFN, self.READMODE)
Jack Jansen518d2612002-04-14 20:17:18 +000075 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 Peters863ac442002-04-16 01:38:40 +000082
Jack Jansen518d2612002-04-14 20:17:18 +000083
Jack Jansen518d2612002-04-14 20:17:18 +000084class TestCRNewlines(TestGenericUnivNewlines):
Tim Petersea572b22002-04-21 06:12:02 +000085 NEWLINE = '\r'
86 DATA = DATA_CR
Tim Peters863ac442002-04-16 01:38:40 +000087
Jack Jansen518d2612002-04-14 20:17:18 +000088class TestLFNewlines(TestGenericUnivNewlines):
Tim Petersea572b22002-04-21 06:12:02 +000089 NEWLINE = '\n'
90 DATA = DATA_LF
Tim Peters863ac442002-04-16 01:38:40 +000091
Jack Jansen518d2612002-04-14 20:17:18 +000092class TestCRLFNewlines(TestGenericUnivNewlines):
Tim Petersea572b22002-04-21 06:12:02 +000093 NEWLINE = '\r\n'
94 DATA = DATA_CRLF
Tim Peters863ac442002-04-16 01:38:40 +000095
Guido van Rossum8ce8a782007-11-01 19:42:39 +000096 def test_tell(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000097 fp = open(support.TESTFN, self.READMODE)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000098 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 Jansen518d2612002-04-14 20:17:18 +0000103class TestMixedNewlines(TestGenericUnivNewlines):
Tim Petersea572b22002-04-21 06:12:02 +0000104 NEWLINE = ('\r', '\n')
105 DATA = DATA_MIXED
Tim Peters863ac442002-04-16 01:38:40 +0000106
Jack Jansen518d2612002-04-14 20:17:18 +0000107
108def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000109 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000110 TestCRNewlines,
111 TestLFNewlines,
112 TestCRLFNewlines,
113 TestMixedNewlines
114 )
Jack Jansen518d2612002-04-14 20:17:18 +0000115
116if __name__ == '__main__':
117 test_main()