Kaydet (Commit) 780145da authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #20498: Fixed io.StringIO tests for newline='\n'. Added new tests.

......@@ -536,6 +536,17 @@ class TextIOTestMixin:
self.assertIsNone(memio.errors)
self.assertFalse(memio.line_buffering)
def test_newline_default(self):
memio = self.ioclass("a\nb\r\nc\rd")
self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
memio = self.ioclass()
self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
memio.seek(0)
self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
def test_newline_none(self):
# newline=None
memio = self.ioclass("a\nb\r\nc\rd", newline=None)
......@@ -545,6 +556,8 @@ class TextIOTestMixin:
self.assertEqual(memio.read(2), "\nb")
self.assertEqual(memio.read(2), "\nc")
self.assertEqual(memio.read(1), "\n")
self.assertEqual(memio.getvalue(), "a\nb\nc\nd")
memio = self.ioclass(newline=None)
self.assertEqual(2, memio.write("a\n"))
self.assertEqual(3, memio.write("b\r\n"))
......@@ -552,6 +565,7 @@ class TextIOTestMixin:
memio.seek(0)
self.assertEqual(memio.read(), "a\nb\nc\nd")
self.assertEqual(memio.getvalue(), "a\nb\nc\nd")
memio = self.ioclass("a\r\nb", newline=None)
self.assertEqual(memio.read(3), "a\nb")
......@@ -564,6 +578,7 @@ class TextIOTestMixin:
self.assertEqual(memio.read(2), "\nc")
self.assertEqual(memio.read(1), "\r")
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
memio = self.ioclass(newline="")
self.assertEqual(2, memio.write("a\n"))
self.assertEqual(2, memio.write("b\r"))
......@@ -571,11 +586,19 @@ class TextIOTestMixin:
self.assertEqual(2, memio.write("\rd"))
memio.seek(0)
self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"])
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
def test_newline_lf(self):
# newline="\n"
memio = self.ioclass("a\nb\r\nc\rd")
memio = self.ioclass("a\nb\r\nc\rd", newline="\n")
self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
memio = self.ioclass(newline="\n")
self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
memio.seek(0)
self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
def test_newline_cr(self):
# newline="\r"
......@@ -583,6 +606,12 @@ class TextIOTestMixin:
self.assertEqual(memio.read(), "a\rb\r\rc\rd")
memio.seek(0)
self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"])
self.assertEqual(memio.getvalue(), "a\rb\r\rc\rd")
memio = self.ioclass(newline="\r")
self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
memio.seek(0)
self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"])
memio.seek(0)
self.assertEqual(memio.readlines(), ["a\r", "b\r", "\r", "c\r", "d"])
self.assertEqual(memio.getvalue(), "a\rb\r\rc\rd")
......@@ -597,6 +626,12 @@ class TextIOTestMixin:
self.assertEqual(memio.readlines(), ["a\r\n", "b\r\r\n", "c\rd"])
self.assertEqual(memio.getvalue(), "a\r\nb\r\r\nc\rd")
memio = self.ioclass(newline="\r\n")
self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
memio.seek(0)
self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"])
self.assertEqual(memio.getvalue(), "a\r\nb\r\r\nc\rd")
def test_issue5265(self):
# StringIO can duplicate newlines in universal newlines mode
memio = self.ioclass("a\r\nb\r\n", newline=None)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment