1# Tests universal newline support for both reading and parsing files.
2import unittest
3import os
4import sys
5from test import test_support
6
7if not hasattr(sys.stdin, 'newlines'):
8    raise unittest.SkipTest, \
9        "This Python does not have universal newline support"
10
11FATX = 'x' * (2**14)
12
13DATA_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
23DATA_LF = "\n".join(DATA_TEMPLATE) + "\n"
24DATA_CR = "\r".join(DATA_TEMPLATE) + "\r"
25DATA_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.
29DATA_MIXED = "\n".join(DATA_TEMPLATE) + "\r"
30DATA_SPLIT = [x + "\n" for x in DATA_TEMPLATE]
31del x
32
33class 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        with open(test_support.TESTFN, self.WRITEMODE) as fp:
41            fp.write(self.DATA)
42
43    def tearDown(self):
44        try:
45            os.unlink(test_support.TESTFN)
46        except:
47            pass
48
49    def test_read(self):
50        with open(test_support.TESTFN, self.READMODE) as fp:
51            data = fp.read()
52        self.assertEqual(data, DATA_LF)
53        self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
54
55    def test_readlines(self):
56        with open(test_support.TESTFN, self.READMODE) as fp:
57            data = fp.readlines()
58        self.assertEqual(data, DATA_SPLIT)
59        self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
60
61    def test_readline(self):
62        with open(test_support.TESTFN, self.READMODE) as fp:
63            data = []
64            d = fp.readline()
65            while d:
66                data.append(d)
67                d = fp.readline()
68        self.assertEqual(data, DATA_SPLIT)
69        self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
70
71    def test_seek(self):
72        with open(test_support.TESTFN, self.READMODE) as fp:
73            fp.readline()
74            pos = fp.tell()
75            data = fp.readlines()
76            self.assertEqual(data, DATA_SPLIT[1:])
77            fp.seek(pos)
78            data = fp.readlines()
79        self.assertEqual(data, DATA_SPLIT[1:])
80
81    def test_execfile(self):
82        namespace = {}
83        with test_support.check_py3k_warnings():
84            execfile(test_support.TESTFN, namespace)
85        func = namespace['line3']
86        self.assertEqual(func.func_code.co_firstlineno, 3)
87        self.assertEqual(namespace['line4'], FATX)
88
89
90class TestNativeNewlines(TestGenericUnivNewlines):
91    NEWLINE = None
92    DATA = DATA_LF
93    READMODE = 'r'
94    WRITEMODE = 'w'
95
96class TestCRNewlines(TestGenericUnivNewlines):
97    NEWLINE = '\r'
98    DATA = DATA_CR
99
100class TestLFNewlines(TestGenericUnivNewlines):
101    NEWLINE = '\n'
102    DATA = DATA_LF
103
104class TestCRLFNewlines(TestGenericUnivNewlines):
105    NEWLINE = '\r\n'
106    DATA = DATA_CRLF
107
108    def test_tell(self):
109        with open(test_support.TESTFN, self.READMODE) as fp:
110            self.assertEqual(repr(fp.newlines), repr(None))
111            data = fp.readline()
112            pos = fp.tell()
113        self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
114
115class TestMixedNewlines(TestGenericUnivNewlines):
116    NEWLINE = ('\r', '\n')
117    DATA = DATA_MIXED
118
119
120def test_main():
121    test_support.run_unittest(
122        TestNativeNewlines,
123        TestCRNewlines,
124        TestLFNewlines,
125        TestCRLFNewlines,
126        TestMixedNewlines
127     )
128
129if __name__ == '__main__':
130    test_main()
131