##// END OF EJS Templates
Add failing test for unbound method repr on Python 3
Thomas Kluyver -
Show More
@@ -1,224 +1,231 b''
1 """Tests for IPython.lib.pretty.
1 """Tests for IPython.lib.pretty.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2011, the IPython Development Team.
4 # Copyright (c) 2011, the IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 from __future__ import print_function
14 from __future__ import print_function
15
15
16 # Third-party imports
16 # Third-party imports
17 import nose.tools as nt
17 import nose.tools as nt
18
18
19 # Our own imports
19 # Our own imports
20 from IPython.lib import pretty
20 from IPython.lib import pretty
21 from IPython.testing.decorators import skip_without
21 from IPython.testing.decorators import skip_without
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Classes and functions
24 # Classes and functions
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27 class MyList(object):
27 class MyList(object):
28 def __init__(self, content):
28 def __init__(self, content):
29 self.content = content
29 self.content = content
30 def _repr_pretty_(self, p, cycle):
30 def _repr_pretty_(self, p, cycle):
31 if cycle:
31 if cycle:
32 p.text("MyList(...)")
32 p.text("MyList(...)")
33 else:
33 else:
34 with p.group(3, "MyList(", ")"):
34 with p.group(3, "MyList(", ")"):
35 for (i, child) in enumerate(self.content):
35 for (i, child) in enumerate(self.content):
36 if i:
36 if i:
37 p.text(",")
37 p.text(",")
38 p.breakable()
38 p.breakable()
39 else:
39 else:
40 p.breakable("")
40 p.breakable("")
41 p.pretty(child)
41 p.pretty(child)
42
42
43
43
44 class MyDict(dict):
44 class MyDict(dict):
45 def _repr_pretty_(self, p, cycle):
45 def _repr_pretty_(self, p, cycle):
46 p.text("MyDict(...)")
46 p.text("MyDict(...)")
47
47
48 class MyObj(object):
49 def somemethod(self):
50 pass
51
48
52
49 class Dummy1(object):
53 class Dummy1(object):
50 def _repr_pretty_(self, p, cycle):
54 def _repr_pretty_(self, p, cycle):
51 p.text("Dummy1(...)")
55 p.text("Dummy1(...)")
52
56
53 class Dummy2(Dummy1):
57 class Dummy2(Dummy1):
54 _repr_pretty_ = None
58 _repr_pretty_ = None
55
59
56 class NoModule(object):
60 class NoModule(object):
57 pass
61 pass
58
62
59 NoModule.__module__ = None
63 NoModule.__module__ = None
60
64
61 class Breaking(object):
65 class Breaking(object):
62 def _repr_pretty_(self, p, cycle):
66 def _repr_pretty_(self, p, cycle):
63 with p.group(4,"TG: ",":"):
67 with p.group(4,"TG: ",":"):
64 p.text("Breaking(")
68 p.text("Breaking(")
65 p.break_()
69 p.break_()
66 p.text(")")
70 p.text(")")
67
71
68 class BreakingRepr(object):
72 class BreakingRepr(object):
69 def __repr__(self):
73 def __repr__(self):
70 return "Breaking(\n)"
74 return "Breaking(\n)"
71
75
72 class BreakingReprParent(object):
76 class BreakingReprParent(object):
73 def _repr_pretty_(self, p, cycle):
77 def _repr_pretty_(self, p, cycle):
74 with p.group(4,"TG: ",":"):
78 with p.group(4,"TG: ",":"):
75 p.pretty(BreakingRepr())
79 p.pretty(BreakingRepr())
76
80
77 class BadRepr(object):
81 class BadRepr(object):
78
82
79 def __repr__(self):
83 def __repr__(self):
80 return 1/0
84 return 1/0
81
85
82
86
83 def test_indentation():
87 def test_indentation():
84 """Test correct indentation in groups"""
88 """Test correct indentation in groups"""
85 count = 40
89 count = 40
86 gotoutput = pretty.pretty(MyList(range(count)))
90 gotoutput = pretty.pretty(MyList(range(count)))
87 expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")"
91 expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")"
88
92
89 nt.assert_equal(gotoutput, expectedoutput)
93 nt.assert_equal(gotoutput, expectedoutput)
90
94
91
95
92 def test_dispatch():
96 def test_dispatch():
93 """
97 """
94 Test correct dispatching: The _repr_pretty_ method for MyDict
98 Test correct dispatching: The _repr_pretty_ method for MyDict
95 must be found before the registered printer for dict.
99 must be found before the registered printer for dict.
96 """
100 """
97 gotoutput = pretty.pretty(MyDict())
101 gotoutput = pretty.pretty(MyDict())
98 expectedoutput = "MyDict(...)"
102 expectedoutput = "MyDict(...)"
99
103
100 nt.assert_equal(gotoutput, expectedoutput)
104 nt.assert_equal(gotoutput, expectedoutput)
101
105
102
106
103 def test_callability_checking():
107 def test_callability_checking():
104 """
108 """
105 Test that the _repr_pretty_ method is tested for callability and skipped if
109 Test that the _repr_pretty_ method is tested for callability and skipped if
106 not.
110 not.
107 """
111 """
108 gotoutput = pretty.pretty(Dummy2())
112 gotoutput = pretty.pretty(Dummy2())
109 expectedoutput = "Dummy1(...)"
113 expectedoutput = "Dummy1(...)"
110
114
111 nt.assert_equal(gotoutput, expectedoutput)
115 nt.assert_equal(gotoutput, expectedoutput)
112
116
113
117
114 def test_sets():
118 def test_sets():
115 """
119 """
116 Test that set and frozenset use Python 3 formatting.
120 Test that set and frozenset use Python 3 formatting.
117 """
121 """
118 objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]),
122 objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]),
119 frozenset([1, 2]), set([-1, -2, -3])]
123 frozenset([1, 2]), set([-1, -2, -3])]
120 expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}',
124 expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}',
121 'frozenset({1, 2})', '{-3, -2, -1}']
125 'frozenset({1, 2})', '{-3, -2, -1}']
122 for obj, expected_output in zip(objects, expected):
126 for obj, expected_output in zip(objects, expected):
123 got_output = pretty.pretty(obj)
127 got_output = pretty.pretty(obj)
124 yield nt.assert_equal, got_output, expected_output
128 yield nt.assert_equal, got_output, expected_output
125
129
126
130
127 @skip_without('xxlimited')
131 @skip_without('xxlimited')
128 def test_pprint_heap_allocated_type():
132 def test_pprint_heap_allocated_type():
129 """
133 """
130 Test that pprint works for heap allocated types.
134 Test that pprint works for heap allocated types.
131 """
135 """
132 import xxlimited
136 import xxlimited
133 output = pretty.pretty(xxlimited.Null)
137 output = pretty.pretty(xxlimited.Null)
134 nt.assert_equal(output, 'xxlimited.Null')
138 nt.assert_equal(output, 'xxlimited.Null')
135
139
136 def test_pprint_nomod():
140 def test_pprint_nomod():
137 """
141 """
138 Test that pprint works for classes with no __module__.
142 Test that pprint works for classes with no __module__.
139 """
143 """
140 output = pretty.pretty(NoModule)
144 output = pretty.pretty(NoModule)
141 nt.assert_equal(output, 'NoModule')
145 nt.assert_equal(output, 'NoModule')
142
146
143 def test_pprint_break():
147 def test_pprint_break():
144 """
148 """
145 Test that p.break_ produces expected output
149 Test that p.break_ produces expected output
146 """
150 """
147 output = pretty.pretty(Breaking())
151 output = pretty.pretty(Breaking())
148 expected = "TG: Breaking(\n ):"
152 expected = "TG: Breaking(\n ):"
149 nt.assert_equal(output, expected)
153 nt.assert_equal(output, expected)
150
154
151 def test_pprint_break_repr():
155 def test_pprint_break_repr():
152 """
156 """
153 Test that p.break_ is used in repr
157 Test that p.break_ is used in repr
154 """
158 """
155 output = pretty.pretty(BreakingReprParent())
159 output = pretty.pretty(BreakingReprParent())
156 expected = "TG: Breaking(\n ):"
160 expected = "TG: Breaking(\n ):"
157 nt.assert_equal(output, expected)
161 nt.assert_equal(output, expected)
158
162
159 def test_bad_repr():
163 def test_bad_repr():
160 """Don't raise, even when repr fails"""
164 """Don't raise, even when repr fails"""
161 output = pretty.pretty(BadRepr())
165 output = pretty.pretty(BadRepr())
162 nt.assert_in("failed", output)
166 nt.assert_in("failed", output)
163 nt.assert_in("at 0x", output)
167 nt.assert_in("at 0x", output)
164 nt.assert_in("test_pretty", output)
168 nt.assert_in("test_pretty", output)
165
169
166 class BadException(Exception):
170 class BadException(Exception):
167 def __str__(self):
171 def __str__(self):
168 return -1
172 return -1
169
173
170 class ReallyBadRepr(object):
174 class ReallyBadRepr(object):
171 __module__ = 1
175 __module__ = 1
172 @property
176 @property
173 def __class__(self):
177 def __class__(self):
174 raise ValueError("I am horrible")
178 raise ValueError("I am horrible")
175
179
176 def __repr__(self):
180 def __repr__(self):
177 raise BadException()
181 raise BadException()
178
182
179 def test_really_bad_repr():
183 def test_really_bad_repr():
180 output = pretty.pretty(ReallyBadRepr())
184 output = pretty.pretty(ReallyBadRepr())
181 nt.assert_in("failed", output)
185 nt.assert_in("failed", output)
182 nt.assert_in("BadException: unknown", output)
186 nt.assert_in("BadException: unknown", output)
183 nt.assert_in("unknown type", output)
187 nt.assert_in("unknown type", output)
184
188
185
189
186 class SA(object):
190 class SA(object):
187 pass
191 pass
188
192
189 class SB(SA):
193 class SB(SA):
190 pass
194 pass
191
195
192 def test_super_repr():
196 def test_super_repr():
193 output = pretty.pretty(super(SA))
197 output = pretty.pretty(super(SA))
194 nt.assert_in("SA", output)
198 nt.assert_in("SA", output)
195
199
196 sb = SB()
200 sb = SB()
197 output = pretty.pretty(super(SA, sb))
201 output = pretty.pretty(super(SA, sb))
198 nt.assert_in("SA", output)
202 nt.assert_in("SA", output)
199
203
200
204
201 def test_long_list():
205 def test_long_list():
202 lis = list(range(10000))
206 lis = list(range(10000))
203 p = pretty.pretty(lis)
207 p = pretty.pretty(lis)
204 last2 = p.rsplit('\n', 2)[-2:]
208 last2 = p.rsplit('\n', 2)[-2:]
205 nt.assert_equal(last2, [' 999,', ' ...]'])
209 nt.assert_equal(last2, [' 999,', ' ...]'])
206
210
207 def test_long_set():
211 def test_long_set():
208 s = set(range(10000))
212 s = set(range(10000))
209 p = pretty.pretty(s)
213 p = pretty.pretty(s)
210 last2 = p.rsplit('\n', 2)[-2:]
214 last2 = p.rsplit('\n', 2)[-2:]
211 nt.assert_equal(last2, [' 999,', ' ...}'])
215 nt.assert_equal(last2, [' 999,', ' ...}'])
212
216
213 def test_long_tuple():
217 def test_long_tuple():
214 tup = tuple(range(10000))
218 tup = tuple(range(10000))
215 p = pretty.pretty(tup)
219 p = pretty.pretty(tup)
216 last2 = p.rsplit('\n', 2)[-2:]
220 last2 = p.rsplit('\n', 2)[-2:]
217 nt.assert_equal(last2, [' 999,', ' ...)'])
221 nt.assert_equal(last2, [' 999,', ' ...)'])
218
222
219 def test_long_dict():
223 def test_long_dict():
220 d = { n:n for n in range(10000) }
224 d = { n:n for n in range(10000) }
221 p = pretty.pretty(d)
225 p = pretty.pretty(d)
222 last2 = p.rsplit('\n', 2)[-2:]
226 last2 = p.rsplit('\n', 2)[-2:]
223 nt.assert_equal(last2, [' 999: 999,', ' ...}'])
227 nt.assert_equal(last2, [' 999: 999,', ' ...}'])
224
228
229 def test_unbound_method():
230 output = pretty.pretty(MyObj.somemethod)
231 nt.assert_in('MyObj.somemethod', output) No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now