diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index dfbc2a06e7346fa..a4c05bb5eca6d34 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -326,6 +326,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], @@ -364,10 +370,38 @@ 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') + 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) @@ -376,6 +410,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. @@ -396,9 +434,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() @@ -740,6 +789,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, [...]]' @@ -747,6 +811,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 @@ -863,6 +942,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 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 000000000000000..0f2d539525a3d93 --- /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.