blob: ae67f06bc4efe4937a5fa0c18255668f9acebddb [file] [log] [blame]
Fred Drake8e6669a2001-07-19 22:27:56 +00001"""
2 Test cases for the repr module
3 Nick Mathewson
4"""
5
Barry Warsaw0bcf6d82001-08-24 18:37:32 +00006import sys
7import os
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00008import shutil
Antoine Pitrouc541f8e2012-02-20 01:48:16 +01009import importlib
Brett Cannon9529fbf2013-06-15 17:11:25 -040010import importlib.util
Fred Drake8e6669a2001-07-19 22:27:56 +000011import unittest
Barry Warsaw0bcf6d82001-08-24 18:37:32 +000012
Antoine Pitrou541b7c82012-06-23 00:07:38 +020013from test.support import run_unittest, create_empty_file, verbose
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +000014from reprlib import repr as r # Don't shadow builtin repr
15from reprlib import Repr
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +000016from reprlib import recursive_repr
Fred Drake8e6669a2001-07-19 22:27:56 +000017
18
19def nestedTuple(nesting):
20 t = ()
21 for i in range(nesting):
22 t = (t,)
23 return t
24
25class ReprTests(unittest.TestCase):
26
27 def test_string(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000028 eq = self.assertEqual
Fred Drake8e6669a2001-07-19 22:27:56 +000029 eq(r("abc"), "'abc'")
30 eq(r("abcdefghijklmnop"),"'abcdefghijklmnop'")
31
32 s = "a"*30+"b"*30
Walter Dörwald70a6b492004-02-12 17:35:32 +000033 expected = repr(s)[:13] + "..." + repr(s)[-14:]
Fred Drake8e6669a2001-07-19 22:27:56 +000034 eq(r(s), expected)
Tim Petersab9ba272001-08-09 21:40:30 +000035
Fred Drake8e6669a2001-07-19 22:27:56 +000036 eq(r("\"'"), repr("\"'"))
37 s = "\""*30+"'"*100
Walter Dörwald70a6b492004-02-12 17:35:32 +000038 expected = repr(s)[:13] + "..." + repr(s)[-14:]
Fred Drake8e6669a2001-07-19 22:27:56 +000039 eq(r(s), expected)
40
Guido van Rossumcd16bf62007-06-13 18:07:49 +000041 def test_tuple(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000042 eq = self.assertEqual
Guido van Rossumcd16bf62007-06-13 18:07:49 +000043 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 Drake8e6669a2001-07-19 22:27:56 +000053 def test_container(self):
Tim Peters6ee04802003-02-05 18:29:34 +000054 from array import array
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000055 from collections import deque
Tim Peters6ee04802003-02-05 18:29:34 +000056
Ezio Melottib3aedd42010-11-20 19:04:17 +000057 eq = self.assertEqual
Fred Drake8e6669a2001-07-19 22:27:56 +000058 # 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 Hettingerba6cd362004-05-21 10:00:15 +000072 # 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 Hettinger1453e4a2004-05-21 23:01:18 +000086 # collections.deque after 6
87 eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])")
88
Fred Drake8e6669a2001-07-19 22:27:56 +000089 # 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 Peters6ee04802003-02-05 18:29:34 +000096 # 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 Drake8e6669a2001-07-19 22:27:56 +0000106 def test_numbers(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000107 eq = self.assertEqual
Fred Drake8e6669a2001-07-19 22:27:56 +0000108 eq(r(123), repr(123))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000109 eq(r(123), repr(123))
Fred Drake8e6669a2001-07-19 22:27:56 +0000110 eq(r(1.0/3), repr(1.0/3))
111
Guido van Rossume2a383d2007-01-15 16:59:06 +0000112 n = 10**100
Walter Dörwald70a6b492004-02-12 17:35:32 +0000113 expected = repr(n)[:18] + "..." + repr(n)[-19:]
Fred Drake8e6669a2001-07-19 22:27:56 +0000114 eq(r(n), expected)
115
116 def test_instance(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000117 eq = self.assertEqual
Fred Drake8e6669a2001-07-19 22:27:56 +0000118 i1 = ClassWithRepr("a")
119 eq(r(i1), repr(i1))
Tim Petersab9ba272001-08-09 21:40:30 +0000120
Fred Drake8e6669a2001-07-19 22:27:56 +0000121 i2 = ClassWithRepr("x"*1000)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000122 expected = repr(i2)[:13] + "..." + repr(i2)[-14:]
Fred Drake8e6669a2001-07-19 22:27:56 +0000123 eq(r(i2), expected)
124
125 i3 = ClassWithFailingRepr()
126 eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%id(i3)))
127
Guido van Rossumcf856f92001-09-05 02:26:26 +0000128 s = r(ClassWithFailingRepr)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000129 self.assertTrue(s.startswith("<class "))
130 self.assertTrue(s.endswith(">"))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000131 self.assertIn(s.find("..."), [12, 13])
Guido van Rossumcf856f92001-09-05 02:26:26 +0000132
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000133 def test_lambda(self):
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100134 r = repr(lambda x: x)
135 self.assertTrue(r.startswith("<function ReprTests.test_lambda.<locals>.<lambda"), r)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000136 # XXX anonymous functions? see func_repr
137
138 def test_builtin_function(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000139 eq = self.assertEqual
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000140 # Functions
141 eq(repr(hash), '<built-in function hash>')
142 # Methods
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000143 self.assertTrue(repr(''.split).startswith(
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000144 '<built-in method split of str object at 0x'))
145
Guido van Rossum805365e2007-05-07 22:24:25 +0000146 def test_range(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000147 eq = self.assertEqual
Guido van Rossum3353a2e2007-05-21 18:14:54 +0000148 eq(repr(range(1)), 'range(0, 1)')
Guido van Rossum805365e2007-05-07 22:24:25 +0000149 eq(repr(range(1, 2)), 'range(1, 2)')
150 eq(repr(range(1, 4, 3)), 'range(1, 4, 3)')
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000151
Fred Drake8e6669a2001-07-19 22:27:56 +0000152 def test_nesting(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000153 eq = self.assertEqual
Fred Drake8e6669a2001-07-19 22:27:56 +0000154 # 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 Warsaw0bcf6d82001-08-24 18:37:32 +0000169 def test_cell(self):
Serhiy Storchaka1f79cdf2013-12-10 10:20:31 +0200170 def get_cell():
171 x = 42
172 def inner():
173 return x
174 return inner
175 x = get_cell().__closure__[0]
Zachary Wareea6854a2013-12-10 14:17:22 -0600176 self.assertRegex(repr(x), r'<cell at 0x[0-9A-Fa-f]+: '
177 r'int object at 0x[0-9A-Fa-f]+>')
Serhiy Storchaka1f79cdf2013-12-10 10:20:31 +0200178 self.assertRegex(r(x), r'<cell at 0x.*\.\.\..*>')
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000179
180 def test_descriptors(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000181 eq = self.assertEqual
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000182 # method descriptors
Tim Petersa427a2b2001-10-29 22:25:45 +0000183 eq(repr(dict.items), "<method 'items' of 'dict' objects>")
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000184 # XXX member descriptors
185 # XXX attribute descriptors
186 # XXX slot descriptors
187 # static and class methods
188 class C:
189 def foo(cls): pass
190 x = staticmethod(C.foo)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000191 self.assertTrue(repr(x).startswith('<staticmethod object at 0x'))
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000192 x = classmethod(C.foo)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000193 self.assertTrue(repr(x).startswith('<classmethod object at 0x'))
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000194
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000195 def test_unsortable(self):
196 # Repr.repr() used to call sorted() on sets, frozensets and dicts
197 # without taking into account that not all objects are comparable
198 x = set([1j, 2j, 3j])
199 y = frozenset(x)
200 z = {1j: 1, 2j: 2}
201 r(x)
202 r(y)
203 r(z)
204
Victor Stinnerbf816222011-06-30 23:25:47 +0200205def write_file(path, text):
206 with open(path, 'w', encoding='ASCII') as fp:
207 fp.write(text)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000208
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000209class LongReprTest(unittest.TestCase):
Antoine Pitrou01296da2012-04-24 13:55:35 +0200210 longname = 'areallylongpackageandmodulenametotestreprtruncation'
211
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000212 def setUp(self):
Antoine Pitrou01296da2012-04-24 13:55:35 +0200213 self.pkgname = os.path.join(self.longname)
214 self.subpkgname = os.path.join(self.longname, self.longname)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000215 # Make the package and subpackage
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000216 shutil.rmtree(self.pkgname, ignore_errors=True)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000217 os.mkdir(self.pkgname)
Victor Stinnerbf816222011-06-30 23:25:47 +0200218 create_empty_file(os.path.join(self.pkgname, '__init__.py'))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000219 shutil.rmtree(self.subpkgname, ignore_errors=True)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000220 os.mkdir(self.subpkgname)
Victor Stinnerbf816222011-06-30 23:25:47 +0200221 create_empty_file(os.path.join(self.subpkgname, '__init__.py'))
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000222 # Remember where we are
223 self.here = os.getcwd()
224 sys.path.insert(0, self.here)
Brett Cannonceffda82012-04-16 20:48:50 -0400225 # When regrtest is run with its -j option, this command alone is not
226 # enough.
Antoine Pitrouc541f8e2012-02-20 01:48:16 +0100227 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000228
229 def tearDown(self):
230 actions = []
Benjamin Peterson699adb92008-05-08 22:27:58 +0000231 for dirpath, dirnames, filenames in os.walk(self.pkgname):
232 for name in dirnames + filenames:
233 actions.append(os.path.join(dirpath, name))
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000234 actions.append(self.pkgname)
235 actions.sort()
236 actions.reverse()
237 for p in actions:
238 if os.path.isdir(p):
239 os.rmdir(p)
240 else:
241 os.remove(p)
242 del sys.path[0]
243
Antoine Pitrou01296da2012-04-24 13:55:35 +0200244 def _check_path_limitations(self, module_name):
245 # base directory
246 source_path_len = len(self.here)
247 # a path separator + `longname` (twice)
248 source_path_len += 2 * (len(self.longname) + 1)
249 # a path separator + `module_name` + ".py"
250 source_path_len += len(module_name) + 1 + len(".py")
Brett Cannon9529fbf2013-06-15 17:11:25 -0400251 cached_path_len = (source_path_len +
252 len(importlib.util.cache_from_source("x.py")) - len("x.py"))
Antoine Pitrou110ee342012-06-23 22:55:58 +0200253 if os.name == 'nt' and cached_path_len >= 258:
Antoine Pitrou01296da2012-04-24 13:55:35 +0200254 # Under Windows, the max path len is 260 including C's terminating
255 # NUL character.
256 # (see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx#maxpath)
257 self.skipTest("test paths too long (%d characters) for Windows' 260 character limit"
258 % cached_path_len)
Antoine Pitrou541b7c82012-06-23 00:07:38 +0200259 elif os.name == 'nt' and verbose:
Antoine Pitrouf0f47422012-06-23 00:49:44 +0200260 print("cached_path_len =", cached_path_len)
Antoine Pitrou01296da2012-04-24 13:55:35 +0200261
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000262 def test_module(self):
Eric Snowb523f842013-11-22 09:05:39 -0700263 self.maxDiff = None
Antoine Pitrou01296da2012-04-24 13:55:35 +0200264 self._check_path_limitations(self.pkgname)
Victor Stinnerbf816222011-06-30 23:25:47 +0200265 create_empty_file(os.path.join(self.subpkgname, self.pkgname + '.py'))
Brett Cannonceffda82012-04-16 20:48:50 -0400266 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000267 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation
Brett Cannon7b383c42012-06-11 11:02:36 -0400268 module = areallylongpackageandmodulenametotestreprtruncation
269 self.assertEqual(repr(module), "<module %r from %r>" % (module.__name__, module.__file__))
270 self.assertEqual(repr(sys), "<module 'sys' (built-in)>")
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000271
272 def test_type(self):
Antoine Pitrou01296da2012-04-24 13:55:35 +0200273 self._check_path_limitations('foo')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000274 eq = self.assertEqual
Victor Stinnerbf816222011-06-30 23:25:47 +0200275 write_file(os.path.join(self.subpkgname, 'foo.py'), '''\
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000276class foo(object):
277 pass
278''')
Brett Cannonceffda82012-04-16 20:48:50 -0400279 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000280 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import foo
281 eq(repr(foo.foo),
Mark Hammondd800ae12003-01-16 04:56:52 +0000282 "<class '%s.foo'>" % foo.__name__)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000283
Zachary Ware9fe6d862013-12-08 00:20:35 -0600284 @unittest.skip('need a suitable object')
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000285 def test_object(self):
286 # XXX Test the repr of a type with a really long tp_name but with no
287 # tp_repr. WIBNI we had ::Inline? :)
288 pass
289
290 def test_class(self):
Antoine Pitrou01296da2012-04-24 13:55:35 +0200291 self._check_path_limitations('bar')
Victor Stinnerbf816222011-06-30 23:25:47 +0200292 write_file(os.path.join(self.subpkgname, 'bar.py'), '''\
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000293class bar:
294 pass
295''')
Brett Cannonceffda82012-04-16 20:48:50 -0400296 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000297 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import bar
Mark Hammondd800ae12003-01-16 04:56:52 +0000298 # Module name may be prefixed with "test.", depending on how run.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000299 self.assertEqual(repr(bar.bar), "<class '%s.bar'>" % bar.__name__)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000300
301 def test_instance(self):
Antoine Pitrou01296da2012-04-24 13:55:35 +0200302 self._check_path_limitations('baz')
Victor Stinnerbf816222011-06-30 23:25:47 +0200303 write_file(os.path.join(self.subpkgname, 'baz.py'), '''\
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000304class baz:
305 pass
306''')
Brett Cannonceffda82012-04-16 20:48:50 -0400307 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000308 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import baz
309 ibaz = baz.baz()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000310 self.assertTrue(repr(ibaz).startswith(
Guido van Rossuma48a3b42006-04-20 16:07:39 +0000311 "<%s.baz object at 0x" % baz.__name__))
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000312
313 def test_method(self):
Antoine Pitrou01296da2012-04-24 13:55:35 +0200314 self._check_path_limitations('qux')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000315 eq = self.assertEqual
Victor Stinnerbf816222011-06-30 23:25:47 +0200316 write_file(os.path.join(self.subpkgname, 'qux.py'), '''\
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000317class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
318 def amethod(self): pass
319''')
Brett Cannonceffda82012-04-16 20:48:50 -0400320 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000321 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import qux
322 # Unbound methods first
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100323 r = repr(qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod)
324 self.assertTrue(r.startswith('<function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod'), r)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000325 # Bound method next
326 iqux = qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100327 r = repr(iqux.amethod)
328 self.assertTrue(r.startswith(
Guido van Rossuma48a3b42006-04-20 16:07:39 +0000329 '<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa object at 0x' \
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100330 % (qux.__name__,) ), r)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000331
Zachary Ware9fe6d862013-12-08 00:20:35 -0600332 @unittest.skip('needs a built-in function with a really long name')
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000333 def test_builtin_function(self):
334 # XXX test built-in functions and methods with really long names
335 pass
Fred Drake8e6669a2001-07-19 22:27:56 +0000336
337class ClassWithRepr:
338 def __init__(self, s):
339 self.s = s
340 def __repr__(self):
Guido van Rossuma48a3b42006-04-20 16:07:39 +0000341 return "ClassWithRepr(%r)" % self.s
Fred Drake8e6669a2001-07-19 22:27:56 +0000342
343
344class ClassWithFailingRepr:
345 def __repr__(self):
346 raise Exception("This should be caught by Repr.repr_instance")
347
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +0000348class MyContainer:
349 'Helper class for TestRecursiveRepr'
350 def __init__(self, values):
351 self.values = list(values)
352 def append(self, value):
353 self.values.append(value)
354 @recursive_repr()
355 def __repr__(self):
356 return '<' + ', '.join(map(str, self.values)) + '>'
357
358class MyContainer2(MyContainer):
359 @recursive_repr('+++')
360 def __repr__(self):
361 return '<' + ', '.join(map(str, self.values)) + '>'
362
363class TestRecursiveRepr(unittest.TestCase):
364 def test_recursive_repr(self):
365 m = MyContainer(list('abcde'))
366 m.append(m)
367 m.append('x')
368 m.append(m)
369 self.assertEqual(repr(m), '<a, b, c, d, e, ..., x, ...>')
370 m = MyContainer2(list('abcde'))
371 m.append(m)
372 m.append('x')
373 m.append(m)
374 self.assertEqual(repr(m), '<a, b, c, d, e, +++, x, +++>')
Fred Drake8e6669a2001-07-19 22:27:56 +0000375
Fred Drake2e2be372001-09-20 21:33:42 +0000376def test_main():
377 run_unittest(ReprTests)
Ronald Oussoren94f25282010-05-05 19:11:21 +0000378 run_unittest(LongReprTest)
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +0000379 run_unittest(TestRecursiveRepr)
Fred Drake2e2be372001-09-20 21:33:42 +0000380
381
382if __name__ == "__main__":
383 test_main()