Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_pprint.QueryTestCase Class Reference

List of all members.

Public Member Functions

def setUp
def test_basic
def test_knotted
def test_unreadable
def test_same_as_repr
def test_basic_line_wrap
def test_nested_indentations
def test_sorted_dict
def test_ordered_dict
def test_subclassing
def test_set_reprs
def test_depth
def test_sort_unorderable_values

Public Attributes

 a
 b
 d

Detailed Description

Definition at line 35 of file test_pprint.py.


Member Function Documentation

Definition at line 37 of file test_pprint.py.

00037 
00038     def setUp(self):
00039         self.a = list(range(100))
00040         self.b = list(range(200))
00041         self.a[-12] = self.b

Here is the caller graph for this function:

Definition at line 42 of file test_pprint.py.

00042 
00043     def test_basic(self):
00044         # Verify .isrecursive() and .isreadable() w/o recursion
00045         pp = pprint.PrettyPrinter()
00046         for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, "yaddayadda",
00047                      self.a, self.b):
00048             # module-level convenience functions
00049             self.assertFalse(pprint.isrecursive(safe),
00050                              "expected not isrecursive for %r" % (safe,))
00051             self.assertTrue(pprint.isreadable(safe),
00052                             "expected isreadable for %r" % (safe,))
00053             # PrettyPrinter methods
00054             self.assertFalse(pp.isrecursive(safe),
00055                              "expected not isrecursive for %r" % (safe,))
00056             self.assertTrue(pp.isreadable(safe),
00057                             "expected isreadable for %r" % (safe,))

Here is the call graph for this function:

Definition at line 135 of file test_pprint.py.

00135 
00136     def test_basic_line_wrap(self):
00137         # verify basic line-wrapping operation
00138         o = {'RPM_cal': 0,
00139              'RPM_cal2': 48059,
00140              'Speed_cal': 0,
00141              'controldesk_runtime_us': 0,
00142              'main_code_runtime_us': 0,
00143              'read_io_runtime_us': 0,
00144              'write_io_runtime_us': 43690}
00145         exp = """\
00146 {'RPM_cal': 0,
00147  'RPM_cal2': 48059,
00148  'Speed_cal': 0,
00149  'controldesk_runtime_us': 0,
00150  'main_code_runtime_us': 0,
00151  'read_io_runtime_us': 0,
00152  'write_io_runtime_us': 43690}"""
00153         for type in [dict, dict2]:
00154             self.assertEqual(pprint.pformat(type(o)), exp)
00155 
00156         o = range(100)
00157         exp = '[%s]' % ',\n '.join(map(str, o))
00158         for type in [list, list2]:
00159             self.assertEqual(pprint.pformat(type(o)), exp)
00160 
00161         o = tuple(range(100))
00162         exp = '(%s)' % ',\n '.join(map(str, o))
00163         for type in [tuple, tuple2]:
00164             self.assertEqual(pprint.pformat(type(o)), exp)
00165 
00166         # indent parameter
00167         o = range(100)
00168         exp = '[   %s]' % ',\n    '.join(map(str, o))
00169         for type in [list, list2]:
00170             self.assertEqual(pprint.pformat(type(o), indent=4), exp)

Here is the call graph for this function:

Definition at line 435 of file test_pprint.py.

00435 
00436     def test_depth(self):
00437         nested_tuple = (1, (2, (3, (4, (5, 6)))))
00438         nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
00439         nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
00440         self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple))
00441         self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
00442         self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
00443 
00444         lv1_tuple = '(1, (...))'
00445         lv1_dict = '{1: {...}}'
00446         lv1_list = '[1, [...]]'
00447         self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
00448         self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
00449         self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)

Here is the call graph for this function:

Definition at line 58 of file test_pprint.py.

00058 
00059     def test_knotted(self):
00060         # Verify .isrecursive() and .isreadable() w/ recursion
00061         # Tie a knot.
00062         self.b[67] = self.a
00063         # Messy dict.
00064         self.d = {}
00065         self.d[0] = self.d[1] = self.d[2] = self.d
00066 
00067         pp = pprint.PrettyPrinter()
00068 
00069         for icky in self.a, self.b, self.d, (self.d, self.d):
00070             self.assertTrue(pprint.isrecursive(icky), "expected isrecursive")
00071             self.assertFalse(pprint.isreadable(icky), "expected not isreadable")
00072             self.assertTrue(pp.isrecursive(icky), "expected isrecursive")
00073             self.assertFalse(pp.isreadable(icky), "expected not isreadable")
00074 
00075         # Break the cycles.
00076         self.d.clear()
00077         del self.a[:]
00078         del self.b[:]
00079 
00080         for safe in self.a, self.b, self.d, (self.d, self.d):
00081             # module-level convenience functions
00082             self.assertFalse(pprint.isrecursive(safe),
00083                              "expected not isrecursive for %r" % (safe,))
00084             self.assertTrue(pprint.isreadable(safe),
00085                             "expected isreadable for %r" % (safe,))
00086             # PrettyPrinter methods
00087             self.assertFalse(pp.isrecursive(safe),
00088                              "expected not isrecursive for %r" % (safe,))
00089             self.assertTrue(pp.isreadable(safe),
00090                             "expected isreadable for %r" % (safe,))

Definition at line 171 of file test_pprint.py.

00171 
00172     def test_nested_indentations(self):
00173         o1 = list(range(10))
00174         o2 = dict(first=1, second=2, third=3)
00175         o = [o1, o2]
00176         expected = """\
00177 [   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
00178     {   'first': 1,
00179         'second': 2,
00180         'third': 3}]"""
00181         self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)

Here is the call graph for this function:

Definition at line 200 of file test_pprint.py.

00200 
00201     def test_ordered_dict(self):
00202         words = 'the quick brown fox jumped over a lazy dog'.split()
00203         d = collections.OrderedDict(zip(words, itertools.count()))
00204         self.assertEqual(pprint.pformat(d),
00205 """\
00206 {'the': 0,
00207  'quick': 1,
00208  'brown': 2,
00209  'fox': 3,
00210  'jumped': 4,
00211  'over': 5,
00212  'a': 6,
00213  'lazy': 7,
 'dog': 8}""")

Here is the call graph for this function:

Definition at line 106 of file test_pprint.py.

00106 
00107     def test_same_as_repr(self):
00108         # Simple objects, small containers and classes that overwrite __repr__
00109         # For those the result should be the same as repr().
00110         # Ahem.  The docs don't say anything about that -- this appears to
00111         # be testing an implementation quirk.  Starting in Python 2.5, it's
00112         # not true for dicts:  pprint always sorts dicts by key now; before,
00113         # it sorted a dict display if and only if the display required
00114         # multiple lines.  For that reason, dicts with more than one element
00115         # aren't tested here.
00116         for simple in (0, 0, 0+0j, 0.0, "", b"",
00117                        (), tuple2(), tuple3(),
00118                        [], list2(), list3(),
00119                        {}, dict2(), dict3(),
00120                        self.assertTrue, pprint,
00121                        -6, -6, -6-6j, -1.5, "x", b"x", (3,), [3], {3: 6},
00122                        (1,2), [3,4], {5: 6},
00123                        tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
00124                        [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
00125                        dict2({5: 6}), dict3({5: 6}),
00126                        range(10, -11, -1)
00127                       ):
00128             native = repr(simple)
00129             for function in "pformat", "saferepr":
00130                 f = getattr(pprint, function)
00131                 got = f(simple)
00132                 self.assertEqual(native, got,
00133                                  "expected %s got %s from pprint.%s" %
00134                                  (native, got, function))

Here is the call graph for this function:

Definition at line 223 of file test_pprint.py.

00223 
00224     def test_set_reprs(self):
00225         # This test creates a complex arrangement of frozensets and
00226         # compares the pretty-printed repr against a string hard-coded in
00227         # the test.  The hard-coded repr depends on the sort order of
00228         # frozensets.
00229         #
00230         # However, as the docs point out: "Since sets only define
00231         # partial ordering (subset relationships), the output of the
00232         # list.sort() method is undefined for lists of sets."
00233         #
00234         # In a nutshell, the test assumes frozenset({0}) will always
00235         # sort before frozenset({1}), but:
00236         #
00237         # >>> frozenset({0}) < frozenset({1})
00238         # False
00239         # >>> frozenset({1}) < frozenset({0})
00240         # False
00241         #
00242         # Consequently, this test is fragile and
00243         # implementation-dependent.  Small changes to Python's sort
00244         # algorithm cause the test to fail when it should pass.
00245 
00246         self.assertEqual(pprint.pformat(set()), 'set()')
00247         self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
00248         self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
00249         self.assertEqual(pprint.pformat(frozenset(range(3))), 'frozenset({0, 1, 2})')
00250         cube_repr_tgt = """\
00251 {frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}),
00252  frozenset({0}): frozenset({frozenset(),
00253                             frozenset({0, 2}),
00254                             frozenset({0, 1})}),
00255  frozenset({1}): frozenset({frozenset(),
00256                             frozenset({1, 2}),
00257                             frozenset({0, 1})}),
00258  frozenset({2}): frozenset({frozenset(),
00259                             frozenset({1, 2}),
00260                             frozenset({0, 2})}),
00261  frozenset({1, 2}): frozenset({frozenset({2}),
00262                                frozenset({1}),
00263                                frozenset({0, 1, 2})}),
00264  frozenset({0, 2}): frozenset({frozenset({2}),
00265                                frozenset({0}),
00266                                frozenset({0, 1, 2})}),
00267  frozenset({0, 1}): frozenset({frozenset({0}),
00268                                frozenset({1}),
00269                                frozenset({0, 1, 2})}),
00270  frozenset({0, 1, 2}): frozenset({frozenset({1, 2}),
00271                                   frozenset({0, 2}),
00272                                   frozenset({0, 1})})}"""
00273         cube = test.test_set.cube(3)
00274         self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
00275         cubo_repr_tgt = """\
00276 {frozenset({frozenset({0, 2}), frozenset({0})}): frozenset({frozenset({frozenset({0,
00277                                                                                   2}),
00278                                                                        frozenset({0,
00279                                                                                   1,
00280                                                                                   2})}),
00281                                                             frozenset({frozenset({0}),
00282                                                                        frozenset({0,
00283                                                                                   1})}),
00284                                                             frozenset({frozenset(),
00285                                                                        frozenset({0})}),
00286                                                             frozenset({frozenset({2}),
00287                                                                        frozenset({0,
00288                                                                                   2})})}),
00289  frozenset({frozenset({0, 1}), frozenset({1})}): frozenset({frozenset({frozenset({0,
00290                                                                                   1}),
00291                                                                        frozenset({0,
00292                                                                                   1,
00293                                                                                   2})}),
00294                                                             frozenset({frozenset({0}),
00295                                                                        frozenset({0,
00296                                                                                   1})}),
00297                                                             frozenset({frozenset({1}),
00298                                                                        frozenset({1,
00299                                                                                   2})}),
00300                                                             frozenset({frozenset(),
00301                                                                        frozenset({1})})}),
00302  frozenset({frozenset({1, 2}), frozenset({1})}): frozenset({frozenset({frozenset({1,
00303                                                                                   2}),
00304                                                                        frozenset({0,
00305                                                                                   1,
00306                                                                                   2})}),
00307                                                             frozenset({frozenset({2}),
00308                                                                        frozenset({1,
00309                                                                                   2})}),
00310                                                             frozenset({frozenset(),
00311                                                                        frozenset({1})}),
00312                                                             frozenset({frozenset({1}),
00313                                                                        frozenset({0,
00314                                                                                   1})})}),
00315  frozenset({frozenset({1, 2}), frozenset({2})}): frozenset({frozenset({frozenset({1,
00316                                                                                   2}),
00317                                                                        frozenset({0,
00318                                                                                   1,
00319                                                                                   2})}),
00320                                                             frozenset({frozenset({1}),
00321                                                                        frozenset({1,
00322                                                                                   2})}),
00323                                                             frozenset({frozenset({2}),
00324                                                                        frozenset({0,
00325                                                                                   2})}),
00326                                                             frozenset({frozenset(),
00327                                                                        frozenset({2})})}),
00328  frozenset({frozenset(), frozenset({0})}): frozenset({frozenset({frozenset({0}),
00329                                                                  frozenset({0,
00330                                                                             1})}),
00331                                                       frozenset({frozenset({0}),
00332                                                                  frozenset({0,
00333                                                                             2})}),
00334                                                       frozenset({frozenset(),
00335                                                                  frozenset({1})}),
00336                                                       frozenset({frozenset(),
00337                                                                  frozenset({2})})}),
00338  frozenset({frozenset(), frozenset({1})}): frozenset({frozenset({frozenset(),
00339                                                                  frozenset({0})}),
00340                                                       frozenset({frozenset({1}),
00341                                                                  frozenset({1,
00342                                                                             2})}),
00343                                                       frozenset({frozenset(),
00344                                                                  frozenset({2})}),
00345                                                       frozenset({frozenset({1}),
00346                                                                  frozenset({0,
00347                                                                             1})})}),
00348  frozenset({frozenset({2}), frozenset()}): frozenset({frozenset({frozenset({2}),
00349                                                                  frozenset({1,
00350                                                                             2})}),
00351                                                       frozenset({frozenset(),
00352                                                                  frozenset({0})}),
00353                                                       frozenset({frozenset(),
00354                                                                  frozenset({1})}),
00355                                                       frozenset({frozenset({2}),
00356                                                                  frozenset({0,
00357                                                                             2})})}),
00358  frozenset({frozenset({0, 1, 2}), frozenset({0, 1})}): frozenset({frozenset({frozenset({1,
00359                                                                                         2}),
00360                                                                              frozenset({0,
00361                                                                                         1,
00362                                                                                         2})}),
00363                                                                   frozenset({frozenset({0,
00364                                                                                         2}),
00365                                                                              frozenset({0,
00366                                                                                         1,
00367                                                                                         2})}),
00368                                                                   frozenset({frozenset({0}),
00369                                                                              frozenset({0,
00370                                                                                         1})}),
00371                                                                   frozenset({frozenset({1}),
00372                                                                              frozenset({0,
00373                                                                                         1})})}),
00374  frozenset({frozenset({0}), frozenset({0, 1})}): frozenset({frozenset({frozenset(),
00375                                                                        frozenset({0})}),
00376                                                             frozenset({frozenset({0,
00377                                                                                   1}),
00378                                                                        frozenset({0,
00379                                                                                   1,
00380                                                                                   2})}),
00381                                                             frozenset({frozenset({0}),
00382                                                                        frozenset({0,
00383                                                                                   2})}),
00384                                                             frozenset({frozenset({1}),
00385                                                                        frozenset({0,
00386                                                                                   1})})}),
00387  frozenset({frozenset({2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({0,
00388                                                                                   2}),
00389                                                                        frozenset({0,
00390                                                                                   1,
00391                                                                                   2})}),
00392                                                             frozenset({frozenset({2}),
00393                                                                        frozenset({1,
00394                                                                                   2})}),
00395                                                             frozenset({frozenset({0}),
00396                                                                        frozenset({0,
00397                                                                                   2})}),
00398                                                             frozenset({frozenset(),
00399                                                                        frozenset({2})})}),
00400  frozenset({frozenset({0, 1, 2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({1,
00401                                                                                         2}),
00402                                                                              frozenset({0,
00403                                                                                         1,
00404                                                                                         2})}),
00405                                                                   frozenset({frozenset({0,
00406                                                                                         1}),
00407                                                                              frozenset({0,
00408                                                                                         1,
00409                                                                                         2})}),
00410                                                                   frozenset({frozenset({0}),
00411                                                                              frozenset({0,
00412                                                                                         2})}),
00413                                                                   frozenset({frozenset({2}),
00414                                                                              frozenset({0,
00415                                                                                         2})})}),
00416  frozenset({frozenset({1, 2}), frozenset({0, 1, 2})}): frozenset({frozenset({frozenset({0,
00417                                                                                         2}),
00418                                                                              frozenset({0,
00419                                                                                         1,
00420                                                                                         2})}),
00421                                                                   frozenset({frozenset({0,
00422                                                                                         1}),
00423                                                                              frozenset({0,
00424                                                                                         1,
00425                                                                                         2})}),
00426                                                                   frozenset({frozenset({2}),
00427                                                                              frozenset({1,
00428                                                                                         2})}),
00429                                                                   frozenset({frozenset({1}),
00430                                                                              frozenset({1,
00431                                                                                         2})})})}"""
00432 
00433         cubo = test.test_set.linegraph(cube)
00434         self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)

Here is the call graph for this function:

Definition at line 450 of file test_pprint.py.

00450 
00451     def test_sort_unorderable_values(self):
00452         # Issue 3976:  sorted pprints fail for unorderable values.
00453         n = 20
00454         keys = [Unorderable() for i in range(n)]
00455         random.shuffle(keys)
00456         skeys = sorted(keys, key=id)
00457         clean = lambda s: s.replace(' ', '').replace('\n','')
00458 
00459         self.assertEqual(clean(pprint.pformat(set(keys))),
00460             '{' + ','.join(map(repr, skeys)) + '}')
00461         self.assertEqual(clean(pprint.pformat(frozenset(keys))),
00462             'frozenset({' + ','.join(map(repr, skeys)) + '})')
00463         self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))),
00464             '{' + ','.join('%r:None' % k for k in skeys) + '}')

Here is the call graph for this function:

Definition at line 182 of file test_pprint.py.

00182 
00183     def test_sorted_dict(self):
00184         # Starting in Python 2.5, pprint sorts dict displays by key regardless
00185         # of how small the dictionary may be.
00186         # Before the change, on 32-bit Windows pformat() gave order
00187         # 'a', 'c', 'b' here, so this test failed.
00188         d = {'a': 1, 'b': 1, 'c': 1}
00189         self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
00190         self.assertEqual(pprint.pformat([d, d]),
00191             "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
00192 
00193         # The next one is kind of goofy.  The sorted order depends on the
00194         # alphabetic order of type names:  "int" < "str" < "tuple".  Before
00195         # Python 2.5, this was in the test_same_as_repr() test.  It's worth
00196         # keeping around for now because it's one of few tests of pprint
00197         # against a crazy mix of types.
00198         self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
00199             r"{5: [[]], 'xy\tab\n': (3,), (): {}}")

Here is the call graph for this function:

Definition at line 214 of file test_pprint.py.

00214 
00215     def test_subclassing(self):
00216         o = {'names with spaces': 'should be presented using repr()',
00217              'others.should.not.be': 'like.this'}
00218         exp = """\
00219 {'names with spaces': 'should be presented using repr()',
00220  others.should.not.be: like.this}"""
00221         self.assertEqual(DottedPrettyPrinter().pformat(o), exp)

Here is the call graph for this function:

Definition at line 91 of file test_pprint.py.

00091 
00092     def test_unreadable(self):
00093         # Not recursive but not readable anyway
00094         pp = pprint.PrettyPrinter()
00095         for unreadable in type(3), pprint, pprint.isrecursive:
00096             # module-level convenience functions
00097             self.assertFalse(pprint.isrecursive(unreadable),
00098                              "expected not isrecursive for %r" % (unreadable,))
00099             self.assertFalse(pprint.isreadable(unreadable),
00100                              "expected not isreadable for %r" % (unreadable,))
00101             # PrettyPrinter methods
00102             self.assertFalse(pp.isrecursive(unreadable),
00103                              "expected not isrecursive for %r" % (unreadable,))
00104             self.assertFalse(pp.isreadable(unreadable),
00105                              "expected not isreadable for %r" % (unreadable,))

Here is the call graph for this function:


Member Data Documentation

Definition at line 38 of file test_pprint.py.

Definition at line 39 of file test_pprint.py.

Definition at line 63 of file test_pprint.py.


The documentation for this class was generated from the following file: