1
##############################################################################
3
# Copyright (c) 2004 Zope Foundation and Contributors.
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
13
##############################################################################
15
# This file is a package rather than a module because we want
19
# to import the stdlib version of doctest rather than the deprecated
20
# zope.testing.doctest, and
22
# from __future__ import absolute_import
24
# is not available on Python 2.4 which we still support.
26
##############################################################################
27
r"""Regular expression pattern normalizing output checker
29
The pattern-normalizing output checker extends the default output checker with
30
an option to normalize expected and actual output.
32
You specify a sequence of patterns and replacements. The replacements are
33
applied to the expected and actual outputs before calling the default outputs
34
checker. Let's look at an example. In this example, we have some times and
38
... <object object at 0xb7f14438>
39
... completed in 1.234 seconds.
41
... <object object at 0xb7f14440>
42
... completed in 123.234 seconds.
44
... <object object at 0xb7f14448>
45
... completed in .234 seconds.
47
... <object object at 0xb7f14450>
48
... completed in 1.234 seconds.
53
... <object object at 0xb7f14458>
54
... completed in 1.235 seconds.
56
... <object object at 0xb7f14460>
57
... completed in 123.233 seconds.
59
... <object object at 0xb7f14468>
60
... completed in .231 seconds.
62
... <object object at 0xb7f14470>
63
... completed in 1.23 seconds.
67
We may wish to consider these two strings to match, even though they differ in
68
actual addresses and times. The default output checker will consider them
71
>>> doctest.OutputChecker().check_output(want, got, 0)
74
We'll use the RENormalizing to normalize both the wanted and gotten strings to
75
ignore differences in times and addresses:
78
>>> checker = RENormalizing([
79
... (re.compile('[0-9]*[.][0-9]* seconds'), '<SOME NUMBER OF> seconds'),
80
... (re.compile('at 0x[0-9a-f]+'), 'at <SOME ADDRESS>'),
83
>>> checker.check_output(want, got, 0)
86
Usual OutputChecker options work as expected:
88
>>> want_ellided = '''\
89
... <object object at 0xb7f14438>
90
... completed in 1.234 seconds.
92
... <object object at 0xb7f14450>
93
... completed in 1.234 seconds.
97
>>> checker.check_output(want_ellided, got, 0)
100
>>> checker.check_output(want_ellided, got, doctest.ELLIPSIS)
103
When we get differencs, we output them with normalized text:
106
... >>> do_something()
107
... <object object at 0xb7f14438>
108
... completed in 1.234 seconds.
110
... <object object at 0xb7f14450>
111
... completed in 1.234 seconds.
115
>>> example = doctest.Example(source, want_ellided)
117
>>> print checker.output_difference(example, got, 0)
119
<object object at <SOME ADDRESS>>
120
completed in <SOME NUMBER OF> seconds.
122
<object object at <SOME ADDRESS>>
123
completed in <SOME NUMBER OF> seconds.
126
<object object at <SOME ADDRESS>>
127
completed in <SOME NUMBER OF> seconds.
129
<object object at <SOME ADDRESS>>
130
completed in <SOME NUMBER OF> seconds.
132
<object object at <SOME ADDRESS>>
133
completed in <SOME NUMBER OF> seconds.
135
<object object at <SOME ADDRESS>>
136
completed in <SOME NUMBER OF> seconds.
140
>>> print checker.output_difference(example, got,
141
... doctest.REPORT_NDIFF)
142
Differences (ndiff with -expected +actual):
143
- <object object at <SOME ADDRESS>>
144
- completed in <SOME NUMBER OF> seconds.
146
<object object at <SOME ADDRESS>>
147
completed in <SOME NUMBER OF> seconds.
149
+ <object object at <SOME ADDRESS>>
150
+ completed in <SOME NUMBER OF> seconds.
152
+ <object object at <SOME ADDRESS>>
153
+ completed in <SOME NUMBER OF> seconds.
155
+ <object object at <SOME ADDRESS>>
156
+ completed in <SOME NUMBER OF> seconds.
160
If the wanted text is empty, however, we don't transform the actual output.
161
This is usful when writing tests. We leave the expected output empty, run
162
the test, and use the actual output as expected, after reviewing it.
165
... >>> do_something()
168
>>> example = doctest.Example(source, '\n')
169
>>> print checker.output_difference(example, got, 0)
173
<object object at 0xb7f14458>
174
completed in 1.235 seconds.
176
<object object at 0xb7f14460>
177
completed in 123.233 seconds.
179
<object object at 0xb7f14468>
180
completed in .231 seconds.
182
<object object at 0xb7f14470>
183
completed in 1.23 seconds.
187
If regular expressions aren't expressive enough, you can use arbitrary Python
188
callables to transform the text. For example, suppose you want to ignore
189
case during comparison:
191
>>> checker = RENormalizing([
192
... lambda s: s.lower(),
193
... lambda s: s.replace('<blankline>', '<BLANKLINE>'),
197
... Usage: thundermonkey [options] [url]
200
... -h display this help message
204
... usage: thundermonkey [options] [URL]
207
... -h Display this help message
210
>>> checker.check_output(want, got, 0)
213
Suppose we forgot that <BLANKLINE> must be in upper case:
215
>>> checker = RENormalizing([
216
... lambda s: s.lower(),
219
>>> checker.check_output(want, got, 0)
222
The difference would show us that:
225
... >>> print_help_message()
227
>>> example = doctest.Example(source, want)
228
>>> print checker.output_difference(example, got,
229
... doctest.REPORT_NDIFF),
230
Differences (ndiff with -expected +actual):
231
usage: thundermonkey [options] [url]
235
-h display this help message
238
It is possible to combine RENormalizing checkers for easy reuse:
240
>>> address_and_time_checker = RENormalizing([
241
... (re.compile('[0-9]*[.][0-9]* seconds'), '<SOME NUMBER OF> seconds'),
242
... (re.compile('at 0x[0-9a-f]+'), 'at <SOME ADDRESS>'),
244
>>> lowercase_checker = RENormalizing([
245
... lambda s: s.lower(),
247
>>> combined_checker = address_and_time_checker + lowercase_checker
248
>>> len(combined_checker.transformers)
251
Combining a checker with something else does not work:
253
>>> lowercase_checker + 5
254
Traceback (most recent call last):
256
TypeError: unsupported operand type(s) for +: 'instance' and 'int'
263
class RENormalizing(doctest.OutputChecker):
264
"""Pattern-normalizing outout checker
267
def __init__(self, patterns):
268
self.transformers = map(self._cook, patterns)
270
def __add__(self, other):
271
if not isinstance(other, RENormalizing):
272
return NotImplemented
273
return RENormalizing(self.transformers + other.transformers)
275
def _cook(self, pattern):
276
if callable(pattern):
278
regexp, replacement = pattern
279
return lambda text: regexp.sub(replacement, text)
281
def check_output(self, want, got, optionflags):
285
for transformer in self.transformers:
286
want = transformer(want)
287
got = transformer(got)
289
return doctest.OutputChecker.check_output(self, want, got, optionflags)
291
def output_difference(self, example, got, optionflags):
295
# If want is empty, use original outputter. This is useful
296
# when setting up tests for the first time. In that case, we
297
# generally use the differencer to display output, which we evaluate
300
return doctest.OutputChecker.output_difference(
301
self, example, got, optionflags)
303
# Dang, this isn't as easy to override as we might wish
306
for transformer in self.transformers:
307
want = transformer(want)
308
got = transformer(got)
310
# temporarily hack example with normalized want:
312
result = doctest.OutputChecker.output_difference(
313
self, example, got, optionflags)
314
example.want = original