1 | n/a | """ |
---|
2 | n/a | Test cases for the repr module |
---|
3 | n/a | Nick Mathewson |
---|
4 | n/a | """ |
---|
5 | n/a | |
---|
6 | n/a | import sys |
---|
7 | n/a | import os |
---|
8 | n/a | import shutil |
---|
9 | n/a | import importlib |
---|
10 | n/a | import importlib.util |
---|
11 | n/a | import unittest |
---|
12 | n/a | |
---|
13 | n/a | from test.support import create_empty_file, verbose |
---|
14 | n/a | from reprlib import repr as r # Don't shadow builtin repr |
---|
15 | n/a | from reprlib import Repr |
---|
16 | n/a | from reprlib import recursive_repr |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | def nestedTuple(nesting): |
---|
20 | n/a | t = () |
---|
21 | n/a | for i in range(nesting): |
---|
22 | n/a | t = (t,) |
---|
23 | n/a | return t |
---|
24 | n/a | |
---|
25 | n/a | class ReprTests(unittest.TestCase): |
---|
26 | n/a | |
---|
27 | n/a | def test_string(self): |
---|
28 | n/a | eq = self.assertEqual |
---|
29 | n/a | eq(r("abc"), "'abc'") |
---|
30 | n/a | eq(r("abcdefghijklmnop"),"'abcdefghijklmnop'") |
---|
31 | n/a | |
---|
32 | n/a | s = "a"*30+"b"*30 |
---|
33 | n/a | expected = repr(s)[:13] + "..." + repr(s)[-14:] |
---|
34 | n/a | eq(r(s), expected) |
---|
35 | n/a | |
---|
36 | n/a | eq(r("\"'"), repr("\"'")) |
---|
37 | n/a | s = "\""*30+"'"*100 |
---|
38 | n/a | expected = repr(s)[:13] + "..." + repr(s)[-14:] |
---|
39 | n/a | eq(r(s), expected) |
---|
40 | n/a | |
---|
41 | n/a | def test_tuple(self): |
---|
42 | n/a | eq = self.assertEqual |
---|
43 | n/a | eq(r((1,)), "(1,)") |
---|
44 | n/a | |
---|
45 | n/a | t3 = (1, 2, 3) |
---|
46 | n/a | eq(r(t3), "(1, 2, 3)") |
---|
47 | n/a | |
---|
48 | n/a | r2 = Repr() |
---|
49 | n/a | r2.maxtuple = 2 |
---|
50 | n/a | expected = repr(t3)[:-2] + "...)" |
---|
51 | n/a | eq(r2.repr(t3), expected) |
---|
52 | n/a | |
---|
53 | n/a | def test_container(self): |
---|
54 | n/a | from array import array |
---|
55 | n/a | from collections import deque |
---|
56 | n/a | |
---|
57 | n/a | eq = self.assertEqual |
---|
58 | n/a | # Tuples give up after 6 elements |
---|
59 | n/a | eq(r(()), "()") |
---|
60 | n/a | eq(r((1,)), "(1,)") |
---|
61 | n/a | eq(r((1, 2, 3)), "(1, 2, 3)") |
---|
62 | n/a | eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)") |
---|
63 | n/a | eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)") |
---|
64 | n/a | |
---|
65 | n/a | # Lists give up after 6 as well |
---|
66 | n/a | eq(r([]), "[]") |
---|
67 | n/a | eq(r([1]), "[1]") |
---|
68 | n/a | eq(r([1, 2, 3]), "[1, 2, 3]") |
---|
69 | n/a | eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]") |
---|
70 | n/a | eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]") |
---|
71 | n/a | |
---|
72 | n/a | # Sets give up after 6 as well |
---|
73 | n/a | eq(r(set([])), "set()") |
---|
74 | n/a | eq(r(set([1])), "{1}") |
---|
75 | n/a | eq(r(set([1, 2, 3])), "{1, 2, 3}") |
---|
76 | n/a | eq(r(set([1, 2, 3, 4, 5, 6])), "{1, 2, 3, 4, 5, 6}") |
---|
77 | n/a | eq(r(set([1, 2, 3, 4, 5, 6, 7])), "{1, 2, 3, 4, 5, 6, ...}") |
---|
78 | n/a | |
---|
79 | n/a | # Frozensets give up after 6 as well |
---|
80 | n/a | eq(r(frozenset([])), "frozenset()") |
---|
81 | n/a | eq(r(frozenset([1])), "frozenset({1})") |
---|
82 | n/a | eq(r(frozenset([1, 2, 3])), "frozenset({1, 2, 3})") |
---|
83 | n/a | eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset({1, 2, 3, 4, 5, 6})") |
---|
84 | n/a | eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset({1, 2, 3, 4, 5, 6, ...})") |
---|
85 | n/a | |
---|
86 | n/a | # collections.deque after 6 |
---|
87 | n/a | eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])") |
---|
88 | n/a | |
---|
89 | n/a | # Dictionaries give up after 4. |
---|
90 | n/a | eq(r({}), "{}") |
---|
91 | n/a | d = {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4} |
---|
92 | n/a | eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}") |
---|
93 | n/a | d['arthur'] = 1 |
---|
94 | n/a | eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}") |
---|
95 | n/a | |
---|
96 | n/a | # array.array after 5. |
---|
97 | n/a | eq(r(array('i')), "array('i')") |
---|
98 | n/a | eq(r(array('i', [1])), "array('i', [1])") |
---|
99 | n/a | eq(r(array('i', [1, 2])), "array('i', [1, 2])") |
---|
100 | n/a | eq(r(array('i', [1, 2, 3])), "array('i', [1, 2, 3])") |
---|
101 | n/a | eq(r(array('i', [1, 2, 3, 4])), "array('i', [1, 2, 3, 4])") |
---|
102 | n/a | eq(r(array('i', [1, 2, 3, 4, 5])), "array('i', [1, 2, 3, 4, 5])") |
---|
103 | n/a | eq(r(array('i', [1, 2, 3, 4, 5, 6])), |
---|
104 | n/a | "array('i', [1, 2, 3, 4, 5, ...])") |
---|
105 | n/a | |
---|
106 | n/a | def test_set_literal(self): |
---|
107 | n/a | eq = self.assertEqual |
---|
108 | n/a | eq(r({1}), "{1}") |
---|
109 | n/a | eq(r({1, 2, 3}), "{1, 2, 3}") |
---|
110 | n/a | eq(r({1, 2, 3, 4, 5, 6}), "{1, 2, 3, 4, 5, 6}") |
---|
111 | n/a | eq(r({1, 2, 3, 4, 5, 6, 7}), "{1, 2, 3, 4, 5, 6, ...}") |
---|
112 | n/a | |
---|
113 | n/a | def test_frozenset(self): |
---|
114 | n/a | eq = self.assertEqual |
---|
115 | n/a | eq(r(frozenset({1})), "frozenset({1})") |
---|
116 | n/a | eq(r(frozenset({1, 2, 3})), "frozenset({1, 2, 3})") |
---|
117 | n/a | eq(r(frozenset({1, 2, 3, 4, 5, 6})), "frozenset({1, 2, 3, 4, 5, 6})") |
---|
118 | n/a | eq(r(frozenset({1, 2, 3, 4, 5, 6, 7})), "frozenset({1, 2, 3, 4, 5, 6, ...})") |
---|
119 | n/a | |
---|
120 | n/a | def test_numbers(self): |
---|
121 | n/a | eq = self.assertEqual |
---|
122 | n/a | eq(r(123), repr(123)) |
---|
123 | n/a | eq(r(123), repr(123)) |
---|
124 | n/a | eq(r(1.0/3), repr(1.0/3)) |
---|
125 | n/a | |
---|
126 | n/a | n = 10**100 |
---|
127 | n/a | expected = repr(n)[:18] + "..." + repr(n)[-19:] |
---|
128 | n/a | eq(r(n), expected) |
---|
129 | n/a | |
---|
130 | n/a | def test_instance(self): |
---|
131 | n/a | eq = self.assertEqual |
---|
132 | n/a | i1 = ClassWithRepr("a") |
---|
133 | n/a | eq(r(i1), repr(i1)) |
---|
134 | n/a | |
---|
135 | n/a | i2 = ClassWithRepr("x"*1000) |
---|
136 | n/a | expected = repr(i2)[:13] + "..." + repr(i2)[-14:] |
---|
137 | n/a | eq(r(i2), expected) |
---|
138 | n/a | |
---|
139 | n/a | i3 = ClassWithFailingRepr() |
---|
140 | n/a | eq(r(i3), ("<ClassWithFailingRepr instance at %#x>"%id(i3))) |
---|
141 | n/a | |
---|
142 | n/a | s = r(ClassWithFailingRepr) |
---|
143 | n/a | self.assertTrue(s.startswith("<class ")) |
---|
144 | n/a | self.assertTrue(s.endswith(">")) |
---|
145 | n/a | self.assertIn(s.find("..."), [12, 13]) |
---|
146 | n/a | |
---|
147 | n/a | def test_lambda(self): |
---|
148 | n/a | r = repr(lambda x: x) |
---|
149 | n/a | self.assertTrue(r.startswith("<function ReprTests.test_lambda.<locals>.<lambda"), r) |
---|
150 | n/a | # XXX anonymous functions? see func_repr |
---|
151 | n/a | |
---|
152 | n/a | def test_builtin_function(self): |
---|
153 | n/a | eq = self.assertEqual |
---|
154 | n/a | # Functions |
---|
155 | n/a | eq(repr(hash), '<built-in function hash>') |
---|
156 | n/a | # Methods |
---|
157 | n/a | self.assertTrue(repr(''.split).startswith( |
---|
158 | n/a | '<built-in method split of str object at 0x')) |
---|
159 | n/a | |
---|
160 | n/a | def test_range(self): |
---|
161 | n/a | eq = self.assertEqual |
---|
162 | n/a | eq(repr(range(1)), 'range(0, 1)') |
---|
163 | n/a | eq(repr(range(1, 2)), 'range(1, 2)') |
---|
164 | n/a | eq(repr(range(1, 4, 3)), 'range(1, 4, 3)') |
---|
165 | n/a | |
---|
166 | n/a | def test_nesting(self): |
---|
167 | n/a | eq = self.assertEqual |
---|
168 | n/a | # everything is meant to give up after 6 levels. |
---|
169 | n/a | eq(r([[[[[[[]]]]]]]), "[[[[[[[]]]]]]]") |
---|
170 | n/a | eq(r([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]") |
---|
171 | n/a | |
---|
172 | n/a | eq(r(nestedTuple(6)), "(((((((),),),),),),)") |
---|
173 | n/a | eq(r(nestedTuple(7)), "(((((((...),),),),),),)") |
---|
174 | n/a | |
---|
175 | n/a | eq(r({ nestedTuple(5) : nestedTuple(5) }), |
---|
176 | n/a | "{((((((),),),),),): ((((((),),),),),)}") |
---|
177 | n/a | eq(r({ nestedTuple(6) : nestedTuple(6) }), |
---|
178 | n/a | "{((((((...),),),),),): ((((((...),),),),),)}") |
---|
179 | n/a | |
---|
180 | n/a | eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]") |
---|
181 | n/a | eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]") |
---|
182 | n/a | |
---|
183 | n/a | def test_cell(self): |
---|
184 | n/a | def get_cell(): |
---|
185 | n/a | x = 42 |
---|
186 | n/a | def inner(): |
---|
187 | n/a | return x |
---|
188 | n/a | return inner |
---|
189 | n/a | x = get_cell().__closure__[0] |
---|
190 | n/a | self.assertRegex(repr(x), r'<cell at 0x[0-9A-Fa-f]+: ' |
---|
191 | n/a | r'int object at 0x[0-9A-Fa-f]+>') |
---|
192 | n/a | self.assertRegex(r(x), r'<cell at 0x.*\.\.\..*>') |
---|
193 | n/a | |
---|
194 | n/a | def test_descriptors(self): |
---|
195 | n/a | eq = self.assertEqual |
---|
196 | n/a | # method descriptors |
---|
197 | n/a | eq(repr(dict.items), "<method 'items' of 'dict' objects>") |
---|
198 | n/a | # XXX member descriptors |
---|
199 | n/a | # XXX attribute descriptors |
---|
200 | n/a | # XXX slot descriptors |
---|
201 | n/a | # static and class methods |
---|
202 | n/a | class C: |
---|
203 | n/a | def foo(cls): pass |
---|
204 | n/a | x = staticmethod(C.foo) |
---|
205 | n/a | self.assertTrue(repr(x).startswith('<staticmethod object at 0x')) |
---|
206 | n/a | x = classmethod(C.foo) |
---|
207 | n/a | self.assertTrue(repr(x).startswith('<classmethod object at 0x')) |
---|
208 | n/a | |
---|
209 | n/a | def test_unsortable(self): |
---|
210 | n/a | # Repr.repr() used to call sorted() on sets, frozensets and dicts |
---|
211 | n/a | # without taking into account that not all objects are comparable |
---|
212 | n/a | x = set([1j, 2j, 3j]) |
---|
213 | n/a | y = frozenset(x) |
---|
214 | n/a | z = {1j: 1, 2j: 2} |
---|
215 | n/a | r(x) |
---|
216 | n/a | r(y) |
---|
217 | n/a | r(z) |
---|
218 | n/a | |
---|
219 | n/a | def write_file(path, text): |
---|
220 | n/a | with open(path, 'w', encoding='ASCII') as fp: |
---|
221 | n/a | fp.write(text) |
---|
222 | n/a | |
---|
223 | n/a | class LongReprTest(unittest.TestCase): |
---|
224 | n/a | longname = 'areallylongpackageandmodulenametotestreprtruncation' |
---|
225 | n/a | |
---|
226 | n/a | def setUp(self): |
---|
227 | n/a | self.pkgname = os.path.join(self.longname) |
---|
228 | n/a | self.subpkgname = os.path.join(self.longname, self.longname) |
---|
229 | n/a | # Make the package and subpackage |
---|
230 | n/a | shutil.rmtree(self.pkgname, ignore_errors=True) |
---|
231 | n/a | os.mkdir(self.pkgname) |
---|
232 | n/a | create_empty_file(os.path.join(self.pkgname, '__init__.py')) |
---|
233 | n/a | shutil.rmtree(self.subpkgname, ignore_errors=True) |
---|
234 | n/a | os.mkdir(self.subpkgname) |
---|
235 | n/a | create_empty_file(os.path.join(self.subpkgname, '__init__.py')) |
---|
236 | n/a | # Remember where we are |
---|
237 | n/a | self.here = os.getcwd() |
---|
238 | n/a | sys.path.insert(0, self.here) |
---|
239 | n/a | # When regrtest is run with its -j option, this command alone is not |
---|
240 | n/a | # enough. |
---|
241 | n/a | importlib.invalidate_caches() |
---|
242 | n/a | |
---|
243 | n/a | def tearDown(self): |
---|
244 | n/a | actions = [] |
---|
245 | n/a | for dirpath, dirnames, filenames in os.walk(self.pkgname): |
---|
246 | n/a | for name in dirnames + filenames: |
---|
247 | n/a | actions.append(os.path.join(dirpath, name)) |
---|
248 | n/a | actions.append(self.pkgname) |
---|
249 | n/a | actions.sort() |
---|
250 | n/a | actions.reverse() |
---|
251 | n/a | for p in actions: |
---|
252 | n/a | if os.path.isdir(p): |
---|
253 | n/a | os.rmdir(p) |
---|
254 | n/a | else: |
---|
255 | n/a | os.remove(p) |
---|
256 | n/a | del sys.path[0] |
---|
257 | n/a | |
---|
258 | n/a | def _check_path_limitations(self, module_name): |
---|
259 | n/a | # base directory |
---|
260 | n/a | source_path_len = len(self.here) |
---|
261 | n/a | # a path separator + `longname` (twice) |
---|
262 | n/a | source_path_len += 2 * (len(self.longname) + 1) |
---|
263 | n/a | # a path separator + `module_name` + ".py" |
---|
264 | n/a | source_path_len += len(module_name) + 1 + len(".py") |
---|
265 | n/a | cached_path_len = (source_path_len + |
---|
266 | n/a | len(importlib.util.cache_from_source("x.py")) - len("x.py")) |
---|
267 | n/a | if os.name == 'nt' and cached_path_len >= 258: |
---|
268 | n/a | # Under Windows, the max path len is 260 including C's terminating |
---|
269 | n/a | # NUL character. |
---|
270 | n/a | # (see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx#maxpath) |
---|
271 | n/a | self.skipTest("test paths too long (%d characters) for Windows' 260 character limit" |
---|
272 | n/a | % cached_path_len) |
---|
273 | n/a | elif os.name == 'nt' and verbose: |
---|
274 | n/a | print("cached_path_len =", cached_path_len) |
---|
275 | n/a | |
---|
276 | n/a | def test_module(self): |
---|
277 | n/a | self.maxDiff = None |
---|
278 | n/a | self._check_path_limitations(self.pkgname) |
---|
279 | n/a | create_empty_file(os.path.join(self.subpkgname, self.pkgname + '.py')) |
---|
280 | n/a | importlib.invalidate_caches() |
---|
281 | n/a | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation |
---|
282 | n/a | module = areallylongpackageandmodulenametotestreprtruncation |
---|
283 | n/a | self.assertEqual(repr(module), "<module %r from %r>" % (module.__name__, module.__file__)) |
---|
284 | n/a | self.assertEqual(repr(sys), "<module 'sys' (built-in)>") |
---|
285 | n/a | |
---|
286 | n/a | def test_type(self): |
---|
287 | n/a | self._check_path_limitations('foo') |
---|
288 | n/a | eq = self.assertEqual |
---|
289 | n/a | write_file(os.path.join(self.subpkgname, 'foo.py'), '''\ |
---|
290 | n/a | class foo(object): |
---|
291 | n/a | pass |
---|
292 | n/a | ''') |
---|
293 | n/a | importlib.invalidate_caches() |
---|
294 | n/a | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import foo |
---|
295 | n/a | eq(repr(foo.foo), |
---|
296 | n/a | "<class '%s.foo'>" % foo.__name__) |
---|
297 | n/a | |
---|
298 | n/a | @unittest.skip('need a suitable object') |
---|
299 | n/a | def test_object(self): |
---|
300 | n/a | # XXX Test the repr of a type with a really long tp_name but with no |
---|
301 | n/a | # tp_repr. WIBNI we had ::Inline? :) |
---|
302 | n/a | pass |
---|
303 | n/a | |
---|
304 | n/a | def test_class(self): |
---|
305 | n/a | self._check_path_limitations('bar') |
---|
306 | n/a | write_file(os.path.join(self.subpkgname, 'bar.py'), '''\ |
---|
307 | n/a | class bar: |
---|
308 | n/a | pass |
---|
309 | n/a | ''') |
---|
310 | n/a | importlib.invalidate_caches() |
---|
311 | n/a | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import bar |
---|
312 | n/a | # Module name may be prefixed with "test.", depending on how run. |
---|
313 | n/a | self.assertEqual(repr(bar.bar), "<class '%s.bar'>" % bar.__name__) |
---|
314 | n/a | |
---|
315 | n/a | def test_instance(self): |
---|
316 | n/a | self._check_path_limitations('baz') |
---|
317 | n/a | write_file(os.path.join(self.subpkgname, 'baz.py'), '''\ |
---|
318 | n/a | class baz: |
---|
319 | n/a | pass |
---|
320 | n/a | ''') |
---|
321 | n/a | importlib.invalidate_caches() |
---|
322 | n/a | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import baz |
---|
323 | n/a | ibaz = baz.baz() |
---|
324 | n/a | self.assertTrue(repr(ibaz).startswith( |
---|
325 | n/a | "<%s.baz object at 0x" % baz.__name__)) |
---|
326 | n/a | |
---|
327 | n/a | def test_method(self): |
---|
328 | n/a | self._check_path_limitations('qux') |
---|
329 | n/a | eq = self.assertEqual |
---|
330 | n/a | write_file(os.path.join(self.subpkgname, 'qux.py'), '''\ |
---|
331 | n/a | class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: |
---|
332 | n/a | def amethod(self): pass |
---|
333 | n/a | ''') |
---|
334 | n/a | importlib.invalidate_caches() |
---|
335 | n/a | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import qux |
---|
336 | n/a | # Unbound methods first |
---|
337 | n/a | r = repr(qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod) |
---|
338 | n/a | self.assertTrue(r.startswith('<function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod'), r) |
---|
339 | n/a | # Bound method next |
---|
340 | n/a | iqux = qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
---|
341 | n/a | r = repr(iqux.amethod) |
---|
342 | n/a | self.assertTrue(r.startswith( |
---|
343 | n/a | '<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa object at 0x' \ |
---|
344 | n/a | % (qux.__name__,) ), r) |
---|
345 | n/a | |
---|
346 | n/a | @unittest.skip('needs a built-in function with a really long name') |
---|
347 | n/a | def test_builtin_function(self): |
---|
348 | n/a | # XXX test built-in functions and methods with really long names |
---|
349 | n/a | pass |
---|
350 | n/a | |
---|
351 | n/a | class ClassWithRepr: |
---|
352 | n/a | def __init__(self, s): |
---|
353 | n/a | self.s = s |
---|
354 | n/a | def __repr__(self): |
---|
355 | n/a | return "ClassWithRepr(%r)" % self.s |
---|
356 | n/a | |
---|
357 | n/a | |
---|
358 | n/a | class ClassWithFailingRepr: |
---|
359 | n/a | def __repr__(self): |
---|
360 | n/a | raise Exception("This should be caught by Repr.repr_instance") |
---|
361 | n/a | |
---|
362 | n/a | class MyContainer: |
---|
363 | n/a | 'Helper class for TestRecursiveRepr' |
---|
364 | n/a | def __init__(self, values): |
---|
365 | n/a | self.values = list(values) |
---|
366 | n/a | def append(self, value): |
---|
367 | n/a | self.values.append(value) |
---|
368 | n/a | @recursive_repr() |
---|
369 | n/a | def __repr__(self): |
---|
370 | n/a | return '<' + ', '.join(map(str, self.values)) + '>' |
---|
371 | n/a | |
---|
372 | n/a | class MyContainer2(MyContainer): |
---|
373 | n/a | @recursive_repr('+++') |
---|
374 | n/a | def __repr__(self): |
---|
375 | n/a | return '<' + ', '.join(map(str, self.values)) + '>' |
---|
376 | n/a | |
---|
377 | n/a | class MyContainer3: |
---|
378 | n/a | def __repr__(self): |
---|
379 | n/a | 'Test document content' |
---|
380 | n/a | pass |
---|
381 | n/a | wrapped = __repr__ |
---|
382 | n/a | wrapper = recursive_repr()(wrapped) |
---|
383 | n/a | |
---|
384 | n/a | class TestRecursiveRepr(unittest.TestCase): |
---|
385 | n/a | def test_recursive_repr(self): |
---|
386 | n/a | m = MyContainer(list('abcde')) |
---|
387 | n/a | m.append(m) |
---|
388 | n/a | m.append('x') |
---|
389 | n/a | m.append(m) |
---|
390 | n/a | self.assertEqual(repr(m), '<a, b, c, d, e, ..., x, ...>') |
---|
391 | n/a | m = MyContainer2(list('abcde')) |
---|
392 | n/a | m.append(m) |
---|
393 | n/a | m.append('x') |
---|
394 | n/a | m.append(m) |
---|
395 | n/a | self.assertEqual(repr(m), '<a, b, c, d, e, +++, x, +++>') |
---|
396 | n/a | |
---|
397 | n/a | def test_assigned_attributes(self): |
---|
398 | n/a | from functools import WRAPPER_ASSIGNMENTS as assigned |
---|
399 | n/a | wrapped = MyContainer3.wrapped |
---|
400 | n/a | wrapper = MyContainer3.wrapper |
---|
401 | n/a | for name in assigned: |
---|
402 | n/a | self.assertIs(getattr(wrapper, name), getattr(wrapped, name)) |
---|
403 | n/a | |
---|
404 | n/a | if __name__ == "__main__": |
---|
405 | n/a | unittest.main() |
---|