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

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

Issue 19105: pprint doesn't use all width
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:
View unified diff | Download patch
« no previous file with comments | « Lib/pprint.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 # indent parameter 183 # indent parameter
184 o = range(100) 184 o = range(100)
185 exp = '[ %s]' % ',\n '.join(map(str, o)) 185 exp = '[ %s]' % ',\n '.join(map(str, o))
186 for type in [list, list2]: 186 for type in [list, list2]:
187 self.assertEqual(pprint.pformat(type(o), indent=4), exp) 187 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
188 188
189 def test_nested_indentations(self): 189 def test_nested_indentations(self):
190 o1 = list(range(10)) 190 o1 = list(range(10))
191 o2 = dict(first=1, second=2, third=3) 191 o2 = dict(first=1, second=2, third=3)
192 o = [o1, o2] 192 o = [o1, o2]
193 expected = """\
194 [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
195 {'first': 1, 'second': 2, 'third': 3}]"""
196 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
193 expected = """\ 197 expected = """\
194 [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 198 [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
195 { 'first': 1, 199 { 'first': 1,
196 'second': 2, 200 'second': 2,
197 'third': 3}]""" 201 'third': 3}]"""
198 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected) 202 self.assertEqual(pprint.pformat(o, indent=4, width=41), expected)
203
204 def test_width(self):
205 expected = """\
206 [[[[[[1, 2, 3],
207 '1 2']]]],
208 {1: [1, 2, 3],
209 2: [12, 34]},
210 'abc def ghi',
211 ('ab cd ef',),
212 set2({1, 23}),
213 [[[[[1, 2, 3],
214 '1 2']]]]]"""
215 o = eval(expected)
216 self.assertEqual(pprint.pformat(o, width=15), expected)
217 self.assertEqual(pprint.pformat(o, width=16), expected)
218 self.assertEqual(pprint.pformat(o, width=25), expected)
219 self.assertEqual(pprint.pformat(o, width=14), """\
220 [[[[[[1,
221 2,
222 3],
223 '1 '
224 '2']]]],
225 {1: [1,
226 2,
227 3],
228 2: [12,
229 34]},
230 'abc def '
231 'ghi',
232 ('ab cd '
233 'ef',),
234 set2({1,
235 23}),
236 [[[[[1,
237 2,
238 3],
239 '1 '
240 '2']]]]]""")
199 241
200 def test_sorted_dict(self): 242 def test_sorted_dict(self):
201 # Starting in Python 2.5, pprint sorts dict displays by key regardless 243 # Starting in Python 2.5, pprint sorts dict displays by key regardless
202 # of how small the dictionary may be. 244 # of how small the dictionary may be.
203 # Before the change, on 32-bit Windows pformat() gave order 245 # Before the change, on 32-bit Windows pformat() gave order
204 # 'a', 'c', 'b' here, so this test failed. 246 # 'a', 'c', 'b' here, so this test failed.
205 d = {'a': 1, 'b': 1, 'c': 1} 247 d = {'a': 1, 'b': 1, 'c': 1}
206 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}") 248 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
207 self.assertEqual(pprint.pformat([d, d]), 249 self.assertEqual(pprint.pformat([d, d]),
208 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]") 250 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 '{1: 0, ' + repr(Unorderable) +': 0}') 570 '{1: 0, ' + repr(Unorderable) +': 0}')
529 571
530 # Issue 14998: TypeError on tuples with NoneTypes as dict keys. 572 # Issue 14998: TypeError on tuples with NoneTypes as dict keys.
531 keys = [(1,), (None,)] 573 keys = [(1,), (None,)]
532 self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)), 574 self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)),
533 '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id))) 575 '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id)))
534 576
535 def test_str_wrap(self): 577 def test_str_wrap(self):
536 # pprint tries to wrap strings intelligently 578 # pprint tries to wrap strings intelligently
537 fox = 'the quick brown fox jumped over a lazy dog' 579 fox = 'the quick brown fox jumped over a lazy dog'
538 self.assertEqual(pprint.pformat(fox, width=20), """\ 580 self.assertEqual(pprint.pformat(fox, width=19), """\
539 ('the quick ' 581 ('the quick brown '
540 'brown fox ' 582 'fox jumped over '
541 'jumped over a ' 583 'a lazy dog')""")
542 'lazy dog')""")
543 self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2}, 584 self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2},
544 width=26), """\ 585 width=25), """\
545 {'a': 1, 586 {'a': 1,
546 'b': 'the quick brown ' 587 'b': 'the quick brown '
547 'fox jumped over ' 588 'fox jumped over '
548 'a lazy dog', 589 'a lazy dog',
549 'c': 2}""") 590 'c': 2}""")
550 # With some special characters 591 # With some special characters
551 # - \n always triggers a new line in the pprint 592 # - \n always triggers a new line in the pprint
552 # - \t and \n are escaped 593 # - \t and \n are escaped
553 # - non-ASCII is allowed 594 # - non-ASCII is allowed
554 # - an apostrophe doesn't disrupt the pprint 595 # - an apostrophe doesn't disrupt the pprint
555 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"
556 self.assertEqual(pprint.pformat(special, width=21), """\ 597 self.assertEqual(pprint.pformat(special, width=68), repr(special))
557 ('Portons dix ' 598 self.assertEqual(pprint.pformat(special, width=31), """\
558 'bons "whiskys"\\n' 599 ('Portons dix bons "whiskys"\\n'
600 "à l'avocat goujat\\t qui "
601 'fumait au zoo')""")
602 self.assertEqual(pprint.pformat(special, width=20), """\
603 ('Portons dix bons '
604 '"whiskys"\\n'
559 "à l'avocat " 605 "à l'avocat "
560 'goujat\\t qui ' 606 'goujat\\t qui '
561 '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']]]]]""")
562 # An unwrappable string is formatted as its repr 625 # An unwrappable string is formatted as its repr
563 unwrappable = "x" * 100 626 unwrappable = "x" * 100
564 self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable )) 627 self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable ))
565 self.assertEqual(pprint.pformat(''), "''") 628 self.assertEqual(pprint.pformat(''), "''")
566 # Check that the pprint is a usable repr 629 # Check that the pprint is a usable repr
567 special *= 10 630 special *= 10
568 for width in range(3, 40): 631 for width in range(3, 40):
569 formatted = pprint.pformat(special, width=width) 632 formatted = pprint.pformat(special, width=width)
570 self.assertEqual(eval(formatted), special) 633 self.assertEqual(eval(formatted), special)
571 formatted = pprint.pformat([special] * 2, width=width) 634 formatted = pprint.pformat([special] * 2, width=width)
572 self.assertEqual(eval(formatted), [special] * 2) 635 self.assertEqual(eval(formatted), [special] * 2)
573 636
574 def test_compact(self): 637 def test_compact(self):
575 o = ([list(range(i * i)) for i in range(5)] + 638 o = ([list(range(i * i)) for i in range(5)] +
576 [list(range(i)) for i in range(6)]) 639 [list(range(i)) for i in range(6)])
577 expected = """\ 640 expected = """\
578 [[], [0], [0, 1, 2, 3], 641 [[], [0], [0, 1, 2, 3],
579 [0, 1, 2, 3, 4, 5, 6, 7, 8], 642 [0, 1, 2, 3, 4, 5, 6, 7, 8],
580 [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,
581 14, 15], 644 14, 15],
582 [], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], 645 [], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3],
583 [0, 1, 2, 3, 4]]""" 646 [0, 1, 2, 3, 4]]"""
584 self.assertEqual(pprint.pformat(o, width=48, compact=True), expected) 647 self.assertEqual(pprint.pformat(o, width=47, compact=True), expected)
648
649 def test_compact_width(self):
650 levels = 20
651 number = 10
652 o = [0] * number
653 for i in range(levels - 1):
654 o = [o]
655 for w in range(levels * 2 + 1, levels + 3 * number - 1):
656 lines = pprint.pformat(o, width=w, compact=True).splitlines()
657 maxwidth = max(map(len, lines))
658 self.assertLessEqual(maxwidth, w)
659 self.assertGreater(maxwidth, w - 3)
585 660
586 661
587 class DottedPrettyPrinter(pprint.PrettyPrinter): 662 class DottedPrettyPrinter(pprint.PrettyPrinter):
588 663
589 def format(self, object, context, maxlevels, level): 664 def format(self, object, context, maxlevels, level):
590 if isinstance(object, str): 665 if isinstance(object, str):
591 if ' ' in object: 666 if ' ' in object:
592 return repr(object), 1, 0 667 return repr(object), 1, 0
593 else: 668 else:
594 return object, 0, 0 669 return object, 0, 0
595 else: 670 else:
596 return pprint.PrettyPrinter.format( 671 return pprint.PrettyPrinter.format(
597 self, object, context, maxlevels, level) 672 self, object, context, maxlevels, level)
598 673
599 674
600 def test_main(): 675 def test_main():
601 test.support.run_unittest(QueryTestCase) 676 test.support.run_unittest(QueryTestCase)
602 677
603 678
604 if __name__ == "__main__": 679 if __name__ == "__main__":
605 test_main() 680 test_main()
OLDNEW
« no previous file with comments | « Lib/pprint.py ('k') | no next file » | no next file with comments »

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