From 7b1f94520dbacd351940f337dc729883a5e6e97d Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 14 Feb 2015 10:55:19 +0200 Subject: Issue #19105: pprint now more efficiently uses free space at the right. --- Lib/test/test_pprint.py | 97 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 86 insertions(+), 11 deletions(-) (limited to 'Lib/test/test_pprint.py') diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index ad6a7a144a..c0568808a0 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -191,11 +191,53 @@ class QueryTestCase(unittest.TestCase): o2 = dict(first=1, second=2, third=3) o = [o1, o2] expected = """\ +[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + {'first': 1, 'second': 2, 'third': 3}]""" + self.assertEqual(pprint.pformat(o, indent=4, width=42), expected) + expected = """\ [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], { 'first': 1, 'second': 2, 'third': 3}]""" - self.assertEqual(pprint.pformat(o, indent=4, width=42), expected) + self.assertEqual(pprint.pformat(o, indent=4, width=41), expected) + + def test_width(self): + expected = """\ +[[[[[[1, 2, 3], + '1 2']]]], + {1: [1, 2, 3], + 2: [12, 34]}, + 'abc def ghi', + ('ab cd ef',), + set2({1, 23}), + [[[[[1, 2, 3], + '1 2']]]]]""" + o = eval(expected) + self.assertEqual(pprint.pformat(o, width=15), expected) + self.assertEqual(pprint.pformat(o, width=16), expected) + self.assertEqual(pprint.pformat(o, width=25), expected) + self.assertEqual(pprint.pformat(o, width=14), """\ +[[[[[[1, + 2, + 3], + '1 ' + '2']]]], + {1: [1, + 2, + 3], + 2: [12, + 34]}, + 'abc def ' + 'ghi', + ('ab cd ' + 'ef',), + set2({1, + 23}), + [[[[[1, + 2, + 3], + '1 ' + '2']]]]]""") def test_sorted_dict(self): # Starting in Python 2.5, pprint sorts dict displays by key regardless @@ -535,13 +577,12 @@ frozenset2({0, def test_str_wrap(self): # pprint tries to wrap strings intelligently fox = 'the quick brown fox jumped over a lazy dog' - self.assertEqual(pprint.pformat(fox, width=20), """\ -('the quick ' - 'brown fox ' - 'jumped over a ' - 'lazy dog')""") + self.assertEqual(pprint.pformat(fox, width=19), """\ +('the quick brown ' + 'fox jumped over ' + 'a lazy dog')""") self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2}, - width=26), """\ + width=25), """\ {'a': 1, 'b': 'the quick brown ' 'fox jumped over ' @@ -553,12 +594,34 @@ frozenset2({0, # - non-ASCII is allowed # - an apostrophe doesn't disrupt the pprint special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo" - self.assertEqual(pprint.pformat(special, width=21), """\ -('Portons dix ' - 'bons "whiskys"\\n' + self.assertEqual(pprint.pformat(special, width=68), repr(special)) + self.assertEqual(pprint.pformat(special, width=31), """\ +('Portons dix bons "whiskys"\\n' + "à l'avocat goujat\\t qui " + 'fumait au zoo')""") + self.assertEqual(pprint.pformat(special, width=20), """\ +('Portons dix bons ' + '"whiskys"\\n' "à l'avocat " 'goujat\\t qui ' 'fumait au zoo')""") + self.assertEqual(pprint.pformat([[[[[special]]]]], width=35), """\ +[[[[['Portons dix bons "whiskys"\\n' + "à l'avocat goujat\\t qui " + 'fumait au zoo']]]]]""") + self.assertEqual(pprint.pformat([[[[[special]]]]], width=25), """\ +[[[[['Portons dix bons ' + '"whiskys"\\n' + "à l'avocat " + 'goujat\\t qui ' + 'fumait au zoo']]]]]""") + self.assertEqual(pprint.pformat([[[[[special]]]]], width=23), """\ +[[[[['Portons dix ' + 'bons "whiskys"\\n' + "à l'avocat " + 'goujat\\t qui ' + 'fumait au ' + 'zoo']]]]]""") # An unwrappable string is formatted as its repr unwrappable = "x" * 100 self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable)) @@ -581,7 +644,19 @@ frozenset2({0, 14, 15], [], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4]]""" - self.assertEqual(pprint.pformat(o, width=48, compact=True), expected) + self.assertEqual(pprint.pformat(o, width=47, compact=True), expected) + + def test_compact_width(self): + levels = 20 + number = 10 + o = [0] * number + for i in range(levels - 1): + o = [o] + for w in range(levels * 2 + 1, levels + 3 * number - 1): + lines = pprint.pformat(o, width=w, compact=True).splitlines() + maxwidth = max(map(len, lines)) + self.assertLessEqual(maxwidth, w) + self.assertGreater(maxwidth, w - 3) class DottedPrettyPrinter(pprint.PrettyPrinter): -- cgit v1.2.1 From d44a8c5e597824db94ac77044eee2ce34b5d8a77 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 24 Mar 2015 19:22:37 +0200 Subject: Issue #17530: pprint now wraps long bytes objects and bytearrays. --- Lib/test/test_pprint.py | 100 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) (limited to 'Lib/test/test_pprint.py') diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index c0568808a0..50d7d59ecc 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -658,6 +658,106 @@ frozenset2({0, self.assertLessEqual(maxwidth, w) self.assertGreater(maxwidth, w - 3) + def test_bytes_wrap(self): + self.assertEqual(pprint.pformat(b'', width=1), "b''") + self.assertEqual(pprint.pformat(b'abcd', width=1), "b'abcd'") + letters = b'abcdefghijklmnopqrstuvwxyz' + self.assertEqual(pprint.pformat(letters, width=29), repr(letters)) + self.assertEqual(pprint.pformat(letters, width=19), """\ +(b'abcdefghijkl' + b'mnopqrstuvwxyz')""") + self.assertEqual(pprint.pformat(letters, width=18), """\ +(b'abcdefghijkl' + b'mnopqrstuvwx' + b'yz')""") + self.assertEqual(pprint.pformat(letters, width=16), """\ +(b'abcdefghijkl' + b'mnopqrstuvwx' + b'yz')""") + special = bytes(range(16)) + self.assertEqual(pprint.pformat(special, width=61), repr(special)) + self.assertEqual(pprint.pformat(special, width=48), """\ +(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b' + b'\\x0c\\r\\x0e\\x0f')""") + self.assertEqual(pprint.pformat(special, width=32), """\ +(b'\\x00\\x01\\x02\\x03' + b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b' + b'\\x0c\\r\\x0e\\x0f')""") + self.assertEqual(pprint.pformat(special, width=1), """\ +(b'\\x00\\x01\\x02\\x03' + b'\\x04\\x05\\x06\\x07' + b'\\x08\\t\\n\\x0b' + b'\\x0c\\r\\x0e\\x0f')""") + self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2}, + width=21), """\ +{'a': 1, + 'b': b'abcdefghijkl' + b'mnopqrstuvwx' + b'yz', + 'c': 2}""") + self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2}, + width=20), """\ +{'a': 1, + 'b': b'abcdefgh' + b'ijklmnop' + b'qrstuvwxyz', + 'c': 2}""") + self.assertEqual(pprint.pformat([[[[[[letters]]]]]], width=25), """\ +[[[[[[b'abcdefghijklmnop' + b'qrstuvwxyz']]]]]]""") + self.assertEqual(pprint.pformat([[[[[[special]]]]]], width=41), """\ +[[[[[[b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07' + b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f']]]]]]""") + # Check that the pprint is a usable repr + for width in range(1, 64): + formatted = pprint.pformat(special, width=width) + self.assertEqual(eval(formatted), special) + formatted = pprint.pformat([special] * 2, width=width) + self.assertEqual(eval(formatted), [special] * 2) + + def test_bytearray_wrap(self): + self.assertEqual(pprint.pformat(bytearray(), width=1), "bytearray(b'')") + letters = bytearray(b'abcdefghijklmnopqrstuvwxyz') + self.assertEqual(pprint.pformat(letters, width=40), repr(letters)) + self.assertEqual(pprint.pformat(letters, width=28), """\ +bytearray(b'abcdefghijkl' + b'mnopqrstuvwxyz')""") + self.assertEqual(pprint.pformat(letters, width=27), """\ +bytearray(b'abcdefghijkl' + b'mnopqrstuvwx' + b'yz')""") + self.assertEqual(pprint.pformat(letters, width=25), """\ +bytearray(b'abcdefghijkl' + b'mnopqrstuvwx' + b'yz')""") + special = bytearray(range(16)) + self.assertEqual(pprint.pformat(special, width=72), repr(special)) + self.assertEqual(pprint.pformat(special, width=57), """\ +bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b' + b'\\x0c\\r\\x0e\\x0f')""") + self.assertEqual(pprint.pformat(special, width=41), """\ +bytearray(b'\\x00\\x01\\x02\\x03' + b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b' + b'\\x0c\\r\\x0e\\x0f')""") + self.assertEqual(pprint.pformat(special, width=1), """\ +bytearray(b'\\x00\\x01\\x02\\x03' + b'\\x04\\x05\\x06\\x07' + b'\\x08\\t\\n\\x0b' + b'\\x0c\\r\\x0e\\x0f')""") + self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2}, + width=31), """\ +{'a': 1, + 'b': bytearray(b'abcdefghijkl' + b'mnopqrstuvwx' + b'yz'), + 'c': 2}""") + self.assertEqual(pprint.pformat([[[[[letters]]]]], width=37), """\ +[[[[[bytearray(b'abcdefghijklmnop' + b'qrstuvwxyz')]]]]]""") + self.assertEqual(pprint.pformat([[[[[special]]]]], width=50), """\ +[[[[[bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07' + b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f')]]]]]""") + class DottedPrettyPrinter(pprint.PrettyPrinter): -- cgit v1.2.1 From a7e1071bd35d41661e5c3cb898c0d81cf42e1496 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 24 Mar 2015 19:31:50 +0200 Subject: Issue #23502: The pprint module now supports mapping proxies. In particular the __dict__ attributes of building types. --- Lib/test/test_pprint.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'Lib/test/test_pprint.py') diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 50d7d59ecc..3a798d946c 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -7,6 +7,7 @@ import test.test_set import random import collections import itertools +import types # list, tuple and dict subclasses that do or don't overwrite __repr__ class list2(list): @@ -271,6 +272,34 @@ class QueryTestCase(unittest.TestCase): 'a': 6, 'lazy': 7, 'dog': 8}""") + + def test_mapping_proxy(self): + words = 'the quick brown fox jumped over a lazy dog'.split() + d = dict(zip(words, itertools.count())) + m = types.MappingProxyType(d) + self.assertEqual(pprint.pformat(m), """\ +mappingproxy({'a': 6, + 'brown': 2, + 'dog': 8, + 'fox': 3, + 'jumped': 4, + 'lazy': 7, + 'over': 5, + 'quick': 1, + 'the': 0})""") + d = collections.OrderedDict(zip(words, itertools.count())) + m = types.MappingProxyType(d) + self.assertEqual(pprint.pformat(m), """\ +mappingproxy({'the': 0, + 'quick': 1, + 'brown': 2, + 'fox': 3, + 'jumped': 4, + 'over': 5, + 'a': 6, + 'lazy': 7, + 'dog': 8})""") + def test_subclassing(self): o = {'names with spaces': 'should be presented using repr()', 'others.should.not.be': 'like.this'} -- cgit v1.2.1 From 9f1e758b105118124d602754f51959922b41671b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 26 Mar 2015 08:43:21 +0200 Subject: Issue #23776: Removed asserts from pprint.PrettyPrinter constructor. --- Lib/test/test_pprint.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'Lib/test/test_pprint.py') diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 3a798d946c..01e19feb6d 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -1,13 +1,14 @@ # -*- coding: utf-8 -*- +import collections +import io +import itertools import pprint +import random import test.support -import unittest import test.test_set -import random -import collections -import itertools import types +import unittest # list, tuple and dict subclasses that do or don't overwrite __repr__ class list2(list): @@ -56,6 +57,18 @@ class QueryTestCase(unittest.TestCase): self.b = list(range(200)) self.a[-12] = self.b + def test_init(self): + pp = pprint.PrettyPrinter() + pp = pprint.PrettyPrinter(indent=4, width=40, depth=5, + stream=io.StringIO(), compact=True) + pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO()) + with self.assertRaises(TypeError): + pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO(), True) + self.assertRaises(ValueError, pprint.PrettyPrinter, indent=-1) + self.assertRaises(ValueError, pprint.PrettyPrinter, depth=0) + self.assertRaises(ValueError, pprint.PrettyPrinter, depth=-1) + self.assertRaises(ValueError, pprint.PrettyPrinter, width=0) + def test_basic(self): # Verify .isrecursive() and .isreadable() w/o recursion pp = pprint.PrettyPrinter() -- cgit v1.2.1 From 86aaa56fc8379886ab482777a7aee47d2fc9d998 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 26 Mar 2015 08:51:33 +0200 Subject: Issue #23775: pprint() of OrderedDict now outputs the same representation as repr(). --- Lib/test/test_pprint.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'Lib/test/test_pprint.py') diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 01e19feb6d..428e77ef21 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -272,19 +272,23 @@ class QueryTestCase(unittest.TestCase): r"{5: [[]], 'xy\tab\n': (3,), (): {}}") def test_ordered_dict(self): + d = collections.OrderedDict() + self.assertEqual(pprint.pformat(d, width=1), 'OrderedDict()') + d = collections.OrderedDict([]) + self.assertEqual(pprint.pformat(d, width=1), 'OrderedDict()') words = 'the quick brown fox jumped over a lazy dog'.split() d = collections.OrderedDict(zip(words, itertools.count())) self.assertEqual(pprint.pformat(d), """\ -{'the': 0, - 'quick': 1, - 'brown': 2, - 'fox': 3, - 'jumped': 4, - 'over': 5, - 'a': 6, - 'lazy': 7, - 'dog': 8}""") +OrderedDict([('the', 0), + ('quick', 1), + ('brown', 2), + ('fox', 3), + ('jumped', 4), + ('over', 5), + ('a', 6), + ('lazy', 7), + ('dog', 8)])""") def test_mapping_proxy(self): words = 'the quick brown fox jumped over a lazy dog'.split() @@ -303,15 +307,15 @@ mappingproxy({'a': 6, d = collections.OrderedDict(zip(words, itertools.count())) m = types.MappingProxyType(d) self.assertEqual(pprint.pformat(m), """\ -mappingproxy({'the': 0, - 'quick': 1, - 'brown': 2, - 'fox': 3, - 'jumped': 4, - 'over': 5, - 'a': 6, - 'lazy': 7, - 'dog': 8})""") +mappingproxy(OrderedDict([('the', 0), + ('quick', 1), + ('brown', 2), + ('fox', 3), + ('jumped', 4), + ('over', 5), + ('a', 6), + ('lazy', 7), + ('dog', 8)]))""") def test_subclassing(self): o = {'names with spaces': 'should be presented using repr()', -- cgit v1.2.1 From 622952b6664f47b1185217e715fff8e1dc34426f Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 6 Apr 2015 22:52:44 +0300 Subject: Issue #22721: An order of multiline pprint output of set or dict containing orderable and non-orderable elements no longer depends on iteration order of set or dict. --- Lib/test/test_pprint.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'Lib/test/test_pprint.py') diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 428e77ef21..ef2a8a58ce 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -50,6 +50,25 @@ class Unorderable: def __repr__(self): return str(id(self)) +# Class Orderable is orderable with any type +class Orderable: + def __init__(self, hash): + self._hash = hash + def __lt__(self, other): + return False + def __gt__(self, other): + return self != other + def __le__(self, other): + return self == other + def __ge__(self, other): + return True + def __eq__(self, other): + return self is other + def __ne__(self, other): + return self is not other + def __hash__(self): + return self._hash + class QueryTestCase(unittest.TestCase): def setUp(self): @@ -620,6 +639,26 @@ frozenset2({0, self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)), '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id))) + def test_sort_orderable_and_unorderable_values(self): + # Issue 22721: sorted pprints is not stable + a = Unorderable() + b = Orderable(hash(a)) # should have the same hash value + # self-test + self.assertLess(a, b) + self.assertLess(str(type(b)), str(type(a))) + self.assertEqual(sorted([b, a]), [a, b]) + self.assertEqual(sorted([a, b]), [a, b]) + # set + self.assertEqual(pprint.pformat(set([b, a]), width=1), + '{%r,\n %r}' % (a, b)) + self.assertEqual(pprint.pformat(set([a, b]), width=1), + '{%r,\n %r}' % (a, b)) + # dict + self.assertEqual(pprint.pformat(dict.fromkeys([b, a]), width=1), + '{%r: None,\n %r: None}' % (a, b)) + self.assertEqual(pprint.pformat(dict.fromkeys([a, b]), width=1), + '{%r: None,\n %r: None}' % (a, b)) + def test_str_wrap(self): # pprint tries to wrap strings intelligently fox = 'the quick brown fox jumped over a lazy dog' -- cgit v1.2.1 From 0e1e4f3be6be943cee07763742fdd1584b9acbcc Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 12 May 2015 13:35:48 +0300 Subject: Issue #23870: The pprint module now supports all standard collections except named tuples. --- Lib/test/test_pprint.py | 148 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) (limited to 'Lib/test/test_pprint.py') diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index ef2a8a58ce..9e5309cc41 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -843,6 +843,154 @@ bytearray(b'\\x00\\x01\\x02\\x03' [[[[[bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07' b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f')]]]]]""") + def test_default_dict(self): + d = collections.defaultdict(int) + self.assertEqual(pprint.pformat(d, width=1), "defaultdict(, {})") + words = 'the quick brown fox jumped over a lazy dog'.split() + d = collections.defaultdict(int, zip(words, itertools.count())) + self.assertEqual(pprint.pformat(d), +"""\ +defaultdict(, + {'a': 6, + 'brown': 2, + 'dog': 8, + 'fox': 3, + 'jumped': 4, + 'lazy': 7, + 'over': 5, + 'quick': 1, + 'the': 0})""") + + def test_counter(self): + d = collections.Counter() + self.assertEqual(pprint.pformat(d, width=1), "Counter()") + d = collections.Counter('senselessness') + self.assertEqual(pprint.pformat(d, width=40), +"""\ +Counter({'s': 6, + 'e': 4, + 'n': 2, + 'l': 1})""") + + def test_chainmap(self): + d = collections.ChainMap() + self.assertEqual(pprint.pformat(d, width=1), "ChainMap({})") + words = 'the quick brown fox jumped over a lazy dog'.split() + items = list(zip(words, itertools.count())) + d = collections.ChainMap(dict(items)) + self.assertEqual(pprint.pformat(d), +"""\ +ChainMap({'a': 6, + 'brown': 2, + 'dog': 8, + 'fox': 3, + 'jumped': 4, + 'lazy': 7, + 'over': 5, + 'quick': 1, + 'the': 0})""") + d = collections.ChainMap(dict(items), collections.OrderedDict(items)) + self.assertEqual(pprint.pformat(d), +"""\ +ChainMap({'a': 6, + 'brown': 2, + 'dog': 8, + 'fox': 3, + 'jumped': 4, + 'lazy': 7, + 'over': 5, + 'quick': 1, + 'the': 0}, + OrderedDict([('the', 0), + ('quick', 1), + ('brown', 2), + ('fox', 3), + ('jumped', 4), + ('over', 5), + ('a', 6), + ('lazy', 7), + ('dog', 8)]))""") + + def test_deque(self): + d = collections.deque() + self.assertEqual(pprint.pformat(d, width=1), "deque([])") + d = collections.deque(maxlen=7) + self.assertEqual(pprint.pformat(d, width=1), "deque([], maxlen=7)") + words = 'the quick brown fox jumped over a lazy dog'.split() + d = collections.deque(zip(words, itertools.count())) + self.assertEqual(pprint.pformat(d), +"""\ +deque([('the', 0), + ('quick', 1), + ('brown', 2), + ('fox', 3), + ('jumped', 4), + ('over', 5), + ('a', 6), + ('lazy', 7), + ('dog', 8)])""") + d = collections.deque(zip(words, itertools.count()), maxlen=7) + self.assertEqual(pprint.pformat(d), +"""\ +deque([('brown', 2), + ('fox', 3), + ('jumped', 4), + ('over', 5), + ('a', 6), + ('lazy', 7), + ('dog', 8)], + maxlen=7)""") + + def test_user_dict(self): + d = collections.UserDict() + self.assertEqual(pprint.pformat(d, width=1), "{}") + words = 'the quick brown fox jumped over a lazy dog'.split() + d = collections.UserDict(zip(words, itertools.count())) + self.assertEqual(pprint.pformat(d), +"""\ +{'a': 6, + 'brown': 2, + 'dog': 8, + 'fox': 3, + 'jumped': 4, + 'lazy': 7, + 'over': 5, + 'quick': 1, + 'the': 0}""") + + def test_user_dict(self): + d = collections.UserList() + self.assertEqual(pprint.pformat(d, width=1), "[]") + words = 'the quick brown fox jumped over a lazy dog'.split() + d = collections.UserList(zip(words, itertools.count())) + self.assertEqual(pprint.pformat(d), +"""\ +[('the', 0), + ('quick', 1), + ('brown', 2), + ('fox', 3), + ('jumped', 4), + ('over', 5), + ('a', 6), + ('lazy', 7), + ('dog', 8)]""") + + def test_user_string(self): + d = collections.UserString('') + self.assertEqual(pprint.pformat(d, width=1), "''") + d = collections.UserString('the quick brown fox jumped over a lazy dog') + self.assertEqual(pprint.pformat(d, width=20), +"""\ +('the quick brown ' + 'fox jumped over ' + 'a lazy dog')""") + self.assertEqual(pprint.pformat({1: d}, width=20), +"""\ +{1: 'the quick ' + 'brown fox ' + 'jumped over a ' + 'lazy dog'}""") + class DottedPrettyPrinter(pprint.PrettyPrinter): -- cgit v1.2.1 From 00ed3b2ee7283e6a1cc8a85e9a23a7734f265ec3 Mon Sep 17 00:00:00 2001 From: R David Murray Date: Tue, 19 May 2015 08:16:04 -0400 Subject: Back out changeset 955dffec3d94 since it broke the buildbots. and the situation has not been addressed in several days. --- Lib/test/test_pprint.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'Lib/test/test_pprint.py') diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index f3508a9966..9e5309cc41 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -91,8 +91,7 @@ class QueryTestCase(unittest.TestCase): def test_basic(self): # Verify .isrecursive() and .isreadable() w/o recursion pp = pprint.PrettyPrinter() - for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, b"def", - bytearray(b"ghi"), True, False, None, + for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, "yaddayadda", self.a, self.b): # module-level convenience functions self.assertFalse(pprint.isrecursive(safe), @@ -162,23 +161,21 @@ class QueryTestCase(unittest.TestCase): # it sorted a dict display if and only if the display required # multiple lines. For that reason, dicts with more than one element # aren't tested here. - for simple in (0, 0, 0+0j, 0.0, "", b"", bytearray(), + for simple in (0, 0, 0+0j, 0.0, "", b"", (), tuple2(), tuple3(), [], list2(), list3(), set(), set2(), set3(), frozenset(), frozenset2(), frozenset3(), {}, dict2(), dict3(), self.assertTrue, pprint, - -6, -6, -6-6j, -1.5, "x", b"x", bytearray(b"x"), - (3,), [3], {3: 6}, + -6, -6, -6-6j, -1.5, "x", b"x", (3,), [3], {3: 6}, (1,2), [3,4], {5: 6}, tuple2((1,2)), tuple3((1,2)), tuple3(range(100)), [3,4], list2([3,4]), list3([3,4]), list3(range(100)), set({7}), set2({7}), set3({7}), frozenset({8}), frozenset2({8}), frozenset3({8}), dict2({5: 6}), dict3({5: 6}), - range(10, -11, -1), - True, False, None, + range(10, -11, -1) ): native = repr(simple) self.assertEqual(pprint.pformat(simple), native) @@ -1008,5 +1005,9 @@ class DottedPrettyPrinter(pprint.PrettyPrinter): self, object, context, maxlevels, level) +def test_main(): + test.support.run_unittest(QueryTestCase) + + if __name__ == "__main__": - unittest.main() + test_main() -- cgit v1.2.1