##// END OF EJS Templates
Add failing test for pretty repr of super() objects
Thomas Kluyver -
Show More
@@ -1,184 +1,199 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
48
49 class Dummy1(object):
49 class Dummy1(object):
50 def _repr_pretty_(self, p, cycle):
50 def _repr_pretty_(self, p, cycle):
51 p.text("Dummy1(...)")
51 p.text("Dummy1(...)")
52
52
53 class Dummy2(Dummy1):
53 class Dummy2(Dummy1):
54 _repr_pretty_ = None
54 _repr_pretty_ = None
55
55
56 class NoModule(object):
56 class NoModule(object):
57 pass
57 pass
58
58
59 NoModule.__module__ = None
59 NoModule.__module__ = None
60
60
61 class Breaking(object):
61 class Breaking(object):
62 def _repr_pretty_(self, p, cycle):
62 def _repr_pretty_(self, p, cycle):
63 with p.group(4,"TG: ",":"):
63 with p.group(4,"TG: ",":"):
64 p.text("Breaking(")
64 p.text("Breaking(")
65 p.break_()
65 p.break_()
66 p.text(")")
66 p.text(")")
67
67
68 class BreakingRepr(object):
68 class BreakingRepr(object):
69 def __repr__(self):
69 def __repr__(self):
70 return "Breaking(\n)"
70 return "Breaking(\n)"
71
71
72 class BreakingReprParent(object):
72 class BreakingReprParent(object):
73 def _repr_pretty_(self, p, cycle):
73 def _repr_pretty_(self, p, cycle):
74 with p.group(4,"TG: ",":"):
74 with p.group(4,"TG: ",":"):
75 p.pretty(BreakingRepr())
75 p.pretty(BreakingRepr())
76
76
77 class BadRepr(object):
77 class BadRepr(object):
78
78
79 def __repr__(self):
79 def __repr__(self):
80 return 1/0
80 return 1/0
81
81
82
82
83 def test_indentation():
83 def test_indentation():
84 """Test correct indentation in groups"""
84 """Test correct indentation in groups"""
85 count = 40
85 count = 40
86 gotoutput = pretty.pretty(MyList(range(count)))
86 gotoutput = pretty.pretty(MyList(range(count)))
87 expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")"
87 expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")"
88
88
89 nt.assert_equal(gotoutput, expectedoutput)
89 nt.assert_equal(gotoutput, expectedoutput)
90
90
91
91
92 def test_dispatch():
92 def test_dispatch():
93 """
93 """
94 Test correct dispatching: The _repr_pretty_ method for MyDict
94 Test correct dispatching: The _repr_pretty_ method for MyDict
95 must be found before the registered printer for dict.
95 must be found before the registered printer for dict.
96 """
96 """
97 gotoutput = pretty.pretty(MyDict())
97 gotoutput = pretty.pretty(MyDict())
98 expectedoutput = "MyDict(...)"
98 expectedoutput = "MyDict(...)"
99
99
100 nt.assert_equal(gotoutput, expectedoutput)
100 nt.assert_equal(gotoutput, expectedoutput)
101
101
102
102
103 def test_callability_checking():
103 def test_callability_checking():
104 """
104 """
105 Test that the _repr_pretty_ method is tested for callability and skipped if
105 Test that the _repr_pretty_ method is tested for callability and skipped if
106 not.
106 not.
107 """
107 """
108 gotoutput = pretty.pretty(Dummy2())
108 gotoutput = pretty.pretty(Dummy2())
109 expectedoutput = "Dummy1(...)"
109 expectedoutput = "Dummy1(...)"
110
110
111 nt.assert_equal(gotoutput, expectedoutput)
111 nt.assert_equal(gotoutput, expectedoutput)
112
112
113
113
114 def test_sets():
114 def test_sets():
115 """
115 """
116 Test that set and frozenset use Python 3 formatting.
116 Test that set and frozenset use Python 3 formatting.
117 """
117 """
118 objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]),
118 objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]),
119 frozenset([1, 2]), set([-1, -2, -3])]
119 frozenset([1, 2]), set([-1, -2, -3])]
120 expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}',
120 expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}',
121 'frozenset({1, 2})', '{-3, -2, -1}']
121 'frozenset({1, 2})', '{-3, -2, -1}']
122 for obj, expected_output in zip(objects, expected):
122 for obj, expected_output in zip(objects, expected):
123 got_output = pretty.pretty(obj)
123 got_output = pretty.pretty(obj)
124 yield nt.assert_equal, got_output, expected_output
124 yield nt.assert_equal, got_output, expected_output
125
125
126
126
127 @skip_without('xxlimited')
127 @skip_without('xxlimited')
128 def test_pprint_heap_allocated_type():
128 def test_pprint_heap_allocated_type():
129 """
129 """
130 Test that pprint works for heap allocated types.
130 Test that pprint works for heap allocated types.
131 """
131 """
132 import xxlimited
132 import xxlimited
133 output = pretty.pretty(xxlimited.Null)
133 output = pretty.pretty(xxlimited.Null)
134 nt.assert_equal(output, 'xxlimited.Null')
134 nt.assert_equal(output, 'xxlimited.Null')
135
135
136 def test_pprint_nomod():
136 def test_pprint_nomod():
137 """
137 """
138 Test that pprint works for classes with no __module__.
138 Test that pprint works for classes with no __module__.
139 """
139 """
140 output = pretty.pretty(NoModule)
140 output = pretty.pretty(NoModule)
141 nt.assert_equal(output, 'NoModule')
141 nt.assert_equal(output, 'NoModule')
142
142
143 def test_pprint_break():
143 def test_pprint_break():
144 """
144 """
145 Test that p.break_ produces expected output
145 Test that p.break_ produces expected output
146 """
146 """
147 output = pretty.pretty(Breaking())
147 output = pretty.pretty(Breaking())
148 expected = "TG: Breaking(\n ):"
148 expected = "TG: Breaking(\n ):"
149 nt.assert_equal(output, expected)
149 nt.assert_equal(output, expected)
150
150
151 def test_pprint_break_repr():
151 def test_pprint_break_repr():
152 """
152 """
153 Test that p.break_ is used in repr
153 Test that p.break_ is used in repr
154 """
154 """
155 output = pretty.pretty(BreakingReprParent())
155 output = pretty.pretty(BreakingReprParent())
156 expected = "TG: Breaking(\n ):"
156 expected = "TG: Breaking(\n ):"
157 nt.assert_equal(output, expected)
157 nt.assert_equal(output, expected)
158
158
159 def test_bad_repr():
159 def test_bad_repr():
160 """Don't raise, even when repr fails"""
160 """Don't raise, even when repr fails"""
161 output = pretty.pretty(BadRepr())
161 output = pretty.pretty(BadRepr())
162 nt.assert_in("failed", output)
162 nt.assert_in("failed", output)
163 nt.assert_in("at 0x", output)
163 nt.assert_in("at 0x", output)
164 nt.assert_in("test_pretty", output)
164 nt.assert_in("test_pretty", output)
165
165
166 class BadException(Exception):
166 class BadException(Exception):
167 def __str__(self):
167 def __str__(self):
168 return -1
168 return -1
169
169
170 class ReallyBadRepr(object):
170 class ReallyBadRepr(object):
171 __module__ = 1
171 __module__ = 1
172 @property
172 @property
173 def __class__(self):
173 def __class__(self):
174 raise ValueError("I am horrible")
174 raise ValueError("I am horrible")
175
175
176 def __repr__(self):
176 def __repr__(self):
177 raise BadException()
177 raise BadException()
178
178
179 def test_really_bad_repr():
179 def test_really_bad_repr():
180 output = pretty.pretty(ReallyBadRepr())
180 output = pretty.pretty(ReallyBadRepr())
181 nt.assert_in("failed", output)
181 nt.assert_in("failed", output)
182 nt.assert_in("BadException: unknown", output)
182 nt.assert_in("BadException: unknown", output)
183 nt.assert_in("unknown type", output)
183 nt.assert_in("unknown type", output)
184
184
185
186 class SA(object):
187 pass
188
189 class SB(SA):
190 pass
191
192 def test_super_repr():
193 output = pretty.pretty(super(SA))
194 nt.assert_in("SA", output)
195
196 sb = SB()
197 output = pretty.pretty(super(SA, sb))
198 nt.assert_in("SA", output)
199 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now