1 import pprint 2 import test.test_support 3 import unittest 4 import test.test_set 5 6 try: 7 uni = unicode 8 except NameError: 9 def uni(x): 10 return x 11 12 # list, tuple and dict subclasses that do or don't overwrite __repr__ 13 class list2(list): 14 pass 15 16 class list3(list): 17 def __repr__(self): 18 return list.__repr__(self) 19 20 class tuple2(tuple): 21 pass 22 23 class tuple3(tuple): 24 def __repr__(self): 25 return tuple.__repr__(self) 26 27 class dict2(dict): 28 pass 29 30 class dict3(dict): 31 def __repr__(self): 32 return dict.__repr__(self) 33 34 class QueryTestCase(unittest.TestCase): 35 36 def setUp(self): 37 self.a = range(100) 38 self.b = range(200) 39 self.a[-12] = self.b 40 41 def test_basic(self): 42 # Verify .isrecursive() and .isreadable() w/o recursion 43 pp = pprint.PrettyPrinter() 44 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"), 45 self.a, self.b): 46 # module-level convenience functions 47 self.assertFalse(pprint.isrecursive(safe), 48 "expected not isrecursive for %r" % (safe,)) 49 self.assertTrue(pprint.isreadable(safe), 50 "expected isreadable for %r" % (safe,)) 51 # PrettyPrinter methods 52 self.assertFalse(pp.isrecursive(safe), 53 "expected not isrecursive for %r" % (safe,)) 54 self.assertTrue(pp.isreadable(safe), 55 "expected isreadable for %r" % (safe,)) 56 57 def test_knotted(self): 58 # Verify .isrecursive() and .isreadable() w/ recursion 59 # Tie a knot. 60 self.b[67] = self.a 61 # Messy dict. 62 self.d = {} 63 self.d[0] = self.d[1] = self.d[2] = self.d 64 65 pp = pprint.PrettyPrinter() 66 67 for icky in self.a, self.b, self.d, (self.d, self.d): 68 self.assertTrue(pprint.isrecursive(icky), "expected isrecursive") 69 self.assertFalse(pprint.isreadable(icky), "expected not isreadable") 70 self.assertTrue(pp.isrecursive(icky), "expected isrecursive") 71 self.assertFalse(pp.isreadable(icky), "expected not isreadable") 72 73 # Break the cycles. 74 self.d.clear() 75 del self.a[:] 76 del self.b[:] 77 78 for safe in self.a, self.b, self.d, (self.d, self.d): 79 # module-level convenience functions 80 self.assertFalse(pprint.isrecursive(safe), 81 "expected not isrecursive for %r" % (safe,)) 82 self.assertTrue(pprint.isreadable(safe), 83 "expected isreadable for %r" % (safe,)) 84 # PrettyPrinter methods 85 self.assertFalse(pp.isrecursive(safe), 86 "expected not isrecursive for %r" % (safe,)) 87 self.assertTrue(pp.isreadable(safe), 88 "expected isreadable for %r" % (safe,)) 89 90 def test_unreadable(self): 91 # Not recursive but not readable anyway 92 pp = pprint.PrettyPrinter() 93 for unreadable in type(3), pprint, pprint.isrecursive: 94 # module-level convenience functions 95 self.assertFalse(pprint.isrecursive(unreadable), 96 "expected not isrecursive for %r" % (unreadable,)) 97 self.assertFalse(pprint.isreadable(unreadable), 98 "expected not isreadable for %r" % (unreadable,)) 99 # PrettyPrinter methods 100 self.assertFalse(pp.isrecursive(unreadable), 101 "expected not isrecursive for %r" % (unreadable,)) 102 self.assertFalse(pp.isreadable(unreadable), 103 "expected not isreadable for %r" % (unreadable,)) 104 105 def test_same_as_repr(self): 106 # Simple objects, small containers and classes that overwrite __repr__ 107 # For those the result should be the same as repr(). 108 # Ahem. The docs don't say anything about that -- this appears to 109 # be testing an implementation quirk. Starting in Python 2.5, it's 110 # not true for dicts: pprint always sorts dicts by key now; before, 111 # it sorted a dict display if and only if the display required 112 # multiple lines. For that reason, dicts with more than one element 113 # aren't tested here. 114 for simple in (0, 0L, 0+0j, 0.0, "", uni(""), 115 (), tuple2(), tuple3(), 116 [], list2(), list3(), 117 {}, dict2(), dict3(), 118 self.assertTrue, pprint, 119 -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6}, 120 (1,2), [3,4], {5: 6}, 121 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)), 122 [3,4], list2([3,4]), list3([3,4]), list3(range(100)), 123 dict2({5: 6}), dict3({5: 6}), 124 range(10, -11, -1) 125 ): 126 native = repr(simple) 127 for function in "pformat", "saferepr": 128 f = getattr(pprint, function) 129 got = f(simple) 130 self.assertEqual(native, got, 131 "expected %s got %s from pprint.%s" % 132 (native, got, function)) 133 134 def test_basic_line_wrap(self): 135 # verify basic line-wrapping operation 136 o = {'RPM_cal': 0, 137 'RPM_cal2': 48059, 138 'Speed_cal': 0, 139 'controldesk_runtime_us': 0, 140 'main_code_runtime_us': 0, 141 'read_io_runtime_us': 0, 142 'write_io_runtime_us': 43690} 143 exp = """\ 144 {'RPM_cal': 0, 145 'RPM_cal2': 48059, 146 'Speed_cal': 0, 147 'controldesk_runtime_us': 0, 148 'main_code_runtime_us': 0, 149 'read_io_runtime_us': 0, 150 'write_io_runtime_us': 43690}""" 151 for type in [dict, dict2]: 152 self.assertEqual(pprint.pformat(type(o)), exp) 153 154 o = range(100) 155 exp = '[%s]' % ',\n '.join(map(str, o)) 156 for type in [list, list2]: 157 self.assertEqual(pprint.pformat(type(o)), exp) 158 159 o = tuple(range(100)) 160 exp = '(%s)' % ',\n '.join(map(str, o)) 161 for type in [tuple, tuple2]: 162 self.assertEqual(pprint.pformat(type(o)), exp) 163 164 # indent parameter 165 o = range(100) 166 exp = '[ %s]' % ',\n '.join(map(str, o)) 167 for type in [list, list2]: 168 self.assertEqual(pprint.pformat(type(o), indent=4), exp) 169 170 def test_nested_indentations(self): 171 o1 = list(range(10)) 172 o2 = dict(first=1, second=2, third=3) 173 o = [o1, o2] 174 expected = """\ 175 [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 176 { 'first': 1, 177 'second': 2, 178 'third': 3}]""" 179 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected) 180 181 def test_sorted_dict(self): 182 # Starting in Python 2.5, pprint sorts dict displays by key regardless 183 # of how small the dictionary may be. 184 # Before the change, on 32-bit Windows pformat() gave order 185 # 'a', 'c', 'b' here, so this test failed. 186 d = {'a': 1, 'b': 1, 'c': 1} 187 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}") 188 self.assertEqual(pprint.pformat([d, d]), 189 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]") 190 191 # The next one is kind of goofy. The sorted order depends on the 192 # alphabetic order of type names: "int" < "str" < "tuple". Before 193 # Python 2.5, this was in the test_same_as_repr() test. It's worth 194 # keeping around for now because it's one of few tests of pprint 195 # against a crazy mix of types. 196 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}), 197 r"{5: [[]], 'xy\tab\n': (3,), (): {}}") 198 199 def test_subclassing(self): 200 o = {'names with spaces': 'should be presented using repr()', 201 'others.should.not.be': 'like.this'} 202 exp = """\ 203 {'names with spaces': 'should be presented using repr()', 204 others.should.not.be: like.this}""" 205 self.assertEqual(DottedPrettyPrinter().pformat(o), exp) 206 207 def test_set_reprs(self): 208 self.assertEqual(pprint.pformat(set()), 'set()') 209 self.assertEqual(pprint.pformat(set(range(3))), 'set([0, 1, 2])') 210 self.assertEqual(pprint.pformat(frozenset()), 'frozenset()') 211 self.assertEqual(pprint.pformat(frozenset(range(3))), 'frozenset([0, 1, 2])') 212 cube_repr_tgt = """\ 213 {frozenset([]): frozenset([frozenset([2]), frozenset([0]), frozenset([1])]), 214 frozenset([0]): frozenset([frozenset(), 215 frozenset([0, 2]), 216 frozenset([0, 1])]), 217 frozenset([1]): frozenset([frozenset(), 218 frozenset([1, 2]), 219 frozenset([0, 1])]), 220 frozenset([2]): frozenset([frozenset(), 221 frozenset([1, 2]), 222 frozenset([0, 2])]), 223 frozenset([1, 2]): frozenset([frozenset([2]), 224 frozenset([1]), 225 frozenset([0, 1, 2])]), 226 frozenset([0, 2]): frozenset([frozenset([2]), 227 frozenset([0]), 228 frozenset([0, 1, 2])]), 229 frozenset([0, 1]): frozenset([frozenset([0]), 230 frozenset([1]), 231 frozenset([0, 1, 2])]), 232 frozenset([0, 1, 2]): frozenset([frozenset([1, 2]), 233 frozenset([0, 2]), 234 frozenset([0, 1])])}""" 235 cube = test.test_set.cube(3) 236 self.assertEqual(pprint.pformat(cube), cube_repr_tgt) 237 cubo_repr_tgt = """\ 238 {frozenset([frozenset([0, 2]), frozenset([0])]): frozenset([frozenset([frozenset([0, 239 2]), 240 frozenset([0, 241 1, 242 2])]), 243 frozenset([frozenset([0]), 244 frozenset([0, 245 1])]), 246 frozenset([frozenset(), 247 frozenset([0])]), 248 frozenset([frozenset([2]), 249 frozenset([0, 250 2])])]), 251 frozenset([frozenset([0, 1]), frozenset([1])]): frozenset([frozenset([frozenset([0, 252 1]), 253 frozenset([0, 254 1, 255 2])]), 256 frozenset([frozenset([0]), 257 frozenset([0, 258 1])]), 259 frozenset([frozenset([1]), 260 frozenset([1, 261 2])]), 262 frozenset([frozenset(), 263 frozenset([1])])]), 264 frozenset([frozenset([1, 2]), frozenset([1])]): frozenset([frozenset([frozenset([1, 265 2]), 266 frozenset([0, 267 1, 268 2])]), 269 frozenset([frozenset([2]), 270 frozenset([1, 271 2])]), 272 frozenset([frozenset(), 273 frozenset([1])]), 274 frozenset([frozenset([1]), 275 frozenset([0, 276 1])])]), 277 frozenset([frozenset([1, 2]), frozenset([2])]): frozenset([frozenset([frozenset([1, 278 2]), 279 frozenset([0, 280 1, 281 2])]), 282 frozenset([frozenset([1]), 283 frozenset([1, 284 2])]), 285 frozenset([frozenset([2]), 286 frozenset([0, 287 2])]), 288 frozenset([frozenset(), 289 frozenset([2])])]), 290 frozenset([frozenset([]), frozenset([0])]): frozenset([frozenset([frozenset([0]), 291 frozenset([0, 292 1])]), 293 frozenset([frozenset([0]), 294 frozenset([0, 295 2])]), 296 frozenset([frozenset(), 297 frozenset([1])]), 298 frozenset([frozenset(), 299 frozenset([2])])]), 300 frozenset([frozenset([]), frozenset([1])]): frozenset([frozenset([frozenset(), 301 frozenset([0])]), 302 frozenset([frozenset([1]), 303 frozenset([1, 304 2])]), 305 frozenset([frozenset(), 306 frozenset([2])]), 307 frozenset([frozenset([1]), 308 frozenset([0, 309 1])])]), 310 frozenset([frozenset([2]), frozenset([])]): frozenset([frozenset([frozenset([2]), 311 frozenset([1, 312 2])]), 313 frozenset([frozenset(), 314 frozenset([0])]), 315 frozenset([frozenset(), 316 frozenset([1])]), 317 frozenset([frozenset([2]), 318 frozenset([0, 319 2])])]), 320 frozenset([frozenset([0, 1, 2]), frozenset([0, 1])]): frozenset([frozenset([frozenset([1, 321 2]), 322 frozenset([0, 323 1, 324 2])]), 325 frozenset([frozenset([0, 326 2]), 327 frozenset([0, 328 1, 329 2])]), 330 frozenset([frozenset([0]), 331 frozenset([0, 332 1])]), 333 frozenset([frozenset([1]), 334 frozenset([0, 335 1])])]), 336 frozenset([frozenset([0]), frozenset([0, 1])]): frozenset([frozenset([frozenset(), 337 frozenset([0])]), 338 frozenset([frozenset([0, 339 1]), 340 frozenset([0, 341 1, 342 2])]), 343 frozenset([frozenset([0]), 344 frozenset([0, 345 2])]), 346 frozenset([frozenset([1]), 347 frozenset([0, 348 1])])]), 349 frozenset([frozenset([2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([0, 350 2]), 351 frozenset([0, 352 1, 353 2])]), 354 frozenset([frozenset([2]), 355 frozenset([1, 356 2])]), 357 frozenset([frozenset([0]), 358 frozenset([0, 359 2])]), 360 frozenset([frozenset(), 361 frozenset([2])])]), 362 frozenset([frozenset([0, 1, 2]), frozenset([0, 2])]): frozenset([frozenset([frozenset([1, 363 2]), 364 frozenset([0, 365 1, 366 2])]), 367 frozenset([frozenset([0, 368 1]), 369 frozenset([0, 370 1, 371 2])]), 372 frozenset([frozenset([0]), 373 frozenset([0, 374 2])]), 375 frozenset([frozenset([2]), 376 frozenset([0, 377 2])])]), 378 frozenset([frozenset([1, 2]), frozenset([0, 1, 2])]): frozenset([frozenset([frozenset([0, 379 2]), 380 frozenset([0, 381 1, 382 2])]), 383 frozenset([frozenset([0, 384 1]), 385 frozenset([0, 386 1, 387 2])]), 388 frozenset([frozenset([2]), 389 frozenset([1, 390 2])]), 391 frozenset([frozenset([1]), 392 frozenset([1, 393 2])])])}""" 394 395 cubo = test.test_set.linegraph(cube) 396 self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt) 397 398 def test_depth(self): 399 nested_tuple = (1, (2, (3, (4, (5, 6))))) 400 nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}} 401 nested_list = [1, [2, [3, [4, [5, [6, []]]]]]] 402 self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple)) 403 self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict)) 404 self.assertEqual(pprint.pformat(nested_list), repr(nested_list)) 405 406 lv1_tuple = '(1, (...))' 407 lv1_dict = '{1: {...}}' 408 lv1_list = '[1, [...]]' 409 self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple) 410 self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict) 411 self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list) 412 413 414 class DottedPrettyPrinter(pprint.PrettyPrinter): 415 416 def format(self, object, context, maxlevels, level): 417 if isinstance(object, str): 418 if ' ' in object: 419 return repr(object), 1, 0 420 else: 421 return object, 0, 0 422 else: 423 return pprint.PrettyPrinter.format( 424 self, object, context, maxlevels, level) 425 426 427 def test_main(): 428 test.test_support.run_unittest(QueryTestCase) 429 430 431 if __name__ == "__main__": 432 test_main() 433