-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_helper.py
245 lines (194 loc) · 7.68 KB
/
test_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from __future__ import absolute_import, division
import textwrap
import sys
import pytest
from IPython.lib.pretty import pretty
from represent import PrettyReprHelper, ReprHelper, ReprHelperMixin
def test_helper_methods():
class ContrivedExample(object):
def __init__(self, description, radians, shape, color, miles):
self.description = description
self.degrees = radians * 180 / 3.141592654
self.shape = shape
self._color = color
self.km = 1.60934 * miles
def _repr_helper(self, r):
r.positional_from_attr('description')
r.positional_with_value(self.degrees * 3.141592654 / 180)
r.keyword_from_attr('shape')
r.keyword_from_attr('color', '_color')
r.keyword_with_value('miles', self.km / 1.60934)
def __repr__(self):
r = ReprHelper(self)
self._repr_helper(r)
return str(r)
def _repr_pretty_(self, p, cycle):
with PrettyReprHelper(self, p, cycle) as r:
self._repr_helper(r)
ce = ContrivedExample('does something', 0.345, 'square', 'red', 22)
assert repr(ce) == ("ContrivedExample('does something', 0.345, "
"shape='square', color='red', miles=22.0)")
prettystr = """
ContrivedExample('does something',
0.345,
shape='square',
color='red',
miles=22.0)"""
assert pretty(ce) == textwrap.dedent(prettystr).lstrip()
class RecursionChecker(object):
def __init__(self, a, b, c, d, e):
self.a = a
self.b = b
self.c = c
self._d = d
self.e = e
def _repr_helper(self, r):
r.positional_from_attr('a')
r.positional_with_value(self.b)
r.keyword_from_attr('c')
r.keyword_from_attr('d', '_d')
r.keyword_with_value('e', self.e)
def _repr_pretty_(self, p, cycle):
with PrettyReprHelper(self, p, cycle) as r:
self._repr_helper(r)
rc = RecursionChecker(None, None, None, None, None)
rc.a = rc
rc.b = rc
rc.c = rc
rc._d = rc
rc.e = rc
prettystr = """
RecursionChecker(RecursionChecker(...),
RecursionChecker(...),
c=RecursionChecker(...),
d=RecursionChecker(...),
e=RecursionChecker(...))"""
assert pretty(rc) == textwrap.dedent(prettystr).lstrip()
def test_helper_exceptions():
class A(object):
def __init__(self, a, b):
self.a = a
self.b = b
def _repr_helper(self, r):
# Try to make a repr where positional arg follows keyword arg.
# Will raise ValueError when repr/pretty is called.
r.keyword_from_attr('a')
r.positional_from_attr('b')
def __repr__(self):
r = ReprHelper(self)
self._repr_helper(r)
return str(r)
def _repr_pretty_(self, p, cycle):
with PrettyReprHelper(self, p, cycle) as r:
self._repr_helper(r)
a = A(1, 2)
with pytest.raises(ValueError):
repr(a)
with pytest.raises(ValueError):
pretty(a)
class B(object):
def __init__(self, a, b):
self.a = a
self.b = b
def _repr_helper(self, r):
# Try to make a repr where positional arg follows keyword arg.
# Will raise ValueError when repr/pretty is called.
r.keyword_from_attr('a')
r.positional_with_value(self.b)
def __repr__(self):
r = ReprHelper(self)
self._repr_helper(r)
return str(r)
def _repr_pretty_(self, p, cycle):
with PrettyReprHelper(self, p, cycle) as r:
self._repr_helper(r)
b = B(1, 2)
with pytest.raises(ValueError):
repr(b)
with pytest.raises(ValueError):
pretty(b)
def test_helper_raw():
class A(ReprHelperMixin, object):
def __init__(self, a, b):
self.a = a
self.b = b
def _repr_helper_(self, r):
r.positional_with_value(self.a, raw=True)
r.keyword_with_value('b', self.b, raw=True)
a = A('a', 'b')
assert repr(a) == 'A(a, b=b)'
assert pretty(a) == 'A(a, b=b)'
def test_helper_mixin():
"""Verify that both __repr__ and _repr_pretty_ get called."""
class ContrivedExample(ReprHelperMixin, object):
def __init__(self, description, radians, shape, color, miles):
self.description = description
self.degrees = radians * 180 / 3.141592654
self.shape = shape
self._color = color
self.km = 1.60934 * miles
def _repr_helper_(self, r):
r.positional_from_attr('description')
r.positional_with_value(self.degrees * 3.141592654 / 180)
r.keyword_from_attr('shape')
r.keyword_from_attr('color', '_color')
r.keyword_with_value('miles', self.km / 1.60934)
ce = ContrivedExample('does something', 0.345, 'square', 'red', 22)
assert repr(ce) == ("ContrivedExample('does something', 0.345, "
"shape='square', color='red', miles=22.0)")
prettystr = """
ContrivedExample('does something',
0.345,
shape='square',
color='red',
miles=22.0)"""
assert pretty(ce) == textwrap.dedent(prettystr).lstrip()
class ContrivedExampleKeywords(ContrivedExample):
def _repr_helper_(self, r):
r.positional_from_attr(attr_name='description')
r.positional_with_value(value=self.degrees * 3.141592654 / 180)
r.keyword_from_attr(name='shape')
r.keyword_from_attr(name='color', attr_name='_color')
r.keyword_with_value(name='miles', value=self.km / 1.60934)
ce = ContrivedExampleKeywords('does something', 0.345, 'square', 'red', 22)
assert repr(ce) == ("ContrivedExampleKeywords('does something', 0.345, "
"shape='square', color='red', miles=22.0)")
prettystr = """
ContrivedExampleKeywords('does something',
0.345,
shape='square',
color='red',
miles=22.0)"""
assert pretty(ce) == textwrap.dedent(prettystr).lstrip()
@pytest.mark.skipif(sys.version_info < (3,2), reason='Requires Python 3.2+')
def test_helper_mixin_recursive():
"""Test that the mixin applies the :func:`reprlib.recursive_repr` decorator."""
class A(ReprHelperMixin):
def __init__(self, a=None):
self.a = a
def _repr_helper_(self, r):
r.keyword_from_attr('a')
a = A()
a.a = a
reprstr = 'A(a=...)'
assert repr(a) == reprstr
def test_helper_parantheses():
class A(object):
def __repr__(self):
r = ReprHelper(self)
r.parantheses = ('<', '>')
r.keyword_with_value('id', hex(id(self)), raw=True)
return str(r)
def _repr_pretty_(self, p, cycle):
r = PrettyReprHelper(self, p, cycle)
r.parantheses = ('<', '>')
with r:
r.keyword_with_value('id', hex(id(self)), raw=True)
a = A()
assert repr(a) == 'A<id={}>'.format(hex(id(a)))
assert pretty(a) == 'A<id={}>'.format(hex(id(a)))
# Test namedtuple for parantheses property
r = ReprHelper(a)
assert repr(r.parantheses) == "Parantheses(left='(', right=')')"
r.parantheses = ('<', '>')
assert repr(r.parantheses) == "Parantheses(left='<', right='>')"