Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / lib / python2.4 / test / test_repr.py
CommitLineData
86530b38
AT
1"""
2 Test cases for the repr module
3 Nick Mathewson
4"""
5
6import sys
7import os
8import unittest
9
10from test.test_support import run_unittest
11from repr import repr as r # Don't shadow builtin repr
12
13
14def nestedTuple(nesting):
15 t = ()
16 for i in range(nesting):
17 t = (t,)
18 return t
19
20class 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 = repr(s)[:13] + "..." + repr(s)[-14:]
29 eq(r(s), expected)
30
31 eq(r("\"'"), repr("\"'"))
32 s = "\""*30+"'"*100
33 expected = repr(s)[:13] + "..." + repr(s)[-14:]
34 eq(r(s), expected)
35
36 def test_container(self):
37 from array import array
38 from collections import deque
39
40 eq = self.assertEquals
41 # Tuples give up after 6 elements
42 eq(r(()), "()")
43 eq(r((1,)), "(1,)")
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, ...)")
47
48 # Lists give up after 6 as well
49 eq(r([]), "[]")
50 eq(r([1]), "[1]")
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, ...]")
54
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, ...])")
61
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, ...])")
68
69 # collections.deque after 6
70 eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])")
71
72 # Dictionaries give up after 4.
73 eq(r({}), "{}")
74 d = {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}
75 eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}")
76 d['arthur'] = 1
77 eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}")
78
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, ...])")
88
89 def test_numbers(self):
90 eq = self.assertEquals
91 eq(r(123), repr(123))
92 eq(r(123L), repr(123L))
93 eq(r(1.0/3), repr(1.0/3))
94
95 n = 10L**100
96 expected = repr(n)[:18] + "..." + repr(n)[-19:]
97 eq(r(n), expected)
98
99 def test_instance(self):
100 eq = self.assertEquals
101 i1 = ClassWithRepr("a")
102 eq(r(i1), repr(i1))
103
104 i2 = ClassWithRepr("x"*1000)
105 expected = repr(i2)[:13] + "..." + repr(i2)[-14:]
106 eq(r(i2), expected)
107
108 i3 = ClassWithFailingRepr()
109 eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%id(i3)))
110
111 s = r(ClassWithFailingRepr)
112 self.failUnless(s.startswith("<class "))
113 self.failUnless(s.endswith(">"))
114 self.failUnless(s.find("...") == 8)
115
116 def test_file(self):
117 fp = open(unittest.__file__)
118 self.failUnless(repr(fp).startswith(
119 "<open file '%s', mode 'r' at 0x" % unittest.__file__))
120 fp.close()
121 self.failUnless(repr(fp).startswith(
122 "<closed file '%s', mode 'r' at 0x" % unittest.__file__))
123
124 def test_lambda(self):
125 self.failUnless(repr(lambda x: x).startswith(
126 "<function <lambda"))
127 # XXX anonymous functions? see func_repr
128
129 def test_builtin_function(self):
130 eq = self.assertEquals
131 # Functions
132 eq(repr(hash), '<built-in function hash>')
133 # Methods
134 self.failUnless(repr(''.split).startswith(
135 '<built-in method split of str object at 0x'))
136
137 def test_xrange(self):
138 import warnings
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)')
143
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([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]")
149
150 eq(r(nestedTuple(6)), "(((((((),),),),),),)")
151 eq(r(nestedTuple(7)), "(((((((...),),),),),),)")
152
153 eq(r({ nestedTuple(5) : nestedTuple(5) }),
154 "{((((((),),),),),): ((((((),),),),),)}")
155 eq(r({ nestedTuple(6) : nestedTuple(6) }),
156 "{((((((...),),),),),): ((((((...),),),),),)}")
157
158 eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]")
159 eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]")
160
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.
164 x = buffer('foo')
165 self.failUnless(repr(x).startswith('<read-only buffer for 0x'))
166
167 def test_cell(self):
168 # XXX Hmm? How to get at a cell object?
169 pass
170
171 def test_descriptors(self):
172 eq = self.assertEquals
173 # method descriptors
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
179 class C:
180 def foo(cls): pass
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'))
185
186def touch(path, text=''):
187 fp = open(path, 'w')
188 fp.write(text)
189 fp.close()
190
191def zap(actions, dirname, names):
192 for name in names:
193 actions.append(os.path.join(dirname, name))
194
195class LongReprTest(unittest.TestCase):
196 def setUp(self):
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)
208
209 def tearDown(self):
210 actions = []
211 os.path.walk(self.pkgname, zap, actions)
212 actions.append(self.pkgname)
213 actions.sort()
214 actions.reverse()
215 for p in actions:
216 if os.path.isdir(p):
217 os.rmdir(p)
218 else:
219 os.remove(p)
220 del sys.path[0]
221
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)>")
229
230 def test_type(self):
231 eq = self.assertEquals
232 touch(os.path.join(self.subpkgname, 'foo'+os.extsep+'py'), '''\
233class foo(object):
234 pass
235''')
236 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import foo
237 eq(repr(foo.foo),
238 "<class '%s.foo'>" % foo.__name__)
239
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? :)
243 pass
244
245 def test_class(self):
246 touch(os.path.join(self.subpkgname, 'bar'+os.extsep+'py'), '''\
247class bar:
248 pass
249''')
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__))
254
255 def test_instance(self):
256 touch(os.path.join(self.subpkgname, 'baz'+os.extsep+'py'), '''\
257class baz:
258 pass
259''')
260 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import baz
261 ibaz = baz.baz()
262 self.failUnless(repr(ibaz).startswith(
263 "<%s.baz instance at 0x" % baz.__name__))
264
265 def test_method(self):
266 eq = self.assertEquals
267 touch(os.path.join(self.subpkgname, 'qux'+os.extsep+'py'), '''\
268class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
269 def amethod(self): pass
270''')
271 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import qux
272 # Unbound methods first
273 eq(repr(qux.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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x' \
279 % (qux.__name__,) ))
280
281 def test_builtin_function(self):
282 # XXX test built-in functions and methods with really long names
283 pass
284
285class ClassWithRepr:
286 def __init__(self, s):
287 self.s = s
288 def __repr__(self):
289 return "ClassWithLongRepr(%r)" % self.s
290
291
292class ClassWithFailingRepr:
293 def __repr__(self):
294 raise Exception("This should be caught by Repr.repr_instance")
295
296
297def test_main():
298 run_unittest(ReprTests)
299 if os.name != 'mac':
300 run_unittest(LongReprTest)
301
302
303if __name__ == "__main__":
304 test_main()