annotate Lib/test/test_repr.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 9fbc41abdd4b
children dba99828d91c 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 """
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 Test cases for the repr module
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 Nick Mathewson
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4 """
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
6 import sys
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
7 import os
38507
d2008b31fd62 Don't fail if the (sub)pkgname already exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35768
diff changeset
8 import shutil
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9 import unittest
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
10
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 23360
diff changeset
11 from test.test_support import run_unittest
47221
9fbc41abdd4b Revert the renaming of repr to reprlib.
Brett Cannon <bcannon@gmail.com>
parents: 47089
diff changeset
12 from repr import repr as r # Don't shadow builtin repr
9fbc41abdd4b Revert the renaming of repr to reprlib.
Brett Cannon <bcannon@gmail.com>
parents: 47089
diff changeset
13 from repr import Repr
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 def nestedTuple(nesting):
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 t = ()
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 for i in range(nesting):
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 t = (t,)
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20 return t
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 class ReprTests(unittest.TestCase):
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 def test_string(self):
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 eq = self.assertEquals
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 eq(r("abc"), "'abc'")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27 eq(r("abcdefghijklmnop"),"'abcdefghijklmnop'")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29 s = "a"*30+"b"*30
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
30 expected = repr(s)[:13] + "..." + repr(s)[-14:]
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31 eq(r(s), expected)
19119
600a9a2b7a18 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18756
diff changeset
32
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33 eq(r("\"'"), repr("\"'"))
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 s = "\""*30+"'"*100
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
35 expected = repr(s)[:13] + "..." + repr(s)[-14:]
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 eq(r(s), expected)
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37
42033
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
38 def test_tuple(self):
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
39 eq = self.assertEquals
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
40 eq(r((1,)), "(1,)")
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
41
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
42 t3 = (1, 2, 3)
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
43 eq(r(t3), "(1, 2, 3)")
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
44
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
45 r2 = Repr()
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
46 r2.maxtuple = 2
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
47 expected = repr(t3)[:-2] + "...)"
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
48 eq(r2.repr(t3), expected)
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40454
diff changeset
49
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 def test_container(self):
27701
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
51 from array import array
32135
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
52 from collections import deque
27701
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
53
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54 eq = self.assertEquals
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 # Tuples give up after 6 elements
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 eq(r(()), "()")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 eq(r((1,)), "(1,)")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58 eq(r((1, 2, 3)), "(1, 2, 3)")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62 # Lists give up after 6 as well
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 eq(r([]), "[]")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 eq(r([1]), "[1]")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 eq(r([1, 2, 3]), "[1, 2, 3]")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
69 # Sets give up after 6 as well
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
70 eq(r(set([])), "set([])")
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
71 eq(r(set([1])), "set([1])")
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
72 eq(r(set([1, 2, 3])), "set([1, 2, 3])")
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
73 eq(r(set([1, 2, 3, 4, 5, 6])), "set([1, 2, 3, 4, 5, 6])")
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
74 eq(r(set([1, 2, 3, 4, 5, 6, 7])), "set([1, 2, 3, 4, 5, 6, ...])")
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
75
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
76 # Frozensets give up after 6 as well
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
77 eq(r(frozenset([])), "frozenset([])")
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
78 eq(r(frozenset([1])), "frozenset([1])")
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
79 eq(r(frozenset([1, 2, 3])), "frozenset([1, 2, 3])")
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
80 eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset([1, 2, 3, 4, 5, 6])")
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
81 eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset([1, 2, 3, 4, 5, 6, ...])")
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
82
32135
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
83 # collections.deque after 6
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
84 eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])")
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
85
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86 # Dictionaries give up after 4.
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87 eq(r({}), "{}")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
88 d = {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89 eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
90 d['arthur'] = 1
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
91 eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92
27701
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
93 # array.array after 5.
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
94 eq(r(array('i')), "array('i', [])")
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
95 eq(r(array('i', [1])), "array('i', [1])")
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
96 eq(r(array('i', [1, 2])), "array('i', [1, 2])")
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
97 eq(r(array('i', [1, 2, 3])), "array('i', [1, 2, 3])")
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
98 eq(r(array('i', [1, 2, 3, 4])), "array('i', [1, 2, 3, 4])")
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
99 eq(r(array('i', [1, 2, 3, 4, 5])), "array('i', [1, 2, 3, 4, 5])")
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
100 eq(r(array('i', [1, 2, 3, 4, 5, 6])),
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
101 "array('i', [1, 2, 3, 4, 5, ...])")
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 27261
diff changeset
102
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103 def test_numbers(self):
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104 eq = self.assertEquals
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 eq(r(123), repr(123))
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106 eq(r(123L), repr(123L))
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
107 eq(r(1.0/3), repr(1.0/3))
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 n = 10L**100
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
110 expected = repr(n)[:18] + "..." + repr(n)[-19:]
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111 eq(r(n), expected)
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
113 def test_instance(self):
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
114 eq = self.assertEquals
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
115 i1 = ClassWithRepr("a")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116 eq(r(i1), repr(i1))
19119
600a9a2b7a18 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18756
diff changeset
117
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118 i2 = ClassWithRepr("x"*1000)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
119 expected = repr(i2)[:13] + "..." + repr(i2)[-14:]
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 eq(r(i2), expected)
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122 i3 = ClassWithFailingRepr()
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%id(i3)))
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
124
19714
450e5a0e926b Add a test for the final branch in repr.Repr.repr1(), which deals with
Guido van Rossum <guido@python.org>
parents: 19478
diff changeset
125 s = r(ClassWithFailingRepr)
450e5a0e926b Add a test for the final branch in repr.Repr.repr1(), which deals with
Guido van Rossum <guido@python.org>
parents: 19478
diff changeset
126 self.failUnless(s.startswith("<class "))
450e5a0e926b Add a test for the final branch in repr.Repr.repr1(), which deals with
Guido van Rossum <guido@python.org>
parents: 19478
diff changeset
127 self.failUnless(s.endswith(">"))
450e5a0e926b Add a test for the final branch in repr.Repr.repr1(), which deals with
Guido van Rossum <guido@python.org>
parents: 19478
diff changeset
128 self.failUnless(s.find("...") == 8)
450e5a0e926b Add a test for the final branch in repr.Repr.repr1(), which deals with
Guido van Rossum <guido@python.org>
parents: 19478
diff changeset
129
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
130 def test_file(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
131 fp = open(unittest.__file__)
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
132 self.failUnless(repr(fp).startswith(
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
133 "<open file '%s', mode 'r' at 0x" % unittest.__file__))
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
134 fp.close()
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
135 self.failUnless(repr(fp).startswith(
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
136 "<closed file '%s', mode 'r' at 0x" % unittest.__file__))
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
137
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
138 def test_lambda(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
139 self.failUnless(repr(lambda x: x).startswith(
35768
576b9a9ce579 Revert change, func_name of lambda's is back to <lambda>.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
140 "<function <lambda"))
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
141 # XXX anonymous functions? see func_repr
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
142
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
143 def test_builtin_function(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
144 eq = self.assertEquals
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
145 # Functions
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
146 eq(repr(hash), '<built-in function hash>')
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
147 # Methods
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
148 self.failUnless(repr(''.split).startswith(
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
149 '<built-in method split of str object at 0x'))
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
150
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
151 def test_xrange(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
152 eq = self.assertEquals
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
153 eq(repr(xrange(1)), 'xrange(1)')
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
154 eq(repr(xrange(1, 2)), 'xrange(1, 2)')
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
155 eq(repr(xrange(1, 2, 3)), 'xrange(1, 4, 3)')
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
156
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
157 def test_nesting(self):
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
158 eq = self.assertEquals
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
159 # everything is meant to give up after 6 levels.
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
160 eq(r([[[[[[[]]]]]]]), "[[[[[[[]]]]]]]")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
161 eq(r([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
162
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
163 eq(r(nestedTuple(6)), "(((((((),),),),),),)")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
164 eq(r(nestedTuple(7)), "(((((((...),),),),),),)")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
165
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
166 eq(r({ nestedTuple(5) : nestedTuple(5) }),
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
167 "{((((((),),),),),): ((((((),),),),),)}")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
168 eq(r({ nestedTuple(6) : nestedTuple(6) }),
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
169 "{((((((...),),),),),): ((((((...),),),),),)}")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
170
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
171 eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
172 eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
173
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
174 def test_buffer(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
175 # XXX doesn't test buffers with no b_base or read-write buffers (see
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
176 # bufferobject.c). The test is fairly incomplete too. Sigh.
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
177 x = buffer('foo')
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
178 self.failUnless(repr(x).startswith('<read-only buffer for 0x'))
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
179
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
180 def test_cell(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
181 # XXX Hmm? How to get at a cell object?
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
182 pass
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
183
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
184 def test_descriptors(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
185 eq = self.assertEquals
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
186 # method descriptors
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
187 eq(repr(dict.items), "<method 'items' of 'dict' objects>")
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
188 # XXX member descriptors
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
189 # XXX attribute descriptors
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
190 # XXX slot descriptors
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
191 # static and class methods
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
192 class C:
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
193 def foo(cls): pass
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
194 x = staticmethod(C.foo)
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
195 self.failUnless(repr(x).startswith('<staticmethod object at 0x'))
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
196 x = classmethod(C.foo)
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
197 self.failUnless(repr(x).startswith('<classmethod object at 0x'))
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
198
43347
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
199 def test_unsortable(self):
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
200 # Repr.repr() used to call sorted() on sets, frozensets and dicts
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
201 # without taking into account that not all objects are comparable
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
202 x = set([1j, 2j, 3j])
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
203 y = frozenset(x)
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
204 z = {1j: 1, 2j: 2}
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
205 r(x)
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
206 r(y)
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
207 r(z)
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
208
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
209 def touch(path, text=''):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
210 fp = open(path, 'w')
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
211 fp.write(text)
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
212 fp.close()
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
213
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
214 class LongReprTest(unittest.TestCase):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
215 def setUp(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
216 longname = 'areallylongpackageandmodulenametotestreprtruncation'
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
217 self.pkgname = os.path.join(longname)
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
218 self.subpkgname = os.path.join(longname, longname)
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
219 # Make the package and subpackage
38507
d2008b31fd62 Don't fail if the (sub)pkgname already exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35768
diff changeset
220 shutil.rmtree(self.pkgname, ignore_errors=True)
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
221 os.mkdir(self.pkgname)
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
222 touch(os.path.join(self.pkgname, '__init__'+os.extsep+'py'))
38507
d2008b31fd62 Don't fail if the (sub)pkgname already exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35768
diff changeset
223 shutil.rmtree(self.subpkgname, ignore_errors=True)
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
224 os.mkdir(self.subpkgname)
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
225 touch(os.path.join(self.subpkgname, '__init__'+os.extsep+'py'))
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
226 # Remember where we are
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
227 self.here = os.getcwd()
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
228 sys.path.insert(0, self.here)
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
229
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
230 def tearDown(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
231 actions = []
46767
dc573ee0d904 Replace instances of os.path.walk with os.walk
Benjamin Peterson <benjamin@python.org>
parents: 43347
diff changeset
232 for dirpath, dirnames, filenames in os.walk(self.pkgname):
dc573ee0d904 Replace instances of os.path.walk with os.walk
Benjamin Peterson <benjamin@python.org>
parents: 43347
diff changeset
233 for name in dirnames + filenames:
dc573ee0d904 Replace instances of os.path.walk with os.walk
Benjamin Peterson <benjamin@python.org>
parents: 43347
diff changeset
234 actions.append(os.path.join(dirpath, name))
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
235 actions.append(self.pkgname)
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
236 actions.sort()
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
237 actions.reverse()
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
238 for p in actions:
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
239 if os.path.isdir(p):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
240 os.rmdir(p)
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
241 else:
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
242 os.remove(p)
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
243 del sys.path[0]
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
244
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
245 def test_module(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
246 eq = self.assertEquals
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
247 touch(os.path.join(self.subpkgname, self.pkgname + os.extsep + 'py'))
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
248 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
249 eq(repr(areallylongpackageandmodulenametotestreprtruncation),
27261
f05764d7082c This test previously failed when run from the 'test' directory. In that
Mark Hammond <mhammond@skippinet.com.au>
parents: 24364
diff changeset
250 "<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__))
21591
5b7b386cb0d4 SF Patch #494872 test repr() of a built-in module
Neal Norwitz <nnorwitz@gmail.com>
parents: 20754
diff changeset
251 eq(repr(sys), "<module 'sys' (built-in)>")
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
252
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
253 def test_type(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
254 eq = self.assertEquals
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
255 touch(os.path.join(self.subpkgname, 'foo'+os.extsep+'py'), '''\
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
256 class foo(object):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
257 pass
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
258 ''')
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
259 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import foo
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
260 eq(repr(foo.foo),
27261
f05764d7082c This test previously failed when run from the 'test' directory. In that
Mark Hammond <mhammond@skippinet.com.au>
parents: 24364
diff changeset
261 "<class '%s.foo'>" % foo.__name__)
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
262
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
263 def test_object(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
264 # XXX Test the repr of a type with a really long tp_name but with no
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
265 # tp_repr. WIBNI we had ::Inline? :)
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
266 pass
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
267
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
268 def test_class(self):
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
269 touch(os.path.join(self.subpkgname, 'bar'+os.extsep+'py'), '''\
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
270 class bar:
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
271 pass
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
272 ''')
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
273 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import bar
27261
f05764d7082c This test previously failed when run from the 'test' directory. In that
Mark Hammond <mhammond@skippinet.com.au>
parents: 24364
diff changeset
274 # Module name may be prefixed with "test.", depending on how run.
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
275 self.failUnless(repr(bar.bar).startswith(
27261
f05764d7082c This test previously failed when run from the 'test' directory. In that
Mark Hammond <mhammond@skippinet.com.au>
parents: 24364
diff changeset
276 "<class %s.bar at 0x" % bar.__name__))
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
277
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
278 def test_instance(self):
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
279 touch(os.path.join(self.subpkgname, 'baz'+os.extsep+'py'), '''\
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
280 class baz:
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
281 pass
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
282 ''')
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
283 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import baz
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
284 ibaz = baz.baz()
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
285 self.failUnless(repr(ibaz).startswith(
27261
f05764d7082c This test previously failed when run from the 'test' directory. In that
Mark Hammond <mhammond@skippinet.com.au>
parents: 24364
diff changeset
286 "<%s.baz instance at 0x" % baz.__name__))
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
287
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
288 def test_method(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
289 eq = self.assertEquals
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
290 touch(os.path.join(self.subpkgname, 'qux'+os.extsep+'py'), '''\
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
291 class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
292 def amethod(self): pass
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
293 ''')
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
294 from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import qux
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
295 # Unbound methods first
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
296 eq(repr(qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod),
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
297 '<unbound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod>')
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
298 # Bound method next
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
299 iqux = qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
300 self.failUnless(repr(iqux.amethod).startswith(
27261
f05764d7082c This test previously failed when run from the 'test' directory. In that
Mark Hammond <mhammond@skippinet.com.au>
parents: 24364
diff changeset
301 '<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x' \
f05764d7082c This test previously failed when run from the 'test' directory. In that
Mark Hammond <mhammond@skippinet.com.au>
parents: 24364
diff changeset
302 % (qux.__name__,) ))
19478
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
303
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
304 def test_builtin_function(self):
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
305 # XXX test built-in functions and methods with really long names
67b325ce0226 Added lots of tests for reprs of "simple" objects, such as file,
Barry Warsaw <barry@python.org>
parents: 19119
diff changeset
306 pass
18756
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
307
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
308 class ClassWithRepr:
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
309 def __init__(self, s):
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
310 self.s = s
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
311 def __repr__(self):
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
312 return "ClassWithLongRepr(%r)" % self.s
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
313
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
314
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
315 class ClassWithFailingRepr:
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
316 def __repr__(self):
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
317 raise Exception("This should be caught by Repr.repr_instance")
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
318
af9ad90a4445 Test for the repr module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
319
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19922
diff changeset
320 def test_main():
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19922
diff changeset
321 run_unittest(ReprTests)
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19922
diff changeset
322 if os.name != 'mac':
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19922
diff changeset
323 run_unittest(LongReprTest)
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19922
diff changeset
324
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19922
diff changeset
325
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19922
diff changeset
326 if __name__ == "__main__":
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19922
diff changeset
327 test_main()