Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 1 | import pprint |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 2 | import unittest |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 3 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 4 | import test_support |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 5 | |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 6 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 7 | class QueryTestCase(unittest.TestCase): |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 8 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 9 | def setUp(self): |
| 10 | self.a = range(100) |
| 11 | self.b = range(200) |
| 12 | self.a[-12] = self.b |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 13 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 14 | def test_basic(self): |
Fred Drake | 992d387 | 2001-05-14 19:15:23 +0000 | [diff] [blame] | 15 | """Verify .isrecursive() and .isreadable() w/o recursion.""" |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 16 | verify = self.assert_ |
| 17 | for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, u"yaddayadda", |
| 18 | self.a, self.b): |
| 19 | verify(not pprint.isrecursive(safe), |
| 20 | "expected not isrecursive for " + `safe`) |
| 21 | verify(pprint.isreadable(safe), |
| 22 | "expected isreadable for " + `safe`) |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 23 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 24 | def test_knotted(self): |
Fred Drake | 992d387 | 2001-05-14 19:15:23 +0000 | [diff] [blame] | 25 | """Verify .isrecursive() and .isreadable() w/ recursion.""" |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 26 | # Tie a knot. |
| 27 | self.b[67] = self.a |
| 28 | # Messy dict. |
| 29 | self.d = {} |
| 30 | self.d[0] = self.d[1] = self.d[2] = self.d |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 31 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 32 | verify = self.assert_ |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 33 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 34 | for icky in self.a, self.b, self.d, (self.d, self.d): |
| 35 | verify(pprint.isrecursive(icky), "expected isrecursive") |
| 36 | verify(not pprint.isreadable(icky), "expected not isreadable") |
| 37 | |
| 38 | # Break the cycles. |
| 39 | self.d.clear() |
| 40 | del self.a[:] |
| 41 | del self.b[:] |
| 42 | |
| 43 | for safe in self.a, self.b, self.d, (self.d, self.d): |
| 44 | verify(not pprint.isrecursive(safe), |
| 45 | "expected not isrecursive for " + `safe`) |
| 46 | verify(pprint.isreadable(safe), |
| 47 | "expected isreadable for " + `safe`) |
| 48 | |
| 49 | def test_unreadable(self): |
| 50 | """Not recursive but not readable anyway.""" |
| 51 | verify = self.assert_ |
| 52 | for unreadable in type(3), pprint, pprint.isrecursive: |
| 53 | verify(not pprint.isrecursive(unreadable), |
| 54 | "expected not isrecursive for " + `unreadable`) |
| 55 | verify(not pprint.isreadable(unreadable), |
| 56 | "expected not isreadable for " + `unreadable`) |
| 57 | |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 58 | def test_same_as_repr(self): |
| 59 | "Simple objects and small containers that should be same as repr()." |
| 60 | verify = self.assert_ |
| 61 | for simple in (0, 0L, 0+0j, 0.0, "", u"", (), [], {}, verify, pprint, |
| 62 | -6, -6L, -6-6j, -1.5, "x", u"x", (3,), [3], {3: 6}, |
| 63 | (1,2), [3,4], {5: 6, 7: 8}, |
| 64 | {"xy\tab\n": (3,), 5: [[]], (): {}}, |
| 65 | range(10, -11, -1) |
| 66 | ): |
| 67 | native = repr(simple) |
| 68 | for function in "pformat", "saferepr": |
| 69 | f = getattr(pprint, function) |
| 70 | got = f(simple) |
| 71 | verify(native == got, "expected %s got %s from pprint.%s" % |
| 72 | (native, got, function)) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 73 | |
| 74 | test_support.run_unittest(QueryTestCase) |