ยปCore Development>Code coverage>Lib/test/test_univnewlines.py

Python code coverage for Lib/test/test_univnewlines.py

#countcontent
1n/a# Tests universal newline support for both reading and parsing files.
2n/aimport io
3n/aimport _pyio as pyio
4n/aimport unittest
5n/aimport os
6n/aimport sys
7n/afrom test import support
8n/a
9n/aif not hasattr(sys.stdin, 'newlines'):
10n/a raise unittest.SkipTest(
11n/a "This Python does not have universal newline support")
12n/a
13n/aFATX = 'x' * (2**14)
14n/a
15n/aDATA_TEMPLATE = [
16n/a "line1=1",
17n/a "line2='this is a very long line designed to go past any default " +
18n/a "buffer limits that exist in io.py but we also want to test " +
19n/a "the uncommon case, naturally.'",
20n/a "def line3():pass",
21n/a "line4 = '%s'" % FATX,
22n/a ]
23n/a
24n/aDATA_LF = "\n".join(DATA_TEMPLATE) + "\n"
25n/aDATA_CR = "\r".join(DATA_TEMPLATE) + "\r"
26n/aDATA_CRLF = "\r\n".join(DATA_TEMPLATE) + "\r\n"
27n/a
28n/a# Note that DATA_MIXED also tests the ability to recognize a lone \r
29n/a# before end-of-file.
30n/aDATA_MIXED = "\n".join(DATA_TEMPLATE) + "\r"
31n/aDATA_SPLIT = [x + "\n" for x in DATA_TEMPLATE]
32n/a
33n/aclass CTest:
34n/a open = io.open
35n/a
36n/aclass PyTest:
37n/a open = staticmethod(pyio.open)
38n/a
39n/aclass TestGenericUnivNewlines:
40n/a # use a class variable DATA to define the data to write to the file
41n/a # and a class variable NEWLINE to set the expected newlines value
42n/a READMODE = 'r'
43n/a WRITEMODE = 'wb'
44n/a
45n/a def setUp(self):
46n/a data = self.DATA
47n/a if "b" in self.WRITEMODE:
48n/a data = data.encode("ascii")
49n/a with self.open(support.TESTFN, self.WRITEMODE) as fp:
50n/a fp.write(data)
51n/a
52n/a def tearDown(self):
53n/a try:
54n/a os.unlink(support.TESTFN)
55n/a except:
56n/a pass
57n/a
58n/a def test_read(self):
59n/a with self.open(support.TESTFN, self.READMODE) as fp:
60n/a data = fp.read()
61n/a self.assertEqual(data, DATA_LF)
62n/a self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
63n/a
64n/a def test_readlines(self):
65n/a with self.open(support.TESTFN, self.READMODE) as fp:
66n/a data = fp.readlines()
67n/a self.assertEqual(data, DATA_SPLIT)
68n/a self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
69n/a
70n/a def test_readline(self):
71n/a with self.open(support.TESTFN, self.READMODE) as fp:
72n/a data = []
73n/a d = fp.readline()
74n/a while d:
75n/a data.append(d)
76n/a d = fp.readline()
77n/a self.assertEqual(data, DATA_SPLIT)
78n/a self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
79n/a
80n/a def test_seek(self):
81n/a with self.open(support.TESTFN, self.READMODE) as fp:
82n/a fp.readline()
83n/a pos = fp.tell()
84n/a data = fp.readlines()
85n/a self.assertEqual(data, DATA_SPLIT[1:])
86n/a fp.seek(pos)
87n/a data = fp.readlines()
88n/a self.assertEqual(data, DATA_SPLIT[1:])
89n/a
90n/a
91n/aclass TestCRNewlines(TestGenericUnivNewlines):
92n/a NEWLINE = '\r'
93n/a DATA = DATA_CR
94n/aclass CTestCRNewlines(CTest, TestCRNewlines, unittest.TestCase): pass
95n/aclass PyTestCRNewlines(PyTest, TestCRNewlines, unittest.TestCase): pass
96n/a
97n/aclass TestLFNewlines(TestGenericUnivNewlines):
98n/a NEWLINE = '\n'
99n/a DATA = DATA_LF
100n/aclass CTestLFNewlines(CTest, TestLFNewlines, unittest.TestCase): pass
101n/aclass PyTestLFNewlines(PyTest, TestLFNewlines, unittest.TestCase): pass
102n/a
103n/aclass TestCRLFNewlines(TestGenericUnivNewlines):
104n/a NEWLINE = '\r\n'
105n/a DATA = DATA_CRLF
106n/a
107n/a def test_tell(self):
108n/a with self.open(support.TESTFN, self.READMODE) as fp:
109n/a self.assertEqual(repr(fp.newlines), repr(None))
110n/a data = fp.readline()
111n/a pos = fp.tell()
112n/a self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
113n/aclass CTestCRLFNewlines(CTest, TestCRLFNewlines, unittest.TestCase): pass
114n/aclass PyTestCRLFNewlines(PyTest, TestCRLFNewlines, unittest.TestCase): pass
115n/a
116n/aclass TestMixedNewlines(TestGenericUnivNewlines):
117n/a NEWLINE = ('\r', '\n')
118n/a DATA = DATA_MIXED
119n/aclass CTestMixedNewlines(CTest, TestMixedNewlines, unittest.TestCase): pass
120n/aclass PyTestMixedNewlines(PyTest, TestMixedNewlines, unittest.TestCase): pass
121n/a
122n/aif __name__ == '__main__':
123n/a unittest.main()