diff options
Diffstat (limited to 'Lib/test/test_reprlib.py')
-rw-r--r-- | Lib/test/test_reprlib.py | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/Lib/test/test_reprlib.py b/Lib/test/test_reprlib.py index ce6ecb67f7..b0dc4d7813 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): @@ -125,7 +126,7 @@ class ReprTests(unittest.TestCase): s = r(ClassWithFailingRepr) self.assertTrue(s.startswith("<class ")) self.assertTrue(s.endswith(">")) - self.assertTrue(s.find("...") in [12, 13]) + self.assertIn(s.find("..."), [12, 13]) def test_lambda(self): self.assertTrue(repr(lambda x: x).startswith( @@ -301,11 +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) - if os.name != 'mac': - run_unittest(LongReprTest) + run_unittest(LongReprTest) + run_unittest(TestRecursiveRepr) if __name__ == "__main__": |