summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_reprlib.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-09-13 21:36:00 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-09-13 21:36:00 (GMT)
commit98a5f3f83883a6c924cfa1360647dcbf3152ea10 (patch)
treeb4cebb01dd9237c72656b942aa34406dc6fed5a0 /Lib/test/test_reprlib.py
parent9f0cbf1c727f7de884c392176ab4f19a49924c9b (diff)
downloadcpython-98a5f3f83883a6c924cfa1360647dcbf3152ea10.zip
cpython-98a5f3f83883a6c924cfa1360647dcbf3152ea10.tar.gz
cpython-98a5f3f83883a6c924cfa1360647dcbf3152ea10.tar.bz2
Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Diffstat (limited to 'Lib/test/test_reprlib.py')
-rw-r--r--Lib/test/test_reprlib.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/test_reprlib.py b/Lib/test/test_reprlib.py
index 0e799f6..4271482 100644
--- a/Lib/test/test_reprlib.py
+++ b/Lib/test/test_reprlib.py
@@ -11,6 +11,7 @@ import unittest
from test.support import run_unittest
from reprlib import repr as r # Don't shadow builtin repr
from reprlib import Repr
+from reprlib import recursive_repr
def nestedTuple(nesting):
@@ -301,10 +302,38 @@ class ClassWithFailingRepr:
def __repr__(self):
raise Exception("This should be caught by Repr.repr_instance")
+class MyContainer:
+ 'Helper class for TestRecursiveRepr'
+ def __init__(self, values):
+ self.values = list(values)
+ def append(self, value):
+ self.values.append(value)
+ @recursive_repr()
+ def __repr__(self):
+ return '<' + ', '.join(map(str, self.values)) + '>'
+
+class MyContainer2(MyContainer):
+ @recursive_repr('+++')
+ def __repr__(self):
+ return '<' + ', '.join(map(str, self.values)) + '>'
+
+class TestRecursiveRepr(unittest.TestCase):
+ def test_recursive_repr(self):
+ m = MyContainer(list('abcde'))
+ m.append(m)
+ m.append('x')
+ m.append(m)
+ self.assertEqual(repr(m), '<a, b, c, d, e, ..., x, ...>')
+ m = MyContainer2(list('abcde'))
+ m.append(m)
+ m.append('x')
+ m.append(m)
+ self.assertEqual(repr(m), '<a, b, c, d, e, +++, x, +++>')
def test_main():
run_unittest(ReprTests)
run_unittest(LongReprTest)
+ run_unittest(TestRecursiveRepr)
if __name__ == "__main__":