Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test cases for the repr module |
| 3 | Nick Mathewson |
| 4 | """ |
| 5 | |
Antoine Pitrou | 01296da | 2012-04-24 13:55:35 +0200 | [diff] [blame] | 6 | import imp |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 7 | import sys |
| 8 | import os |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 9 | import shutil |
Antoine Pitrou | c541f8e | 2012-02-20 01:48:16 +0100 | [diff] [blame] | 10 | import importlib |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 11 | import unittest |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 12 | |
Antoine Pitrou | 541b7c8 | 2012-06-23 00:07:38 +0200 | [diff] [blame] | 13 | from test.support import run_unittest, create_empty_file, verbose |
Alexandre Vassalotti | 1f2ba4b | 2008-05-16 07:12:44 +0000 | [diff] [blame] | 14 | from reprlib import repr as r # Don't shadow builtin repr |
| 15 | from reprlib import Repr |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 16 | from reprlib import recursive_repr |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | def nestedTuple(nesting): |
| 20 | t = () |
| 21 | for i in range(nesting): |
| 22 | t = (t,) |
| 23 | return t |
| 24 | |
| 25 | class ReprTests(unittest.TestCase): |
| 26 | |
| 27 | def test_string(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 28 | eq = self.assertEqual |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 29 | eq(r("abc"), "'abc'") |
| 30 | eq(r("abcdefghijklmnop"),"'abcdefghijklmnop'") |
| 31 | |
| 32 | s = "a"*30+"b"*30 |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 33 | expected = repr(s)[:13] + "..." + repr(s)[-14:] |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 34 | eq(r(s), expected) |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 35 | |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 36 | eq(r("\"'"), repr("\"'")) |
| 37 | s = "\""*30+"'"*100 |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 38 | expected = repr(s)[:13] + "..." + repr(s)[-14:] |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 39 | eq(r(s), expected) |
| 40 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 41 | def test_tuple(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 42 | eq = self.assertEqual |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 43 | eq(r((1,)), "(1,)") |
| 44 | |
| 45 | t3 = (1, 2, 3) |
| 46 | eq(r(t3), "(1, 2, 3)") |
| 47 | |
| 48 | r2 = Repr() |
| 49 | r2.maxtuple = 2 |
| 50 | expected = repr(t3)[:-2] + "...)" |
| 51 | eq(r2.repr(t3), expected) |
| 52 | |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 53 | def test_container(self): |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 54 | from array import array |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 55 | from collections import deque |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 56 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 57 | eq = self.assertEqual |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 58 | # Tuples give up after 6 elements |
| 59 | eq(r(()), "()") |
| 60 | eq(r((1,)), "(1,)") |
| 61 | eq(r((1, 2, 3)), "(1, 2, 3)") |
| 62 | eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)") |
| 63 | eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)") |
| 64 | |
| 65 | # Lists give up after 6 as well |
| 66 | eq(r([]), "[]") |
| 67 | eq(r([1]), "[1]") |
| 68 | eq(r([1, 2, 3]), "[1, 2, 3]") |
| 69 | eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]") |
| 70 | eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]") |
| 71 | |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 72 | # Sets give up after 6 as well |
| 73 | eq(r(set([])), "set([])") |
| 74 | eq(r(set([1])), "set([1])") |
| 75 | eq(r(set([1, 2, 3])), "set([1, 2, 3])") |
| 76 | eq(r(set([1, 2, 3, 4, 5, 6])), "set([1, 2, 3, 4, 5, 6])") |
| 77 | eq(r(set([1, 2, 3, 4, 5, 6, 7])), "set([1, 2, 3, 4, 5, 6, ...])") |
| 78 | |
| 79 | # Frozensets give up after 6 as well |
| 80 | eq(r(frozenset([])), "frozenset([])") |
| 81 | eq(r(frozenset([1])), "frozenset([1])") |
| 82 | eq(r(frozenset([1, 2, 3])), "frozenset([1, 2, 3])") |
| 83 | eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset([1, 2, 3, 4, 5, 6])") |
| 84 | eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset([1, 2, 3, 4, 5, 6, ...])") |
| 85 | |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 86 | # collections.deque after 6 |
| 87 | eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])") |
| 88 | |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 89 | # Dictionaries give up after 4. |
| 90 | eq(r({}), "{}") |
| 91 | d = {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4} |
| 92 | eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}") |
| 93 | d['arthur'] = 1 |
| 94 | eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}") |
| 95 | |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 96 | # array.array after 5. |
| 97 | eq(r(array('i')), "array('i', [])") |
| 98 | eq(r(array('i', [1])), "array('i', [1])") |
| 99 | eq(r(array('i', [1, 2])), "array('i', [1, 2])") |
| 100 | eq(r(array('i', [1, 2, 3])), "array('i', [1, 2, 3])") |
| 101 | eq(r(array('i', [1, 2, 3, 4])), "array('i', [1, 2, 3, 4])") |
| 102 | eq(r(array('i', [1, 2, 3, 4, 5])), "array('i', [1, 2, 3, 4, 5])") |
| 103 | eq(r(array('i', [1, 2, 3, 4, 5, 6])), |
| 104 | "array('i', [1, 2, 3, 4, 5, ...])") |
| 105 | |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 106 | def test_numbers(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 107 | eq = self.assertEqual |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 108 | eq(r(123), repr(123)) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 109 | eq(r(123), repr(123)) |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 110 | eq(r(1.0/3), repr(1.0/3)) |
| 111 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 112 | n = 10**100 |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 113 | expected = repr(n)[:18] + "..." + repr(n)[-19:] |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 114 | eq(r(n), expected) |
| 115 | |
| 116 | def test_instance(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 117 | eq = self.assertEqual |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 118 | i1 = ClassWithRepr("a") |
| 119 | eq(r(i1), repr(i1)) |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 120 | |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 121 | i2 = ClassWithRepr("x"*1000) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 122 | expected = repr(i2)[:13] + "..." + repr(i2)[-14:] |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 123 | eq(r(i2), expected) |
| 124 | |
| 125 | i3 = ClassWithFailingRepr() |
| 126 | eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%id(i3))) |
| 127 | |
Guido van Rossum | cf856f9 | 2001-09-05 02:26:26 +0000 | [diff] [blame] | 128 | s = r(ClassWithFailingRepr) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 129 | self.assertTrue(s.startswith("<class ")) |
| 130 | self.assertTrue(s.endswith(">")) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 131 | self.assertIn(s.find("..."), [12, 13]) |
Guido van Rossum | cf856f9 | 2001-09-05 02:26:26 +0000 | [diff] [blame] | 132 | |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 133 | def test_lambda(self): |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 134 | r = repr(lambda x: x) |
| 135 | self.assertTrue(r.startswith("<function ReprTests.test_lambda.<locals>.<lambda"), r) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 136 | # XXX anonymous functions? see func_repr |
| 137 | |
| 138 | def test_builtin_function(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 139 | eq = self.assertEqual |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 140 | # Functions |
| 141 | eq(repr(hash), '<built-in function hash>') |
| 142 | # Methods |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 143 | self.assertTrue(repr(''.split).startswith( |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 144 | '<built-in method split of str object at 0x')) |
| 145 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 146 | def test_range(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 147 | eq = self.assertEqual |
Guido van Rossum | 3353a2e | 2007-05-21 18:14:54 +0000 | [diff] [blame] | 148 | eq(repr(range(1)), 'range(0, 1)') |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 149 | eq(repr(range(1, 2)), 'range(1, 2)') |
| 150 | eq(repr(range(1, 4, 3)), 'range(1, 4, 3)') |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 151 | |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 152 | def test_nesting(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 153 | eq = self.assertEqual |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 154 | # everything is meant to give up after 6 levels. |
| 155 | eq(r([[[[[[[]]]]]]]), "[[[[[[[]]]]]]]") |
| 156 | eq(r([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]") |
| 157 | |
| 158 | eq(r(nestedTuple(6)), "(((((((),),),),),),)") |
| 159 | eq(r(nestedTuple(7)), "(((((((...),),),),),),)") |
| 160 | |
| 161 | eq(r({ nestedTuple(5) : nestedTuple(5) }), |
| 162 | "{((((((),),),),),): ((((((),),),),),)}") |
| 163 | eq(r({ nestedTuple(6) : nestedTuple(6) }), |
| 164 | "{((((((...),),),),),): ((((((...),),),),),)}") |
| 165 | |
| 166 | eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]") |
| 167 | eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]") |
| 168 | |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 169 | def test_cell(self): |
| 170 | # XXX Hmm? How to get at a cell object? |
| 171 | pass |
| 172 | |
| 173 | def test_descriptors(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 174 | eq = self.assertEqual |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 175 | # method descriptors |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 176 | eq(repr(dict.items), "<method 'items' of 'dict' objects>") |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 177 | # XXX member descriptors |
| 178 | # XXX attribute descriptors |
| 179 | # XXX slot descriptors |
| 180 | # static and class methods |
| 181 | class C: |
| 182 | def foo(cls): pass |
| 183 | x = staticmethod(C.foo) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 184 | self.assertTrue(repr(x).startswith('<staticmethod object at 0x')) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 185 | x = classmethod(C.foo) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 186 | self.assertTrue(repr(x).startswith('<classmethod object at 0x')) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 187 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 188 | def test_unsortable(self): |
| 189 | # Repr.repr() used to call sorted() on sets, frozensets and dicts |
| 190 | # without taking into account that not all objects are comparable |
| 191 | x = set([1j, 2j, 3j]) |
| 192 | y = frozenset(x) |
| 193 | z = {1j: 1, 2j: 2} |
| 194 | r(x) |
| 195 | r(y) |
| 196 | r(z) |
| 197 | |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 198 | def write_file(path, text): |
| 199 | with open(path, 'w', encoding='ASCII') as fp: |
| 200 | fp.write(text) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 201 | |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 202 | class LongReprTest(unittest.TestCase): |
Antoine Pitrou | 01296da | 2012-04-24 13:55:35 +0200 | [diff] [blame] | 203 | longname = 'areallylongpackageandmodulenametotestreprtruncation' |
| 204 | |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 205 | def setUp(self): |
Antoine Pitrou | 01296da | 2012-04-24 13:55:35 +0200 | [diff] [blame] | 206 | self.pkgname = os.path.join(self.longname) |
| 207 | self.subpkgname = os.path.join(self.longname, self.longname) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 208 | # Make the package and subpackage |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 209 | shutil.rmtree(self.pkgname, ignore_errors=True) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 210 | os.mkdir(self.pkgname) |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 211 | create_empty_file(os.path.join(self.pkgname, '__init__.py')) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 212 | shutil.rmtree(self.subpkgname, ignore_errors=True) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 213 | os.mkdir(self.subpkgname) |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 214 | create_empty_file(os.path.join(self.subpkgname, '__init__.py')) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 215 | # Remember where we are |
| 216 | self.here = os.getcwd() |
| 217 | sys.path.insert(0, self.here) |
Brett Cannon | ceffda8 | 2012-04-16 20:48:50 -0400 | [diff] [blame] | 218 | # When regrtest is run with its -j option, this command alone is not |
| 219 | # enough. |
Antoine Pitrou | c541f8e | 2012-02-20 01:48:16 +0100 | [diff] [blame] | 220 | importlib.invalidate_caches() |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 221 | |
| 222 | def tearDown(self): |
| 223 | actions = [] |
Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 224 | for dirpath, dirnames, filenames in os.walk(self.pkgname): |
| 225 | for name in dirnames + filenames: |
| 226 | actions.append(os.path.join(dirpath, name)) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 227 | actions.append(self.pkgname) |
| 228 | actions.sort() |
| 229 | actions.reverse() |
| 230 | for p in actions: |
| 231 | if os.path.isdir(p): |
| 232 | os.rmdir(p) |
| 233 | else: |
| 234 | os.remove(p) |
| 235 | del sys.path[0] |
| 236 | |
Antoine Pitrou | 01296da | 2012-04-24 13:55:35 +0200 | [diff] [blame] | 237 | def _check_path_limitations(self, module_name): |
| 238 | # base directory |
| 239 | source_path_len = len(self.here) |
| 240 | # a path separator + `longname` (twice) |
| 241 | source_path_len += 2 * (len(self.longname) + 1) |
| 242 | # a path separator + `module_name` + ".py" |
| 243 | source_path_len += len(module_name) + 1 + len(".py") |
| 244 | cached_path_len = source_path_len + len(imp.cache_from_source("x.py")) - len("x.py") |
Antoine Pitrou | 110ee34 | 2012-06-23 22:55:58 +0200 | [diff] [blame] | 245 | if os.name == 'nt' and cached_path_len >= 258: |
Antoine Pitrou | 01296da | 2012-04-24 13:55:35 +0200 | [diff] [blame] | 246 | # Under Windows, the max path len is 260 including C's terminating |
| 247 | # NUL character. |
| 248 | # (see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx#maxpath) |
| 249 | self.skipTest("test paths too long (%d characters) for Windows' 260 character limit" |
| 250 | % cached_path_len) |
Antoine Pitrou | 541b7c8 | 2012-06-23 00:07:38 +0200 | [diff] [blame] | 251 | elif os.name == 'nt' and verbose: |
Antoine Pitrou | f0f4742 | 2012-06-23 00:49:44 +0200 | [diff] [blame] | 252 | print("cached_path_len =", cached_path_len) |
Antoine Pitrou | 01296da | 2012-04-24 13:55:35 +0200 | [diff] [blame] | 253 | |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 254 | def test_module(self): |
Antoine Pitrou | 01296da | 2012-04-24 13:55:35 +0200 | [diff] [blame] | 255 | self._check_path_limitations(self.pkgname) |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 256 | create_empty_file(os.path.join(self.subpkgname, self.pkgname + '.py')) |
Brett Cannon | ceffda8 | 2012-04-16 20:48:50 -0400 | [diff] [blame] | 257 | importlib.invalidate_caches() |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 258 | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation |
Brett Cannon | 7b383c4 | 2012-06-11 11:02:36 -0400 | [diff] [blame] | 259 | module = areallylongpackageandmodulenametotestreprtruncation |
| 260 | self.assertEqual(repr(module), "<module %r from %r>" % (module.__name__, module.__file__)) |
| 261 | self.assertEqual(repr(sys), "<module 'sys' (built-in)>") |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 262 | |
| 263 | def test_type(self): |
Antoine Pitrou | 01296da | 2012-04-24 13:55:35 +0200 | [diff] [blame] | 264 | self._check_path_limitations('foo') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 265 | eq = self.assertEqual |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 266 | write_file(os.path.join(self.subpkgname, 'foo.py'), '''\ |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 267 | class foo(object): |
| 268 | pass |
| 269 | ''') |
Brett Cannon | ceffda8 | 2012-04-16 20:48:50 -0400 | [diff] [blame] | 270 | importlib.invalidate_caches() |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 271 | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import foo |
| 272 | eq(repr(foo.foo), |
Mark Hammond | d800ae1 | 2003-01-16 04:56:52 +0000 | [diff] [blame] | 273 | "<class '%s.foo'>" % foo.__name__) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 274 | |
| 275 | def test_object(self): |
| 276 | # XXX Test the repr of a type with a really long tp_name but with no |
| 277 | # tp_repr. WIBNI we had ::Inline? :) |
| 278 | pass |
| 279 | |
| 280 | def test_class(self): |
Antoine Pitrou | 01296da | 2012-04-24 13:55:35 +0200 | [diff] [blame] | 281 | self._check_path_limitations('bar') |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 282 | write_file(os.path.join(self.subpkgname, 'bar.py'), '''\ |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 283 | class bar: |
| 284 | pass |
| 285 | ''') |
Brett Cannon | ceffda8 | 2012-04-16 20:48:50 -0400 | [diff] [blame] | 286 | importlib.invalidate_caches() |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 287 | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import bar |
Mark Hammond | d800ae1 | 2003-01-16 04:56:52 +0000 | [diff] [blame] | 288 | # Module name may be prefixed with "test.", depending on how run. |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 289 | self.assertEqual(repr(bar.bar), "<class '%s.bar'>" % bar.__name__) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 290 | |
| 291 | def test_instance(self): |
Antoine Pitrou | 01296da | 2012-04-24 13:55:35 +0200 | [diff] [blame] | 292 | self._check_path_limitations('baz') |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 293 | write_file(os.path.join(self.subpkgname, 'baz.py'), '''\ |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 294 | class baz: |
| 295 | pass |
| 296 | ''') |
Brett Cannon | ceffda8 | 2012-04-16 20:48:50 -0400 | [diff] [blame] | 297 | importlib.invalidate_caches() |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 298 | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import baz |
| 299 | ibaz = baz.baz() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 300 | self.assertTrue(repr(ibaz).startswith( |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 301 | "<%s.baz object at 0x" % baz.__name__)) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 302 | |
| 303 | def test_method(self): |
Antoine Pitrou | 01296da | 2012-04-24 13:55:35 +0200 | [diff] [blame] | 304 | self._check_path_limitations('qux') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 305 | eq = self.assertEqual |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 306 | write_file(os.path.join(self.subpkgname, 'qux.py'), '''\ |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 307 | class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: |
| 308 | def amethod(self): pass |
| 309 | ''') |
Brett Cannon | ceffda8 | 2012-04-16 20:48:50 -0400 | [diff] [blame] | 310 | importlib.invalidate_caches() |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 311 | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import qux |
| 312 | # Unbound methods first |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 313 | r = repr(qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod) |
| 314 | self.assertTrue(r.startswith('<function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod'), r) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 315 | # Bound method next |
| 316 | iqux = qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 317 | r = repr(iqux.amethod) |
| 318 | self.assertTrue(r.startswith( |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 319 | '<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa object at 0x' \ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 320 | % (qux.__name__,) ), r) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 321 | |
| 322 | def test_builtin_function(self): |
| 323 | # XXX test built-in functions and methods with really long names |
| 324 | pass |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 325 | |
| 326 | class ClassWithRepr: |
| 327 | def __init__(self, s): |
| 328 | self.s = s |
| 329 | def __repr__(self): |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 330 | return "ClassWithRepr(%r)" % self.s |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 331 | |
| 332 | |
| 333 | class ClassWithFailingRepr: |
| 334 | def __repr__(self): |
| 335 | raise Exception("This should be caught by Repr.repr_instance") |
| 336 | |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 337 | class MyContainer: |
| 338 | 'Helper class for TestRecursiveRepr' |
| 339 | def __init__(self, values): |
| 340 | self.values = list(values) |
| 341 | def append(self, value): |
| 342 | self.values.append(value) |
| 343 | @recursive_repr() |
| 344 | def __repr__(self): |
| 345 | return '<' + ', '.join(map(str, self.values)) + '>' |
| 346 | |
| 347 | class MyContainer2(MyContainer): |
| 348 | @recursive_repr('+++') |
| 349 | def __repr__(self): |
| 350 | return '<' + ', '.join(map(str, self.values)) + '>' |
| 351 | |
| 352 | class TestRecursiveRepr(unittest.TestCase): |
| 353 | def test_recursive_repr(self): |
| 354 | m = MyContainer(list('abcde')) |
| 355 | m.append(m) |
| 356 | m.append('x') |
| 357 | m.append(m) |
| 358 | self.assertEqual(repr(m), '<a, b, c, d, e, ..., x, ...>') |
| 359 | m = MyContainer2(list('abcde')) |
| 360 | m.append(m) |
| 361 | m.append('x') |
| 362 | m.append(m) |
| 363 | self.assertEqual(repr(m), '<a, b, c, d, e, +++, x, +++>') |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 364 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 365 | def test_main(): |
| 366 | run_unittest(ReprTests) |
Ronald Oussoren | 94f2528 | 2010-05-05 19:11:21 +0000 | [diff] [blame] | 367 | run_unittest(LongReprTest) |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 368 | run_unittest(TestRecursiveRepr) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 369 | |
| 370 | |
| 371 | if __name__ == "__main__": |
| 372 | test_main() |