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
= `s`
[:13] + "..." + `s`
[-14:]
31 eq(r("\"'"), repr("\"'"))
33 expected
= `s`
[:13] + "..." + `s`
[-14:]
36 def test_container(self
):
37 eq
= self
.assertEquals
38 # Tuples give up after 6 elements
41 eq(r((1, 2, 3)), "(1, 2, 3)")
42 eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)")
43 eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)")
45 # Lists give up after 6 as well
48 eq(r([1, 2, 3]), "[1, 2, 3]")
49 eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]")
50 eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]")
52 # Dictionaries give up after 4.
54 d
= {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}
55 eq(r(d
), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}")
57 eq(r(d
), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}")
59 def test_numbers(self
):
60 eq
= self
.assertEquals
62 eq(r(123L), repr(123L))
63 eq(r(1.0/3), repr(1.0/3))
66 expected
= `n`
[:18] + "..." + `n`
[-19:]
69 def test_instance(self
):
70 eq
= self
.assertEquals
71 i1
= ClassWithRepr("a")
74 i2
= ClassWithRepr("x"*1000)
75 expected
= `i2`
[:13] + "..." + `i2`
[-14:]
78 i3
= ClassWithFailingRepr()
79 eq(r(i3
), ("<ClassWithFailingRepr instance at %x>"%id(i3
)))
81 s
= r(ClassWithFailingRepr
)
82 self
.failUnless(s
.startswith("<class "))
83 self
.failUnless(s
.endswith(">"))
84 self
.failUnless(s
.find("...") == 8)
87 fp
= open(unittest
.__file
__)
88 self
.failUnless(repr(fp
).startswith(
89 "<open file '%s', mode 'r' at 0x" % unittest
.__file
__))
91 self
.failUnless(repr(fp
).startswith(
92 "<closed file '%s', mode 'r' at 0x" % unittest
.__file
__))
94 def test_lambda(self
):
95 self
.failUnless(repr(lambda x
: x
).startswith(
97 # XXX anonymous functions? see func_repr
99 def test_builtin_function(self
):
100 eq
= self
.assertEquals
102 eq(repr(hash), '<built-in function hash>')
104 self
.failUnless(repr(''.split
).startswith(
105 '<built-in method split of str object at 0x'))
107 def test_xrange(self
):
109 eq
= self
.assertEquals
110 eq(repr(xrange(1)), 'xrange(1)')
111 eq(repr(xrange(1, 2)), 'xrange(1, 2)')
112 eq(repr(xrange(1, 2, 3)), 'xrange(1, 4, 3)')
114 def test_nesting(self
):
115 eq
= self
.assertEquals
116 # everything is meant to give up after 6 levels.
117 eq(r([[[[[[[]]]]]]]), "[[[[[[[]]]]]]]")
118 eq(r([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]")
120 eq(r(nestedTuple(6)), "(((((((),),),),),),)")
121 eq(r(nestedTuple(7)), "(((((((...),),),),),),)")
123 eq(r({ nestedTuple(5) : nestedTuple(5) }),
124 "{((((((),),),),),): ((((((),),),),),)}")
125 eq(r({ nestedTuple(6) : nestedTuple(6) }),
126 "{((((((...),),),),),): ((((((...),),),),),)}")
128 eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]")
129 eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]")
131 def test_buffer(self
):
132 # XXX doesn't test buffers with no b_base or read-write buffers (see
133 # bufferobject.c). The test is fairly incomplete too. Sigh.
135 self
.failUnless(repr(x
).startswith('<read-only buffer for 0x'))
138 # XXX Hmm? How to get at a cell object?
141 def test_descriptors(self
):
142 eq
= self
.assertEquals
144 eq(repr(dict.items
), "<method 'items' of 'dict' objects>")
145 # XXX member descriptors
146 # XXX attribute descriptors
147 # XXX slot descriptors
148 # static and class methods
151 x
= staticmethod(C
.foo
)
152 self
.failUnless(repr(x
).startswith('<staticmethod object at 0x'))
153 x
= classmethod(C
.foo
)
154 self
.failUnless(repr(x
).startswith('<classmethod object at 0x'))
156 def touch(path
, text
=''):
161 def zap(actions
, dirname
, names
):
163 actions
.append(os
.path
.join(dirname
, name
))
165 class LongReprTest(unittest
.TestCase
):
167 longname
= 'areallylongpackageandmodulenametotestreprtruncation'
168 self
.pkgname
= os
.path
.join(longname
)
169 self
.subpkgname
= os
.path
.join(longname
, longname
)
170 # Make the package and subpackage
171 os
.mkdir(self
.pkgname
)
172 touch(os
.path
.join(self
.pkgname
, '__init__'+os
.extsep
+'py'))
173 os
.mkdir(self
.subpkgname
)
174 touch(os
.path
.join(self
.subpkgname
, '__init__'+os
.extsep
+'py'))
175 # Remember where we are
176 self
.here
= os
.getcwd()
177 sys
.path
.insert(0, self
.here
)
181 os
.path
.walk(self
.pkgname
, zap
, actions
)
182 actions
.append(self
.pkgname
)
192 def test_module(self
):
193 eq
= self
.assertEquals
194 touch(os
.path
.join(self
.subpkgname
, self
.pkgname
+ os
.extsep
+ 'py'))
195 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import areallylongpackageandmodulenametotestreprtruncation
196 eq(repr(areallylongpackageandmodulenametotestreprtruncation
),
197 "<module 'areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation' from '%s'>" % areallylongpackageandmodulenametotestreprtruncation
.__file
__)
198 eq(repr(sys
), "<module 'sys' (built-in)>")
201 eq
= self
.assertEquals
202 touch(os
.path
.join(self
.subpkgname
, 'foo'+os
.extsep
+'py'), '''\
206 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import foo
208 "<class 'areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation.foo.foo'>")
210 def test_object(self
):
211 # XXX Test the repr of a type with a really long tp_name but with no
212 # tp_repr. WIBNI we had ::Inline? :)
215 def test_class(self
):
216 touch(os
.path
.join(self
.subpkgname
, 'bar'+os
.extsep
+'py'), '''\
220 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import bar
221 self
.failUnless(repr(bar
.bar
).startswith(
222 "<class areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation.bar.bar at 0x"))
224 def test_instance(self
):
225 touch(os
.path
.join(self
.subpkgname
, 'baz'+os
.extsep
+'py'), '''\
229 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import baz
231 self
.failUnless(repr(ibaz
).startswith(
232 "<areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation.baz.baz instance at 0x"))
234 def test_method(self
):
235 eq
= self
.assertEquals
236 touch(os
.path
.join(self
.subpkgname
, 'qux'+os
.extsep
+'py'), '''\
237 class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
238 def amethod(self): pass
240 from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import qux
241 # Unbound methods first
242 eq(repr(qux
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
.amethod
),
243 '<unbound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod>')
245 iqux
= qux
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
246 self
.failUnless(repr(iqux
.amethod
).startswith(
247 '<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation.qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x'))
249 def test_builtin_function(self
):
250 # XXX test built-in functions and methods with really long names
254 def __init__(self
, s
):
257 return "ClassWithLongRepr(%r)" % self
.s
260 class ClassWithFailingRepr
:
262 raise Exception("This should be caught by Repr.repr_instance")
266 run_unittest(ReprTests
)
268 run_unittest(LongReprTest
)
271 if __name__
== "__main__":