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 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 6 | try: |
| 7 | uni = unicode |
| 8 | except NameError: |
| 9 | def uni(x):return x |
| 10 | |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 11 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 12 | class QueryTestCase(unittest.TestCase): |
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 setUp(self): |
| 15 | self.a = range(100) |
| 16 | self.b = range(200) |
| 17 | self.a[-12] = self.b |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 18 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 19 | def test_basic(self): |
Fred Drake | 992d387 | 2001-05-14 19:15:23 +0000 | [diff] [blame] | 20 | """Verify .isrecursive() and .isreadable() w/o recursion.""" |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 21 | verify = self.assert_ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 22 | for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"), |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 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`) |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 28 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 29 | def test_knotted(self): |
Fred Drake | 992d387 | 2001-05-14 19:15:23 +0000 | [diff] [blame] | 30 | """Verify .isrecursive() and .isreadable() w/ recursion.""" |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 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 |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 36 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 37 | verify = self.assert_ |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 38 | |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 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 | |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 63 | def test_same_as_repr(self): |
| 64 | "Simple objects and small containers that should be same as repr()." |
| 65 | verify = self.assert_ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 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}, |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 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)) |
Fred Drake | 43913dd | 2001-05-14 17:41:20 +0000 | [diff] [blame] | 78 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 79 | |
| 80 | def test_main(): |
| 81 | test_support.run_unittest(QueryTestCase) |
| 82 | |
| 83 | |
| 84 | if __name__ == "__main__": |
| 85 | test_main() |