0001 """ 0002 Test cases for the repr module 0003 Nick Mathewson 0004 """ 0005 0006 import sys 0007 import os 0008 import unittest 0009 0010 from test.test_support import run_unittest 0011 from repr import repr as r # Don't shadow builtin repr 0012 0013 0014 def nestedTuple(nesting): 0015 t = () 0016 for i in range(nesting): 0017 t = (t,) 0018 return t 0019 0020 class ReprTests(unittest.TestCase): 0021 0022 def test_string(self): 0023 eq = self.assertEquals 0024 eq(r("abc"), "'abc'") 0025 eq(r("abcdefghijklmnop"),"'abcdefghijklmnop'") 0026 0027 s = "a"*30+"b"*30 0028 expected = repr(s)[:13] + "..." + repr(s)[-14:] 0029 eq(r(s), expected) 0030 0031 eq(r("\"'"), repr("\"'")) 0032 s = "\""*30+"'"*100 0033 expected = repr(s)[:13] + "..." + repr(s)[-14:] 0034 eq(r(s), expected) 0035 0036 def test_container(self): 0037 from array import array 0038 from collections import deque 0039 0040 eq = self.assertEquals 0041 # Tuples give up after 6 elements 0042 eq(r(()), "()") 0043 eq(r((1,)), "(1,)") 0044 eq(r((1, 2, 3)), "(1, 2, 3)") 0045 eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)") 0046 eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)") 0047 0048 # Lists give up after 6 as well 0049 eq(r([]), "[]") 0050 eq(r([1]), "[1]") 0051 eq(r([1, 2, 3]), "[1, 2, 3]") 0052 eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]") 0053 eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]") 0054 0055 # Sets give up after 6 as well 0056 eq(r(set([])), "set([])") 0057 eq(r(set([1])), "set([1])") 0058 eq(r(set([1, 2, 3])), "set([1, 2, 3])") 0059 eq(r(set([1, 2, 3, 4, 5, 6])), "set([1, 2, 3, 4, 5, 6])") 0060 eq(r(set([1, 2, 3, 4, 5, 6, 7])), "set([1, 2, 3, 4, 5, 6, ...])") 0061 0062 # Frozensets give up after 6 as well 0063 eq(r(frozenset([])), "frozenset([])") 0064 eq(r(frozenset([1])), "frozenset([1])") 0065 eq(r(frozenset([1, 2, 3])), "frozenset([1, 2, 3])") 0066 eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset([1, 2, 3, 4, 5, 6])") 0067 eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset([1, 2, 3, 4, 5, 6, ...])") 0068 0069 # collections.deque after 6 0070 eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])") 0071 0072 # Dictionaries give up after 4. 0073 eq(r({}), "{}") 0074 d = {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4} 0075 eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}") 0076 d['arthur'] = 1 0077 eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}") 0078 0079 # array.array after 5. 0080 eq(r(array('i')), "array('i', [])") 0081 eq(r(array('i', [1])), "array('i', [1])") 0082 eq(r(array('i', [1, 2])), "array('i', [1, 2])") 0083 eq(r(array('i', [1, 2, 3])), "array('i', [1, 2, 3])") 0084 eq(r(array('i', [1, 2, 3, 4])), "array('i', [1, 2, 3, 4])") 0085 eq(r(array('i', [1, 2, 3, 4, 5])), "array('i', [1, 2, 3, 4, 5])") 0086 eq(r(array('i', [1, 2, 3, 4, 5, 6])), 0087 "array('i', [1, 2, 3, 4, 5, ...])") 0088 0089 def test_numbers(self): 0090 eq = self.assertEquals 0091 eq(r(123), repr(123)) 0092 eq(r(123L), repr(123L)) 0093 eq(r(1.0/3), repr(1.0/3)) 0094 0095 n = 10L**100 0096 expected = repr(n)[:18] + "..." + repr(n)[-19:] 0097 eq(r(n), expected) 0098 0099 def test_instance(self): 0100 eq = self.assertEquals 0101 i1 = ClassWithRepr("a") 0102 eq(r(i1), repr(i1)) 0103 0104 i2 = ClassWithRepr("x"*1000) 0105 expected = repr(i2)[:13] + "..." + repr(i2)[-14:] 0106 eq(r(i2), expected) 0107 0108 i3 = ClassWithFailingRepr() 0109 eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%id(i3))) 0110 0111 s = r(ClassWithFailingRepr) 0112 self.failUnless(s.startswith("<class ")) 0113 self.failUnless(s.endswith(">")) 0114 self.failUnless(s.find("...") == 8) 0115 0116 def test_file(self): 0117 fp = open(unittest.__file__) 0118 self.failUnless(repr(fp).startswith( 0119 "<open file '%s', mode 'r' at 0x" % unittest.__file__)) 0120 fp.close() 0121 self.failUnless(repr(fp).startswith( 0122 "<closed file '%s', mode 'r' at 0x" % unittest.__file__)) 0123 0124 def test_lambda(self): 0125 self.failUnless(repr(lambda x: x).startswith( 0126 "<function <lambda")) 0127 # XXX anonymous functions? see func_repr 0128 0129 def test_builtin_function(self): 0130 eq = self.assertEquals 0131 # Functions 0132 eq(repr(hash), '<built-in function hash>') 0133 # Methods 0134 self.failUnless(repr(''.split).startswith( 0135 '<built-in method split of str object at 0x')) 0136 0137 def test_xrange(self): 0138 import warnings 0139 eq = self.assertEquals 0140 eq(repr(xrange(1)), 'xrange(1)') 0141 eq(repr(xrange(1, 2)), 'xrange(1, 2)') 0142 eq(repr(xrange(1, 2, 3)), 'xrange(1, 4, 3)') 0143 0144 def test_nesting(self): 0145 eq = self.assertEquals 0146 # everything is meant to give up after 6 levels. 0147 eq(r([[[[[[[]]]]]]]), "[[[[[[[]]]]]]]") 0148 eq(r([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]") 0149 0150 eq(r(nestedTuple(6)), "(((((((),),),),),),)") 0151 eq(r(nestedTuple(7)), "(((((((...),),),),),),)") 0152 0153 eq(r({ nestedTuple(5) : nestedTuple(5) }), 0154 "{((((((),),),),),): ((((((),),),),),)}") 0155 eq(r({ nestedTuple(6) : nestedTuple(6) }), 0156 "{((((((...),),),),),): ((((((...),),),),),)}") 0157 0158 eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]") 0159 eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]") 0160 0161 def test_buffer(self): 0162 # XXX doesn't test buffers with no b_base or read-write buffers (see 0163 # bufferobject.c). The test is fairly incomplete too. Sigh. 0164 x = buffer('foo') 0165 self.failUnless(repr(x).startswith('<read-only buffer for 0x')) 0166 0167 def test_cell(self): 0168 # XXX Hmm? How to get at a cell object? 0169 pass 0170 0171 def test_descriptors(self): 0172 eq = self.assertEquals 0173 # method descriptors 0174 eq(repr(dict.items), "<method 'items' of 'dict' objects>") 0175 # XXX member descriptors 0176 # XXX attribute descriptors 0177 # XXX slot descriptors 0178 # static and class methods 0179 class C: 0180 def foo(cls): pass 0181 x = staticmethod(C.foo) 0182 self.failUnless(repr(x).startswith('<staticmethod object at 0x')) 0183 x = classmethod(C.foo) 0184 self.failUnless(repr(x).startswith('<classmethod object at 0x')) 0185 0186 def touch(path, text=''): 0187 fp = open(path, 'w') 0188 fp.write(text) 0189 fp.close() 0190 0191 def zap(actions, dirname, names): 0192 for name in names: 0193 actions.append(os.path.join(dirname, name)) 0194 0195 class LongReprTest(unittest.TestCase): 0196 def setUp(self): 0197 longname = 'areallylongpackageandmodulenametotestreprtruncation' 0198 self.pkgname = os.path.join(longname) 0199 self.subpkgname = os.path.join(longname, longname) 0200 # Make the package and subpackage 0201 os.mkdir(self.pkgname) 0202 touch(os.path.join(self.pkgname, '__init__'+os.extsep+'py')) 0203 os.mkdir(self.subpkgname) 0204 touch(os.path.join(self.subpkgname, '__init__'+os.extsep+'py')) 0205 # Remember where we are 0206 self.here = os.getcwd() 0207 sys.path.insert(0, self.here) 0208 0209 def tearDown(self): 0210 actions = [] 0211 os.path.walk(self.pkgname, zap, actions) 0212 actions.append(self.pkgname) 0213 actions.sort() 0214 actions.reverse() 0215 for p in actions: 0216 if os.path.isdir(p): 0217 os.rmdir(p) 0218 else: 0219 os.remove(p) 0220 del sys.path[0] 0221 0222 def test_module(self): 0223 eq = self.assertEquals 0224 touch(os.path.join(self.subpkgname, self.pkgname + os.extsep + 'py')) 0225 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation 0226 eq(repr(areallylongpackageandmodulenametotestreprtruncation), 0227 "<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__)) 0228 eq(repr(sys), "<module 'sys' (built-in)>") 0229 0230 def test_type(self): 0231 eq = self.assertEquals 0232 touch(os.path.join(self.subpkgname, 'foo'+os.extsep+'py'), '''\ 0233 class foo(object): 0234 pass 0235 ''') 0236 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import foo 0237 eq(repr(foo.foo), 0238 "<class '%s.foo'>" % foo.__name__) 0239 0240 def test_object(self): 0241 # XXX Test the repr of a type with a really long tp_name but with no 0242 # tp_repr. WIBNI we had ::Inline? :) 0243 pass 0244 0245 def test_class(self): 0246 touch(os.path.join(self.subpkgname, 'bar'+os.extsep+'py'), '''\ 0247 class bar: 0248 pass 0249 ''') 0250 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import bar 0251 # Module name may be prefixed with "test.", depending on how run. 0252 self.failUnless(repr(bar.bar).startswith( 0253 "<class %s.bar at 0x" % bar.__name__)) 0254 0255 def test_instance(self): 0256 touch(os.path.join(self.subpkgname, 'baz'+os.extsep+'py'), '''\ 0257 class baz: 0258 pass 0259 ''') 0260 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import baz 0261 ibaz = baz.baz() 0262 self.failUnless(repr(ibaz).startswith( 0263 "<%s.baz instance at 0x" % baz.__name__)) 0264 0265 def test_method(self): 0266 eq = self.assertEquals 0267 touch(os.path.join(self.subpkgname, 'qux'+os.extsep+'py'), '''\ 0268 class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: 0269 def amethod(self): pass 0270 ''') 0271 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import qux 0272 # Unbound methods first 0273 eq(repr(qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod), 0274 '<unbound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod>') 0275 # Bound method next 0276 iqux = qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() 0277 self.failUnless(repr(iqux.amethod).startswith( 0278 '<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x' \ 0279 % (qux.__name__,) )) 0280 0281 def test_builtin_function(self): 0282 # XXX test built-in functions and methods with really long names 0283 pass 0284 0285 class ClassWithRepr: 0286 def __init__(self, s): 0287 self.s = s 0288 def __repr__(self): 0289 return "ClassWithLongRepr(%r)" % self.s 0290 0291 0292 class ClassWithFailingRepr: 0293 def __repr__(self): 0294 raise Exception("This should be caught by Repr.repr_instance") 0295 0296 0297 def test_main(): 0298 run_unittest(ReprTests) 0299 if os.name != 'mac': 0300 run_unittest(LongReprTest) 0301 0302 0303 if __name__ == "__main__": 0304 test_main() 0305
Generated by PyXR 0.9.4