Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test cases for the repr module |
| 3 | Nick Mathewson |
| 4 | """ |
| 5 | |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 6 | import sys |
| 7 | import os |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 8 | import unittest |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 9 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 10 | from test.test_support import run_unittest |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 11 | from repr import repr as r # Don't shadow builtin repr |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | def nestedTuple(nesting): |
| 15 | t = () |
| 16 | for i in range(nesting): |
| 17 | t = (t,) |
| 18 | return t |
| 19 | |
| 20 | class ReprTests(unittest.TestCase): |
| 21 | |
| 22 | def test_string(self): |
| 23 | eq = self.assertEquals |
| 24 | eq(r("abc"), "'abc'") |
| 25 | eq(r("abcdefghijklmnop"),"'abcdefghijklmnop'") |
| 26 | |
| 27 | s = "a"*30+"b"*30 |
| 28 | expected = `s`[:13] + "..." + `s`[-14:] |
| 29 | eq(r(s), expected) |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 30 | |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 31 | eq(r("\"'"), repr("\"'")) |
| 32 | s = "\""*30+"'"*100 |
| 33 | expected = `s`[:13] + "..." + `s`[-14:] |
| 34 | eq(r(s), expected) |
| 35 | |
| 36 | def test_container(self): |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 37 | from array import array |
| 38 | |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 39 | eq = self.assertEquals |
| 40 | # Tuples give up after 6 elements |
| 41 | eq(r(()), "()") |
| 42 | eq(r((1,)), "(1,)") |
| 43 | eq(r((1, 2, 3)), "(1, 2, 3)") |
| 44 | eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)") |
| 45 | eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)") |
| 46 | |
| 47 | # Lists give up after 6 as well |
| 48 | eq(r([]), "[]") |
| 49 | eq(r([1]), "[1]") |
| 50 | eq(r([1, 2, 3]), "[1, 2, 3]") |
| 51 | eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]") |
| 52 | eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]") |
| 53 | |
| 54 | # Dictionaries give up after 4. |
| 55 | eq(r({}), "{}") |
| 56 | d = {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4} |
| 57 | eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}") |
| 58 | d['arthur'] = 1 |
| 59 | eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}") |
| 60 | |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 61 | # array.array after 5. |
| 62 | eq(r(array('i')), "array('i', [])") |
| 63 | eq(r(array('i', [1])), "array('i', [1])") |
| 64 | eq(r(array('i', [1, 2])), "array('i', [1, 2])") |
| 65 | eq(r(array('i', [1, 2, 3])), "array('i', [1, 2, 3])") |
| 66 | eq(r(array('i', [1, 2, 3, 4])), "array('i', [1, 2, 3, 4])") |
| 67 | eq(r(array('i', [1, 2, 3, 4, 5])), "array('i', [1, 2, 3, 4, 5])") |
| 68 | eq(r(array('i', [1, 2, 3, 4, 5, 6])), |
| 69 | "array('i', [1, 2, 3, 4, 5, ...])") |
| 70 | |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 71 | def test_numbers(self): |
| 72 | eq = self.assertEquals |
| 73 | eq(r(123), repr(123)) |
| 74 | eq(r(123L), repr(123L)) |
| 75 | eq(r(1.0/3), repr(1.0/3)) |
| 76 | |
| 77 | n = 10L**100 |
| 78 | expected = `n`[:18] + "..." + `n`[-19:] |
| 79 | eq(r(n), expected) |
| 80 | |
| 81 | def test_instance(self): |
| 82 | eq = self.assertEquals |
| 83 | i1 = ClassWithRepr("a") |
| 84 | eq(r(i1), repr(i1)) |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 85 | |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 86 | i2 = ClassWithRepr("x"*1000) |
| 87 | expected = `i2`[:13] + "..." + `i2`[-14:] |
| 88 | eq(r(i2), expected) |
| 89 | |
| 90 | i3 = ClassWithFailingRepr() |
| 91 | eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%id(i3))) |
| 92 | |
Guido van Rossum | cf856f9 | 2001-09-05 02:26:26 +0000 | [diff] [blame] | 93 | s = r(ClassWithFailingRepr) |
| 94 | self.failUnless(s.startswith("<class ")) |
| 95 | self.failUnless(s.endswith(">")) |
| 96 | self.failUnless(s.find("...") == 8) |
| 97 | |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 98 | def test_file(self): |
| 99 | fp = open(unittest.__file__) |
| 100 | self.failUnless(repr(fp).startswith( |
| 101 | "<open file '%s', mode 'r' at 0x" % unittest.__file__)) |
| 102 | fp.close() |
| 103 | self.failUnless(repr(fp).startswith( |
| 104 | "<closed file '%s', mode 'r' at 0x" % unittest.__file__)) |
| 105 | |
| 106 | def test_lambda(self): |
| 107 | self.failUnless(repr(lambda x: x).startswith( |
Jeremy Hylton | dd32138 | 2001-09-14 23:01:49 +0000 | [diff] [blame] | 108 | "<function <lambda")) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 109 | # XXX anonymous functions? see func_repr |
| 110 | |
| 111 | def test_builtin_function(self): |
| 112 | eq = self.assertEquals |
| 113 | # Functions |
| 114 | eq(repr(hash), '<built-in function hash>') |
| 115 | # Methods |
| 116 | self.failUnless(repr(''.split).startswith( |
| 117 | '<built-in method split of str object at 0x')) |
| 118 | |
| 119 | def test_xrange(self): |
Tim Peters | d392506 | 2002-04-16 01:27:44 +0000 | [diff] [blame] | 120 | import warnings |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 121 | eq = self.assertEquals |
| 122 | eq(repr(xrange(1)), 'xrange(1)') |
| 123 | eq(repr(xrange(1, 2)), 'xrange(1, 2)') |
| 124 | eq(repr(xrange(1, 2, 3)), 'xrange(1, 4, 3)') |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 125 | |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 126 | def test_nesting(self): |
| 127 | eq = self.assertEquals |
| 128 | # everything is meant to give up after 6 levels. |
| 129 | eq(r([[[[[[[]]]]]]]), "[[[[[[[]]]]]]]") |
| 130 | eq(r([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]") |
| 131 | |
| 132 | eq(r(nestedTuple(6)), "(((((((),),),),),),)") |
| 133 | eq(r(nestedTuple(7)), "(((((((...),),),),),),)") |
| 134 | |
| 135 | eq(r({ nestedTuple(5) : nestedTuple(5) }), |
| 136 | "{((((((),),),),),): ((((((),),),),),)}") |
| 137 | eq(r({ nestedTuple(6) : nestedTuple(6) }), |
| 138 | "{((((((...),),),),),): ((((((...),),),),),)}") |
| 139 | |
| 140 | eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]") |
| 141 | eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]") |
| 142 | |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 143 | def test_buffer(self): |
| 144 | # XXX doesn't test buffers with no b_base or read-write buffers (see |
| 145 | # bufferobject.c). The test is fairly incomplete too. Sigh. |
| 146 | x = buffer('foo') |
| 147 | self.failUnless(repr(x).startswith('<read-only buffer for 0x')) |
| 148 | |
| 149 | def test_cell(self): |
| 150 | # XXX Hmm? How to get at a cell object? |
| 151 | pass |
| 152 | |
| 153 | def test_descriptors(self): |
| 154 | eq = self.assertEquals |
| 155 | # method descriptors |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 156 | eq(repr(dict.items), "<method 'items' of 'dict' objects>") |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 157 | # XXX member descriptors |
| 158 | # XXX attribute descriptors |
| 159 | # XXX slot descriptors |
| 160 | # static and class methods |
| 161 | class C: |
| 162 | def foo(cls): pass |
| 163 | x = staticmethod(C.foo) |
| 164 | self.failUnless(repr(x).startswith('<staticmethod object at 0x')) |
| 165 | x = classmethod(C.foo) |
| 166 | self.failUnless(repr(x).startswith('<classmethod object at 0x')) |
| 167 | |
| 168 | def touch(path, text=''): |
| 169 | fp = open(path, 'w') |
| 170 | fp.write(text) |
| 171 | fp.close() |
| 172 | |
| 173 | def zap(actions, dirname, names): |
| 174 | for name in names: |
| 175 | actions.append(os.path.join(dirname, name)) |
| 176 | |
| 177 | class LongReprTest(unittest.TestCase): |
| 178 | def setUp(self): |
| 179 | longname = 'areallylongpackageandmodulenametotestreprtruncation' |
| 180 | self.pkgname = os.path.join(longname) |
| 181 | self.subpkgname = os.path.join(longname, longname) |
| 182 | # Make the package and subpackage |
| 183 | os.mkdir(self.pkgname) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 184 | touch(os.path.join(self.pkgname, '__init__'+os.extsep+'py')) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 185 | os.mkdir(self.subpkgname) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 186 | touch(os.path.join(self.subpkgname, '__init__'+os.extsep+'py')) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 187 | # Remember where we are |
| 188 | self.here = os.getcwd() |
| 189 | sys.path.insert(0, self.here) |
| 190 | |
| 191 | def tearDown(self): |
| 192 | actions = [] |
| 193 | os.path.walk(self.pkgname, zap, actions) |
| 194 | actions.append(self.pkgname) |
| 195 | actions.sort() |
| 196 | actions.reverse() |
| 197 | for p in actions: |
| 198 | if os.path.isdir(p): |
| 199 | os.rmdir(p) |
| 200 | else: |
| 201 | os.remove(p) |
| 202 | del sys.path[0] |
| 203 | |
| 204 | def test_module(self): |
| 205 | eq = self.assertEquals |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 206 | touch(os.path.join(self.subpkgname, self.pkgname + os.extsep + 'py')) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 207 | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation |
| 208 | eq(repr(areallylongpackageandmodulenametotestreprtruncation), |
Mark Hammond | d800ae1 | 2003-01-16 04:56:52 +0000 | [diff] [blame] | 209 | "<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__)) |
Neal Norwitz | 7076901 | 2001-12-29 00:25:42 +0000 | [diff] [blame] | 210 | eq(repr(sys), "<module 'sys' (built-in)>") |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 211 | |
| 212 | def test_type(self): |
| 213 | eq = self.assertEquals |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 214 | touch(os.path.join(self.subpkgname, 'foo'+os.extsep+'py'), '''\ |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 215 | class foo(object): |
| 216 | pass |
| 217 | ''') |
| 218 | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import foo |
| 219 | eq(repr(foo.foo), |
Mark Hammond | d800ae1 | 2003-01-16 04:56:52 +0000 | [diff] [blame] | 220 | "<class '%s.foo'>" % foo.__name__) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 221 | |
| 222 | def test_object(self): |
| 223 | # XXX Test the repr of a type with a really long tp_name but with no |
| 224 | # tp_repr. WIBNI we had ::Inline? :) |
| 225 | pass |
| 226 | |
| 227 | def test_class(self): |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 228 | touch(os.path.join(self.subpkgname, 'bar'+os.extsep+'py'), '''\ |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 229 | class bar: |
| 230 | pass |
| 231 | ''') |
| 232 | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import bar |
Mark Hammond | d800ae1 | 2003-01-16 04:56:52 +0000 | [diff] [blame] | 233 | # Module name may be prefixed with "test.", depending on how run. |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 234 | self.failUnless(repr(bar.bar).startswith( |
Mark Hammond | d800ae1 | 2003-01-16 04:56:52 +0000 | [diff] [blame] | 235 | "<class %s.bar at 0x" % bar.__name__)) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 236 | |
| 237 | def test_instance(self): |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 238 | touch(os.path.join(self.subpkgname, 'baz'+os.extsep+'py'), '''\ |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 239 | class baz: |
| 240 | pass |
| 241 | ''') |
| 242 | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import baz |
| 243 | ibaz = baz.baz() |
| 244 | self.failUnless(repr(ibaz).startswith( |
Mark Hammond | d800ae1 | 2003-01-16 04:56:52 +0000 | [diff] [blame] | 245 | "<%s.baz instance at 0x" % baz.__name__)) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 246 | |
| 247 | def test_method(self): |
| 248 | eq = self.assertEquals |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 249 | touch(os.path.join(self.subpkgname, 'qux'+os.extsep+'py'), '''\ |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 250 | class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: |
| 251 | def amethod(self): pass |
| 252 | ''') |
| 253 | from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import qux |
| 254 | # Unbound methods first |
| 255 | eq(repr(qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod), |
| 256 | '<unbound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod>') |
| 257 | # Bound method next |
| 258 | iqux = qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() |
| 259 | self.failUnless(repr(iqux.amethod).startswith( |
Mark Hammond | d800ae1 | 2003-01-16 04:56:52 +0000 | [diff] [blame] | 260 | '<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x' \ |
| 261 | % (qux.__name__,) )) |
Barry Warsaw | 0bcf6d8 | 2001-08-24 18:37:32 +0000 | [diff] [blame] | 262 | |
| 263 | def test_builtin_function(self): |
| 264 | # XXX test built-in functions and methods with really long names |
| 265 | pass |
Fred Drake | 8e6669a | 2001-07-19 22:27:56 +0000 | [diff] [blame] | 266 | |
| 267 | class ClassWithRepr: |
| 268 | def __init__(self, s): |
| 269 | self.s = s |
| 270 | def __repr__(self): |
| 271 | return "ClassWithLongRepr(%r)" % self.s |
| 272 | |
| 273 | |
| 274 | class ClassWithFailingRepr: |
| 275 | def __repr__(self): |
| 276 | raise Exception("This should be caught by Repr.repr_instance") |
| 277 | |
| 278 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 279 | def test_main(): |
| 280 | run_unittest(ReprTests) |
| 281 | if os.name != 'mac': |
| 282 | run_unittest(LongReprTest) |
| 283 | |
| 284 | |
| 285 | if __name__ == "__main__": |
| 286 | test_main() |