Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(104)

Side by Side Diff: Lib/test/test_memoryio.py

Issue 20435: Discrepancy between io.StringIO and _pyio.StringIO with univeral newlines
Patch Set: Created 5 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/_pyio.py ('k') | Modules/_io/stringio.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Unit tests for memory-based file-like objects. 1 """Unit tests for memory-based file-like objects.
2 StringIO -- for unicode strings 2 StringIO -- for unicode strings
3 BytesIO -- for bytes 3 BytesIO -- for bytes
4 """ 4 """
5 5
6 import unittest 6 import unittest
7 from test import support 7 from test import support
8 8
9 import io 9 import io
10 import _pyio as pyio 10 import _pyio as pyio
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 532
533 # These are just dummy values but we nevertheless check them for fear 533 # These are just dummy values but we nevertheless check them for fear
534 # of unexpected breakage. 534 # of unexpected breakage.
535 self.assertIsNone(memio.encoding) 535 self.assertIsNone(memio.encoding)
536 self.assertIsNone(memio.errors) 536 self.assertIsNone(memio.errors)
537 self.assertFalse(memio.line_buffering) 537 self.assertFalse(memio.line_buffering)
538 538
539 def test_newline_none(self): 539 def test_newline_none(self):
540 # newline=None 540 # newline=None
541 memio = self.ioclass("a\nb\r\nc\rd", newline=None) 541 memio = self.ioclass("a\nb\r\nc\rd", newline=None)
542 #self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
542 self.assertEqual(list(memio), ["a\n", "b\n", "c\n", "d"]) 543 self.assertEqual(list(memio), ["a\n", "b\n", "c\n", "d"])
543 memio.seek(0) 544 memio.seek(0)
544 self.assertEqual(memio.read(1), "a") 545 self.assertEqual(memio.read(1), "a")
545 self.assertEqual(memio.read(2), "\nb") 546 self.assertEqual(memio.read(2), "\nb")
546 self.assertEqual(memio.read(2), "\nc") 547 self.assertEqual(memio.read(2), "\nc")
547 self.assertEqual(memio.read(1), "\n") 548 self.assertEqual(memio.read(1), "\n")
549
548 memio = self.ioclass(newline=None) 550 memio = self.ioclass(newline=None)
549 self.assertEqual(2, memio.write("a\n")) 551 self.assertEqual(2, memio.write("a\n"))
550 self.assertEqual(3, memio.write("b\r\n")) 552 self.assertEqual(3, memio.write("b\r\n"))
551 self.assertEqual(3, memio.write("c\rd")) 553 self.assertEqual(3, memio.write("c\rd"))
554 #self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
552 memio.seek(0) 555 memio.seek(0)
553 self.assertEqual(memio.read(), "a\nb\nc\nd") 556 self.assertEqual(memio.read(), "a\nb\nc\nd")
557
554 memio = self.ioclass("a\r\nb", newline=None) 558 memio = self.ioclass("a\r\nb", newline=None)
555 self.assertEqual(memio.read(3), "a\nb") 559 self.assertEqual(memio.read(3), "a\nb")
556 560
557 def test_newline_empty(self): 561 def test_newline_empty(self):
558 # newline="" 562 # newline=""
559 memio = self.ioclass("a\nb\r\nc\rd", newline="") 563 memio = self.ioclass("a\nb\r\nc\rd", newline="")
564 self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
560 self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"]) 565 self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"])
561 memio.seek(0) 566 memio.seek(0)
562 self.assertEqual(memio.read(4), "a\nb\r") 567 self.assertEqual(memio.read(4), "a\nb\r")
563 self.assertEqual(memio.read(2), "\nc") 568 self.assertEqual(memio.read(2), "\nc")
564 self.assertEqual(memio.read(1), "\r") 569 self.assertEqual(memio.read(1), "\r")
570
565 memio = self.ioclass(newline="") 571 memio = self.ioclass(newline="")
566 self.assertEqual(2, memio.write("a\n")) 572 self.assertEqual(2, memio.write("a\n"))
567 self.assertEqual(2, memio.write("b\r")) 573 self.assertEqual(2, memio.write("b\r"))
568 self.assertEqual(2, memio.write("\nc")) 574 self.assertEqual(2, memio.write("\nc"))
569 self.assertEqual(2, memio.write("\rd")) 575 self.assertEqual(2, memio.write("\rd"))
576 self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
570 memio.seek(0) 577 memio.seek(0)
571 self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"]) 578 self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"])
572 579
573 def test_newline_lf(self): 580 def test_newline_lf(self):
574 # newline="\n" 581 # newline="\n"
575 memio = self.ioclass("a\nb\r\nc\rd") 582 memio = self.ioclass("a\nb\r\nc\rd", newline="\n")
583 self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
584 self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
585
586 memio = self.ioclass(newline="\n")
587 self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
588 self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
589 memio.seek(0)
576 self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"]) 590 self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
577 591
578 def test_newline_cr(self): 592 def test_newline_cr(self):
579 # newline="\r" 593 # newline="\r"
580 memio = self.ioclass("a\nb\r\nc\rd", newline="\r") 594 memio = self.ioclass("a\nb\r\nc\rd", newline="\r")
581 self.assertEqual(memio.read(), "a\rb\r\rc\rd") 595 self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
596 self.assertEqual(memio.read(), "a\nb\r\nc\rd")
597 memio.seek(0)
598 self.assertEqual(list(memio),['a\nb\r', '\nc\r', 'd'])
599
600 memio = self.ioclass(newline="\r")
601 self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
602 self.assertEqual(memio.getvalue(), "a\rb\r\rc\rd")
582 memio.seek(0) 603 memio.seek(0)
583 self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"]) 604 self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"])
584 605
585 def test_newline_crlf(self): 606 def test_newline_crlf(self):
586 # newline="\r\n" 607 # newline="\r\n"
587 memio = self.ioclass("a\nb\r\nc\rd", newline="\r\n") 608 memio = self.ioclass("a\nb\r\nc\rd", newline="\r\n")
588 self.assertEqual(memio.read(), "a\r\nb\r\r\nc\rd") 609 self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
610 self.assertEqual(memio.read(), "a\nb\r\nc\rd")
611 memio.seek(0)
612 self.assertEqual(list(memio), ['a\nb\r\n', 'c\rd'])
613
614 memio = self.ioclass(newline="\r\n")
615 self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
616 self.assertEqual(memio.getvalue(), "a\r\nb\r\r\nc\rd")
589 memio.seek(0) 617 memio.seek(0)
590 self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"]) 618 self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"])
591 619
592 def test_issue5265(self): 620 def test_issue5265(self):
593 # StringIO can duplicate newlines in universal newlines mode 621 # StringIO can duplicate newlines in universal newlines mode
594 memio = self.ioclass("a\r\nb\r\n", newline=None) 622 memio = self.ioclass("a\r\nb\r\n", newline=None)
595 self.assertEqual(memio.read(5), "a\nb\n") 623 self.assertEqual(memio.read(5), "a\nb\n")
596 624
597 def test_newline_argument(self): 625 def test_newline_argument(self):
598 self.assertRaises(TypeError, self.ioclass, newline=b"\n") 626 self.assertRaises(TypeError, self.ioclass, newline=b"\n")
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 pass 759 pass
732 760
733 761
734 def test_main(): 762 def test_main():
735 tests = [PyBytesIOTest, PyStringIOTest, CBytesIOTest, CStringIOTest, 763 tests = [PyBytesIOTest, PyStringIOTest, CBytesIOTest, CStringIOTest,
736 PyStringIOPickleTest, CStringIOPickleTest] 764 PyStringIOPickleTest, CStringIOPickleTest]
737 support.run_unittest(*tests) 765 support.run_unittest(*tests)
738 766
739 if __name__ == '__main__': 767 if __name__ == '__main__':
740 test_main() 768 test_main()
OLDNEW
« no previous file with comments | « Lib/_pyio.py ('k') | Modules/_io/stringio.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+