/Lib/test/test_pprint.py

http://unladen-swallow.googlecode.com/ · Python · 435 lines · 367 code · 36 blank · 32 comment · 17 complexity · 57478217c80e2a4d5f3f80c00a3c9670 MD5 · raw file

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