1 # Tests universal newline support for both reading and parsing files.
3 # NOTE: this file tests the new `io` library backported from Python 3.x.
4 # Similar tests for the builtin file object can be found in test_univnewlines2k.py.
6 from __future__
import print_function
7 from __future__
import unicode_literals
14 from test
import test_support
as support
16 if not hasattr(sys
.stdin
, 'newlines'):
17 raise unittest
.SkipTest(
18 "This Python does not have universal newline support")
24 "line2='this is a very long line designed to go past any default " +
25 "buffer limits that exist in io.py but we also want to test " +
26 "the uncommon case, naturally.'",
28 "line4 = '%s'" % FATX
,
31 DATA_LF
= "\n".join(DATA_TEMPLATE
) + "\n"
32 DATA_CR
= "\r".join(DATA_TEMPLATE
) + "\r"
33 DATA_CRLF
= "\r\n".join(DATA_TEMPLATE
) + "\r\n"
35 # Note that DATA_MIXED also tests the ability to recognize a lone \r
37 DATA_MIXED
= "\n".join(DATA_TEMPLATE
) + "\r"
38 DATA_SPLIT
= [x
+ "\n" for x
in DATA_TEMPLATE
]
40 class TestGenericUnivNewlines(unittest
.TestCase
):
41 # use a class variable DATA to define the data to write to the file
42 # and a class variable NEWLINE to set the expected newlines value
48 if "b" in self
.WRITEMODE
:
49 data
= data
.encode("ascii")
50 with self
.open(support
.TESTFN
, self
.WRITEMODE
) as fp
:
55 os
.unlink(support
.TESTFN
)
60 with self
.open(support
.TESTFN
, self
.READMODE
) as fp
:
62 self
.assertEqual(data
, DATA_LF
)
63 self
.assertEqual(set(fp
.newlines
), set(self
.NEWLINE
))
65 def test_readlines(self
):
66 with self
.open(support
.TESTFN
, self
.READMODE
) as fp
:
68 self
.assertEqual(data
, DATA_SPLIT
)
69 self
.assertEqual(set(fp
.newlines
), set(self
.NEWLINE
))
71 def test_readline(self
):
72 with self
.open(support
.TESTFN
, self
.READMODE
) as fp
:
78 self
.assertEqual(data
, DATA_SPLIT
)
79 self
.assertEqual(set(fp
.newlines
), set(self
.NEWLINE
))
82 with self
.open(support
.TESTFN
, self
.READMODE
) as fp
:
86 self
.assertEqual(data
, DATA_SPLIT
[1:])
89 self
.assertEqual(data
, DATA_SPLIT
[1:])
92 class TestCRNewlines(TestGenericUnivNewlines
):
96 class TestLFNewlines(TestGenericUnivNewlines
):
100 class TestCRLFNewlines(TestGenericUnivNewlines
):
105 with self
.open(support
.TESTFN
, self
.READMODE
) as fp
:
106 self
.assertEqual(repr(fp
.newlines
), repr(None))
109 self
.assertEqual(repr(fp
.newlines
), repr(self
.NEWLINE
))
111 class TestMixedNewlines(TestGenericUnivNewlines
):
112 NEWLINE
= ('\r', '\n')
117 base_tests
= (TestCRNewlines
,
122 # Test the C and Python implementations.
123 for test
in base_tests
:
126 CTest
.__name
__ = str("C" + test
.__name
__)
128 open = staticmethod(pyio
.open)
129 PyTest
.__name
__ = str("Py" + test
.__name
__)
132 support
.run_unittest(*tests
)
134 if __name__
== '__main__':