move sections
[python/dscho.git] / Lib / test / test_univnewlines2k.py
blob4eca31d8ba41fc38091e85b1280178a9e037614a
1 # Tests universal newline support for both reading and parsing files.
2 import unittest
3 import os
4 import sys
5 from test import test_support
7 if not hasattr(sys.stdin, 'newlines'):
8 raise unittest.SkipTest, \
9 "This Python does not have universal newline support"
11 FATX = 'x' * (2**14)
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,
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"
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
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'
39 def setUp(self):
40 with open(test_support.TESTFN, self.WRITEMODE) as fp:
41 fp.write(self.DATA)
43 def tearDown(self):
44 try:
45 os.unlink(test_support.TESTFN)
46 except:
47 pass
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))
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))
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))
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:])
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)
90 class TestNativeNewlines(TestGenericUnivNewlines):
91 NEWLINE = None
92 DATA = DATA_LF
93 READMODE = 'r'
94 WRITEMODE = 'w'
96 class TestCRNewlines(TestGenericUnivNewlines):
97 NEWLINE = '\r'
98 DATA = DATA_CR
100 class TestLFNewlines(TestGenericUnivNewlines):
101 NEWLINE = '\n'
102 DATA = DATA_LF
104 class TestCRLFNewlines(TestGenericUnivNewlines):
105 NEWLINE = '\r\n'
106 DATA = DATA_CRLF
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))
115 class TestMixedNewlines(TestGenericUnivNewlines):
116 NEWLINE = ('\r', '\n')
117 DATA = DATA_MIXED
120 def test_main():
121 test_support.run_unittest(
122 TestNativeNewlines,
123 TestCRNewlines,
124 TestLFNewlines,
125 TestCRLFNewlines,
126 TestMixedNewlines
129 if __name__ == '__main__':
130 test_main()