Test cases for the repr module
from test
.test_support
import run_unittest
from repr import repr as r
# Don't shadow builtin repr
def nestedTuple(nesting
):
class ReprTests(unittest
.TestCase
):
eq(r("abcdefghijklmnop"),"'abcdefghijklmnop'")
expected
= repr(s
)[:13] + "..." + repr(s
)[-14:]
eq(r("\"'"), repr("\"'"))
expected
= repr(s
)[:13] + "..." + repr(s
)[-14:]
def test_container(self
):
from collections
import deque
# Tuples give up after 6 elements
eq(r((1, 2, 3)), "(1, 2, 3)")
eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)")
eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)")
# Lists give up after 6 as well
eq(r([1, 2, 3]), "[1, 2, 3]")
eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]")
eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]")
# Sets give up after 6 as well
eq(r(set([])), "set([])")
eq(r(set([1])), "set([1])")
eq(r(set([1, 2, 3])), "set([1, 2, 3])")
eq(r(set([1, 2, 3, 4, 5, 6])), "set([1, 2, 3, 4, 5, 6])")
eq(r(set([1, 2, 3, 4, 5, 6, 7])), "set([1, 2, 3, 4, 5, 6, ...])")
# Frozensets give up after 6 as well
eq(r(frozenset([])), "frozenset([])")
eq(r(frozenset([1])), "frozenset([1])")
eq(r(frozenset([1, 2, 3])), "frozenset([1, 2, 3])")
eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset([1, 2, 3, 4, 5, 6])")
eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset([1, 2, 3, 4, 5, 6, ...])")
# collections.deque after 6
eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])")
# Dictionaries give up after 4.
d
= {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}
eq(r(d
), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}")
eq(r(d
), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}")
eq(r(array('i')), "array('i', [])")
eq(r(array('i', [1])), "array('i', [1])")
eq(r(array('i', [1, 2])), "array('i', [1, 2])")
eq(r(array('i', [1, 2, 3])), "array('i', [1, 2, 3])")
eq(r(array('i', [1, 2, 3, 4])), "array('i', [1, 2, 3, 4])")
eq(r(array('i', [1, 2, 3, 4, 5])), "array('i', [1, 2, 3, 4, 5])")
eq(r(array('i', [1, 2, 3, 4, 5, 6])),
"array('i', [1, 2, 3, 4, 5, ...])")
eq(r(1.0/3), repr(1.0/3))
expected
= repr(n
)[:18] + "..." + repr(n
)[-19:]
i2
= ClassWithRepr("x"*1000)
expected
= repr(i2
)[:13] + "..." + repr(i2
)[-14:]
i3
= ClassWithFailingRepr()
eq(r(i3
), ("<ClassWithFailingRepr instance at %x>"%id(i3
)))
s
= r(ClassWithFailingRepr
)
self
.failUnless(s
.startswith("<class "))
self
.failUnless(s
.endswith(">"))
self
.failUnless(s
.find("...") == 8)
fp
= open(unittest
.__file
__)
self
.failUnless(repr(fp
).startswith(
"<open file '%s', mode 'r' at 0x" % unittest
.__file
__))
self
.failUnless(repr(fp
).startswith(
"<closed file '%s', mode 'r' at 0x" % unittest
.__file
__))
self
.failUnless(repr(lambda x
: x
).startswith(
# XXX anonymous functions? see func_repr
def test_builtin_function(self
):
eq(repr(hash), '<built-in function hash>')
self
.failUnless(repr(''.split
).startswith(
'<built-in method split of str object at 0x'))
eq(repr(xrange(1)), 'xrange(1)')
eq(repr(xrange(1, 2)), 'xrange(1, 2)')
eq(repr(xrange(1, 2, 3)), 'xrange(1, 4, 3)')
# everything is meant to give up after 6 levels.
eq(r([[[[[[[]]]]]]]), "[[[[[[[]]]]]]]")
eq(r([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]")
eq(r(nestedTuple(6)), "(((((((),),),),),),)")
eq(r(nestedTuple(7)), "(((((((...),),),),),),)")
eq(r({ nestedTuple(5) : nestedTuple(5) }),
"{((((((),),),),),): ((((((),),),),),)}")
eq(r({ nestedTuple(6) : nestedTuple(6) }),
"{((((((...),),),),),): ((((((...),),),),),)}")
eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]")
eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]")
# XXX doesn't test buffers with no b_base or read-write buffers (see
# bufferobject.c). The test is fairly incomplete too. Sigh.
self
.failUnless(repr(x
).startswith('<read-only buffer for 0x'))
# XXX Hmm? How to get at a cell object?
def test_descriptors(self
):
eq(repr(dict.items
), "<method 'items' of 'dict' objects>")
# XXX attribute descriptors
# static and class methods
self
.failUnless(repr(x
).startswith('<staticmethod object at 0x'))
self
.failUnless(repr(x
).startswith('<classmethod object at 0x'))
def touch(path
, text
=''):
def zap(actions
, dirname
, names
):
actions
.append(os
.path
.join(dirname
, name
))
class LongReprTest(unittest
.TestCase
):
longname
= 'areallylongpackageandmodulenametotestreprtruncation'
self
.pkgname
= os
.path
.join(longname
)
self
.subpkgname
= os
.path
.join(longname
, longname
)
# Make the package and subpackage
touch(os
.path
.join(self
.pkgname
, '__init__'+os
.extsep
+'py'))
os
.mkdir(self
.subpkgname
)
touch(os
.path
.join(self
.subpkgname
, '__init__'+os
.extsep
+'py'))
sys
.path
.insert(0, self
.here
)
os
.path
.walk(self
.pkgname
, zap
, actions
)
actions
.append(self
.pkgname
)
touch(os
.path
.join(self
.subpkgname
, self
.pkgname
+ os
.extsep
+ 'py'))
from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import areallylongpackageandmodulenametotestreprtruncation
eq(repr(areallylongpackageandmodulenametotestreprtruncation
),
"<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation
.__name
__, areallylongpackageandmodulenametotestreprtruncation
.__file
__))
eq(repr(sys
), "<module 'sys' (built-in)>")
touch(os
.path
.join(self
.subpkgname
, 'foo'+os
.extsep
+'py'), '''\
from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import foo
"<class '%s.foo'>" % foo
.__name
__)
# XXX Test the repr of a type with a really long tp_name but with no
# tp_repr. WIBNI we had ::Inline? :)
touch(os
.path
.join(self
.subpkgname
, 'bar'+os
.extsep
+'py'), '''\
from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import bar
# Module name may be prefixed with "test.", depending on how run.
self
.failUnless(repr(bar
.bar
).startswith(
"<class %s.bar at 0x" % bar
.__name
__))
touch(os
.path
.join(self
.subpkgname
, 'baz'+os
.extsep
+'py'), '''\
from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import baz
self
.failUnless(repr(ibaz
).startswith(
"<%s.baz instance at 0x" % baz
.__name
__))
touch(os
.path
.join(self
.subpkgname
, 'qux'+os
.extsep
+'py'), '''\
class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
from areallylongpackageandmodulenametotestreprtruncation
.areallylongpackageandmodulenametotestreprtruncation
import qux
eq(repr(qux
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
.amethod
),
'<unbound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod>')
iqux
= qux
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
self
.failUnless(repr(iqux
.amethod
).startswith(
'<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x' \
def test_builtin_function(self
):
# XXX test built-in functions and methods with really long names
return "ClassWithLongRepr(%r)" % self
.s
class ClassWithFailingRepr
:
raise Exception("This should be caught by Repr.repr_instance")
run_unittest(LongReprTest
)
if __name__
== "__main__":