home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_pprint.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  3.4 KB  |  105 lines

  1. import pprint
  2. import unittest
  3.  
  4. import test_support
  5.  
  6. try:
  7.     uni = unicode
  8. except NameError:
  9.     def uni(x):return x
  10.  
  11.  
  12. class QueryTestCase(unittest.TestCase):
  13.  
  14.     def setUp(self):
  15.         self.a = range(100)
  16.         self.b = range(200)
  17.         self.a[-12] = self.b
  18.  
  19.     def test_basic(self):
  20.         """Verify .isrecursive() and .isreadable() w/o recursion."""
  21.         verify = self.assert_
  22.         for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
  23.                      self.a, self.b):
  24.             verify(not pprint.isrecursive(safe),
  25.                    "expected not isrecursive for " + `safe`)
  26.             verify(pprint.isreadable(safe),
  27.                    "expected isreadable for " + `safe`)
  28.  
  29.     def test_knotted(self):
  30.         """Verify .isrecursive() and .isreadable() w/ recursion."""
  31.         # Tie a knot.
  32.         self.b[67] = self.a
  33.         # Messy dict.
  34.         self.d = {}
  35.         self.d[0] = self.d[1] = self.d[2] = self.d
  36.  
  37.         verify = self.assert_
  38.  
  39.         for icky in self.a, self.b, self.d, (self.d, self.d):
  40.             verify(pprint.isrecursive(icky), "expected isrecursive")
  41.             verify(not pprint.isreadable(icky),  "expected not isreadable")
  42.  
  43.         # Break the cycles.
  44.         self.d.clear()
  45.         del self.a[:]
  46.         del self.b[:]
  47.  
  48.         for safe in self.a, self.b, self.d, (self.d, self.d):
  49.             verify(not pprint.isrecursive(safe),
  50.                    "expected not isrecursive for " + `safe`)
  51.             verify(pprint.isreadable(safe),
  52.                    "expected isreadable for " + `safe`)
  53.  
  54.     def test_unreadable(self):
  55.         """Not recursive but not readable anyway."""
  56.         verify = self.assert_
  57.         for unreadable in type(3), pprint, pprint.isrecursive:
  58.             verify(not pprint.isrecursive(unreadable),
  59.                    "expected not isrecursive for " + `unreadable`)
  60.             verify(not pprint.isreadable(unreadable),
  61.                    "expected not isreadable for " + `unreadable`)
  62.  
  63.     def test_same_as_repr(self):
  64.         "Simple objects and small containers that should be same as repr()."
  65.         verify = self.assert_
  66.         for simple in (0, 0L, 0+0j, 0.0, "", uni(""), (), [], {}, verify, pprint,
  67.                        -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6},
  68.                        (1,2), [3,4], {5: 6, 7: 8},
  69.                        {"xy\tab\n": (3,), 5: [[]], (): {}},
  70.                        range(10, -11, -1)
  71.                       ):
  72.             native = repr(simple)
  73.             for function in "pformat", "saferepr":
  74.                 f = getattr(pprint, function)
  75.                 got = f(simple)
  76.                 verify(native == got, "expected %s got %s from pprint.%s" %
  77.                                       (native, got, function))
  78.  
  79.  
  80.     def test_basic_line_wrap(self):
  81.         """verify basic line-wrapping operation"""
  82.         o = {'RPM_cal': 0,
  83.              'RPM_cal2': 48059,
  84.              'Speed_cal': 0,
  85.              'controldesk_runtime_us': 0,
  86.              'main_code_runtime_us': 0,
  87.              'read_io_runtime_us': 0,
  88.              'write_io_runtime_us': 43690}
  89.         exp = """\
  90. {'RPM_cal': 0,
  91.  'RPM_cal2': 48059,
  92.  'Speed_cal': 0,
  93.  'controldesk_runtime_us': 0,
  94.  'main_code_runtime_us': 0,
  95.  'read_io_runtime_us': 0,
  96.  'write_io_runtime_us': 43690}"""
  97.         self.assertEqual(pprint.pformat(o), exp)
  98.  
  99. def test_main():
  100.     test_support.run_unittest(QueryTestCase)
  101.  
  102.  
  103. if __name__ == "__main__":
  104.     test_main()
  105.