Regular expression pattern normalizing output checker
The pattern-normalizing output checker extends the default output checker with an option to normalize expected an 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 = '''\ ...
>>> got = '''\
...
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:
>>> doctest.OutputChecker().check_output(want, got, 0) False
We'll use the RENormalizing to normalize both the wanted and gotten strings to ignore differences in times and addresses:
>>> import re
>>> checker = RENormalizing([
... (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
>>> want_ellided = '''\
...
>>> checker.check_output(want_ellided, got, 0) False
>>> checker.check_output(want_ellided, got, doctest.ELLIPSIS) True
>>> source = '''\
... >>> do_something()
...
>>> example = doctest.Example(source, want_ellided)
>>> print checker.output_difference(example, got, 0)
Expected:
>>> print checker.output_difference(example, got,
... doctest.REPORT_NDIFF)
Differences (ndiff with -expected +actual):
-
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: