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