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

Delta Between Two Patch Sets: Lib/test/test_pprint.py

Issue 19105: pprint doesn't use all width
Left Patch Set: Created 6 years, 3 months ago
Right Patch Set: Created 5 years, 1 month 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/pprint.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 2
3 import pprint 3 import pprint
4 import test.support 4 import test.support
5 import unittest 5 import unittest
6 import test.test_set 6 import test.test_set
7 import random 7 import random
8 import collections 8 import collections
9 import itertools 9 import itertools
10 10
(...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 '{1: 0, ' + repr(Unorderable) +': 0}') 570 '{1: 0, ' + repr(Unorderable) +': 0}')
571 571
572 # Issue 14998: TypeError on tuples with NoneTypes as dict keys. 572 # Issue 14998: TypeError on tuples with NoneTypes as dict keys.
573 keys = [(1,), (None,)] 573 keys = [(1,), (None,)]
574 self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)), 574 self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)),
575 '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id))) 575 '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id)))
576 576
577 def test_str_wrap(self): 577 def test_str_wrap(self):
578 # pprint tries to wrap strings intelligently 578 # pprint tries to wrap strings intelligently
579 fox = 'the quick brown fox jumped over a lazy dog' 579 fox = 'the quick brown fox jumped over a lazy dog'
580 self.assertEqual(pprint.pformat(fox, width=20), """\ 580 self.assertEqual(pprint.pformat(fox, width=19), """\
581 'the quick brown ' 581 ('the quick brown '
582 'fox jumped over a ' 582 'fox jumped over '
583 'lazy dog'""") 583 'a lazy dog')""")
584 self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2}, 584 self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2},
585 width=25), """\ 585 width=25), """\
586 {'a': 1, 586 {'a': 1,
587 'b': 'the quick brown ' 587 'b': 'the quick brown '
588 'fox jumped over ' 588 'fox jumped over '
589 'a lazy dog', 589 'a lazy dog',
590 'c': 2}""") 590 'c': 2}""")
591 # With some special characters 591 # With some special characters
592 # - \n always triggers a new line in the pprint 592 # - \n always triggers a new line in the pprint
593 # - \t and \n are escaped 593 # - \t and \n are escaped
594 # - non-ASCII is allowed 594 # - non-ASCII is allowed
595 # - an apostrophe doesn't disrupt the pprint 595 # - an apostrophe doesn't disrupt the pprint
596 special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo" 596 special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo"
597 self.assertEqual(pprint.pformat(special, width=68), repr(special))
598 self.assertEqual(pprint.pformat(special, width=31), """\
599 ('Portons dix bons "whiskys"\\n'
600 "à l'avocat goujat\\t qui "
601 'fumait au zoo')""")
597 self.assertEqual(pprint.pformat(special, width=20), """\ 602 self.assertEqual(pprint.pformat(special, width=20), """\
598 'Portons dix bons ' 603 ('Portons dix bons '
599 '"whiskys"\\n' 604 '"whiskys"\\n'
600 "à l'avocat " 605 "à l'avocat "
601 'goujat\\t qui ' 606 'goujat\\t qui '
602 'fumait au zoo'""") 607 'fumait au zoo')""")
608 self.assertEqual(pprint.pformat([[[[[special]]]]], width=35), """\
609 [[[[['Portons dix bons "whiskys"\\n'
610 "à l'avocat goujat\\t qui "
611 'fumait au zoo']]]]]""")
612 self.assertEqual(pprint.pformat([[[[[special]]]]], width=25), """\
613 [[[[['Portons dix bons '
614 '"whiskys"\\n'
615 "à l'avocat "
616 'goujat\\t qui '
617 'fumait au zoo']]]]]""")
618 self.assertEqual(pprint.pformat([[[[[special]]]]], width=23), """\
619 [[[[['Portons dix '
620 'bons "whiskys"\\n'
621 "à l'avocat "
622 'goujat\\t qui '
623 'fumait au '
624 'zoo']]]]]""")
603 # An unwrappable string is formatted as its repr 625 # An unwrappable string is formatted as its repr
604 unwrappable = "x" * 100 626 unwrappable = "x" * 100
605 self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable )) 627 self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable ))
606 self.assertEqual(pprint.pformat(''), "''") 628 self.assertEqual(pprint.pformat(''), "''")
607 # Check that the pprint is a usable repr 629 # Check that the pprint is a usable repr
608 special *= 10 630 special *= 10
609 for width in range(3, 40): 631 for width in range(3, 40):
610 formatted = pprint.pformat(special, width=width) 632 formatted = pprint.pformat(special, width=width)
611 self.assertEqual(eval("(" + formatted + ")"), special) 633 self.assertEqual(eval(formatted), special)
634 formatted = pprint.pformat([special] * 2, width=width)
635 self.assertEqual(eval(formatted), [special] * 2)
612 636
613 def test_compact(self): 637 def test_compact(self):
614 o = ([list(range(i * i)) for i in range(5)] + 638 o = ([list(range(i * i)) for i in range(5)] +
615 [list(range(i)) for i in range(6)]) 639 [list(range(i)) for i in range(6)])
616 expected = """\ 640 expected = """\
617 [[], [0], [0, 1, 2, 3], 641 [[], [0], [0, 1, 2, 3],
618 [0, 1, 2, 3, 4, 5, 6, 7, 8], 642 [0, 1, 2, 3, 4, 5, 6, 7, 8],
619 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 643 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
620 14, 15], 644 14, 15],
621 [], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], 645 [], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3],
(...skipping 25 matching lines...) Expand all
647 return pprint.PrettyPrinter.format( 671 return pprint.PrettyPrinter.format(
648 self, object, context, maxlevels, level) 672 self, object, context, maxlevels, level)
649 673
650 674
651 def test_main(): 675 def test_main():
652 test.support.run_unittest(QueryTestCase) 676 test.support.run_unittest(QueryTestCase)
653 677
654 678
655 if __name__ == "__main__": 679 if __name__ == "__main__":
656 test_main() 680 test_main()
LEFTRIGHT

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