summaryrefslogtreecommitdiff
path: root/Lib/test/test_reprlib.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-09-13 21:36:00 +0000
committerRaymond Hettinger <python@rcn.com>2010-09-13 21:36:00 +0000
commit84c8769f7052e87e23e0f5a67ef3ce4d578a2822 (patch)
treeb65de3ae85bb1bfb60b674a114b2ef2ac29b7c84 /Lib/test/test_reprlib.py
parent8ee11f7e599b2c5b4de39047bf11bde2246e968f (diff)
downloadcpython-84c8769f7052e87e23e0f5a67ef3ce4d578a2822.tar.gz
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 0e799f647c..4271482106 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__":