2 Test cases for the repr module
11 from test
.test_support
import run_unittest
12 from repr import repr as r
# Don't shadow builtin repr
16 def nestedTuple(nesting
):
18 for i
in range(nesting
):
22 class ReprTests(unittest
.TestCase
):
24 def test_string(self
):
25 eq
= self
.assertEquals
27 eq(r("abcdefghijklmnop"),"'abcdefghijklmnop'")
30 expected
= repr(s
)[:13] + "..." + repr(s
)[-14:]
33 eq(r("\"'"), repr("\"'"))
35 expected
= repr(s
)[:13] + "..." + repr(s
)[-14:]
39 eq
= self
.assertEquals
43 eq(r(t3
), "(1, 2, 3)")
47 expected
= repr(t3
)[:-2] + "...)"
48 eq(r2
.repr(t3
), expected
)
50 def test_container(self
):
51 from array
import array
52 from collections
import deque
54 eq
= self
.assertEquals
55 # Tuples give up after 6 elements
58 eq(r((1, 2, 3)), "(1, 2, 3)")
59 eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)")
60 eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)")
62 # Lists give up after 6 as well
65 eq(r([1, 2, 3]), "[1, 2, 3]")
66 eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]")
67 eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]")
69 # Sets give up after 6 as well
70 eq(r(set([])), "set([])")
71 eq(r(set([1])), "set([1])")
72 eq(r(set([1, 2, 3])), "set([1, 2, 3])")
73 eq(r(set([1, 2, 3, 4, 5, 6])), "set([1, 2, 3, 4, 5, 6])")
74 eq(r(set([1, 2, 3, 4, 5, 6, 7])), "set([1, 2, 3, 4, 5, 6, ...])")
76 # Frozensets give up after 6 as well
77 eq(r(frozenset([])), "frozenset([])")
78 eq(r(frozenset([1])), "frozenset([1])")
79 eq(r(frozenset([1, 2, 3])), "frozenset([1, 2, 3])")
80 eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset([1, 2, 3, 4, 5, 6])")
81 eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset([1, 2, 3, 4, 5, 6, ...])")
83 # collections.deque after 6
84 eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])")
86 # Dictionaries give up after 4.
88 d
= {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}
89 eq(r(d
), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}")
91 eq(r(d
), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}")
93 # array.array after 5.
94 eq(r(array('i')), "array('i', [])")
95 eq(r(array('i', [1])), "array('i', [1])")
96 eq(r(array('i', [1, 2])), "array('i', [1, 2])")
97 eq(r(array('i', [1, 2, 3])), "array('i', [1, 2, 3])")
98 eq(r(array('i', [1, 2, 3, 4])), "array('i', [1, 2, 3, 4])")
99 eq(r(array('i', [1, 2, 3, 4, 5])), "array('i', [1, 2, 3, 4, 5])")
100 eq(r(array('i', [1, 2, 3, 4, 5, 6])),
101 "array('i', [1, 2, 3, 4, 5, ...])")
103 def test_numbers(self
):
104 eq
= self
.assertEquals
105 eq(r(123), repr(123))
106 eq(r(123L), repr(123L))
107 eq(r(1.0/3), repr(1.0/3))
110 expected
= repr(n
)[:18] + "..." + repr(n
)[-19:]
113 def test_instance(self
):
114 eq
= self
.assertEquals
115 i1
= ClassWithRepr("a")
118 i2
= ClassWithRepr("x"*1000)
119 expected
= repr(i2
)[:13] + "..." + repr(i2
)[-14:]
122 i3
= ClassWithFailingRepr()
123 eq(r(i3
), ("<ClassWithFailingRepr instance at %x>"%id(i3
)))
125 s
= r(ClassWithFailingRepr
)
126 self
.assertTrue(s
.startswith("<class "))
127 self
.assertTrue(s
.endswith(">"))
128 self
.assertTrue(s
.find("...") == 8)
131 fp
= open(unittest
.__file
__)
132 self
.assertTrue(repr(fp
).startswith(
133 "<open file '%s', mode 'r' at 0x" % unittest
.__file
__))
135 self
.assertTrue(repr(fp
).startswith(
136 "<closed file '%s', mode 'r' at 0x" % unittest
.__file
__))
138 def test_lambda(self
):
139 self
.assertTrue(repr(lambda x
: x
).startswith(
140 "<function <lambda"))
141 # XXX anonymous functions? see func_repr
143 def test_builtin_function(self
):
144 eq
= self
.assertEquals
146 eq(repr(hash), '<built-in function hash>')
148 self
.assertTrue(repr(''.split
).startswith(
149 '<built-in method split of str object at 0x'))
151 def test_xrange(self
):
152 eq
= self
.assertEquals
153 eq(repr(xrange(1)), 'xrange(1)')
154 eq(repr(xrange(1, 2)), 'xrange(1, 2)')
155 eq(repr(xrange(1, 2, 3)), 'xrange(1, 4, 3)')
157 def test_nesting(self
):
158 eq
= self
.assertEquals
159 # everything is meant to give up after 6 levels.
160 eq(r([[[[[[[]]]]]]]), "[[[[[[[]]]]]]]")
161 eq(r([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]")
163 eq(r(nestedTuple(6)), "(((((((),),),),),),)")
164 eq(r(nestedTuple(7)), "(((((((...),),),),),),)")
166 eq(r({ nestedTuple(5) : nestedTuple(5) }),
167 "{((((((),),),),),): ((((((),),),),),)}")
168 eq(r({ nestedTuple(6) : nestedTuple(6) }),
169 "{((((((...),),),),),): ((((((...),),),),),)}")
171 eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]")
172 eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]")
174 def test_buffer(self
):
175 # XXX doesn't test buffers with no b_base or read-write buffers (see
176 # bufferobject.c). The test is fairly incomplete too. Sigh.
178 self
.assertTrue(repr(x
).startswith('<read-only buffer for 0x'))
181 # XXX Hmm? How to get at a cell object?
184 def test_descriptors(self
):
185 eq
= self
.assertEquals
187 eq(repr(dict.items
), "<method 'items' of 'dict' objects>")
188 # XXX member descriptors
189 # XXX attribute descriptors
190 # XXX slot descriptors
191 # static and class methods
194 x
= staticmethod(C
.foo
)
195 self
.assertTrue(repr(x
).startswith('<staticmethod object at 0x'))
196 x
= classmethod(C
.foo
)
197 self
.assertTrue(repr(x
).startswith('<classmethod object at 0x'))
199 def test_unsortable(self
):
200 # Repr.repr() used to call sorted() on sets, frozensets and dicts
201 # without taking into account that not all objects are comparable
202 x
= set([1j
, 2j
, 3j
])
209 def touch(path
, text
=''):
214 class LongReprTest(unittest
.TestCase
):
216 longname
= 'areallylongpackageandmodulenametotestreprtruncation'
217 self
.pkgname
= os
.path
.join(longname
)
218 self
.subpkgname
= os
.path
.join(longname
, longname
)
219 # Make the package and subpackage
220 shutil
.rmtree(self
.pkgname
, ignore_errors
=True)
221 os
.mkdir(self
.pkgname
)
222 touch(os
.path
.join(self
.pkgname
, '__init__'+os
.extsep
+'py'))
223 shutil
.rmtree(self
.subpkgname
, ignore_errors
=True)
224 os
.mkdir(self
.subpkgname
)
225 touch(os
.path
.join(self
.subpkgname
, '__init__'+os
.extsep
+'py'))
226 # Remember where we are
227 self
.here
= os
.getcwd()
228 sys
.path
.insert(0, self
.here
)
232 for dirpath
, dirnames
, filenames
in os
.walk(self
.pkgname
):
233 for name
in dirnames
+ filenames
:
234 actions
.append(os
.path
.join(dirpath
, name
))
235 actions
.append(self
.pkgname
)
245 def test_module(self
):
246 eq
= self
.assertEquals
247 touch(os
.path
.join(self
.subpkgname
, self
.pkgname
+ os
.extsep
+ 'py'))
248 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import areallylongpackageandmodulenametotestreprtruncation
249 eq(repr(areallylongpackageandmodulenametotestreprtruncation
),
250 "<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation
.__name
__, areallylongpackageandmodulenametotestreprtruncation
.__file
__))
251 eq(repr(sys
), "<module 'sys' (built-in)>")
254 eq
= self
.assertEquals
255 touch(os
.path
.join(self
.subpkgname
, 'foo'+os
.extsep
+'py'), '''\
259 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import foo
261 "<class '%s.foo'>" % foo
.__name
__)
263 def test_object(self
):
264 # XXX Test the repr of a type with a really long tp_name but with no
265 # tp_repr. WIBNI we had ::Inline? :)
268 def test_class(self
):
269 touch(os
.path
.join(self
.subpkgname
, 'bar'+os
.extsep
+'py'), '''\
273 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import bar
274 # Module name may be prefixed with "test.", depending on how run.
275 self
.assertTrue(repr(bar
.bar
).startswith(
276 "<class %s.bar at 0x" % bar
.__name
__))
278 def test_instance(self
):
279 touch(os
.path
.join(self
.subpkgname
, 'baz'+os
.extsep
+'py'), '''\
283 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import baz
285 self
.assertTrue(repr(ibaz
).startswith(
286 "<%s.baz instance at 0x" % baz
.__name
__))
288 def test_method(self
):
289 eq
= self
.assertEquals
290 touch(os
.path
.join(self
.subpkgname
, 'qux'+os
.extsep
+'py'), '''\
291 class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
292 def amethod(self): pass
294 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import qux
295 # Unbound methods first
296 eq(repr(qux
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
.amethod
),
297 '<unbound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod>')
299 iqux
= qux
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
300 self
.assertTrue(repr(iqux
.amethod
).startswith(
301 '<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x' \
304 def test_builtin_function(self
):
305 # XXX test built-in functions and methods with really long names
309 def __init__(self
, s
):
312 return "ClassWithLongRepr(%r)" % self
.s
315 class ClassWithFailingRepr
:
317 raise Exception("This should be caught by Repr.repr_instance")
321 run_unittest(ReprTests
)
323 run_unittest(LongReprTest
)
326 if __name__
== "__main__":