2 Test cases for the repr module
10 from test
.test_support
import run_unittest
11 from repr import repr as r
# Don't shadow builtin repr
14 def nestedTuple(nesting
):
16 for i
in range(nesting
):
20 class ReprTests(unittest
.TestCase
):
22 def test_string(self
):
23 eq
= self
.assertEquals
25 eq(r("abcdefghijklmnop"),"'abcdefghijklmnop'")
28 expected
= repr(s
)[:13] + "..." + repr(s
)[-14:]
31 eq(r("\"'"), repr("\"'"))
33 expected
= repr(s
)[:13] + "..." + repr(s
)[-14:]
36 def test_container(self
):
37 from array
import array
38 from collections
import deque
40 eq
= self
.assertEquals
41 # Tuples give up after 6 elements
44 eq(r((1, 2, 3)), "(1, 2, 3)")
45 eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)")
46 eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)")
48 # Lists give up after 6 as well
51 eq(r([1, 2, 3]), "[1, 2, 3]")
52 eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]")
53 eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]")
55 # Sets give up after 6 as well
56 eq(r(set([])), "set([])")
57 eq(r(set([1])), "set([1])")
58 eq(r(set([1, 2, 3])), "set([1, 2, 3])")
59 eq(r(set([1, 2, 3, 4, 5, 6])), "set([1, 2, 3, 4, 5, 6])")
60 eq(r(set([1, 2, 3, 4, 5, 6, 7])), "set([1, 2, 3, 4, 5, 6, ...])")
62 # Frozensets give up after 6 as well
63 eq(r(frozenset([])), "frozenset([])")
64 eq(r(frozenset([1])), "frozenset([1])")
65 eq(r(frozenset([1, 2, 3])), "frozenset([1, 2, 3])")
66 eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset([1, 2, 3, 4, 5, 6])")
67 eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset([1, 2, 3, 4, 5, 6, ...])")
69 # collections.deque after 6
70 eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])")
72 # Dictionaries give up after 4.
74 d
= {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}
75 eq(r(d
), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}")
77 eq(r(d
), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}")
79 # array.array after 5.
80 eq(r(array('i')), "array('i', [])")
81 eq(r(array('i', [1])), "array('i', [1])")
82 eq(r(array('i', [1, 2])), "array('i', [1, 2])")
83 eq(r(array('i', [1, 2, 3])), "array('i', [1, 2, 3])")
84 eq(r(array('i', [1, 2, 3, 4])), "array('i', [1, 2, 3, 4])")
85 eq(r(array('i', [1, 2, 3, 4, 5])), "array('i', [1, 2, 3, 4, 5])")
86 eq(r(array('i', [1, 2, 3, 4, 5, 6])),
87 "array('i', [1, 2, 3, 4, 5, ...])")
89 def test_numbers(self
):
90 eq
= self
.assertEquals
92 eq(r(123L), repr(123L))
93 eq(r(1.0/3), repr(1.0/3))
96 expected
= repr(n
)[:18] + "..." + repr(n
)[-19:]
99 def test_instance(self
):
100 eq
= self
.assertEquals
101 i1
= ClassWithRepr("a")
104 i2
= ClassWithRepr("x"*1000)
105 expected
= repr(i2
)[:13] + "..." + repr(i2
)[-14:]
108 i3
= ClassWithFailingRepr()
109 eq(r(i3
), ("<ClassWithFailingRepr instance at %x>"%id(i3
)))
111 s
= r(ClassWithFailingRepr
)
112 self
.failUnless(s
.startswith("<class "))
113 self
.failUnless(s
.endswith(">"))
114 self
.failUnless(s
.find("...") == 8)
117 fp
= open(unittest
.__file
__)
118 self
.failUnless(repr(fp
).startswith(
119 "<open file '%s', mode 'r' at 0x" % unittest
.__file
__))
121 self
.failUnless(repr(fp
).startswith(
122 "<closed file '%s', mode 'r' at 0x" % unittest
.__file
__))
124 def test_lambda(self
):
125 self
.failUnless(repr(lambda x
: x
).startswith(
126 "<function <lambda"))
127 # XXX anonymous functions? see func_repr
129 def test_builtin_function(self
):
130 eq
= self
.assertEquals
132 eq(repr(hash), '<built-in function hash>')
134 self
.failUnless(repr(''.split
).startswith(
135 '<built-in method split of str object at 0x'))
137 def test_xrange(self
):
139 eq
= self
.assertEquals
140 eq(repr(xrange(1)), 'xrange(1)')
141 eq(repr(xrange(1, 2)), 'xrange(1, 2)')
142 eq(repr(xrange(1, 2, 3)), 'xrange(1, 4, 3)')
144 def test_nesting(self
):
145 eq
= self
.assertEquals
146 # everything is meant to give up after 6 levels.
147 eq(r([[[[[[[]]]]]]]), "[[[[[[[]]]]]]]")
148 eq(r([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]")
150 eq(r(nestedTuple(6)), "(((((((),),),),),),)")
151 eq(r(nestedTuple(7)), "(((((((...),),),),),),)")
153 eq(r({ nestedTuple(5) : nestedTuple(5) }),
154 "{((((((),),),),),): ((((((),),),),),)}")
155 eq(r({ nestedTuple(6) : nestedTuple(6) }),
156 "{((((((...),),),),),): ((((((...),),),),),)}")
158 eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]")
159 eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]")
161 def test_buffer(self
):
162 # XXX doesn't test buffers with no b_base or read-write buffers (see
163 # bufferobject.c). The test is fairly incomplete too. Sigh.
165 self
.failUnless(repr(x
).startswith('<read-only buffer for 0x'))
168 # XXX Hmm? How to get at a cell object?
171 def test_descriptors(self
):
172 eq
= self
.assertEquals
174 eq(repr(dict.items
), "<method 'items' of 'dict' objects>")
175 # XXX member descriptors
176 # XXX attribute descriptors
177 # XXX slot descriptors
178 # static and class methods
181 x
= staticmethod(C
.foo
)
182 self
.failUnless(repr(x
).startswith('<staticmethod object at 0x'))
183 x
= classmethod(C
.foo
)
184 self
.failUnless(repr(x
).startswith('<classmethod object at 0x'))
186 def touch(path
, text
=''):
191 def zap(actions
, dirname
, names
):
193 actions
.append(os
.path
.join(dirname
, name
))
195 class LongReprTest(unittest
.TestCase
):
197 longname
= 'areallylongpackageandmodulenametotestreprtruncation'
198 self
.pkgname
= os
.path
.join(longname
)
199 self
.subpkgname
= os
.path
.join(longname
, longname
)
200 # Make the package and subpackage
201 os
.mkdir(self
.pkgname
)
202 touch(os
.path
.join(self
.pkgname
, '__init__'+os
.extsep
+'py'))
203 os
.mkdir(self
.subpkgname
)
204 touch(os
.path
.join(self
.subpkgname
, '__init__'+os
.extsep
+'py'))
205 # Remember where we are
206 self
.here
= os
.getcwd()
207 sys
.path
.insert(0, self
.here
)
211 os
.path
.walk(self
.pkgname
, zap
, actions
)
212 actions
.append(self
.pkgname
)
222 def test_module(self
):
223 eq
= self
.assertEquals
224 touch(os
.path
.join(self
.subpkgname
, self
.pkgname
+ os
.extsep
+ 'py'))
225 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import areallylongpackageandmodulenametotestreprtruncation
226 eq(repr(areallylongpackageandmodulenametotestreprtruncation
),
227 "<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation
.__name
__, areallylongpackageandmodulenametotestreprtruncation
.__file
__))
228 eq(repr(sys
), "<module 'sys' (built-in)>")
231 eq
= self
.assertEquals
232 touch(os
.path
.join(self
.subpkgname
, 'foo'+os
.extsep
+'py'), '''\
236 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import foo
238 "<class '%s.foo'>" % foo
.__name
__)
240 def test_object(self
):
241 # XXX Test the repr of a type with a really long tp_name but with no
242 # tp_repr. WIBNI we had ::Inline? :)
245 def test_class(self
):
246 touch(os
.path
.join(self
.subpkgname
, 'bar'+os
.extsep
+'py'), '''\
250 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import bar
251 # Module name may be prefixed with "test.", depending on how run.
252 self
.failUnless(repr(bar
.bar
).startswith(
253 "<class %s.bar at 0x" % bar
.__name
__))
255 def test_instance(self
):
256 touch(os
.path
.join(self
.subpkgname
, 'baz'+os
.extsep
+'py'), '''\
260 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import baz
262 self
.failUnless(repr(ibaz
).startswith(
263 "<%s.baz instance at 0x" % baz
.__name
__))
265 def test_method(self
):
266 eq
= self
.assertEquals
267 touch(os
.path
.join(self
.subpkgname
, 'qux'+os
.extsep
+'py'), '''\
268 class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
269 def amethod(self): pass
271 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import qux
272 # Unbound methods first
273 eq(repr(qux
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
.amethod
),
274 '<unbound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod>')
276 iqux
= qux
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
277 self
.failUnless(repr(iqux
.amethod
).startswith(
278 '<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x' \
281 def test_builtin_function(self
):
282 # XXX test built-in functions and methods with really long names
286 def __init__(self
, s
):
289 return "ClassWithLongRepr(%r)" % self
.s
292 class ClassWithFailingRepr
:
294 raise Exception("This should be caught by Repr.repr_instance")
298 run_unittest(ReprTests
)
300 run_unittest(LongReprTest
)
303 if __name__
== "__main__":