blob: eb2b17a8b515db2a53f48947b26ee605b1f64c2c [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
Fred Drake8e6669a2001-07-19 22:27:56 +000010import unittest
Barry Warsaw0bcf6d82001-08-24 18:37:32 +000011
Victor Stinnerbf816222011-06-30 23:25:47 +020012from test.support import run_unittest, create_empty_file
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +000013from reprlib import repr as r # Don't shadow builtin repr
14from reprlib import Repr
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +000015from reprlib import recursive_repr
Fred Drake8e6669a2001-07-19 22:27:56 +000016
17
18def nestedTuple(nesting):
19 t = ()
20 for i in range(nesting):
21 t = (t,)
22 return t
23
24class ReprTests(unittest.TestCase):
25
26 def test_string(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000027 eq = self.assertEqual
Fred Drake8e6669a2001-07-19 22:27:56 +000028 eq(r("abc"), "'abc'")
29 eq(r("abcdefghijklmnop"),"'abcdefghijklmnop'")
30
31 s = "a"*30+"b"*30
Walter Dörwald70a6b492004-02-12 17:35:32 +000032 expected = repr(s)[:13] + "..." + repr(s)[-14:]
Fred Drake8e6669a2001-07-19 22:27:56 +000033 eq(r(s), expected)
Tim Petersab9ba272001-08-09 21:40:30 +000034
Fred Drake8e6669a2001-07-19 22:27:56 +000035 eq(r("\"'"), repr("\"'"))
36 s = "\""*30+"'"*100
Walter Dörwald70a6b492004-02-12 17:35:32 +000037 expected = repr(s)[:13] + "..." + repr(s)[-14:]
Fred Drake8e6669a2001-07-19 22:27:56 +000038 eq(r(s), expected)
39
Guido van Rossumcd16bf62007-06-13 18:07:49 +000040 def test_tuple(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000041 eq = self.assertEqual
Guido van Rossumcd16bf62007-06-13 18:07:49 +000042 eq(r((1,)), "(1,)")
43
44 t3 = (1, 2, 3)
45 eq(r(t3), "(1, 2, 3)")
46
47 r2 = Repr()
48 r2.maxtuple = 2
49 expected = repr(t3)[:-2] + "...)"
50 eq(r2.repr(t3), expected)
51
Fred Drake8e6669a2001-07-19 22:27:56 +000052 def test_container(self):
Tim Peters6ee04802003-02-05 18:29:34 +000053 from array import array
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000054 from collections import deque
Tim Peters6ee04802003-02-05 18:29:34 +000055
Ezio Melottib3aedd42010-11-20 19:04:17 +000056 eq = self.assertEqual
Fred Drake8e6669a2001-07-19 22:27:56 +000057 # Tuples give up after 6 elements
58 eq(r(()), "()")
59 eq(r((1,)), "(1,)")
60 eq(r((1, 2, 3)), "(1, 2, 3)")
61 eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)")
62 eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)")
63
64 # Lists give up after 6 as well
65 eq(r([]), "[]")
66 eq(r([1]), "[1]")
67 eq(r([1, 2, 3]), "[1, 2, 3]")
68 eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]")
69 eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]")
70
Raymond Hettingerba6cd362004-05-21 10:00:15 +000071 # Sets give up after 6 as well
72 eq(r(set([])), "set([])")
73 eq(r(set([1])), "set([1])")
74 eq(r(set([1, 2, 3])), "set([1, 2, 3])")
75 eq(r(set([1, 2, 3, 4, 5, 6])), "set([1, 2, 3, 4, 5, 6])")
76 eq(r(set([1, 2, 3, 4, 5, 6, 7])), "set([1, 2, 3, 4, 5, 6, ...])")
77
78 # Frozensets give up after 6 as well
79 eq(r(frozenset([])), "frozenset([])")
80 eq(r(frozenset([1])), "frozenset([1])")
81 eq(r(frozenset([1, 2, 3])), "frozenset([1, 2, 3])")
82 eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset([1, 2, 3, 4, 5, 6])")
83 eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset([1, 2, 3, 4, 5, 6, ...])")
84
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000085 # collections.deque after 6
86 eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])")
87
Fred Drake8e6669a2001-07-19 22:27:56 +000088 # Dictionaries give up after 4.
89 eq(r({}), "{}")
90 d = {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}
91 eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}")
92 d['arthur'] = 1
93 eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}")
94
Tim Peters6ee04802003-02-05 18:29:34 +000095 # array.array after 5.
96 eq(r(array('i')), "array('i', [])")
97 eq(r(array('i', [1])), "array('i', [1])")
98 eq(r(array('i', [1, 2])), "array('i', [1, 2])")
99 eq(r(array('i', [1, 2, 3])), "array('i', [1, 2, 3])")
100 eq(r(array('i', [1, 2, 3, 4])), "array('i', [1, 2, 3, 4])")
101 eq(r(array('i', [1, 2, 3, 4, 5])), "array('i', [1, 2, 3, 4, 5])")
102 eq(r(array('i', [1, 2, 3, 4, 5, 6])),
103 "array('i', [1, 2, 3, 4, 5, ...])")
104
Fred Drake8e6669a2001-07-19 22:27:56 +0000105 def test_numbers(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000106 eq = self.assertEqual
Fred Drake8e6669a2001-07-19 22:27:56 +0000107 eq(r(123), repr(123))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000108 eq(r(123), repr(123))
Fred Drake8e6669a2001-07-19 22:27:56 +0000109 eq(r(1.0/3), repr(1.0/3))
110
Guido van Rossume2a383d2007-01-15 16:59:06 +0000111 n = 10**100
Walter Dörwald70a6b492004-02-12 17:35:32 +0000112 expected = repr(n)[:18] + "..." + repr(n)[-19:]
Fred Drake8e6669a2001-07-19 22:27:56 +0000113 eq(r(n), expected)
114
115 def test_instance(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000116 eq = self.assertEqual
Fred Drake8e6669a2001-07-19 22:27:56 +0000117 i1 = ClassWithRepr("a")
118 eq(r(i1), repr(i1))
Tim Petersab9ba272001-08-09 21:40:30 +0000119
Fred Drake8e6669a2001-07-19 22:27:56 +0000120 i2 = ClassWithRepr("x"*1000)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000121 expected = repr(i2)[:13] + "..." + repr(i2)[-14:]
Fred Drake8e6669a2001-07-19 22:27:56 +0000122 eq(r(i2), expected)
123
124 i3 = ClassWithFailingRepr()
125 eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%id(i3)))
126
Guido van Rossumcf856f92001-09-05 02:26:26 +0000127 s = r(ClassWithFailingRepr)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000128 self.assertTrue(s.startswith("<class "))
129 self.assertTrue(s.endswith(">"))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000130 self.assertIn(s.find("..."), [12, 13])
Guido van Rossumcf856f92001-09-05 02:26:26 +0000131
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000132 def test_lambda(self):
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100133 r = repr(lambda x: x)
134 self.assertTrue(r.startswith("<function ReprTests.test_lambda.<locals>.<lambda"), r)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000135 # XXX anonymous functions? see func_repr
136
137 def test_builtin_function(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000138 eq = self.assertEqual
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000139 # Functions
140 eq(repr(hash), '<built-in function hash>')
141 # Methods
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000142 self.assertTrue(repr(''.split).startswith(
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000143 '<built-in method split of str object at 0x'))
144
Guido van Rossum805365e2007-05-07 22:24:25 +0000145 def test_range(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000146 eq = self.assertEqual
Guido van Rossum3353a2e2007-05-21 18:14:54 +0000147 eq(repr(range(1)), 'range(0, 1)')
Guido van Rossum805365e2007-05-07 22:24:25 +0000148 eq(repr(range(1, 2)), 'range(1, 2)')
149 eq(repr(range(1, 4, 3)), 'range(1, 4, 3)')
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000150
Fred Drake8e6669a2001-07-19 22:27:56 +0000151 def test_nesting(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000152 eq = self.assertEqual
Fred Drake8e6669a2001-07-19 22:27:56 +0000153 # everything is meant to give up after 6 levels.
154 eq(r([[[[[[[]]]]]]]), "[[[[[[[]]]]]]]")
155 eq(r([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]")
156
157 eq(r(nestedTuple(6)), "(((((((),),),),),),)")
158 eq(r(nestedTuple(7)), "(((((((...),),),),),),)")
159
160 eq(r({ nestedTuple(5) : nestedTuple(5) }),
161 "{((((((),),),),),): ((((((),),),),),)}")
162 eq(r({ nestedTuple(6) : nestedTuple(6) }),
163 "{((((((...),),),),),): ((((((...),),),),),)}")
164
165 eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]")
166 eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]")
167
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000168 def test_cell(self):
169 # XXX Hmm? How to get at a cell object?
170 pass
171
172 def test_descriptors(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000173 eq = self.assertEqual
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000174 # method descriptors
Tim Petersa427a2b2001-10-29 22:25:45 +0000175 eq(repr(dict.items), "<method 'items' of 'dict' objects>")
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000176 # XXX member descriptors
177 # XXX attribute descriptors
178 # XXX slot descriptors
179 # static and class methods
180 class C:
181 def foo(cls): pass
182 x = staticmethod(C.foo)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000183 self.assertTrue(repr(x).startswith('<staticmethod object at 0x'))
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000184 x = classmethod(C.foo)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000185 self.assertTrue(repr(x).startswith('<classmethod object at 0x'))
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000186
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000187 def test_unsortable(self):
188 # Repr.repr() used to call sorted() on sets, frozensets and dicts
189 # without taking into account that not all objects are comparable
190 x = set([1j, 2j, 3j])
191 y = frozenset(x)
192 z = {1j: 1, 2j: 2}
193 r(x)
194 r(y)
195 r(z)
196
Victor Stinnerbf816222011-06-30 23:25:47 +0200197def write_file(path, text):
198 with open(path, 'w', encoding='ASCII') as fp:
199 fp.write(text)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000200
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000201class LongReprTest(unittest.TestCase):
202 def setUp(self):
203 longname = 'areallylongpackageandmodulenametotestreprtruncation'
204 self.pkgname = os.path.join(longname)
205 self.subpkgname = os.path.join(longname, longname)
206 # Make the package and subpackage
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000207 shutil.rmtree(self.pkgname, ignore_errors=True)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000208 os.mkdir(self.pkgname)
Victor Stinnerbf816222011-06-30 23:25:47 +0200209 create_empty_file(os.path.join(self.pkgname, '__init__.py'))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000210 shutil.rmtree(self.subpkgname, ignore_errors=True)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000211 os.mkdir(self.subpkgname)
Victor Stinnerbf816222011-06-30 23:25:47 +0200212 create_empty_file(os.path.join(self.subpkgname, '__init__.py'))
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000213 # Remember where we are
214 self.here = os.getcwd()
215 sys.path.insert(0, self.here)
Brett Cannonceffda82012-04-16 20:48:50 -0400216 # When regrtest is run with its -j option, this command alone is not
217 # enough.
Antoine Pitrouc541f8e2012-02-20 01:48:16 +0100218 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000219
220 def tearDown(self):
221 actions = []
Benjamin Peterson699adb92008-05-08 22:27:58 +0000222 for dirpath, dirnames, filenames in os.walk(self.pkgname):
223 for name in dirnames + filenames:
224 actions.append(os.path.join(dirpath, name))
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000225 actions.append(self.pkgname)
226 actions.sort()
227 actions.reverse()
228 for p in actions:
229 if os.path.isdir(p):
230 os.rmdir(p)
231 else:
232 os.remove(p)
233 del sys.path[0]
234
235 def test_module(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000236 eq = self.assertEqual
Victor Stinnerbf816222011-06-30 23:25:47 +0200237 create_empty_file(os.path.join(self.subpkgname, self.pkgname + '.py'))
Brett Cannonceffda82012-04-16 20:48:50 -0400238 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000239 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation
240 eq(repr(areallylongpackageandmodulenametotestreprtruncation),
Victor Stinnere1ea8292011-02-23 14:14:48 +0000241 "<module %r from %r>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__))
Neal Norwitz70769012001-12-29 00:25:42 +0000242 eq(repr(sys), "<module 'sys' (built-in)>")
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000243
244 def test_type(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000245 eq = self.assertEqual
Victor Stinnerbf816222011-06-30 23:25:47 +0200246 write_file(os.path.join(self.subpkgname, 'foo.py'), '''\
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000247class foo(object):
248 pass
249''')
Brett Cannonceffda82012-04-16 20:48:50 -0400250 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000251 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import foo
252 eq(repr(foo.foo),
Mark Hammondd800ae12003-01-16 04:56:52 +0000253 "<class '%s.foo'>" % foo.__name__)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000254
255 def test_object(self):
256 # XXX Test the repr of a type with a really long tp_name but with no
257 # tp_repr. WIBNI we had ::Inline? :)
258 pass
259
260 def test_class(self):
Victor Stinnerbf816222011-06-30 23:25:47 +0200261 write_file(os.path.join(self.subpkgname, 'bar.py'), '''\
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000262class bar:
263 pass
264''')
Brett Cannonceffda82012-04-16 20:48:50 -0400265 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000266 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import bar
Mark Hammondd800ae12003-01-16 04:56:52 +0000267 # Module name may be prefixed with "test.", depending on how run.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000268 self.assertEqual(repr(bar.bar), "<class '%s.bar'>" % bar.__name__)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000269
270 def test_instance(self):
Victor Stinnerbf816222011-06-30 23:25:47 +0200271 write_file(os.path.join(self.subpkgname, 'baz.py'), '''\
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000272class baz:
273 pass
274''')
Brett Cannonceffda82012-04-16 20:48:50 -0400275 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000276 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import baz
277 ibaz = baz.baz()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000278 self.assertTrue(repr(ibaz).startswith(
Guido van Rossuma48a3b42006-04-20 16:07:39 +0000279 "<%s.baz object at 0x" % baz.__name__))
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000280
281 def test_method(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000282 eq = self.assertEqual
Victor Stinnerbf816222011-06-30 23:25:47 +0200283 write_file(os.path.join(self.subpkgname, 'qux.py'), '''\
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000284class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
285 def amethod(self): pass
286''')
Brett Cannonceffda82012-04-16 20:48:50 -0400287 importlib.invalidate_caches()
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000288 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import qux
289 # Unbound methods first
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100290 r = repr(qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod)
291 self.assertTrue(r.startswith('<function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod'), r)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000292 # Bound method next
293 iqux = qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100294 r = repr(iqux.amethod)
295 self.assertTrue(r.startswith(
Guido van Rossuma48a3b42006-04-20 16:07:39 +0000296 '<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa object at 0x' \
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100297 % (qux.__name__,) ), r)
Barry Warsaw0bcf6d82001-08-24 18:37:32 +0000298
299 def test_builtin_function(self):
300 # XXX test built-in functions and methods with really long names
301 pass
Fred Drake8e6669a2001-07-19 22:27:56 +0000302
303class ClassWithRepr:
304 def __init__(self, s):
305 self.s = s
306 def __repr__(self):
Guido van Rossuma48a3b42006-04-20 16:07:39 +0000307 return "ClassWithRepr(%r)" % self.s
Fred Drake8e6669a2001-07-19 22:27:56 +0000308
309
310class ClassWithFailingRepr:
311 def __repr__(self):
312 raise Exception("This should be caught by Repr.repr_instance")
313
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +0000314class MyContainer:
315 'Helper class for TestRecursiveRepr'
316 def __init__(self, values):
317 self.values = list(values)
318 def append(self, value):
319 self.values.append(value)
320 @recursive_repr()
321 def __repr__(self):
322 return '<' + ', '.join(map(str, self.values)) + '>'
323
324class MyContainer2(MyContainer):
325 @recursive_repr('+++')
326 def __repr__(self):
327 return '<' + ', '.join(map(str, self.values)) + '>'
328
329class TestRecursiveRepr(unittest.TestCase):
330 def test_recursive_repr(self):
331 m = MyContainer(list('abcde'))
332 m.append(m)
333 m.append('x')
334 m.append(m)
335 self.assertEqual(repr(m), '<a, b, c, d, e, ..., x, ...>')
336 m = MyContainer2(list('abcde'))
337 m.append(m)
338 m.append('x')
339 m.append(m)
340 self.assertEqual(repr(m), '<a, b, c, d, e, +++, x, +++>')
Fred Drake8e6669a2001-07-19 22:27:56 +0000341
Fred Drake2e2be372001-09-20 21:33:42 +0000342def test_main():
343 run_unittest(ReprTests)
Ronald Oussoren94f25282010-05-05 19:11:21 +0000344 run_unittest(LongReprTest)
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +0000345 run_unittest(TestRecursiveRepr)
Fred Drake2e2be372001-09-20 21:33:42 +0000346
347
348if __name__ == "__main__":
349 test_main()