From 73f5a592f2f43e34471bf08c3b0c9db3e39ea2fb Mon Sep 17 00:00:00 2001 From: hp310780 <43389959+hp310780@users.noreply.github.com> Date: Tue, 26 Jul 2022 13:28:12 +0100 Subject: [PATCH 1/3] gh-89722: Add pprint.pprint tests for indent, width and depth arguments --- Lib/test/test_pprint.py | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 6ea7e7db2ce113..3e7c16228aed55 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -327,6 +327,12 @@ def test_nested_indentations(self): 'third': 3}]""" self.assertEqual(pprint.pformat(o, indent=4, width=41), expected) + expected = "[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n" \ + " { 'first': 1,\n 'second': 2,\n 'third': 3}]\n" + stream = io.StringIO() + pprint.pprint(o, stream=stream, indent=4, width=41) + self.assertEqual(stream.getvalue(), expected) + def test_width(self): expected = """\ [[[[[[1, 2, 3], @@ -365,6 +371,26 @@ def test_width(self): '1 ' '2']]]]]""") + expected = "[[[[[[1, 2, 3],\n '1 2']]]],\n {1: [1, 2, 3],\n 2: [12, 34]},\n "\ + "'abc def ghi',\n ('ab cd ef',),\n set2({1, 23}),\n [[[[[1, 2, 3],\n " \ + "'1 2']]]]]\n" + stream = io.StringIO() + pprint.pprint(o, stream=stream, width=15) + self.assertEqual(stream.getvalue(), expected) + stream = io.StringIO() + pprint.pprint(o, stream=stream, width=16) + stream = io.StringIO() + pprint.pprint(o, stream=stream, width=25) + self.assertEqual(stream.getvalue(), expected) + stream = io.StringIO() + pprint.pprint(o, stream=stream, width=14) + expected = "[[[[[[1,\n 2,\n 3],\n '1 '\n '2']]]],\n {1: [1,\n " \ + "2,\n 3],\n 2: [12,\n 34]},\n 'abc def '\n 'ghi',\n ('ab cd '\n " \ + "'ef',),\n set2({1,\n 23}),\n [[[[[1,\n 2,\n 3],\n " \ + "'1 '\n '2']]]]]\n" + + self.assertEqual(stream.getvalue(), expected) + def test_integer(self): self.assertEqual(pprint.pformat(1234567), '1234567') self.assertEqual(pprint.pformat(1234567, underscore_numbers=True), '1_234_567') @@ -839,6 +865,21 @@ def test_depth(self): self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict)) self.assertEqual(pprint.pformat(nested_list), repr(nested_list)) + stream = io.StringIO() + pprint.pprint(nested_tuple, stream=stream) + expected = f"{repr(nested_tuple)}\n" + self.assertEqual(stream.getvalue(), expected) + + stream = io.StringIO() + pprint.pprint(nested_dict, stream=stream) + expected = f"{repr(nested_dict)}\n" + self.assertEqual(stream.getvalue(), expected) + + stream = io.StringIO() + pprint.pprint(nested_list, stream=stream) + expected = f"{repr(nested_list)}\n" + self.assertEqual(stream.getvalue(), expected) + lv1_tuple = '(1, (...))' lv1_dict = '{1: {...}}' lv1_list = '[1, [...]]' @@ -846,6 +887,21 @@ def test_depth(self): self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict) self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list) + stream = io.StringIO() + pprint.pprint(nested_tuple, stream=stream, depth=1) + expected = lv1_tuple + "\n" + self.assertEqual(stream.getvalue(), expected) + + stream = io.StringIO() + pprint.pprint(nested_dict, stream=stream, depth=1) + expected = lv1_dict + "\n" + self.assertEqual(stream.getvalue(), expected) + + stream = io.StringIO() + pprint.pprint(nested_list, stream=stream, depth=1) + expected = lv1_list + "\n" + self.assertEqual(stream.getvalue(), expected) + def test_sort_unorderable_values(self): # Issue 3976: sorted pprints fail for unorderable values. n = 20 From 6e437c3bdb3160e481b73429b5e93d8c0f5a27b6 Mon Sep 17 00:00:00 2001 From: hp310780 <43389959+hp310780@users.noreply.github.com> Date: Tue, 26 Jul 2022 13:53:56 +0100 Subject: [PATCH 2/3] gh-89722: Add pprint.pprint tests for compact, sort_dict and underscore_numbers arguments --- Lib/test/test_pprint.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 3e7c16228aed55..cdad3a12773c60 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -395,6 +395,14 @@ def test_integer(self): self.assertEqual(pprint.pformat(1234567), '1234567') self.assertEqual(pprint.pformat(1234567, underscore_numbers=True), '1_234_567') + stream = io.StringIO() + pprint.pprint(1234567, stream=stream) + self.assertEqual(stream.getvalue(), '1234567\n') + + stream = io.StringIO() + pprint.pprint(1234567, stream=stream, underscore_numbers=True) + self.assertEqual(stream.getvalue(), '1_234_567\n') + class Temperature(int): def __new__(cls, celsius_degrees): return super().__new__(Temperature, celsius_degrees) @@ -403,6 +411,10 @@ def __repr__(self): return f"{kelvin_degrees}°K" self.assertEqual(pprint.pformat(Temperature(1000)), '1273.15°K') + stream = io.StringIO() + pprint.pprint(Temperature(1000), stream=stream) + self.assertEqual(stream.getvalue(), '1273.15°K\n') + def test_sorted_dict(self): # Starting in Python 2.5, pprint sorts dict displays by key regardless # of how small the dictionary may be. @@ -423,9 +435,20 @@ def test_sorted_dict(self): def test_sort_dict(self): d = dict.fromkeys('cba') - self.assertEqual(pprint.pformat(d, sort_dicts=False), "{'c': None, 'b': None, 'a': None}") + + expected_unsorted = "{'c': None, 'b': None, 'a': None}" + expected_unsorted_list = "[{'c': None, 'b': None, 'a': None}, {'c': None, 'b': None, 'a': None}]" + self.assertEqual(pprint.pformat(d, sort_dicts=False), expected_unsorted) self.assertEqual(pprint.pformat([d, d], sort_dicts=False), - "[{'c': None, 'b': None, 'a': None}, {'c': None, 'b': None, 'a': None}]") + expected_unsorted_list) + + stream = io.StringIO() + pprint.pprint(d, stream=stream, sort_dicts=False) + self.assertEqual(stream.getvalue(), expected_unsorted + "\n") + + stream = io.StringIO() + pprint.pprint([d, d], stream=stream, sort_dicts=False) + self.assertEqual(stream.getvalue(), expected_unsorted_list + "\n") def test_ordered_dict(self): d = collections.OrderedDict() @@ -1018,6 +1041,11 @@ def test_compact(self): [0, 1, 2, 3, 4]]""" self.assertEqual(pprint.pformat(o, width=47, compact=True), expected) + stream = io.StringIO() + pprint.pprint(o, stream=stream, width=47, compact=True) + expected_pprint = expected + "\n" + self.assertEqual(stream.getvalue(), expected_pprint) + def test_compact_width(self): levels = 20 number = 10 From 1ce25bef64953e5456a2795b3216f6928a904a99 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 26 Jul 2022 13:27:45 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Tests/2022-07-26-13-27-44.gh-issue-89722.5X9CkT.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2022-07-26-13-27-44.gh-issue-89722.5X9CkT.rst diff --git a/Misc/NEWS.d/next/Tests/2022-07-26-13-27-44.gh-issue-89722.5X9CkT.rst b/Misc/NEWS.d/next/Tests/2022-07-26-13-27-44.gh-issue-89722.5X9CkT.rst new file mode 100644 index 00000000000000..0f2d539525a3d9 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2022-07-26-13-27-44.gh-issue-89722.5X9CkT.rst @@ -0,0 +1 @@ +Add tests for pprint.pprint arguments.