-
Notifications
You must be signed in to change notification settings - Fork 13
/
renormalizing.txt
240 lines (203 loc) · 7.12 KB
/
renormalizing.txt
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
=======================================================
Regular expression pattern normalizing output checker
=======================================================
.. currentmodule:: zope.testing.renormalizing
The pattern-normalizing output checker from
`zope.testing.renormalizing` extends the default output checker with
an option to normalize expected and actual output.
You specify a sequence of patterns and replacements. The replacements are
applied to the expected and actual outputs before calling the default outputs
checker. Let's look at an example. In this example, we have some times and
addresses:
>>> want = '''\
... <object object at 0xb7f14438>
... completed in 1.234 seconds.
... <BLANKLINE>
... <object object at 0xb7f14440>
... completed in 123.234 seconds.
... <BLANKLINE>
... <object object at 0xb7f14448>
... completed in .234 seconds.
... <BLANKLINE>
... <object object at 0xb7f14450>
... completed in 1.234 seconds.
... <BLANKLINE>
... '''
>>> got = '''\
... <object object at 0xb7f14458>
... completed in 1.235 seconds.
...
... <object object at 0xb7f14460>
... completed in 123.233 seconds.
...
... <object object at 0xb7f14468>
... completed in .231 seconds.
...
... <object object at 0xb7f14470>
... completed in 1.23 seconds.
...
... '''
We may wish to consider these two strings to match, even though they differ in
actual addresses and times. The default output checker will consider them
different:
>>> import doctest
>>> doctest.OutputChecker().check_output(want, got, 0)
False
We'll use the `zope.testing.renormalizing.OutputChecker` to normalize both the
wanted and gotten strings to ignore differences in times and
addresses:
>>> import re
>>> from zope.testing.renormalizing import OutputChecker
>>> checker = OutputChecker([
... (re.compile('[0-9]*[.][0-9]* seconds'), '<SOME NUMBER OF> seconds'),
... (re.compile('at 0x[0-9a-f]+'), 'at <SOME ADDRESS>'),
... ])
>>> checker.check_output(want, got, 0)
True
Usual `doctest.OutputChecker` options work as expected:
>>> want_ellided = '''\
... <object object at 0xb7f14438>
... completed in 1.234 seconds.
... ...
... <object object at 0xb7f14450>
... completed in 1.234 seconds.
... <BLANKLINE>
... '''
>>> checker.check_output(want_ellided, got, 0)
False
>>> checker.check_output(want_ellided, got, doctest.ELLIPSIS)
True
When we get differencs, we output them with normalized text:
>>> source = '''\
... >>> do_something()
... <object object at 0xb7f14438>
... completed in 1.234 seconds.
... ...
... <object object at 0xb7f14450>
... completed in 1.234 seconds.
... <BLANKLINE>
... '''
>>> example = doctest.Example(source, want_ellided)
>>> print_(checker.output_difference(example, got, 0))
Expected:
<object object at <SOME ADDRESS>>
completed in <SOME NUMBER OF> seconds.
...
<object object at <SOME ADDRESS>>
completed in <SOME NUMBER OF> seconds.
<BLANKLINE>
Got:
<object object at <SOME ADDRESS>>
completed in <SOME NUMBER OF> seconds.
<BLANKLINE>
<object object at <SOME ADDRESS>>
completed in <SOME NUMBER OF> seconds.
<BLANKLINE>
<object object at <SOME ADDRESS>>
completed in <SOME NUMBER OF> seconds.
<BLANKLINE>
<object object at <SOME ADDRESS>>
completed in <SOME NUMBER OF> seconds.
<BLANKLINE>
<BLANKLINE>
>>> print_(checker.output_difference(example, got,
... doctest.REPORT_NDIFF))
Differences (ndiff with -expected +actual):
- <object object at <SOME ADDRESS>>
- completed in <SOME NUMBER OF> seconds.
- ...
<object object at <SOME ADDRESS>>
completed in <SOME NUMBER OF> seconds.
<BLANKLINE>
+ <object object at <SOME ADDRESS>>
+ completed in <SOME NUMBER OF> seconds.
+ <BLANKLINE>
+ <object object at <SOME ADDRESS>>
+ completed in <SOME NUMBER OF> seconds.
+ <BLANKLINE>
+ <object object at <SOME ADDRESS>>
+ completed in <SOME NUMBER OF> seconds.
+ <BLANKLINE>
<BLANKLINE>
If the wanted text is empty, however, we don't transform the actual
output. This is usful when writing tests. We leave the expected output
empty, run the test, and use the actual output as expected, after
reviewing it.
>>> source = '''\
... >>> do_something()
... '''
>>> example = doctest.Example(source, '\n')
>>> print_(checker.output_difference(example, got, 0))
Expected:
<BLANKLINE>
Got:
<object object at 0xb7f14458>
completed in 1.235 seconds.
<BLANKLINE>
<object object at 0xb7f14460>
completed in 123.233 seconds.
<BLANKLINE>
<object object at 0xb7f14468>
completed in .231 seconds.
<BLANKLINE>
<object object at 0xb7f14470>
completed in 1.23 seconds.
<BLANKLINE>
<BLANKLINE>
If regular expressions aren't expressive enough, you can use arbitrary Python
callables to transform the text. For example, suppose you want to ignore
case during comparison:
>>> checker = OutputChecker([
... lambda s: s.lower(),
... lambda s: s.replace('<blankline>', '<BLANKLINE>'),
... ])
>>> want = '''\
... Usage: thundermonkey [options] [url]
... <BLANKLINE>
... Options:
... -h display this help message
... '''
>>> got = '''\
... usage: thundermonkey [options] [URL]
...
... options:
... -h Display this help message
... '''
>>> checker.check_output(want, got, 0)
True
Suppose we forgot that ``<BLANKLINE>`` must be in upper case:
>>> checker = OutputChecker([
... lambda s: s.lower(),
... ])
>>> checker.check_output(want, got, 0)
False
The difference would show us that:
>>> source = '''\
... >>> print_help_message()
... ''' + want
>>> example = doctest.Example(source, want)
>>> print_(checker.output_difference(example, got,
... doctest.REPORT_NDIFF))
Differences (ndiff with -expected +actual):
usage: thundermonkey [options] [url]
- <blankline>
+ <BLANKLINE>
options:
-h display this help message
<BLANKLINE>
It is possible to combine `OutputChecker` checkers for easy reuse:
>>> address_and_time_checker = OutputChecker([
... (re.compile('[0-9]*[.][0-9]* seconds'), '<SOME NUMBER OF> seconds'),
... (re.compile('at 0x[0-9a-f]+'), 'at <SOME ADDRESS>'),
... ])
>>> lowercase_checker = OutputChecker([
... lambda s: s.lower(),
... ])
>>> combined_checker = address_and_time_checker + lowercase_checker
>>> len(combined_checker.transformers)
3
Combining a checker with something else does not work:
>>> lowercase_checker + 5 #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: ...